codeparrot/xlcost-text-to-code huggingface dataset:
Check if all rows of a Binary Matrix have all ones placed adjacently or not | Function to check if all 1 s are placed adjacently in an array or not ; Base Case ; Stores the sum of XOR of all pair of adjacent elements ; Calculate sum of XOR of all pair of adjacent elements ; Check for corner cases ; Return true ; Function to check if all the rows have all 1 s grouped together or not ; Traverse each row ; Check if all 1 s are placed together in the ith row or not ; Function to check if all 1 s in a row are grouped together in a matrix or not ; Print the result ; Given matrix ; Function Call
function checkGroup ( arr ) { if ( arr . length <= 2 ) return true ; let corner = arr [ 0 ] + arr [ arr . length - 1 ] ; let xorSum = 0 ; for ( let i = 0 ; i < arr . length - 1 ; i ++ ) xorSum += ( arr [ i ] ^ arr [ i + 1 ] ) ; if ( corner == 0 ) if ( xorSum > 2 ) return false ; else if ( corner == 1 ) if ( xorSum > 1 ) return false ; else if ( xorSum > 0 ) return false ; return true ; } function isInGroupUtil ( mat ) { for ( let i = 0 ; i < mat . length ; i ++ ) { let arr = [ ] for ( let j = 0 ; j < mat [ i ] . length ; j ++ ) { arr . push ( mat [ i ] [ j ] ) ; } if ( ! checkGroup ( arr ) ) return false ; } return true ; } function isInGroup ( mat ) { let ans = isInGroupUtil ( mat ) ; if ( ans ) document . write ( " " ) ; else document . write ( " " ) ; } let mat = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 ] ] ; isInGroup ( mat ) ;
Print all numbers up to N having product of digits equal to K | Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to prlet all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Prlet that number ; If no numbers are found ; Given value of N & K ; Function call to prlet all numbers from [ 1 , N ] with product of digits K
function productOfDigits ( N ) { let product = 1 ; while ( N != 0 ) { product = product * ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } return product ; } function productOfDigitsK ( N , K ) { let flag = 0 ; for ( let i = 1 ; i <= N ; ++ i ) { if ( K == productOfDigits ( i ) ) { document . write ( i + " " ) ; flag = 1 ; } } if ( flag == 0 ) document . write ( - 1 ) ; } let N = 500 , K = 10 ; productOfDigitsK ( N , K ) ;
Minimize difference between maximum and minimum array elements by removing a K | Function to minimize difference between maximum and minimum array elements by removing a K - length subarray ; Size of array ; Stores the maximum and minimum in the suffix subarray [ i . . N - 1 ] ; Traverse the array ; Stores the maximum and minimum in the prefix subarray [ 0 . . i - 1 ] ; Store the minimum difference ; Traverse the array ; If the suffix doesn 't exceed  the end of the array ; Store the maximum element in array after removing subarray of size K ; Stores the maximum element in array after removing subarray of size K ; Update minimum difference ; Updating the maxPrefix and minPrefix with current element ; Print the minimum difference ; Driver Code
function minimiseDifference ( arr , K ) { var N = arr . length ; var maxSuffix = new Array ( N + 1 ) ; var minSuffix = new Array ( N + 1 ) ; maxSuffix [ N ] = - 1e9 ; minSuffix [ N ] = 1e9 ; maxSuffix [ N - 1 ] = arr [ N - 1 ] ; minSuffix [ N - 1 ] = arr [ N - 1 ] ; for ( var i = N - 2 ; i >= 0 ; -- i ) { maxSuffix [ i ] = Math . max ( maxSuffix [ i + 1 ] , arr [ i ] ) ; minSuffix [ i ] = Math . min ( minSuffix [ i + 1 ] , arr [ i ] ) ; } var maxPrefix = arr [ 0 ] ; var minPrefix = arr [ 0 ] ; var minDiff = maxSuffix [ K ] - minSuffix [ K ] ; for ( var i = 1 ; i < N ; ++ i ) { if ( i + K <= N ) { var maximum = Math . max ( maxSuffix [ i + K ] , maxPrefix ) ; var minimum = Math . min ( minSuffix [ i + K ] , minPrefix ) ; minDiff = Math . min ( minDiff , maximum - minimum ) ; } maxPrefix = Math . max ( maxPrefix , arr [ i ] ) ; minPrefix = Math . min ( minPrefix , arr [ i ] ) ; } document . write ( minDiff + " " ) ; } var arr = [ 4 , 5 , 8 , 9 , 1 , 2 ] ; var K = 2 ; minimiseDifference ( arr , K ) ;
Longest substring where all the characters appear at least K times | Set 3 | Function to find the length of the longest substring ; Store the required answer ; Create a frequency map of the characters of the string ; Store the length of the string ; Traverse the string , s ; Increment the frequency of the current character by 1 ; Stores count of unique characters ; Find the number of unique characters in string ; Iterate in range [ 1 , unique ] ; Initialize frequency of all characters as 0 ; Stores the start and the end of the window ; Stores the current number of unique characters and characters occuring atleast K times ; New unique character ; New character which occurs atleast k times ; Expand window by incrementing end by 1 ; Check if this character is present atleast k times ; Check if this character is unique ; Shrink the window by incrementing start by 1 ; If there are curr_unique characters and each character is atleast k times ; Update the overall maximum length ; Print the answer ; Driver Code
function longestSubstring ( s , k ) { let ans = 0 ; let freq = new Array ( 26 ) ; freq . fill ( 0 ) ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) freq [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let unique = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] != 0 ) unique ++ ; for ( let curr_unique = 1 ; curr_unique <= unique ; curr_unique ++ ) { for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } let start = 0 , end = 0 ; let cnt = 0 , count_k = 0 ; while ( end < n ) { if ( cnt <= curr_unique ) { let ind = s [ end ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( freq [ ind ] == 0 ) cnt ++ ; freq [ ind ] ++ ; if ( freq [ ind ] == k ) count_k ++ ; end ++ ; } else { let ind = s [ start ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( freq [ ind ] == k ) count_k -- ; freq [ ind ] -- ; if ( freq [ ind ] == 0 ) cnt -- ; start ++ ; } if ( cnt == curr_unique && count_k == curr_unique ) ans = Math . max ( ans , end - start ) ; } } document . write ( ans ) ; } let S = " " ; let K = 3 ; longestSubstring ( S , K ) ;
Print matrix elements using DFS traversal | Direction vectors ; Function to check if current position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited or not ; Utility function to print matrix elements using DFS Traversal ; Mark the current cell visited ; Print the element at the cell ; Traverse all four adjacent cells of the current element ; Check if x and y is valid index or not ; Function to print the matrix elements ; Initialize a visiting matrix ; Function call to print matrix elements by DFS traversal ; Given matrix ; Row of the matrix ; Column of the matrix
let dRow = [ - 1 , 0 , 1 , 0 ] ; let dCol = [ 0 , 1 , 0 , - 1 ] ; function isValid ( vis , row , col , COL , ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } function DFSUtil ( row , col , grid , vis , M , N ) { vis [ row ] [ col ] = true ; document . write ( grid [ row ] [ col ] + " " ) ; for ( let i = 0 ; i < 4 ; i ++ ) { let x = row + dRow [ i ] ; let y = col + dCol [ i ] ; if ( isValid ( vis , x , y , M , N ) ) DFSUtil ( x , y , grid , vis , M , N ) ; } } function DFS ( row , col , grid , M , N ) { let vis = new Array ( M + 1 ) ; for ( var i = 0 ; i < vis . length ; i ++ ) { vis [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < M + 1 ; i ++ ) { for ( let j = 0 ; j < N + 1 ; j ++ ) { vis [ i ] [ j ] = false ; } } DFSUtil ( 0 , 0 , grid , vis , M , N ) ; } let grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; let M = grid . length ; let N = grid [ 0 ] . length ; DFS ( 0 , 0 , grid , M , N ) ;
Print matrix elements using DFS traversal | Direction vectors ; Function to check if curruent position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited ; Function to print the matrix elements ; Stores if a position in the matrix been visited or not ; Initialize stack to implement DFS ; Push the first position of grid [ ] [ ] in the stack ; Mark the cell ( 0 , 0 ) visited ; Stores top element of stack ; Delete the top ( ) element of stack ; Print element at the cell ; Traverse in all four adjacent sides of current positions ; Check if x and y is valid position and then push the position of current cell in the stack ; Push the current cell ; Mark current cell visited ; Given matrix ; Row of the matrix ; Column of the matrix
let dRow = [ - 1 , 0 , 1 , 0 ] ; let dCol = [ 0 , 1 , 0 , - 1 ] ; let vis ; function isValid ( row , col , COL , ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } function DFS_iterative ( grid , M , N ) { vis = [ ] ; for ( let i = 0 ; i < M + 5 ; i ++ ) { vis . push ( [ ] ) ; for ( let j = 0 ; j < N + 5 ; j ++ ) { vis [ i ] . push ( false ) ; } } let st = [ ] ; st . push ( [ 0 , 0 ] ) ; vis [ 0 ] [ 0 ] = true ; while ( st . length > 0 ) { let p = st [ st . length - 1 ] ; st . pop ( ) ; let row = p [ 0 ] ; let col = p [ 1 ] ; document . write ( grid [ row ] [ col ] + " " ) ; for ( let i = 0 ; i < 4 ; i ++ ) { let x = row + dRow [ i ] ; let y = col + dCol [ i ] ; if ( isValid ( x , y , M , N ) ) { st . push ( [ x , y ] ) ; vis [ x ] [ y ] = true ; } } } } let grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; let M = grid . length ; let N = grid [ 0 ] . length ; DFS_iterative ( grid , M , N ) ;
Segregate 1 s and 0 s in separate halves of a Binary String | Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Given string ; Length of the string ; Prints the minimum count of operations required
function minOps ( s , N ) { var ans = 0 ; for ( i = 1 ; i < N ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) { ans ++ ; } } if ( ans % 2 == 1 ) { document . write ( ( ans - 1 ) / 2 ) ; return ; } document . write ( ans / 2 ) ; } var str = " " ; var N = str . length ; minOps ( str , N ) ;
Count prime triplets upto N having sum of first two elements equal to the third number | Javascript program for the above approach ; Boolean array to mark prime numbers ; To count the prime triplets having the sum of the first two numbers equal to the third element ; Function to count prime triplets having sum of the first two elements equal to the third element ; Sieve of Eratosthenes ; Checks for the prime numbers having difference equal to2 ; Update count of triplets ; Print the answer ; Driver Code
let MAX = 1000001 let prime = new Array ( MAX ) . fill ( true ) ; let cntTriplet = new Array ( MAX ) . fill ( 0 ) ; function primeTriplet ( N ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( let i = 3 ; i <= N ; i ++ ) { for ( let i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( let i = 3 ; i <= N ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) { cntTriplet [ i ] = cntTriplet [ i - 1 ] + 1 ; } else { cntTriplet [ i ] = cntTriplet [ i - 1 ] ; } } document . write ( cntTriplet [ N ] ) ; } let N = 7 ; primeTriplet ( N ) ;
Find index of the element differing in parity with all other array elements | Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ; Given array ; Size of the array
function OddOneOut ( arr , N ) { let e = new Map ( ) ; let o = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { e . set ( arr [ i ] , i ) ; } else { o . set ( arr [ i ] , i ) ; } } if ( e . size == 1 ) { document . write ( Array . from ( e . values ( ) ) [ 0 ] ) ; } else { document . write ( Array . from ( o . values ( ) ) [ 0 ] ) ; } } let arr = [ 2 , 4 , 7 , 8 , 10 ] ; let N = arr . length ; OddOneOut ( arr , N ) ;
Count indices where the maximum in the prefix array is less than that in the suffix array | in which the maximum in prefix arrays is less than that in the suffix array ; If size of array is 1 ; pre [ ] : Prefix array suf [ ] : Suffix array ; Stores the required count ; Find the maximum in prefix array ; Find the maximum in suffix array ; Traverse the array ; If maximum in prefix array is less than maximum in the suffix array ; Print the answer ; Driver code ; Function Call
function count ( a , n ) { if ( n == 1 ) { document . write ( 0 ) ; return ; } let pre = new Array ( n - 1 ) ; let suf = new Array ( n - 1 ) ; let max = a [ 0 ] ; let ans = 0 , i ; pre [ 0 ] = a [ 0 ] ; for ( i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; pre [ i ] = max ; } max = a [ n - 1 ] ; suf [ n - 2 ] = a [ n - 1 ] ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( a [ i ] > max ) max = a [ i ] ; suf [ i - 1 ] = max ; } for ( i = 0 ; i < n - 1 ; i ++ ) { if ( pre [ i ] < suf [ i ] ) ans ++ ; } document . write ( ans ) ; } let arr = [ 2 , 3 , 4 , 8 , 1 , 4 ] ; let N = arr . length ; count ( arr , N ) ;
Length of the longest subsequence such that XOR of adjacent elements is equal to K | Function to find maximum length of subsequence ; Stores maximum length of subsequence ; Dictionary to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Retrieve the longest length of subsequence ending at integer [ ] a ^ K ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in Dictionary ; Return the ans if ans >= 2. Otherwise , return 0 ; Input ; Print the length of the longest subsequence
function xorSubsequence ( a , n , k ) { var ans = 0 ; var map = new Map ( ) ; var dp = Array ( n ) . fill ( 0 ) ; map . set ( a [ 0 ] , 1 ) dp [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { var dpj ; if ( map . has ( a [ i ] ^ k ) ) { dpj = map . get ( a [ i ] ^ k ) ; } else { dpj = - 1 ; } if ( dpj != 0 ) dp [ i ] = Math . max ( dp [ i ] , dpj + 1 ) ; ans = Math . max ( ans , dp [ i ] ) ; if ( map . has ( a [ i ] ) ) { map . set ( a [ i ] , Math . max ( map . get ( a [ i ] ) + 1 , dp [ i ] ) ) ; } else { map . set ( a [ i ] , Math . max ( 1 , dp [ i ] ) ) ; } } return ans >= 2 ? ans : 0 ; } var arr = [ 3 , 2 , 4 , 3 , 5 ] ; var N = arr . length ; var K = 1 ; document . write ( xorSubsequence ( arr , N , K ) ) ;
Sum of subtree depths for every node of a given Binary Tree | JavaScript program for the above approach ; Binary tree node ; Function to allocate a new node with the given data and null in its left and right pointers ; DFS function to calculate the sum of depths of all subtrees depth sum ; Store total number of node in its subtree and total sum of depth in its subtree ; Check if left is not null ; Call recursively the DFS function for left child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of noded in left subtree ; Check if right is not null ; Call recursively the DFS function for right child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of nodes in right subtree ; Increment the result by total sum of depth in current subtree ; Return p ; Given Tree ; Print the result
var ans ; class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } class TreeNode { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } function newNode ( data ) { var Node = new TreeNode ( ) ; Node . data = data ; Node . left = null ; Node . right = null ; return Node ; } function sumofsubtree ( root ) { var p = new pair ( 1 , 0 ) ; if ( root . left != null ) { var ptemp = sumofsubtree ( root . left ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } if ( root . right != null ) { var ptemp = sumofsubtree ( root . right ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } ans += p . second ; return p ; } var root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; ans = 0 ; sumofsubtree ( root ) ; document . write ( ans ) ;
Minimum time required to fill given N slots | Function to return the minimum time to fill all the slots ; Stores visited slots ; Checks if a slot is visited or not ; Insert all filled slots ; Iterate until queue is not empty ; Iterate through all slots in the queue ; Front index ; If previous slot is present and not visited ; If next slot is present and not visited ; Increment the time at each level ; Print the answer ; Driver Code ; Function Call
function minTime ( arr , N , K ) { var q = [ ] ; var vis = Array ( N + 1 ) . fill ( false ) ; var time = 0 ; for ( var i = 0 ; i < K ; i ++ ) { q . push ( arr [ i ] ) ; vis [ arr [ i ] ] = true ; } while ( q . length > 0 ) { for ( var i = 0 ; i < q . length ; i ++ ) { var curr = q [ 0 ] ; q . pop ( ) ; if ( curr - 1 >= 1 && ! vis [ curr - 1 ] ) { vis [ curr - 1 ] = true ; q . push ( curr - 1 ) ; } if ( curr + 1 <= N && ! vis [ curr + 1 ] ) { vis [ curr + 1 ] = true ; q . push ( curr + 1 ) ; } } time ++ ; } document . write ( time - 1 ) ; } var N = 6 ; var arr = [ 2 , 6 ] ; var K = arr . length ; minTime ( arr , N , K ) ;
Count of integers having difference with its reverse equal to D | Maximum digits in N ; Utility function to find count of N such that N + D = reverse ( N ) ; If d is a multiple of 9 , no such value N found ; Divide D by 9 check reverse of number and its sum ; B [ i ] : Stores power of ( 10 , i ) ; Calculate power ( 10 , i ) ; Update B [ i ] ; Stores count of N such that N + D = reverse ( N ) ; Iterate over the range [ 1 , MAXL ] ; Update ans ; Function to find count of possible values of N such that N + D = reverse ( N ) ; Base Case ; If D is not qual to 0 ; Stores count of possible values of N such that N + D = reverse ( N ) ; Update ans ; If l is even ; Update ans ; Stores count of possible values of N such that N + D = reverse ( N ) ; Iterae over the range [ - 9 , 9 ] ; Update ans ; Driver Code ; Function call
let MAXL = 17 ; let N ; let v = [ ] function findN ( D ) { if ( D % 9 != 0 ) return 0 ; D /= 9 ; let B = new Array ( MAXL ) . fill ( 0 ) ; B [ 0 ] = 1 ; for ( let i = 1 ; i < MAXL ; i ++ ) { B [ i ] = B [ i - 1 ] * 10 ; } let ans = 0 ; for ( let i = 1 ; i <= MAXL ; i ++ ) { N = Math . floor ( ( i + 1 ) / 2 ) ; v = new Array ( N ) . fill ( 0 ) ; for ( let j = 0 ; j < N ; j ++ ) for ( let k = j ; k < i - j ; k ++ ) v [ j ] += B [ k ] ; ans += count ( D , i , 0 , new Array ( N ) ) ; } return ans ; } function count ( D , l , t , x ) { if ( t == N ) { if ( D != 0 ) return 0 ; let ans = 1 ; for ( let i = 0 ; i < N ; i ++ ) { ans *= ( i == 0 ? 9 : 10 ) - Math . abs ( x [ i ] ) ; } if ( l % 2 == 0 ) { ans *= 10 ; } return ans ; } let ans = 0 ; for ( let m = - 9 ; m <= 9 ; m ++ ) { if ( - v [ t ] < D + v [ t ] * m && D + v [ t ] * m < v [ t ] ) { x [ t ] = m ; ans += count ( D + v [ t ] * m , l , t + 1 , x ) ; } } return ans ; } let D = 63 ; document . write ( findN ( D ) ) ;
Search for an element in a Mountain Array | Function to find the index of the peak element in the array ; Stores left most index in which the peak element can be found ; Stores right most index in which the peak element can be found ; Stores mid of left and right ; If element at mid is less than element at ( mid + 1 ) ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; If X is greater than mid ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; Update right ; Update left ; Function to find the smallest index of X ; Stores index of peak element in array ; Stores index of X in the array ; If X greater than or equal to first element of array and less than the peak element ; Update res ; If element not found on left side of peak element ; Update res ; Print res ; Driver Code ; Given X ; Given array ; Function Call
function findPeak ( arr ) { var left = 0 ; var right = arr . length - 1 ; while ( left < right ) { var mid = left + parseInt ( ( right - left ) / 2 ) ; if ( arr [ mid ] < arr [ mid + 1 ] ) { left = mid + 1 ; } else { right = mid ; } } return left ; } function BS ( X , left , right , arr ) { while ( left <= right ) { var mid = left + parseInt ( ( right - left ) / 2 ) ; if ( arr [ mid ] == X ) { return mid ; } else if ( X > arr [ mid ] ) { left = mid + 1 ; } else { right = mid - 1 ; } } return - 1 ; } function reverseBS ( X , left , right , arr ) { while ( left <= right ) { var mid = left + parseInt ( ( right - left ) / 2 ) ; if ( arr [ mid ] == X ) { return mid ; } else if ( X > arr [ mid ] ) { right = mid - 1 ; } else { left = mid + 1 ; } } return - 1 ; } function findInMA ( X , mountainArr ) { var peakIndex = findPeak ( mountainArr ) ; var res = - 1 ; if ( X >= mountainArr [ 0 ] && X <= mountainArr [ peakIndex ] ) { res = BS ( X , 0 , peakIndex , mountainArr ) ; } if ( res == - 1 ) { res = reverseBS ( X , peakIndex + 1 , mountainArr . length - 1 , mountainArr ) ; } document . write ( res + " " ) ; } var X = 3 ; var X = 3 ; var list = [ 1 , 2 , 3 , 4 , 5 , 3 , 1 ] ; findInMA ( X , list ) ;
Rearrange given array to obtain positive prefix sums at exactly X indices | Javascript program for the above approach ; Function to rearrange the array according to the given condition ; Using 2 nd operation making all values positive ; Sort the array ; Assign K = N - K ; Count number of zeros ; If number of zeros if greater ; Using 2 nd operation convert it into one negative ; Using 2 nd operation convert it into one negative ; Print the array ; driver function ; Function Call
function count ( a ) { let counter = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == 0 ) { counter ++ ; } } return counter ; } function rearrange ( a , n , x ) { for ( let i = 0 ; i < n ; i ++ ) a [ i ] = Math . abs ( a [ i ] ) ; a . sort ( ) ; x = n - x ; let z = count ( a ) ; if ( x > n - z ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < n && x > 0 ; i += 2 ) { a [ i ] = - a [ i ] ; x -- ; } for ( let i = n - 1 ; i >= 0 && x > 0 ; i -- ) { if ( a [ i ] > 0 ) { a [ i ] = - a [ i ] ; x -- ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let arr = [ 0 , - 2 , 4 , 5 , - 3 ] ; let K = 3 ; let N = arr . length ; rearrange ( arr , N , K ) ;