code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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:41:20+05:18 · Edit