codeparrot/xlcost-text-to-code huggingface dataset:
Minimum distance to visit given K points on X | Function to find the minimum distance travelled to visit K polet ; Stores minimum distance travelled to visit K polet ; Stores distance travelled to visit polets ; Traverse the array arr [ ] ; If arr [ i ] and arr [ i + K - 1 ] are positive ; Update dist ; Update dist ; Update res ; Driver Code ; initial the array
function MinDistK ( arr , N , K ) { let res = Number . MAX_VALUE ; let dist = 0 ; for ( let i = 0 ; i <= ( N - K ) ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i + K - 1 ] >= 0 ) { dist = Math . max ( arr [ i ] , arr [ i + K - 1 ] ) ; } else { dist = Math . abs ( arr [ i ] ) + Math . abs ( arr [ i + K - 1 ] ) + Math . min ( Math . abs ( arr [ i ] ) , Math . abs ( arr [ i + K - 1 ] ) ) ; } res = Math . min ( res , dist ) ; } return res ; } let K = 3 ; let arr = [ - 30 , - 10 , 10 , 20 , 50 ] ; let N = arr . length ; document . write ( MinDistK ( arr , N , K ) ) ;
Maximize the minimum array element by M subarray increments of size S | Function to return index of minimum element in the array ; Initialize a [ 0 ] as minValue ; Traverse the array ; If a [ i ] < existing minValue ; Return the minimum index ; Function that maximize the minimum element of array after incrementing subarray of size S by 1 , M times ; Iterating through the array or M times ; Find minimum element index ; Increment the minimum value ; Storing the left index and right index ; Incrementing S - 1 minimum elements to the left and right of minValue ; Reached extreme left ; Reached extreme right ; Left value is minimum ; Right value is minimum ; Find the minValue in A [ ] after M operations ; Return the minimum value ; Driver Code ; Function Call
function min1 ( a , n ) { let minIndex = 0 , minValue = a [ 0 ] , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < minValue ) { minValue = a [ i ] ; minIndex = i ; } } return minIndex ; } function maximizeMin ( A , N , S , M ) { let minIndex , left , right , i , j ; for ( i = 0 ; i < M ; i ++ ) { minIndex = min1 ( A , N ) ; A [ minIndex ] ++ ; left = minIndex - 1 ; right = minIndex + 1 ; for ( j = 0 ; j < S - 1 ; j ++ ) { if ( left == - 1 ) A [ right ++ ] ++ ; else if ( right == N ) A [ left -- ] ++ ; else { if ( A [ left ] < A [ right ] ) A [ left -- ] ++ ; else A [ right ++ ] ++ ; } } } minIndex = min1 ( A , N ) ; return A [ minIndex ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let N = arr . length ; let S = 2 , M = 3 ; document . write ( maximizeMin ( arr , N , S , M ) ) ;
Sum of nodes in the path from root to N | Function to find sum of aint nodes from root to N ; If N is equal to 1 ; If N is equal to 2 or 3 ; Stores the number of nodes at ( i + 1 ) - th level ; Stores the number of nodes ; Stores if the current level is even or odd ; If level is odd ; If level is even ; If level with node N is reached ; Push into vector ; Compute prefix sums of count of nodes in each level ; Stores the level in which node N s present ; Stores the required sum ; Add temp to the sum ; Driver code ; Function Call
function sumOfPathNodes ( N ) { if ( N == 1 ) { return 1 ; } else if ( N == 2 N == 3 ) { return N + 1 ; } let arr = [ ] ; arr . push ( 1 ) ; let k = 1 ; let flag = true ; while ( k < N ) { if ( flag == true ) { k *= 2 ; flag = false ; } else { k *= 4 ; flag = true ; } if ( k > N ) { break ; } arr . push ( k ) ; } let len = arr . length ; let prefix = new Array ( len ) ; prefix [ 0 ] = 1 ; for ( let i = 1 ; i < len ; ++ i ) { prefix [ i ] = arr [ i ] + prefix [ i - 1 ] ; } let it = lowerBound ( prefix , 0 , len , N ) + 1 ; let ind = it - prefix [ 0 ] ; let final_ans = 0 ; let temp = N ; while ( ind > 1 ) { let val = temp - prefix [ ind - 1 ] ; if ( ind % 2 != 0 ) { temp = prefix [ ind - 2 ] + parseInt ( ( val + 1 ) / 2 , 10 ) ; } else { temp = prefix [ ind - 2 ] + parseInt ( ( val + 3 ) / 4 , 10 ) ; } -- ind ; final_ans += temp ; } final_ans += ( N + 1 ) ; return final_ans ; } function lowerBound ( a , low , high , element ) { while ( low < high ) { let middle = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } let N = 13 ; document . write ( sumOfPathNodes ( N ) + " " ) ;
Check if given point lies in range of any of the given towers | Function to check if the point ( X , Y ) exists in the towers network - range or not ; Traverse the array arr [ ] ; Stores distance of the point ( X , Y ) from i - th tower ; If dist lies within the range of the i - th tower ; If the point ( X , Y ) does not lie in the range of any of the towers ; Driver Code ; If point ( X , Y ) lies in the range of any of the towers ; Otherwise
function checkPointRange ( arr , X , Y , N ) { for ( let i = 0 ; i < N ; i ++ ) { let dist = Math . sqrt ( ( arr [ i ] [ 0 ] - X ) * ( arr [ i ] [ 0 ] - X ) + ( arr [ i ] [ 1 ] - Y ) * ( arr [ i ] [ 1 ] - Y ) ) ; if ( dist <= arr [ i ] [ 2 ] ) { return true ; } } return false ; } let arr = [ [ 1 , 1 , 3 ] , [ 10 , 10 , 3 ] , [ 15 , 15 , 15 ] ] ; let X = 5 , Y = 5 ; let N = arr . length ; if ( checkPointRange ( arr , X , Y , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find the winner of the Game of removing odd or replacing even array elements | Function to evaluate the winner of the game ; Stores count of odd array elements ; Stores count of even array elements ; Traverse the array ; If array element is odd ; Otherwise ; If count of even is zero ; If count of odd is even ; If count of odd is odd ; If count of odd is odd and count of even is one ; Otherwise ; Driver Code
function findWinner ( arr , N ) { let odd = 0 ; let even = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == 0 ) { if ( odd % 2 == 0 ) { document . write ( " " ) ; } else if ( odd % 2 == 1 ) { document . write ( " " ) ; } } else if ( even == 1 && odd % 2 == 1 ) { document . write ( " " ) ; } else { document . write ( - 1 ) ; } } let arr = [ 3 , 1 , 9 , 7 ] ; let N = arr . length ; findWinner ( arr , N ) ;
Rearrange array to maximize count of local minima | Function to rearrange array elements to maximize count of local minima in the array ; Sort the array in ascending order ; Stores index of left pointer ; Stores index of right pointer ; Traverse the array elements ; If right is less than N ; Print array element ; Update right ; Print array element ; Update left ; Driver Code
function rearrangeArrMaxcntMinima ( arr , N ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let left = 0 ; let right = Math . floor ( N / 2 ) ; while ( left < Math . floor ( N / 2 ) right < N ) { if ( right < N ) { document . write ( arr [ right ] + " " ) ; right ++ ; } if ( left < Math . floor ( N / 2 ) ) { document . write ( arr [ left ] + " " ) ; left ++ ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; rearrangeArrMaxcntMinima ( arr , N ) ;
Minimum jumps required to make a group of persons sit together | Javascript program for the above approach ; Function to find the minimum jumps required to make the whole group sit adjacently ; Store the indexes ; Stores the count of occupants ; Length of the string ; Traverse the seats ; If current place is occupied ; Push the current position in the vector ; Base Case : ; The index of the median element ; The value of the median element ; Traverse the position [ ] ; Update the ans ; Return the final count ; Given arrange of seats ; Function Call
let MOD = 1e9 + 7 ; function minJumps ( seats ) { let position = [ ] ; let count = 0 ; let len = seats . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( seats [ i ] == ' ' ) { position . push ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; let med_index = parseInt ( ( count - 1 ) / 2 , 10 ) ; let med_val = position [ med_index ] ; let ans = 0 ; for ( let i = 0 ; i < position . length ; i ++ ) { ans = ( ans % MOD + Math . abs ( position [ i ] - med_val ) % MOD ) % MOD ; } return ans % MOD ; } let S = " " ; document . write ( minJumps ( S ) ) ;
Count pairs whose product contains single distinct prime factor | Function to find a single distinct prime factor of N ; Stores distinct prime factors of N ; Calculate prime factor of N ; Calculate distinct prime factor ; Insert i into disPrimeFact ; Update N ; If N is not equal to 1 ; Insert N into disPrimeFact ; If N contains a single distinct prime factor ; Return single distinct prime factor of N ; If N contains more than one distinct prime factor ; Function to count pairs in the array whose product contains only single distinct prime factor ; Stores count of 1 s in the array ; mp [ i ] : Stores count of array elements whose distinct prime factor is only i ; Traverse the array arr [ ] ; If current element is 1 ; Store distinct prime factor of arr [ i ] ; If arr [ i ] contains more than one prime factor ; If arr [ i ] contains a single prime factor ; Stores the count of pairs whose product of elements contains only a single distinct prime factor ; Traverse the map mp [ ] ; Stores count of array elements whose prime factor is ( it . first ) ; Update res ; Driver Code
function singlePrimeFactor ( N ) { var disPrimeFact = { } ; for ( var i = 2 ; i * i <= N ; ++ i ) { while ( N % i === 0 ) { disPrimeFact [ i ] = 1 ; N = parseInt ( N / i ) ; } } if ( N !== 1 ) { disPrimeFact [ N ] = 1 ; } if ( Object . keys ( disPrimeFact ) . length === 1 ) { for ( const [ key , value ] of Object . entries ( disPrimeFact ) ) { return key ; } } return - 1 ; } function cntsingleFactorPair ( arr , N ) { var countOf1 = 0 ; var mp = { } ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] === 1 ) { countOf1 ++ ; continue ; } var factorValue = singlePrimeFactor ( arr [ i ] ) ; if ( factorValue === - 1 ) { continue ; } else { if ( mp . hasOwnProperty ( factorValue ) ) mp [ factorValue ] = mp [ factorValue ] + 1 ; else mp [ factorValue ] = 1 ; } } var res = 0 ; for ( const [ key , value ] of Object . entries ( mp ) ) { var X = value ; res = parseInt ( res + countOf1 * X + ( X * ( X - 1 ) ) / 2 ) ; } return res ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( cntsingleFactorPair ( arr , N ) ) ;
Minimize replacements or swapping of same indexed characters required to make two given strings palindromic | Function to find minimum operations to make both the strings palindromic ; Stores index of the left pointer ; Stores index of the right pointer ; Stores count of minimum operations to make both the strings palindromic ; if str1 [ i ] equal to str1 [ j ] and str2 [ i ] not equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] not equal to str1 [ j ] and str2 [ i ] equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] is not equal to str1 [ j ] and str2 [ i ] is not equal to str2 [ j ] ; If str1 [ i ] is equal to str2 [ j ] and str2 [ i ] is equal to str1 [ j ] ; Update cntOp ; Update cntOp ; Update i and j ; Driver Code ; Stores length of str1
function MincntBothPalin ( str1 , str2 , N ) { var i = 0 ; var j = N - 1 ; var cntOp = 0 ; while ( i < j ) { if ( str1 [ i ] === str1 [ j ] && str2 [ i ] !== str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] !== str1 [ j ] && str2 [ i ] === str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] !== str1 [ j ] && str2 [ i ] !== str2 [ j ] ) { if ( str1 [ i ] === str2 [ j ] && str2 [ i ] === str1 [ j ] ) { cntOp += 1 ; } else { cntOp += 2 ; } } i += 1 ; j -= 1 ; } return cntOp ; } var str1 = " " ; var str2 = " " ; var N = str1 . length ; document . write ( MincntBothPalin ( str1 , str2 , N ) ) ;
Find an integral solution of the non | Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X . ; Update xMax ; Stores maximum possible of Y . ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code
function power ( x , N ) { let res = 1 ; while ( N > 0 ) { if ( ( N & 1 ) != 0 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } function findValX_Y ( N ) { if ( N <= 1 ) { document . write ( - 1 ) ; return ; } let xMax ; xMax = Math . log ( N ) ; let yMax ; yMax = Math . log ( N ) / Math . log ( 5.0 ) ; for ( let i = 1 ; i <= xMax ; i ++ ) { for ( let j = 1 ; j <= yMax ; j ++ ) { let a = power ( 2 , i ) ; let b = power ( 5 , j ) ; if ( a + b == N ) { document . write ( i + " " + j ) ; return ; } } } document . write ( " " ) ; } let N = 129 ; findValX_Y ( N ) ;
Count pairs from two arrays with difference exceeding K | set 2 | Function to count pairs that satisfy the given conditions ; Stores the count of pairs ; If v1 [ ] is smaller than v2 [ ] ; Sort the array v1 [ ] ; Traverse the array v2 [ ] ; Returns the address of the first number which is >= v2 [ j ] - k ; Increase the count by all numbers less than v2 [ j ] - k ; Otherwise ; Sort the array v2 [ ] ; Traverse the array v1 [ ] ; Returns the address of the first number which is > v1 [ i ] + k ; Increase the count by all numbers greater than v1 [ i ] + k ; Return the total count of pairs ; Driver Code
function countPairs ( v1 , v2 , n , m , k ) { let count = 0 ; if ( n <= m ) { v1 . sort ( ) ; for ( let j = 0 ; j < m ; j ++ ) { let index = lowerBound ( v1 , 0 , n , v2 [ j ] - k ) ; count += index ; } } else { v2 . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let index = upperBound ( v2 , 0 , m , v1 [ i ] + k ) ; count += m - index ; } } return count ; } function lowerBound ( a , low , high , element ) { while ( low < high ) { let middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function upperBound ( a , low , high , element ) { while ( low < high ) { let middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let arr = [ 5 , 9 , 1 , 8 ] ; let brr = [ 10 , 12 , 7 , 4 , 2 , 3 ] ; let K = 3 ; let N = arr . length ; let M = brr . length ; document . write ( countPairs ( arr , brr , N , M , K ) ) ;
Print siblings of a given Node in N | Structure of a node of N - ary tree ; Function to create a new node ; Function to find the siblings of the node / value ; Stores nodes level wise ; Push the root ; Continue until all levels are traversed ; Stores current node ; Enqueue all children of the current node ; If node value is found ; Print all children of current node except value as the answer ; Push the child nodes of temp into the queue ; Stores root of the constructed tree ; Print siblings of Node X
class Node { constructor ( key ) { this . child = [ ] ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return temp ; } function Siblings ( root , value ) { let flag = 0 ; if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( q . length > 0 ) { let temp = q [ 0 ] ; q . shift ( ) ; for ( let i = 0 ; i < temp . child . length ; i ++ ) { if ( temp . child [ i ] . key == value ) { flag = 1 ; for ( let j = 0 ; j < temp . child . length ; j ++ ) { if ( value != temp . child [ j ] . key ) document . write ( temp . child [ j ] . key + " " ) ; } break ; } q . push ( temp . child [ i ] ) ; } } if ( flag == 0 ) document . write ( " " ) ; } function constructTree ( ) { let root = newNode ( 10 ) ; ( root . child ) . push ( newNode ( 20 ) ) ; ( root . child ) . push ( newNode ( 30 ) ) ; ( root . child ) . push ( newNode ( 40 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 50 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 60 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 70 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 80 ) ) ; ( root . child [ 2 ] . child ) . push ( newNode ( 90 ) ) ; ( root . child [ 2 ] . child ) . push ( newNode ( 100 ) ) ; ( root . child [ 2 ] . child ) . push ( newNode ( 110 ) ) ; return root ; } let root = constructTree ( ) ; let X = 30 ; Siblings ( root , X ) ;
Maximum sum subarray of size K with sum less than X | Function to calculate maximum sum among all subarrays of size K with the sum less than X ; Initialize sum_K to 0 ; Calculate sum of first K elements ; If sum_K is less than X ; Initialize MaxSum with sum_K ; Iterate over the array from ( K + 1 ) - th index ; Subtract the first element from the previous K elements and add the next element ; If sum_K is less than X ; Update the Max_Sum ; Driver Code ; Size of Array ; Function Call
function maxSumSubarr ( A , N , K , X ) { let sum_K = 0 ; for ( let i = 0 ; i < K ; i ++ ) { sum_K += A [ i ] ; } let Max_Sum = 0 ; if ( sum_K < X ) { Max_Sum = sum_K ; } for ( let i = K ; i < N ; i ++ ) { sum_K -= ( A [ i - K ] - A [ i ] ) ; if ( sum_K < X ) { Max_Sum = Math . max ( Max_Sum , sum_K ) ; } } document . write ( Max_Sum ) ; } let arr = [ - 5 , 8 , 7 , 2 , 10 , 1 , 20 , - 4 , 6 , 9 ] ; let K = 5 ; let X = 30 ; let N = arr . length ; maxSumSubarr ( arr , N , K , X ) ;
Rearrange array to make sum of all subarrays starting from first index non | Function to rearrange the array such that sum of all elements of subarrays from the 1 st index is non - zero ; Initialize sum of subarrays ; Sum of all elements of array ; If sum is 0 , the required array could never be formed ; If sum is non zero , array might be formed ; Sort array in ascending order ; When current subarray sum becomes 0 replace it with the largest element ; Swap Operation ; If largest element is same as element to be replaced , then rearrangement impossible ; If b = 1 , then rearrangement is not possible . Hence check with reverse configuration ; Sort array in descending order ; When current subarray sum becomes 0 replace it with the smallest element ; Swap Operation ; If smallest element is same as element to be replaced , then rearrangement impossible ; If neither of the configurations worked then print " - 1" ; Otherwise , print the formed rearrangement ; Given array ; Size of array ; Function Call
function rearrangeArray ( a , N ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } if ( sum == 0 ) { document . write ( " " ) ; return ; } sum = 0 ; let b = 0 ; a . sort ( ) ; for ( let i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( sum == 0 ) { if ( a [ i ] != a [ N - 1 ] ) { sum -= a [ i ] ; let temp = a [ i ] ; a [ i ] = a [ N - 1 ] ; a [ N - 1 ] = temp ; sum += a [ i ] ; } else { b = 1 ; break ; } } } if ( b == 1 ) { b = 0 ; sum = 0 ; a . sort ( ) ; for ( let i = N - 1 ; i >= 0 ; i -- ) { sum += a [ i ] ; if ( sum == 0 ) { if ( a [ i ] != a [ 0 ] ) { sum -= a [ i ] ; let temp = a [ i ] ; a [ i ] = a [ 0 ] ; a [ 0 ] = temp ; sum += a [ i ] ; } else { b = 1 ; break ; } } } } if ( b == 1 ) { document . write ( " " + " " ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let arr = [ 1 , - 1 , 2 , 4 , 0 ] ; let N = arr . length ; rearrangeArray ( arr , N ) ;
Length of smallest subarray to be removed to make sum of remaining elements divisible by K | Function to find the length of the smallest subarray to be removed such that sum of elements is divisible by K ; Stores the remainder of each arr [ i ] when divided by K ; Stores total sum of elements ; K has been added to each arr [ i ] to handle - ve integers ; Update the total sum ; Remainder when total_sum is divided by K ; If given array is already divisible by K ; Stores curr_remainder and the most recent index at which curr_remainder has occurred ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; If not possible ; Print the result ; Given array arr [ ] ; Size of array ; Given K ; Function Call
function removeSmallestSubarray ( arr , n , k ) { let mod_arr = new Array ( n ) ; let total_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { mod_arr [ i ] = ( arr [ i ] + k ) % k ; total_sum += arr [ i ] ; } let target_remainder = total_sum % k ; if ( target_remainder == 0 ) { document . write ( " " ) ; return ; } let map1 = new Map ( ) ; map1 . set ( 0 , - 1 ) ; let curr_remainder = 0 ; let res = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 . set ( curr_remainder , i ) ; let mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . has ( mod ) ) res = Math . min ( res , i - map1 . get ( mod ) ) ; } if ( res == Number . MAX_SAFE_INTEGER res == n ) { res = - 1 ; } document . write ( res ) ; } let arr = [ 3 , 1 , 4 , 2 ] ; let N = arr . length ; let K = 6 ; removeSmallestSubarray ( arr , N , K ) ;
Queries to flip characters of a binary string in given range | Function to find the binary String by performing all the given queries ; Stores length of the String ; prefixCnt [ i ] : Stores number of times str [ i ] toggled by performing all the queries ; Update prefixCnt [ Q [ i ] [ 0 ] ] ; Update prefixCnt [ Q [ i ] [ 1 ] + 1 ] ; Calculate prefix sum of prefixCnt [ i ] ; Traverse prefixCnt [ ] array ; If ith element toggled odd number of times ; Toggled i - th element of binary String ; Driver Code
function toggleQuery ( str , Q , M ) { let N = str . length ; let prefixCnt = new Array ( N + 1 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) { prefixCnt [ i ] = 0 ; } for ( let i = 0 ; i < M ; i ++ ) { prefixCnt [ Q [ i ] [ 0 ] ] += 1 ; prefixCnt [ Q [ i ] [ 1 ] + 1 ] -= 1 ; } for ( let i = 1 ; i <= N ; i ++ ) { prefixCnt [ i ] += prefixCnt [ i - 1 ] ; } for ( let i = 0 ; i < N ; i ++ ) { if ( prefixCnt [ i ] % 2 == 1 ) { str [ i ] = String . fromCharCode ( ' ' . charCodeAt ( 0 ) - str [ i ] . charCodeAt ( 0 ) + ' ' . charCodeAt ( 0 ) ) ; } } return ( str ) . join ( " " ) ; } let str = " " ; let Q = [ [ 0 , 1 ] , [ 2 , 5 ] , [ 2 , 3 ] , [ 1 , 4 ] , [ 0 , 5 ] ] ; let M = Q . length ; document . write ( toggleQuery ( str . split ( " " ) , Q , M ) ) ;
Maximum length possible by cutting N given woods into at least K pieces | Function to check if it is possible to cut woods into K pieces of length len ; Stores count of pieces having length equal to K ; Traverse wood array ; Update count ; Function to find the maximum value of L ; Stores minimum possible of L ; Stores maximum possible value of L ; Apply binary search over the range [ left , right ] ; Stores mid value of left and right ; If it is possible to cut woods into K pieces having length of each piece equal to mid ; Update left ; Update right ; Driver Code
function isValid ( wood , N , len , K ) { var count = 0 ; for ( i = 0 ; i < N ; i ++ ) { count += parseInt ( wood [ i ] / len ) ; } return count >= K ; } function findMaxLen ( wood , N , K ) { var left = 1 ; var right = Math . max . apply ( Math , wood ) ; while ( left <= right ) { var mid = left + ( right - left ) / 2 ; if ( isValid ( wood , N , mid , K ) ) { left = mid + 1 ; } else { right = mid - 1 ; } } return right ; } var wood = [ 5 , 9 , 7 ] ; var N = wood . length ; var K = 4 ; document . write ( findMaxLen ( wood , N , K ) ) ;
Count quadruplets with sum K from given array | Function to return the number of quadruplets with the given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second elements ; All possible third elements ; All possible fourth elements ; Increment counter by 1 if quadruplet sum is S ; Return the final count ; Given array arr [ ] ; Given sum S ; Function Call
function countSum ( a , n , sum ) { let i , j , k , l ; let count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { for ( k = j + 1 ; k < n - 1 ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ i ] + a [ j ] + a [ k ] + a [ l ] == sum ) count ++ ; } } } } return count ; } let arr = [ 4 , 5 , 3 , 1 , 2 , 4 ] ; let S = 13 ; let N = arr . length ; document . write ( countSum ( arr , N , S ) ) ;