Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,961 questions

51,903 answers

573 users

How to search insert position of K in a sorted array with TypeScript

2 Answers

0 votes
// Given a sorted array/vector/list of distinct integers and a target value K, 
// return the index if the target is found. 
// If not, return the index where it would be if it were inserted in order.

// Function to find the index of k or the position where it should be inserted
function searchInsertPositionOfK(arr: number[], k: number): number {
    for (let i: number = 0; i < arr.length; i++) {
        // If k is found or needs to be inserted before arr[i]
        if (arr[i] >= k) {
            return i;
        }
    }
    // If k is greater than all elements, it should be inserted at the end
    return arr.length;
}

const arr1: number[] = [1, 3, 5, 6, 7, 8];
const k1: number = 5;
console.log(searchInsertPositionOfK(arr1, k1));

const arr2: number[] = [1, 3, 5, 6, 7, 8];
const k2: number = 2;
console.log(searchInsertPositionOfK(arr2, k2));

const arr3: number[] = [1, 3, 5, 6, 7, 8];
const k3: number = 9;
console.log(searchInsertPositionOfK(arr3, k3));


 
/*
run:
 
2
1
6
 
*/
 

 



answered May 10, 2025 by avibootz
0 votes
// Given a sorted array/vector/list of distinct integers and a target value K, 
// return the index if the target is found. 
// If not, return the index where it would be if it were inserted in order.
 
// Function to find the index of k or the position where it should be inserted - Using Binary Search
function searchInsertPositionOfK(arr: number[], k: number): number {
    let left: number = 0, right: number = arr.length - 1;
    let mid: number = 0;
 
    while (left <= right) {
        mid = left + Math.floor((right - left) / 2);
 
        if (arr[mid] === k) {
            return mid;
        }
        // If k is smaller, search in the left half
        else if (arr[mid] > k) {
            right = mid - 1;
        }
        // If k is larger, search in the right half
        else {
            left = mid + 1;
        }
    }
 
    // If k is not found, return the index where it should be inserted
    return left;
}

const arr1: number[] = [1, 3, 5, 6, 7, 8];
const k1: number = 5;
console.log(searchInsertPositionOfK(arr1, k1));

const arr2: number[] = [1, 3, 5, 6, 7, 8];
const k2: number = 2;
console.log(searchInsertPositionOfK(arr2, k2));

const arr3: number[] = [1, 3, 5, 6, 7, 8];
const k3: number = 9;
console.log(searchInsertPositionOfK(arr3, k3));


 
/*
run:
 
2
1
6
 
*/
 

 



answered May 10, 2025 by avibootz
...