codeparrot/xlcost-text-to-code huggingface dataset:
Count array elements whose all distinct digits appear in K | Function to the count of array elements whose distinct digits are a subset of the digits of K ; Stores distinct digits of K ; Iterate over all the digits of K ; Insert current digit into set ; Update K ; Stores the count of array elements whose distinct digits are a subset of the digits of K ; Traverse the array , arr ; Stores current element ; Check if all the digits of arr [ i ] are present in K or not ; Iterate over all the digits of arr [ i ] ; Stores current digit ; If digit not present in the set ; Update flag ; Update no ; If all the digits of arr [ i ] present in set ; Update count ; Driver Code
function noOfValidKbers ( K , arr ) { var set = new Set ( ) ; while ( K != 0 ) { set . add ( K % 10 ) ; K = parseInt ( K / 10 ) ; } var count = 0 ; for ( i = 0 ; i < arr . length ; i ++ ) { var no = arr [ i ] ; var flag = true ; while ( no != 0 ) { var digit = no % 10 ; if ( ! set . has ( digit ) ) { flag = false ; break ; } no = parseInt ( no / 10 ) ; } if ( flag == true ) { count ++ ; } } return count ; } var K = 12 ; var arr = [ 1 , 12 , 1222 , 13 , 2 ] ; document . write ( noOfValidKbers ( K , arr ) ) ;
Queries to find the minimum index in a range [ L , R ] having at least value X with updates | Javascript program to implement the above approach ; Stores nodes value of the Tree ; Function to build segment tree ; Base Case ; Find the value of mid ; Update for left subtree ; Update for right subtree ; Update the value at the current index ; Function for finding the index of the first element at least x ; If current range does not lie in query range ; If current range is inside of query range ; Maximum value in this range is less than x ; Finding index of first value in this range ; Update the value of the minimum index ; Find mid of the current range ; Left subtree ; If it does not lie in left subtree ; Function for updating segment tree ; Update the value , we reached leaf node ; Find the mid ; If pos lies in the left subtree ; pos lies in the right subtree ; Update the maximum value in the range ; Function to print the answer for the given queries ; Build segment tree ; Find index of first value atleast 2 in range [ 0 , n - 1 ] ; Update value at index 2 to 5 ; Find index of first value atleast 4 in range [ 0 , n - 1 ] ; Find index of first value atleast 0 in range [ 0 , n - 1 ] ; Driver code ; Function Call
let maxN = 100 ; let Tree = new Array ( 4 * maxN ) ; function build ( arr , index , s , e ) { if ( s == e ) Tree [ index ] = arr [ s ] ; else { let m = parseInt ( ( s + e ) / 2 , 10 ) ; build ( arr , 2 * index , s , m ) ; build ( arr , 2 * index + 1 , m + 1 , e ) ; Tree [ index ] = Math . max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } function atleast_x ( index , s , e , ql , qr , x ) { if ( ql > e qr < s ) return - 1 ; if ( s <= ql && e <= qr ) { if ( Tree [ index ] < x ) return - 1 ; while ( s != e ) { let m = parseInt ( ( s + e ) / 2 , 10 ) ; if ( Tree [ 2 * index ] >= x ) { e = m ; index = 2 * index ; } else { s = m + 1 ; index = 2 * index + 1 ; } } return s ; } let m = parseInt ( ( s + e ) / 2 , 10 ) ; let val = atleast_x ( 2 * index , s , m , ql , qr , x ) ; if ( val != - 1 ) return val ; return atleast_x ( 2 * index + 1 , m + 1 , e , ql , qr , x ) ; } function update ( index , s , e , new_val , pos ) { if ( s == e ) Tree [ index ] = new_val ; else { let m = parseInt ( ( s + e ) / 2 , 10 ) ; if ( pos <= m ) { update ( 2 * index , s , m , new_val , pos ) ; } else { update ( 2 * index + 1 , m + 1 , e , new_val , pos ) ; } Tree [ index ] = Math . max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } function printAnswer ( arr , n ) { build ( arr , 1 , 0 , n - 1 ) ; document . write ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 2 ) + " " ) ; arr [ 2 ] = 5 ; update ( 1 , 0 , n - 1 , 5 , 2 ) ; document . write ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 4 ) + " " ) ; document . write ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 0 ) + " " ) ; } let arr = [ 1 , 3 , 2 , 4 , 6 ] ; let N = arr . length ; printAnswer ( arr , N ) ;
Smallest Subtree with all the Deepest Nodes | Structure of a Node ; Function to return depth of the Tree from root ; If current node is a leaf node ; Function to find the root of the smallest subtree consisting of all deepest nodes ; Stores height of left subtree ; Stores height of right subtree ; If height of left subtree exceeds that of the right subtree ; Traverse left subtree ; If height of right subtree exceeds that of the left subtree ; Otherwise ; Return current node ; Driver Code
class TreeNode { constructor ( data ) { this . left = null ; this . right = null ; this . val = data ; } } function find_ht ( root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; return Math . max ( find_ht ( root . left ) , find_ht ( root . right ) ) + 1 ; } function find_node ( root , req_node ) { if ( root == null ) return req_node ; let left_ht = find_ht ( root . left ) ; let right_ht = find_ht ( root . right ) ; if ( left_ht > right_ht ) { req_node = find_node ( root . left , req_node ) ; } else if ( right_ht > left_ht ) { req_node = find_node ( root . right , req_node ) ; } else { req_node = root ; return req_node ; } return req_node ; } let root = new TreeNode ( 1 ) ; root . left = new TreeNode ( 2 ) ; root . right = new TreeNode ( 3 ) ; let req_node = null ; req_node = find_node ( root , req_node ) ; document . write ( req_node . val ) ;
Maximum even numbers present in any subarray of size K | Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the maximum count of even numbers from all the subarrays of size K ; Generate all subarrays of size K ; Store count of even numbers in current subarray of size K ; Traverse the current subarray ; If current element is an even number ; Update the answer ; Return answer ; Driver Code ; Size of the input array
function maxEvenIntegers ( arr , N , M ) { let ans = 0 ; for ( let i = 0 ; i <= N - M ; i ++ ) { let cnt = 0 ; for ( let j = 0 ; j < M ; j ++ ) { if ( arr [ i + j ] % 2 == 0 ) cnt ++ ; } ans = Math . max ( ans , cnt ) ; } return ans ; } let arr = [ 2 , 3 , 5 , 4 , 7 , 6 ] ; let K = 3 ; let N = arr . length ; document . write ( maxEvenIntegers ( arr , N , K ) + " " ) ;
Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver code ; To display the result
function countDiv ( arr ) { let rem0 = 0 ; let rem1 = 0 ; let rem2 = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { let digitSum = 0 ; digitSum += arr [ i ] ; if ( digitSum % 3 == 0 ) { rem0 += 1 ; } else if ( digitSum % 3 == 1 ) { rem1 += 1 ; } else { rem2 += 1 ; } } return ( parseInt ( rem0 / 2 , 10 ) + Math . min ( rem1 , rem2 ) ) ; } let arr = [ 5 , 3 , 2 , 8 , 7 ] ; document . write ( countDiv ( arr ) ) ;
Maximum Sum Subsequence | Function to print the maximum non - emepty subsequence sum ; Stores the maximum non - emepty subsequence sum in an array ; Stores the largest element in the array ; Traverse the array ; If a [ i ] is greater than 0 ; Update sum ; Driver Code
function MaxNonEmpSubSeq ( a , n ) { let sum = 0 ; let max = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( max < a [ i ] ) { max = a [ i ] ; } } if ( max <= 0 ) { return max ; } for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } let arr = [ - 2 , 11 , - 4 , 2 , - 3 , - 10 ] ; let N = arr . length ; document . write ( MaxNonEmpSubSeq ( arr , N ) ) ;
Count numbers up to N having Kth bit set | Function to return the count of number of 1 's at ith bit  in a range [1, n - 1] ; Store count till nearest power of 2 less than N ; If K - th bit is set in N ; Add to result the nearest power of 2 less than N ; Return result ; Driver Code ; Function Call
function getcount ( n , k ) { let res = ( n >> ( k + 1 ) ) << k ; if ( ( ( n >> k ) & 1 ) != 0 ) res += n & ( ( 1 << k ) - 1 ) ; return res ; } let N = 14 ; let K = 2 ; document . write ( getcount ( N + 1 , K ) ) ;
Check if a string can be split into two substrings such that one substring is a substring of the other | Function to check if a String can be divided into two subStrings such that one subString is subString of the other ; Store the last character of S ; Traverse the characters at indices [ 0 , N - 2 ] ; Check if the current character is equal to the last character ; If true , set f = 1 ; Break out of the loop ; Given String , S ; Store the size of S ; Function Call
function splitString ( S , N ) { var c = S . charAt ( N - 1 ) ; var f = 0 ; for ( var i = 0 ; i < N - 1 ; i ++ ) { if ( S . charAt ( i ) == c ) { f = 1 ; break ; } } if ( f > 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var S = " " ; var N = S . length ; splitString ( S , N ) ;
Find the light bulb with maximum glowing time | Function to find the bulb having maximum glow ; Initialize variables ; Traverse the array consisting of glowing time of the bulbs ; For 1 st bulb ; Calculate the glowing time ; Update the maximum glow ; Find lexicographically largest bulb ; Bulb with maximum time ; Return the resultant bulb ; Driver code ; Function call
function longestLastingBulb ( onTime , s ) { let ans ; let n = onTime . length ; let maxDur = Number . MIN_VALUE ; let maxPos = Number . MIN_VALUE ; let currentDiff = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { currentDiff = onTime [ i ] ; maxDur = currentDiff ; maxPos = i ; } else { currentDiff = onTime [ i ] - onTime [ i - 1 ] ; if ( maxDur < currentDiff ) { maxDur = currentDiff ; maxPos = i ; } else { if ( maxDur == currentDiff ) { let one = s [ i ] ; let two = s [ maxPos ] ; if ( one > two ) { maxDur = currentDiff ; maxPos = i ; } } } } } ans = s [ maxPos ] ; return ans ; } let S = " " ; let arr = [ 12 , 23 , 36 , 46 , 62 ] ; document . write ( longestLastingBulb ( arr , S ) ) ;
Find a pair of intersecting ranges from a given array | Store ranges and their corresponding array indices ; Function to find a pair of intersecting ranges ; Stores ending point of every range ; Stores the maximum ending point obtained ; Iterate from 0 to N - 1 ; Starting point of the current range ; End point of the current range ; Push pairs into tup ; Sort the tup vector sort ( tup . begin ( ) , tup . end ( ) ) ; ; Iterate over the ranges ; If starting points are equal ; Print the indices of the intersecting ranges ; If no such pair of segments exist ; Given N ; Given 2d ranges [ ] [ ] array ; Function call
let tup = [ ] ; function findIntersectingRange ( N , ranges ) { let curr ; let currPos ; for ( let i = 0 ; i < N ; i ++ ) { let x , y ; x = ranges [ i ] [ 0 ] ; y = ranges [ i ] [ 1 ] ; let arr = [ x , y , i + 1 ] ; tup . push ( arr ) ; } tup . sort ( function ( a , b ) { if ( a [ 0 ] == b [ 0 ] ) { if ( a [ 1 ] == b [ 1 ] ) { return a [ 2 ] - b [ 2 ] ; } else { return a [ 1 ] - b [ 1 ] ; } } return a [ 0 ] - b [ 0 ] ; } ) ; curr = tup [ 0 ] [ 1 ] ; currPos = tup [ 0 ] [ 2 ] ; for ( let i = 1 ; i < N ; i ++ ) { let Q = tup [ i - 1 ] [ 0 ] ; let R = tup [ i ] [ 0 ] ; if ( Q == R ) { if ( tup [ i - 1 ] [ 1 ] < tup [ i ] [ 1 ] ) document . write ( tup [ i - 1 ] [ 2 ] + " " + tup [ i ] [ 2 ] ) ; else document . write ( tup [ i ] [ 2 ] + " " + tup [ i - 1 ] [ 2 ] ) ; return ; } let T = tup [ i ] [ 1 ] ; if ( T <= curr ) { document . write ( tup [ i ] [ 2 ] + " " + currPos ) ; return ; } else { curr = T ; currPos = tup [ i ] [ 2 ] ; } } document . write ( " " ) ; } let N = 5 ; let ranges = [ [ 1 , 5 ] , [ 2 , 10 ] , [ 3 , 10 ] , [ 2 , 2 ] , [ 2 , 15 ] ] ; findIntersectingRange ( N , ranges ) ;
Find the next greater element in a Circular Array | Set 2 | Function to find the NGE for the given circular array [ ] arr ; Initialize stack and nge [ ] array ; Initialize nge [ ] array to - 1 ; Traverse the array ; If stack is not empty and current element is greater than top element of the stack ; Assign next greater element for the top element of the stack ; Pop the top element of the stack ; Print the nge [ ] array ; Driver Code ; Function Call
function printNGE ( arr , n ) { let s = [ ] ; let nge = new Array ( n ) ; let i = 0 ; for ( i = 0 ; i < n ; i ++ ) { nge [ i ] = - 1 ; } i = 0 ; while ( i < 2 * n ) { while ( s . length != 0 && arr [ i % n ] > arr [ s [ s . length - 1 ] ] ) { nge [ s [ s . length - 1 ] ] = arr [ i % n ] ; s . pop ( ) ; } s . push ( i % n ) ; i ++ ; } for ( i = 0 ; i < n ; i ++ ) { document . write ( nge [ i ] + " " ) ; } } let arr = [ 4 , - 2 , 5 , 8 ] ; let N = arr . length ; printNGE ( arr , N ) ;
Count array elements whose product of digits is a Composite Number | javascript program for the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to calculate the product of digits of the given number ; Stores the product of digits ; Extract digits and add to the sum ; Return the product of its digits ; Function to print number of distinct values with digit product as composite ; Initialize set ; Initialize boolean array ; Pre - compute primes ; Traverse array ; Stores the product of digits of the current array element ; If Product of digits is less than or equal to 1 ; If Product of digits is not a prime ; Print the answer ; Given array ; Given size ; Function call
var N = 100005 ; function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; var p , i ; for ( p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function digitProduct ( number ) { var res = 1 ; while ( number > 0 ) { res *= ( number % 10 ) ; number = parseInt ( number / 10 ) ; } return res ; } function DistinctCompositeDigitProduct ( arr , n ) { var output = new Set ( ) ; var prime = Array ( N + 1 ) . fill ( true ) ; SieveOfEratosthenes ( prime , N ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { var ans = digitProduct ( arr [ i ] ) ; if ( ans <= 1 ) { continue ; } if ( prime [ ans ] == false ) { output . add ( ans ) ; } } document . write ( output . size ) ; } var arr = [ 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 ] ; var n = arr . length ; DistinctCompositeDigitProduct ( arr , n ) ;
Count pairs from a given range whose sum is a Prime Number in that range | Function to find all prime numbers in range [ 1 , lmt ] using sieve of Eratosthenes ; segmentedSieve [ i ] : Stores if i is a prime number ( True ) or not ( False ) ; Initialize all elements of segmentedSieve [ ] to false ; Set 0 and 1 as non - prime ; Iterate over the range [ 2 , lmt ] ; If i is a prime number ; Append i into prime ; Set all multiple of i non - prime ; Update Sieve [ j ] ; Function to find all the prime numbers in the range [ low , high ] ; Stores square root of high + 1 ; Stores all the prime numbers in the range [ 1 , lmt ] ; Find all the prime numbers in the range [ 1 , lmt ] ; Stores count of elements in the range [ low , high ] ; segmentedSieve [ i ] : Check if ( i - low ) is a prime number or not ; Traverse the array prime [ ] ; Store smallest multiple of prime [ i ] in the range [ low , high ] ; If lowLim is less than low ; Update lowLim ; Iterate over all multiples of prime [ i ] ; If j not equal to prime [ i ] ; Update segmentedSieve [ j - low ] ; Function to count the number of pairs in the range [ L , R ] whose sum is a prime number in the range [ L , R ] ; segmentedSieve [ i ] : Check if ( i - L ) is a prime number or not ; Stores count of pairs whose sum of elements is a prime and in range [ L , R ] ; Iterate over [ L , R ] ; If ( i - L ) is a prime ; Update cntPairs ; Driver Code
function simpleSieve ( lmt , prime ) { let Sieve = new Array ( lmt + 1 ) ; Sieve . fill ( true ) Sieve [ 0 ] = Sieve [ 1 ] = false ; for ( let i = 2 ; i <= lmt ; ++ i ) { if ( Sieve [ i ] == true ) { prime . push ( i ) ; for ( let j = i * i ; j <= lmt ; j += i ) { Sieve [ j ] = false ; } } } } function SegmentedSieveFn ( low , high ) { let lmt = Math . floor ( Math . sqrt ( high ) ) + 1 ; let prime = new Array ( ) ; simpleSieve ( lmt , prime ) ; let n = high - low + 1 ; let segmentedSieve = new Array ( n + 1 ) . fill ( true ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { let lowLim = Math . floor ( low / prime [ i ] ) * prime [ i ] ; if ( lowLim < low ) { lowLim += prime [ i ] ; } for ( let j = lowLim ; j <= high ; j += prime [ i ] ) { if ( j != prime [ i ] ) { segmentedSieve [ j - low ] = false ; } } } return segmentedSieve ; } function countPairsWhoseSumPrimeL_R ( L , R ) { let segmentedSieve = SegmentedSieveFn ( L , R ) ; let cntPairs = 0 ; for ( let i = L ; i <= R ; i ++ ) { if ( segmentedSieve [ i - L ] ) { cntPairs += Math . floor ( i / 2 ) ; } } return cntPairs ; } let L = 1 , R = 5 ; document . write ( countPairsWhoseSumPrimeL_R ( L , R ) ) ;
Lexicographically smallest permutation of a string that can be reduced to length K by removing K | Function to count the number of zeroes present in the string ; Traverse the string ; Return the count ; Function to rearrange the string s . t the string can be reduced to a length K as per the given rules ; Distribute the count of 0 s and 1 s in segment of length 2 k ; Store string that are initially have formed lexicographically smallest 2 k length substring ; Store the lexicographically smallest string of length n that satisfy the condition ; Insert temp_str into final_str ( n / 2 k ) times and add ( n % 2 k ) characters of temp_str at end ; Return the final string ; Function to reduce the string to length K that follows the given conditions ; If the string contains either 0 s or 1 s then it always be reduced into a K length string ; If the string contains only 0 s 1 s then it always reduces to a K length string ; If K = 1 ; Check whether the given string is K reducing string or not ; Otherwise recursively find the string ; Driver Code ; Function Call
function count_zeroes ( n , str ) { var cnt = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] === " " ) cnt ++ ; } return cnt ; } function kReducingStringUtil ( n , k , str , no_of_zeroes ) { var zeroes_in_2k = parseInt ( ( no_of_zeroes * ( 2 * k ) ) / n ) ; var ones_in_2k = 2 * k - zeroes_in_2k ; var temp_str = " " ; for ( var i = 0 ; i < zeroes_in_2k / 2 ; i ++ ) { temp_str += " " ; } for ( var i = 0 ; i < ones_in_2k ; i ++ ) { temp_str += " " ; } for ( var i = 0 ; i < zeroes_in_2k / 2 ; i ++ ) { temp_str += " " ; } var final_str = " " ; for ( var i = 0 ; i < n / ( 2 * k ) ; i ++ ) { final_str += temp_str ; } for ( var i = 0 ; i < n % ( 2 * k ) ; i ++ ) { final_str += temp_str [ i ] ; } return final_str ; } function kReducingString ( n , k , str ) { var no_of_zeroes = count_zeroes ( n , str ) ; var no_of_ones = n - no_of_zeroes ; if ( no_of_zeroes === 0 no_of_zeroes === n ) { return str ; } if ( k === 1 ) { if ( no_of_zeroes === 0 no_of_zeroes === n ) { return str ; } else { return " " ; } } var check = false ; for ( var i = n / k ; i < n ; i += n / k ) { if ( no_of_zeroes === i no_of_ones === i ) { check = true ; break ; } } if ( check === false ) { return " " ; } return kReducingStringUtil ( n , k , str , no_of_zeroes ) ; } var str = " " ; var K = 4 ; var N = str . length ; document . write ( kReducingString ( N , K , str ) ) ;
Count quadruplets with sum K from given array | Function to return the number of quadruplets having the given sum ; Initialize variables ; Initialize answer ; Store the frequency of sum of first two elements ; Traverse from 0 to N - 1 , where arr [ i ] is the 3 rd element ; All possible 4 th elements ; Sum of last two element ; Frequency of sum of first two elements ; Store frequency of all possible sums of first two elements ; Return the answer ; Given array arr [ ] ; Given sum S ; Function Call
function countSum ( a , n , sum ) { let i , j , k ; let count = 0 ; let m = new Map ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { let temp = a [ i ] + a [ j ] ; if ( temp < sum && m . has ( sum - temp ) ) count += m . get ( sum - temp ) ; } for ( j = 0 ; j < i ; j ++ ) { let temp = a [ i ] + a [ j ] ; if ( temp < sum ) if ( m . has ( temp ) ) m . set ( temp , m . get ( temp ) + 1 ) ; else m . set ( temp , 1 ) ; } } return count ; } let arr = [ 4 , 5 , 3 , 1 , 2 , 4 ] ; let S = 13 ; let N = arr . length ; document . write ( countSum ( arr , N , S ) ) ;
Check if all subarrays contains at least one unique element | Function to check if all subarrays of array have at least one unique element ; Stores frequency of subarray elements ; Generate all subarrays ; Insert first element in map ; Update frequency of current subarray in the HashMap ; Check if at least one element occurs once in current subarray ; If any subarray doesn 't  have unique element ; Clear map for next subarray ; Return Yes if all subarray having at least 1 unique element ; Given array arr [ ] ; Function Call
function check ( arr , n ) { var hm = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { hm . set ( arr [ i ] , 1 ) ; for ( var j = i + 1 ; j < n ; j ++ ) { if ( hm . has ( arr [ j ] ) ) hm . set ( arr [ j ] , hm . get ( arr [ j ] ) + 1 ) ; else hm . set ( arr [ j ] , 1 ) var flag = false ; hm . forEach ( ( value , key ) => { if ( value == 1 ) { flag = true ; } } ) ; if ( ! flag ) return " " ; } hm = new Map ( ) ; } return " " ; } var arr = [ 1 , 2 , 1 ] ; var N = arr . length ; document . write ( check ( arr , N ) ) ;
Search insert position of K in a sorted array | Function to find insert position of K ; Lower and upper bounds ; Traverse the search space ; If K is found ; Return insert position ; Driver Code
function find_index ( arr , n , K ) { let start = 0 ; let end = n - 1 ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( arr [ mid ] == K ) return mid ; else if ( arr [ mid ] < K ) start = mid + 1 ; else end = mid - 1 ; } return end + 1 ; } let arr = [ 1 , 3 , 5 , 6 ] ; let n = arr . length ; let K = 2 ; document . write ( find_index ( arr , n , K ) + " " ) ;
Queries to find minimum swaps required to sort given array with updates | Function to return the position of the given value using binary search ; Return 0 if every value is greater than the given value ; Return N - 1 if every value is smaller than the given value ; Perform Binary Search ; Iterate till start < end ; Find the mid ; Update start and end ; Return the position of the given value ; Function to return the number of make the array sorted ; Index x to update ; Increment value by y ; Set newElement equals to x + y ; Compute the new index ; Print the minimum number of swaps ; Given array arr [ ] ; Given Queries ; Function Call
function computePos ( arr , n , value ) { if ( value < arr [ 0 ] ) return 0 ; if ( value > arr [ n - 1 ] ) return n - 1 ; var start = 0 ; var end = n - 1 ; while ( start < end ) { var mid = parseInt ( ( start + end + 1 ) / 2 ) ; if ( arr [ mid ] >= value ) end = mid - 1 ; else start = mid ; } return start ; } function countShift ( arr , n , queries ) { for ( var i = 0 ; i < queries . length ; i ++ ) { var index = queries [ i ] [ 0 ] ; var update = queries [ i ] [ 1 ] ; var newElement = arr [ index ] + update ; var newIndex = computePos ( arr , n , newElement ) ; document . write ( Math . abs ( newIndex - index ) + " " ) ; } } var arr = [ 2 , 3 , 4 , 5 , 6 ] ; var N = arr . length ; var queries = [ [ 0 , - 1 ] , [ 4 , - 11 ] ] ; countShift ( arr , N , queries ) ;