function BinarySearchIndex(arr,key){
    var low = 0;
    var high = arr.length-1;
    
    while(high>=low){
        var m =low + Math.floor((high-low)/2); 
        if(arr[m] == key)
            return m;
        else if(arr[m]<key)
            low = m+1;
        else if(arr[m]>key)
            high = m-1;
    }
    return -1;
}



function BinaryInsertAtPosition(arr,key,n){
    
  // Find position of element to be deleted
    let posIndex = BinarySearchIndex(arr,key);
  
    if(posIndex ===-1){
        console.log('Element is not found!!');
        return n;
    }
        
        
    for(let i=posIndex;i<=n-1;i++){
        arr[i]=arr[i+1];
    }

    return n-1;
}

let array = [1, 3, 5, 8, 10, 13, 15, 18, 20];
let key = 15;
let n = array.length;


console.log('Array Before Delete');
for(let i=0;i<n;i++){
    console.log(array[i],'');
}

n = BinaryInsertAtPosition(array,key,n);


console.log('Array After Delete');
for(let i=0;i<n;i++){
    console.log(array[i],'');
}





Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: