codeparrot/xlcost-text-to-code huggingface dataset:
Maximize value at Kth index to create N size array with adjacent difference 1 and sum less than M | Function to calculate maximum value that can be placed at the Kth index in a distribution in which difference of adjacent elements is less than 1 and total sum of distribution is M . ; variable to store final answer ; variables for binary search ; Binary search ; variable for binary search ; variable to store total sum of array ; number of indices on the left excluding the Kth index ; number of indices on the left excluding the Kth index ; add mid to final sum ; distribution on left side is possible ; sum of distribution on the left side ; sum of distribution on the left side with ( L - mid ) 1 s ; distribution on right side is possible ; sum of distribution on the right side ; sum of distribution on the left side with ( R - mid ) 1 s ; Distribution is valid ; return answer ; Input ; Function call
function calculateMax ( N , M , K ) { var ans = - 1 ; var low = 0 , high = M ; while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var val = 0 ; var L = K - 1 ; var R = N - K ; val += mid ; if ( mid >= L ) { val += ( L ) * ( ( 2 * mid - L - 1 ) / 2 ) ; } else { val += mid * parseInt ( ( mid - 1 ) / 2 ) + ( L - mid ) ; } if ( mid >= R ) { val += ( R ) * ( 2 * mid - R - 1 ) / 2 ; } else { val += mid * parseInt ( ( mid - 1 ) / 2 ) + ( R - mid ) ; } if ( val <= M ) { ans = Math . max ( ans , mid ) ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } let N = 7 , M = 100 , K = 6 ; document . write ( calculateMax ( N , M , K ) ) ;
Minimum time required to color all edges of a Tree | Stores the required answer ; Stores the graph ; Function to add edges ; Function to calculate the minimum time required to color all the edges of a tree ; Starting from time = 0 , for all the child edges ; If the edge is not visited yet . ; Time of coloring of the current edge ; If the parent edge has been colored at the same time ; Update the maximum time ; Recursively call the function to its child node ; Driver Code ; Function call ; Finally , print the answer
let ans = 0 ; let edges = new Array ( 100000 ) ; for ( let i = 0 ; i < 100000 ; i ++ ) edges [ i ] = [ ] ; function Add_edge ( u , v ) { edges [ u ] . push ( v ) ; edges [ v ] . push ( u ) ; } function minTimeToColor ( node , parent , arrival_time ) { let current_time = 0 ; for ( let x = 0 ; x < edges [ node ] . length ; x ++ ) { if ( edges [ node ] [ x ] != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = Math . max ( ans , current_time ) ; minTimeToColor ( edges [ node ] [ x ] , node , current_time ) ; } } } let A = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ] ; for ( let i = 0 ; i < A . length ; i ++ ) { Add_edge ( A [ i ] [ 0 ] , A [ i ] [ 1 ] ) ; } minTimeToColor ( 1 , - 1 , 0 ) ; document . write ( ans ) ;
Largest number having both positive and negative values present in the array | Function to find the largest number k such that both k and - k are present in the array ; Stores the resultant value of K ; Sort the array arr [ ] ; Initialize two variables to use two pointers technique ; Iterate until the value of l is less than r ; Find the value of the sum ; If the sum is 0 , then the resultant element is found ; If the sum is negative ; Otherwise , decrement r ; Driver Code
function largestNum ( arr ) { let res = 0 ; arr . sort ( ( a , b ) => a - b ) ; let l = 0 , r = arr . length - 1 ; while ( l < r ) { let sum = arr [ l ] + arr [ r ] ; if ( sum == 0 ) { res = Math . max ( res , Math . max ( arr [ l ] , arr [ r ] ) ) ; return res ; } else if ( sum < 0 ) { l ++ ; } else { r -- ; } } return res ; } let arr = [ 3 , 2 , - 2 , 5 , - 3 ] ; document . write ( ( largestNum ( arr ) ) ) ;
Find elements larger than half of the elements in an array | Set 2 | Function to find the element that are larger than half of elements of the array ; Find the value of mid ; Stores the maximum element ; Stores the frequency of each array element ; Traverse the array in the reverse order ; Decrement the value of count [ i ] and mid ; Print the current element ; Check if the value of mid is equal to 0 ; Driver Code
function findLarger ( arr , n ) { let mid = ( n + 1 ) / 2 ; let mx = Math . max . apply ( null , arr ) var count = new Array ( mx + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( let i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; document . write ( i + ' ' ) ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } var arr = [ 10 , 4 , 2 , 8 , 9 ] ; var N = 5 ; findLarger ( arr , N ) ;
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | Function to find probability such that x < y and X belongs to arr1 [ ] and Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Traverse the arr2 [ ] ; If arr2 [ j ] is greater than arr1 [ i ] ; Increment res by y ; Update the value of res ; Return resultant probability ; Driver Code
function probability ( arr1 , arr2 ) { let N = arr1 . length ; let M = arr2 . length ; let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let y = 0 ; for ( let j = 0 ; j < M ; j ++ ) { if ( arr2 [ j ] > arr1 [ i ] ) y ++ ; } res += y ; } res = ( res / ( N * M ) ) ; return res ; } let arr1 = [ 5 , 2 , 6 , 1 ] ; let arr2 = [ 1 , 6 , 10 , 1 ] ; document . write ( probability ( arr1 , arr2 ) ) ;
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | Function to find probability such that x < y and X belongs to arr1 [ ] & Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Sort the arr2 [ ] in the ascending order ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Increment res by y ; Update the resultant probability ; Return the result ; Function to return the count of elements from the array which are greater than k ; Stores the index of the leftmost element from the array which is at least k ; Finds number of elements greater than k ; If mid element is at least K , then update the value of leftGreater and r ; Update leftGreater ; Update r ; If mid element is at most K , then update the value of l ; Return the count of elements greater than k ; Driver Code
function probability ( arr1 , arr2 ) { var N = 4 ; var M = 4 ; var res = 0 ; arr2 . sort ( function ( a , b ) { return a - b ; } ) ; for ( var i = 0 ; i < N ; i ++ ) { var y = countGreater ( arr2 , arr1 [ i ] ) ; res += y ; } res = res / ( N * M ) ; return res ; } function countGreater ( arr , k ) { var n = 4 ; var l = 0 ; var r = n - 1 ; var leftGreater = n ; while ( l <= r ) { var m = Math . floor ( l + ( r - l ) / 2 ) ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return n - leftGreater ; } var arr1 = [ 5 , 2 , 6 , 1 ] ; var arr2 = [ 1 , 6 , 10 , 1 ] ; document . write ( probability ( arr1 , arr2 ) ) ;
Minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | Function to find the minimum cost of flooring with the given tiles ; Store the size of the 2d array ; Stores the minimum cost of flooring ; Traverse the 2d array row - wise ; If the current character is ' * ' , then skip it ; Choose the 1 * 1 tile if j is m - 1 ; If consecutive ' . ' are present , the greedily choose tile with the minimum cost ; Otherwise choose the 1 * 1 tile ; Print the minimum cost ; Driver Code
function minCost ( arr , A , B ) { let n = arr . length ; let m = arr [ 0 ] . length ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == ' ' ) continue ; if ( j == m - 1 ) ans += A ; else { if ( arr [ i ] [ j + 1 ] == ' ' ) { ans += Math . min ( 2 * A , B ) ; j ++ ; } else ans += A ; } } } document . write ( ans ) ; } var arr = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let A = 2 , B = 10 ; minCost ( arr , A , B ) ;
Count inversions in a permutation of first N natural numbers | Function to count number of inversions in a permutation of first N natural numbers ; Store array elements in sorted order ; Store the count of inversions ; Traverse the array ; Store the index of first occurrence of arr [ i ] in vector V ; Add count of smaller elements than current element ; Erase current element from vector and go to next index ; Print the result ; Given Input ; Function Call
function countInversions ( arr , n ) { var v = [ ] ; var i ; for ( i = 1 ; i <= n ; i ++ ) { v . push ( i ) ; } var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { var index = v . indexOf ( arr [ i ] ) ; ans += index ; v . splice ( index , 1 ) ; } document . write ( ans ) ; } var arr = [ 2 , 3 , 1 , 5 , 4 ] ; var n = arr . length ; countInversions ( arr , n ) ;
Find all possible pairs with given Bitwise OR and Bitwise XOR values | Function to find pairs with XOR equal to A and OR equal to B ; Iterate from 1 to B ; Check if ( i OR y ) is B ; Driver Code
function findPairs ( A , B ) { for ( let i = 1 ; i <= B ; i ++ ) { let y = A ^ i ; if ( y > 0 && ( i y ) == B ) { document . write ( i + " " + y + " " ) ; } } } let A = 8 , B = 10 ; findPairs ( A , B ) ;
Count distinct elements from a range of a sorted sequence from a given frequency array | Function to find the first index with value is at least element ; Update the value of left ; Binary search for the element ; Find the middle element ; Check if the value lies between the elements at index mid - 1 and mid ; Check in the right subarray ; Update the value of left ; Check in left subarray ; Update the value of right ; Function to count the number of distinct elements over the range [ L , R ] in the sorted sequence ; Stores the count of distinct elements ; Create the prefix sum array ; Update the value of count ; Update the value of pref [ i ] ; Calculating the first index of L and R using binary search ; Print the resultant count ; Driver Code
function binarysearch ( array , right , element ) { let left = 1 ; while ( left <= right ) { let mid = Math . floor ( ( left + right / 2 ) ) ; if ( array [ mid ] == element ) { return mid ; } if ( mid - 1 > 0 && array [ mid ] > element && array [ mid - 1 ] < element ) { return mid ; } else if ( array [ mid ] < element ) { left = mid + 1 ; } else { right = mid - 1 ; } } return 1 ; } function countDistinct ( arr , L , R ) { let count = 0 ; let pref = Array . from ( { length : arr . length + 1 } , ( _ , i ) => 0 ) ; for ( let i = 1 ; i <= arr . length ; ++ i ) { count += arr [ i - 1 ] ; pref [ i ] = count ; } let left = binarysearch ( pref , arr . length + 1 , L ) ; let right = binarysearch ( pref , arr . length + 1 , R ) ; document . write ( ( right - left ) + 1 ) ; } let arr = [ 3 , 6 , 7 , 1 , 8 ] ; let L = 3 ; let R = 7 ; countDistinct ( arr , L , R ) ;
Count substrings having frequency of a character exceeding that of another character in a string | Function to find the number of substrings having the frequency of ' a ' greater than frequency of ' c ' ; Stores the size of the string ; Stores the resultant count of substrings ; Traverse the given string ; Store the difference between frequency of ' a ' and ' c ' ; Traverse all substrings beginning at index i ; If the frequency of ' a ' is greater than ' c ' ; Print the answer ; Drive Code
function countSubstrings ( s ) { var n = s . length ; var ans = 0 ; var i , j ; for ( i = 0 ; i < n ; i ++ ) { var cnt = 0 ; for ( j = i ; j < n ; j ++ ) { if ( s [ j ] == ' ' ) cnt ++ ; else if ( s [ j ] == ' ' ) cnt -- ; if ( cnt > 0 ) { ans ++ ; } } } document . write ( ans ) ; } var S = " " ; countSubstrings ( S ) ;
Find Unique ID and Domain Name of a Website from a string | Function to check if a character is alphabet or not ; Function to check if a character is a numeric or not ; Function to find ID and Domain name from a given string ; Stores ID and the domain names ; Stores the words of string S ; Stores the temporary word ; Traverse the string S ; If the current character is space ; Push the curr in words ; Update the curr ; Otherwise ; If curr is not empty ; If length of ss is 10 ; Traverse the string ss ; If j is in the range [ 5 , 9 ) ; If current character is not numeric ; Mark flag 1 ; Otherwise ; If current character is not alphabet ; Mark flag 1 ; If flag is false ; Assign ss to ID ; If substring formed by the first 3 character is " www " and last 3 character is " moc " ; Update the domain name ; Print ID and Domain ; Driver Code
function ischar ( x ) { if ( ( x >= ' ' && x <= ' ' ) || ( x >= ' ' && x <= ' ' ) ) { return true ; } return false ; } function isnum ( x ) { if ( x >= ' ' && x <= ' ' ) return true ; return false ; } function findIdandDomain ( S , N ) { let ID , Domain ; let words = [ ] ; let curr = " " ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { words . push ( curr ) ; curr = " " ; } else { if ( S [ i ] == ' ' ) { if ( i + 1 == N || ( i + 1 < N && S [ i + 1 ] == ' ' ) ) continue ; } curr += S [ i ] ; } } if ( curr . length >= 1 ) words . push ( curr ) ; for ( let i = 0 ; i < words . length ; i ++ ) { if ( words [ i ] . length == 10 ) { let flag = 0 ; for ( let j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( words [ i ] [ j ] ) == 0 ) flag = 1 ; } else { if ( ischar ( words [ i ] [ j ] ) == 0 ) flag = 1 ; } } if ( ! flag ) { ID = words [ i ] ; } } if ( words [ i ] . substring ( 0 , 3 ) == " " && words [ i ] . substring ( words [ i ] . length - 3 ) == " " ) { Domain = words [ i ] . substring ( 4 ) ; } } document . write ( " " + ID + " " ) ; document . write ( " " + Domain ) ; } let S = " " + " " + " " ; let N = S . length ; findIdandDomain ( S , N ) ;
Capacity To Ship Packages Within D Days | Function to check if the weights can be delivered in D days or not ; Stores the count of days required to ship all the weights if the maximum capacity is mx ; Traverse all the weights ; If total weight is more than the maximum capacity ; If days are more than D , then return false ; Return true for the days < D ; Function to find the least weight capacity of a boat to ship all the weights within D days ; Stores the total weights to be shipped ; Find the sum of weights ; Stores the maximum weight in the array that has to be shipped ; Store the ending value for the search space ; Store the required result ; Perform binary search ; Store the middle value ; If mid can be shipped , then update the result and end value of the search space ; Search for minimum value in the right part ; Print the result ; Driver Code
function isValid ( weight , n , D , mx ) { let st = 1 ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += weight [ i ] ; if ( sum > mx ) { st ++ ; sum = weight [ i ] ; } if ( st > D ) return false ; } return true ; } function shipWithinDays ( weight , D , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += weight [ i ] ; let s = weight [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { s = Math . max ( s , weight [ i ] ) ; } let e = sum ; let res = - 1 ; while ( s <= e ) { let mid = s + Math . floor ( ( e - s ) / 2 ) ; if ( isValid ( weight , n , D , mid ) ) { res = mid ; e = mid - 1 ; } else s = mid + 1 ; } document . write ( res ) ; } let weight = [ 9 , 8 , 10 ] ; let D = 3 ; let N = weight . length ; shipWithinDays ( weight , D , N ) ;
Find a point whose sum of distances from all given points on a line is K | Function to find the sum of distances of all points from a given point ; Stores sum of distances ; Traverse the array ; Return the sum ; Function to find such a point having sum of distances of all other points from this point equal to K ; If N is odd keep left as arr [ n / 2 ] else keep left as arr [ n / 2 - 1 ] + 1 ; ; Keep right as arr [ N - 1 ] ; Perform binary search in the right half ; Calculate the mid index of the range ; If temp is equal to K ; Print the value of mid ; If the value of K < temp ; Update right to mid - 1 ; If the value of K > temp ; Update left to mid + 1 ; Update the value of left ; Update the value of right ; Perform binary search on the left half ; Calculate the mid index of the range ; If temp is equal to K ; Print mid ; If K > temp ; Update right to mid - 1 ; If K < temp ; Update left to mid + 1 ; If no such point found ; Driver Code
function findSum ( arr , N , pt ) { var sum = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { sum += Math . abs ( arr [ i ] - pt ) ; } return sum ; } function findPoint ( arr , N , K ) { var left ; if ( N % 2 == 1 ) { left = arr [ parseInt ( N / 2 ) ] ; } else { left = arr [ parseInt ( N / 2 ) - 1 ] + 1 ; } var right = arr [ N - 1 ] ; while ( left <= right ) { var mid = parseInt ( ( left + right ) / 2 ) ; var temp = findSum ( arr , N , mid ) ; if ( temp == K ) { document . write ( mid ) ; return ; } else if ( K < temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } left = arr [ 0 ] ; right = arr [ parseInt ( N / 2 ) ] - 1 ; while ( left <= right ) { var mid = parseInt ( ( left + right ) / 2 ) ; var temp = findSum ( arr , N , mid ) ; if ( temp == K ) { document . write ( mid ) ; return ; } else if ( K > temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } document . write ( " " ) ; } var arr = [ 1 , 3 , 6 , 7 , 11 ] ; var K = 18 ; var N = arr . length ; findPoint ( arr , N , K ) ;
Generate an array consisting of most frequent greater elements present on the right side of each array element | Function to generate an array containing the most frequent greater element on the right side of each array element ; Stores the generated array ; Traverse the array arr [ ] ; Store the result for the current index and its frequency ; Iterate over the right subarray ; Store the frequency of the current array element let curr_c = count ( & arr [ j ] , & arr [ n ] , arr [ j ] ) ; ; If the frequencies are equal ; Update ans to smaller of the two elements ; If count of new element is more than count of ans ; Insert answer in the array ; Print the resultant array ; Given Input
function findArray ( arr , n ) { let v = new Array ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let ans = - 1 , old_c = 0 ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { let curr_c = 0 ; arr . slice ( j , n ) . forEach ( ( item ) => { if ( item == arr [ j ] ) { curr_c ++ } } ) if ( curr_c == old_c ) { if ( arr [ j ] < ans ) ans = arr [ j ] ; } if ( curr_c > old_c ) { ans = arr [ j ] ; old_c = curr_c ; } } } v . push ( ans ) ; } for ( let i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + " " ) ; } let arr = [ 4 , 5 , 2 , 25 , 10 , 5 , 10 , 3 , 10 , 5 ] ; let size = arr . length findArray ( arr , size ) ;
Smallest index that splits an array into two subarrays with equal product | Function to find the smallest index that splits the array into two subarrays with equal product ; Stores the product of the array ; Traverse the given array ; Stores the product of left and the right subarrays ; Traverse the given array ; Update the products ; Check if product is equal ; Print resultant index ; If no partition exists , then print - 1. ; Driver Code
function prodEquilibrium ( arr , N ) { let product = 1 ; for ( let i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } let left = 1 ; let right = product ; for ( let i = 0 ; i < N ; i ++ ) { left = left * arr [ i ] ; right = right / arr [ i ] ; if ( left == right ) { document . write ( ( i + 1 ) + " " ) ; return ; } } document . write ( ( - 1 ) + " " ) ; } let arr = [ 1 , 2 , 3 , 3 , 2 , 1 ] ; let N = arr . length ; prodEquilibrium ( arr , N ) ;
Count triplets from a sorted array having difference between adjacent elements equal to D | Function to count the number of triplets having difference between adjacent elements equal to D ; Stores the frequency of array elements ; Stores the count of resultant triplets ; Traverse the array ; Check if arr [ i ] - D and arr [ i ] - 2 * D exists in the Hashmap or not ; Update the value of ans ; Increase the frequency of the current element ; Return the resultant count ; Driver Code
function countTriplets ( D , arr ) { var freq = new Map ( ) ; var ans = 0 ; var i ; for ( i = 0 ; i < arr . length ; i ++ ) { if ( freq . has ( arr [ i ] - D ) && freq . has ( arr [ i ] - 2 * D ) ) { ans += freq . get ( arr [ i ] - D ) * freq . get ( arr [ i ] - 2 * D ) ; } freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } return ans ; } var arr = [ 1 , 2 , 4 , 5 , 7 , 8 , 10 ] ; var D = 1 ; document . write ( countTriplets ( D , arr ) ) ;
Find the array element having equal sum of Prime Numbers on its left and right | Function to find an index in the array having sum of prime numbers to its left and right equal ; Stores the maximum value present in the array ; Stores all positive elements which are <= max_value ; If 1 is present ; Remove 1 ; Sieve of Eratosthenes to store all prime numbers which are <= max_value in the Map ; Erase non - prime numbers ; Stores the sum of prime numbers from left ; Stores the sum of prime numbers to the left of each index ; Stores the sum of prime numbers to the left of the current index ; Add current value to the prime sum if the current value is prime ; Stores the sum of prime numbers from right ; Stores the sum of prime numbers to the right of each index ; Stores the sum of prime numbers to the right of the current index ; Add current value to the prime sum if the current value is prime ; Traverse through the two arrays to find the index ; Compare the values present at the current index ; Return the index where both the values are same ; No index is found . ; Given array arr [ ] ; Size of Array ; Function Call
function find_index ( arr , N ) { let max_value = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { max_value = Math . max ( max_value , arr [ i ] ) ; } let store = new Map ( ) ; for ( let i = 1 ; i <= max_value ; i ++ ) { if ( ! store . has ( i ) ) store . set ( i , 0 ) ; store . set ( i , store . get ( i ) + 1 ) ; } if ( store . has ( 1 ) ) { store . delete ( 1 ) ; } for ( let i = 2 ; i <= Math . sqrt ( max_value ) ; i ++ ) { let multiple = 2 ; while ( ( i * multiple ) <= max_value ) { if ( store . has ( i * multiple ) ) { store . delete ( i * multiple ) ; } multiple ++ ; } } let prime_sum_from_left = 0 ; let first_array = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { first_array [ i ] = prime_sum_from_left ; if ( store . has ( arr [ i ] ) ) { prime_sum_from_left += arr [ i ] ; } } let prime_sum_from_right = 0 ; let second_array = new Array ( N ) ; for ( let i = N - 1 ; i >= 0 ; i -- ) { second_array [ i ] = prime_sum_from_right ; if ( store . has ( arr [ i ] ) ) { prime_sum_from_right += arr [ i ] ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( first_array [ i ] == second_array [ i ] ) { return i ; } } return - 1 ; } let arr = [ 11 , 4 , 7 , 6 , 13 , 1 , 5 ] ; let N = arr . length ; document . write ( find_index ( arr , N ) ) ;
Convert an array to reduced form | Set 3 ( Binary Search ) | Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Print the current index and break ; Update the value of l ; Update the value of r ; Driver Code
function convert ( arr , n ) { var brr = Array ( n ) . fill ( 0 ) ; var i ; for ( i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; brr . sort ( ) ; for ( i = 0 ; i < n ; i ++ ) { var l = 0 , r = n - 1 , mid ; while ( l <= r ) { mid = parseInt ( ( l + r ) / 2 , 10 ) ; if ( brr [ mid ] == arr [ i ] ) { document . write ( mid + ' ' ) ; break ; } else if ( brr [ mid ] < arr [ i ] ) { l = mid + 1 ; } else { r = mid - 1 ; } } } } var arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] ; var N = arr . length ; convert ( arr , N ) ;