Search insert position of K in a sorted array | Function to find insert position of K ; Traverse the array ; If K is found ; If current array element exceeds K ; If all elements are smaller than K ; Driver code
function find_index ( arr , n , K ) { for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] == K ) return i ; else if ( arr [ i ] > K ) return i ; return n ; } let arr = [ 1 , 3 , 5 , 6 ] ; let n = arr . length ; let K = 2 ; document . write ( find_index ( arr , n , K ) ) ;