code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

13 Where Do I Belong

/**
 * Created by Ashish Patel
 * Copyright © 2017 ashish.me
 * ashishsushilpatel@gmail.com
 */

/**
 * Problem:
 * Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.
 * The returned value should be a number.
 * For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
 */

function getIndexToIns(arr, num) {
    arr.push(num);
    arr = arr.sort((a, b) => a - b);
    return arr.indexOf(num);
}

console.log(getIndexToIns([20, 5, 3], 19));

Created 2019-11-24T04:53:20+05:30 · Edit