codeparrot/xlcost-text-to-code huggingface dataset:
Queries to calculate Bitwise AND of an array with updates | Store the number of set bits at each position ; Function to precompute the prefix count array ; Iterate over the range [ 0 , 31 ] ; Set the bit at position i if arr [ 0 ] is set at position i ; Traverse the array and take prefix sum ; Update prefixCount [ i ] [ j ] ; Function to find the Bitwise AND of all array elements ; Stores the required result ; Iterate over the range [ 0 , 31 ] ; Stores the number of set bits at position i ; If temp is N , then set ith position in the result ; Print the result ; Function to update the prefix count array in each query ; Iterate through all the bits of the current number ; Store the bit at position i in the current value and the new value ; If bit2 is set and bit1 is unset , then increase the set bits at position i by 1 ; If bit1 is set and bit2 is unset , then decrease the set bits at position i by 1 ; Function to find the bitwise AND of the array after each query ; Fill the prefix count array ; Traverse the queries ; Store the index and the new value ; Store the current element at the index ; Update the array element ; Apply the changes to the prefix count array ; Print the bitwise AND of the modified array ; Driver code
let prefixCount = [ [ ] ] ; function findPrefixCount ( arr , size ) { for ( let i = 0 ; i < 32 ; i ++ ) { prefixCount [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( let j = 1 ; j < size ; j ++ ) { prefixCount [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefixCount [ i ] [ j ] += prefixCount [ i ] [ j - 1 ] ; } } } function arrayBitwiseAND ( size ) { let result = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { let temp = prefixCount [ i ] [ size - 1 ] ; if ( temp == size ) result = ( result | ( 1 << i ) ) ; } document . write ( result + " " ) ; } function applyQuery ( currentVal , newVal , size ) { for ( let i = 0 ; i < 32 ; i ++ ) { let bit1 = ( ( currentVal >> i ) & 1 ) ; let bit2 = ( ( newVal >> i ) & 1 ) ; if ( bit2 > 0 && bit1 == 0 ) prefixCount [ i ] [ size - 1 ] ++ ; else if ( bit1 > 0 && bit2 == 0 ) prefixCount [ i ] [ size - 1 ] -- ; } } function findbitwiseAND ( queries , arr , N , M ) { prefixCount = new Array ( 32 ) ; for ( var i = 0 ; i < prefixCount . length ; i ++ ) { prefixCount [ i ] = new Array ( 2 ) ; } findPrefixCount ( arr , N ) ; for ( let i = 0 ; i < M ; i ++ ) { let id = queries [ i ] [ 0 ] ; let newVal = queries [ i ] [ 1 ] ; let currentVal = arr [ id ] ; arr [ id ] = newVal ; applyQuery ( currentVal , newVal , N ) ; arrayBitwiseAND ( N ) ; } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let queries = [ [ 0 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] ] ; let N = arr . length ; let M = queries . length ; findbitwiseAND ( queries , arr , N , M ) ;
Find the winner of a game of donating i candies in every i | Function to find the winning player in a game of donating i candies to opponent in i - th move ; Steps in which number of candies of player A finishes ; Steps in which number of candies of player B finishes ; If A 's candies finishes first ; Otherwise ; Candies possessed by player A ; Candies possessed by player B
function stepscount ( a , b ) { let chanceA = 2 * a - 1 ; ; let chanceB = 2 * b ; if ( chanceA < chanceB ) { document . write ( " " ) ; } else if ( chanceB < chanceA ) { document . write ( " " ) ; } return 0 ; } let A = 2 ; let B = 3 ; stepscount ( A , B ) ;
Check if a point is inside , outside or on a Hyperbola | Function to check if the point ( x , y ) lies inside , on or outside the given hyperbola ; Stores the value of the equation ; Generate output based on value of p ; Driver Code
function checkpoint ( h , k , x , y , a , b ) { p = ( ( Math . pow ( ( x - h ) , 2 ) / Math . pow ( a , 2 ) ) - ( Math . pow ( ( y - k ) , 2 ) / Math . pow ( b , 2 ) ) ) if ( p > 1 ) console . log ( " " ) ; else if ( p == 1 ) console . log ( " " ) ; else console . log ( " " ) ; } function main ( ) { var h = 0 ; var k = 0 ; var x = 2 ; var y = 1 ; var a = 4 ; var b = 5 ; checkpoint ( h , k , x , y , a , b ) ; } main ( )
Count subarrays having even Bitwise XOR | Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Print the result ; Given array ; Stores the size of the array ; Function Call
function evenXorSubarray ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let XOR = 0 ; for ( let j = i ; j < n ; j ++ ) { XOR = XOR ^ arr [ j ] ; if ( ( XOR & 1 ) == 0 ) ans ++ ; } } document . write ( ans ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; evenXorSubarray ( arr , N ) ;
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | Function to find the Kth element in the required permutation ; Store the required result ; If K is in the first N / 2 elements , print K * 2 ; Otherwise , K is greater than N / 2 ; If N is even ; If N is odd ; Print the required result ; Driver Code ; functions call
function findKthElement ( N , K ) { let ans = 0 ; if ( K <= N / 2 ) { ans = K * 2 ; } else { if ( N % 2 == 0 ) { ans = ( K * 2 ) - N - 1 ; } else { ans = ( K * 2 ) - N ; } } document . write ( ans ) ; } let N = 10 , K = 3 ; findKthElement ( N , K ) ; findKthElement ( N , K ) ;
Minimize replacements with values up to K to make sum of two given arrays equal | Function to find minimum number of replacements required to make the sum of two arrays equal ; Stores the sum of a [ ] ; Stores the sum of b [ ] ; Calculate sum of the array a [ ] ; Calculate sum of the array b [ ] ; Stores the difference between a [ ] and b [ ] ; Left and Right pointer to traverse the array a [ ] ; Left and Right pointer to traverse the array b [ ] ; Stores the count of moves ; Sort the arrays in ascending order ; Iterate while diff != 0 and l1 <= r1 or l2 <= r2 ; If diff is greater than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; If diff is less than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; Increment count of res by one ; If diff is 0 , then return res ; Otherwise , return - 1 ; Driver Code
function makeSumEqual ( a , b , K , M , N ) { let sum1 = 0 ; let sum2 = 0 ; for ( let el = 0 ; el < a . length ; el ++ ) sum1 += a [ el ] ; for ( let el = 0 ; el < b . length ; el ++ ) sum2 += b [ el ] ; let diff = sum1 - sum2 ; let l1 = 0 , r1 = M - 1 ; let l2 = 0 , r2 = N - 1 ; let res = 0 ; a . sort ( function ( c , d ) { return c - d ; } ) ; b . sort ( function ( c , d ) { return c - d ; } ) ; while ( l1 <= r1 l2 <= r2 ) { if ( diff == 0 ) { break ; } if ( diff > 0 ) { if ( l2 <= r2 && l1 <= r1 ) { if ( K - b [ l2 ] < a [ r1 ] - 1 ) { let sub = Math . min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { let sub = Math . min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else if ( l1 <= r1 ) { let sub = Math . min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { let sub = Math . min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else { if ( l1 <= r1 && l2 <= r2 ) { if ( K - a [ l1 ] < b [ r2 ] - 1 ) { let sub = Math . min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { let sub = Math . min ( K - a [ l1 ] , - 1 * diff ) ; diff += sub ; a [ l1 ] -= sub ; l1 ++ ; } } else if ( l2 <= r2 ) { let sub = Math . min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { let sub = Math . min ( K - a [ l1 ] , diff ) ; diff += sub ; a [ l1 ] += sub ; l1 ++ ; } } res ++ ; } if ( diff == 0 ) return res ; else return - 1 ; } let A = [ 1 , 4 , 3 ] ; let B = [ 6 , 6 , 6 ] ; let M = A . length ; let N = B . length ; let K = 6 ; document . write ( makeSumEqual ( A , B , K , M , N ) ) ;
Modify a Binary String by flipping characters such that any pair of indices consisting of 1 s are neither co | Function to modify a string such that there doesn 't exist any pair  of indices consisting of 1s, whose  GCD is 1 and are divisible by each other ; Flips characters at indices 4 N , 4 N - 2 , 4 N - 4 . ... upto N terms ; Print the string ; Driver code ; Initialize the string S ; function call
function findString ( S , N ) { var strLen = 4 * N ; for ( var i = 1 ; i <= N ; i ++ ) { S [ strLen - 1 ] = " " ; strLen -= 2 ; } for ( var i = 0 ; i < 4 * N ; i ++ ) { document . write ( S [ i ] ) ; } } var N = 2 ; var S = [ ... Array ( 4 * N ) ] ; for ( var i = 0 ; i < 4 * N ; i ++ ) S [ i ] = " " ; findString ( S , N ) ;
Check if a string can be split into two substrings with equal number of vowels | JavaScript program for the above approach Function to check if any character is a vowel or not ; Lowercase vowels ; Uppercase vowels ; Otherwise ; Function to check if string S can be split into two substrings with equal number of vowels ; Stores the count of vowels in the string S ; Traverse over the string ; If S [ i ] is vowel ; Stores the count of vowels upto the current index ; Traverse over the string ; If S [ i ] is vowel ; If vowelsTillNow and totalVowels are equal ; Otherwise ; Driver Code
function isVowel ( ch ) { if ( ch === " " ch === " " ch === " " ch === " " ch === " " ) return true ; if ( ch === " " ch === " " ch === " " ch === " " ch === " " ) return true ; return false ; } function containsEqualStrings ( S ) { var totalVowels = 0 ; for ( var i = 0 ; i < S . length ; i ++ ) { if ( isVowel ( S [ i ] ) ) totalVowels ++ ; } var vowelsTillNow = 0 ; for ( var i = 0 ; i < S . length ; i ++ ) { if ( isVowel ( S [ i ] ) ) { vowelsTillNow ++ ; totalVowels -- ; if ( vowelsTillNow === totalVowels ) { return " " ; } } } return " " ; } var S = " " ; document . write ( containsEqualStrings ( S ) ) ;
Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | Javascript program to implement the above approach ; Store the maximum sum of matrix possible ; Stores the count of negative values in the matrix ; Store minimum absolute value present in the matrix ; Traverse the matrix row - wise ; Update sum ; If current element is negative , increment the negative count ; If current value is less than the overall minimum in A [ ] , update the overall minimum ; If there are odd number of negative values , then the answer will be sum of the absolute values of all elements - 2 * minVal ; Print maximum sum ; Given matrix ; Dimensions of matrix
function getMaxSum ( A , M , N ) { let sum = 0 ; let negative = 0 ; let minVal = Number . MAX_VALUE ; for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { sum += Math . abs ( A [ i ] [ j ] ) ; if ( A [ i ] [ j ] < 0 ) { negative ++ ; } minVal = Math . min ( minVal , Math . abs ( A [ i ] [ j ] ) ) ; } } if ( negative % 2 != 0 ) { sum -= 2 * minVal ; } document . write ( sum ) ; } let A = [ [ 4 , - 8 , 6 ] , [ 3 , 7 , 2 ] ] ; let M = A . length ; let N = A [ 0 ] . length ; getMaxSum ( A , M , N ) ;
Calculate total wall area of houses painted | Function to find the total area of walls painted in N row - houses ; Stores total area of N row - houses that needs to be painted ; Traverse the array of wall heights ; Update total area painted ; Update total ; Traverse all the houses and print the shared walls ; Update total ; Print total area needs to paint ; Given N , W & L ; Given heights of houses ; Function Call
function areaToPaint ( N , W , L , Heights ) { let total = 0 ; for ( let i = 0 ; i < N ; i ++ ) { total += 2 * Heights [ i ] * W ; } total += L * ( Heights [ 0 ] + Heights [ N - 1 ] ) ; for ( let i = 1 ; i < N ; i ++ ) { total += L * Math . abs ( Heights [ i ] - Heights [ i - 1 ] ) ; } document . write ( total ) ; } let N = 7 , W = 1 , L = 1 ; let Heights = [ 4 , 3 , 1 , 2 , 3 , 4 , 2 ] ; areaToPaint ( N , W , L , Heights ) ;
Generate a matrix with each row and column of given sum | Function to generate a matrix with sum of each row equal to sum of r [ ] and sum of each column equal to sum of c [ ] ; Initialize a matrix ; Traverse each cell ( i , j ) of the matrix ; Assign the minimum of the row or column value ; Subtract the minimum from both row and column sum ; Prlet the matrix obtained ; Driver Code
function restoreGem ( r , c ) { let dp = new Array ( r . length ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < r . length ; i ++ ) { for ( let j = 0 ; j < c . length ; j ++ ) { let m = Math . min ( r [ i ] , c [ j ] ) ; dp [ i ] [ j ] = m ; r [ i ] -= m ; c [ j ] -= m ; } } return dp ; } function prletMatrix ( ans , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( ans [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let rowSum = [ 5 , 7 , 10 ] ; let colSum = [ 8 , 6 , 8 ] ; let ans = restoreGem ( rowSum , colSum ) ; prletMatrix ( ans , rowSum . length , colSum . length ) ;
Maximize sum of count of distinct prime factors of K array elements | JavaScript program of the above approach ; Function to find the maximum sum of count of distinct prime factors of K array elements ; Stores the count of distinct primes ; Stores 1 and 0 at prime and non - prime indices respectively ; Initialize the count of factors to 0 ; Sieve of Eratosthenes ; Count of factors of a prime number is 1 ; Increment CountDistinct of all multiples of i ; Mark its multiples non - prime ; Stores the maximum sum of distinct prime factors of K array elements ; Stores the count of all distinct prime factors ; Traverse the array to find count of all array elements ; Maximum sum of K prime factors of array elements ; Check for the largest prime factor ; Increment sum ; Decrement its count and K ; Print the maximum sum ; Given array ; Size of the array ; Given value of K
let MAX = 1000000 ; function maxSumOfDistinctPrimeFactors ( arr , N , K ) { let CountDistinct = [ ] ; for ( let i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; } let prime = [ ] ; for ( let i = 0 ; i <= MAX ; i ++ ) { prime [ i ] = 0 ; } for ( let i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( let i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( let j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } let sum = 0 ; let PrimeFactor = [ ] ; for ( let i = 0 ; i <= 20 ; i ++ ) { PrimeFactor [ i ] = 0 ; } for ( let i = 0 ; i < N ; i ++ ) { PrimeFactor [ CountDistinct [ arr [ i ] ] ] ++ ; } for ( let i = 19 ; i >= 1 ; i -- ) { while ( PrimeFactor [ i ] > 0 ) { sum += i ; PrimeFactor [ i ] -- ; K -- ; if ( K == 0 ) break ; } if ( K == 0 ) break ; } document . write ( sum ) ; } let arr = [ 6 , 9 , 12 ] ; let N = arr . length ; let K = 2 ; maxSumOfDistinctPrimeFactors ( arr , N , K ) ;
Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | JavaScript Program to implement the above approach Function to count the changes required such that no palindromic subString of length exceeding 1 is present in the String ; Base Case ; Stores the count ; Iterate over the String ; Palindromic SubString of Length 2 ; Replace the next character ; Increment changes ; Palindromic SubString of Length 3 ; Replace the next character ; Increment changes ; Driver Code
function maxChange ( str ) { if ( str . length <= 1 ) { return 0 ; } var minChanges = 0 ; for ( var i = 0 ; i < str . length - 1 ; i ++ ) { if ( str [ i ] === str [ i + 1 ] ) { str [ i + 1 ] = " " ; minChanges += 1 ; } else if ( i > 0 && str [ i - 1 ] === str [ i + 1 ] ) { str [ i + 1 ] = " " ; minChanges += 1 ; } } return minChanges ; } var str = " " ; document . write ( maxChange ( str . split ( " " ) ) ) ;
Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; Iterator to store first element of B ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Given array ; Given size ; Print the required smallest integer
function findMinKToMakeAllEqual ( N , A ) { var B = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) B . add ( A [ i ] ) ; var M = B . size ; var itr = [ ... B ] . sort ( ( a , b ) => a - b ) ; if ( M > 3 ) document . write ( " " ) ; else if ( M == 3 ) { var B_1 = itr [ 0 ] ; var B_2 = itr [ 1 ] ; var B_3 = itr [ 2 ] ; if ( B_2 - B_1 == B_3 - B_2 ) document . write ( B_2 - B_1 ) ; else document . write ( " " ) ; } else if ( M == 2 ) { var B_1 = itr [ 0 ] ; var B_2 = itr [ 1 ] ; if ( ( B_2 - B_1 ) % 2 == 0 ) document . write ( parseInt ( ( B_2 - B_1 ) / 2 ) ) ; else document . write ( B_2 - B_1 ) ; } else document . write ( 0 ) ; } var A = [ 1 , 3 , 5 , 1 ] ; var N = A . length ; findMinKToMakeAllEqual ( N , A ) ;
Maximum number of times a given string needs to be concatenated to form a substring of another string | Function to find lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Iterate string to calculate lps [ i ] ; If the current character of the pattern matches ; Otherwise ; Otherwise ; Function to implement KMP algorithm ; Stores the longest prefix and suffix values for pattern ; Preprocess the pattern and find the lps [ ] array ; Index for txt [ ] ; Index for pat [ ] ; If pattern is found return 1 ; Mismatch after j matches ; Don 't match lps[0, lps[j - 1]]  characters they will  match anyway ; Return 0 if the pattern is not found ; Function to find the maximum value K for which string S2 concatenated K times is a substring of string S1 ; Store the required maximum number ; Create a temporary string to store string word ; Store the maximum number of times string S2 can occur in string S1 ; Traverse in range [ 0 , numWords - 1 ] ; If curWord is found in sequence ; Concatenate word to curWord ; Increment resCount by 1 ; Otherwise break the loop ; Print the answer ; Driver Code ; Function Call
function computeLPSArray ( pat , M , lps ) { var len = 0 ; lps [ 0 ] = 0 ; var i = 1 ; while ( i < M ) { if ( pat [ i ] == pat [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } } function KMPSearch ( pat , txt ) { var M = pat . length ; var N = txt . length ; var lps = Array ( M ) ; computeLPSArray ( pat , M , lps ) ; var i = 0 ; var j = 0 ; while ( i < N ) { if ( pat [ j ] == txt [ i ] ) { j ++ ; i ++ ; } if ( j == M ) { return 1 ; j = lps [ j - 1 ] ; } else if ( i < N && pat [ j ] != txt [ i ] ) { if ( j != 0 ) j = lps [ j - 1 ] ; else i = i + 1 ; } } return 0 ; } function maxRepeating ( seq , word ) { var resCount = 0 ; var curWord = word ; var numWords = seq . length / word . length ; for ( var i = 0 ; i < numWords ; i ++ ) { if ( KMPSearch ( curWord , seq ) ) { curWord += word ; resCount ++ ; } else break ; } document . write ( resCount ) ; } var S1 = " " , S2 = " " ; maxRepeating ( S1 , S2 ) ;
Count all possible strings that can be generated by placing spaces | Function to count the number of strings that can be generated by placing spaces between pair of adjacent characters ; Count of positions for spaces ; Count of possible strings ; Driver Code
function countNumberOfStrings ( s ) { let n = s . length - 1 ; let count = ( Math . pow ( 2 , n ) ) ; return count ; } let S = " " ; document . write ( countNumberOfStrings ( S ) ) ;
Minimum time required to reach a given score | Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Print the time required ; Driver Code ; Function Call
function findMinimumTime ( p , n , target ) { let um = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( um [ p [ i ] ] ) um [ p [ i ] ] ++ ; else um [ p [ i ] ] = 1 } let time = 0 ; let sum = 0 ; while ( sum < target ) { sum = 0 ; time ++ ; for ( let it in um ) { sum += um [ it ] * Math . floor ( time / it ) ; } } document . write ( time ) ; } let arr = [ 1 , 3 , 3 , 4 ] ; let N = arr . length ; let target = 10 ; findMinimumTime ( arr , N , target ) ;
Construct MEX array from the given array | Javascript program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Print the array B ;  ; Size of array ; Function call
var MAXN = 100001 ; function constructMEX ( arr , N ) { var hash = Array ( MAXN ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] = 1 ; } var MexOfArr ; for ( var i = 1 ; i < MAXN ; i ++ ) { if ( hash [ i ] == 0 ) { MexOfArr = i ; break ; } } var B = Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < MexOfArr ) B [ i ] = arr [ i ] ; else B [ i ] = MexOfArr ; } for ( var i = 0 ; i < N ; i ++ ) document . write ( B [ i ] + ' ' ) ; } Given array * / var arr = [ 2 , 1 , 5 , 3 ] ; var N = arr . length ; constructMEX ( arr , N ) ;
Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | Utility pair ; Function to print the smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of arr [ ] in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array arr [ ] ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print the element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print all the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print current pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function smallestNumberLeftInPQ ( arr , N ) { let pq = [ ] ; let pairsArr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { pq . push ( arr [ i ] ) ; } pq . sort ( function ( a , b ) { return b - a ; } ) ; while ( pq . length > 1 ) { let X = pq [ 0 ] ; pq . shift ( ) ; let Y = pq [ 0 ] ; pq . shift ( ) ; pq . push ( Math . floor ( ( X + Y + 1 ) / 2 ) ) ; pq . sort ( function ( a , b ) { return b - a ; } ) ; pairsArr . push ( new pair ( X , Y ) ) ; } document . write ( " " + pq [ 0 ] + " " ) ; let sz = pairsArr . length ; for ( let i = 0 ; i < sz ; i ++ ) { if ( i == 0 ) { document . write ( " " ) ; } document . write ( " " + pairsArr [ i ] . first + " " + pairsArr [ i ] . second + " " ) ; if ( i != sz - 1 ) { document . write ( " " ) ; } if ( i == sz - 1 ) { document . write ( " " ) ; } } } let arr = [ 3 , 2 , 1 ] ; let N = arr . length ; smallestNumberLeftInPQ ( arr , N ) ;