codeparrot/xlcost-text-to-code huggingface dataset:
Count distinct occurrences as a subsequence | JavaScript program to count number of times S appears as a subsequence in T ; T can 't appear as a subsequence in S ; mat [ i ] [ j ] stores the count of occurrences of T ( 1. . i ) in S ( 1. . j ) . ; Initializing first column with all 0 s . An emptystring can 't have  another string as suhsequence ; Initializing first row with all 1 s . An empty string is subsequence of all . ; Fill mat [ ] [ ] in bottom up manner ; If last characters don 't match,  then value is same as the value  without last character in S. ; Else value is obtained considering two cases . a ) All substrings without last character in S b ) All substrings without last characters in both . ; uncomment this to print matrix mat for ( int i = 1 ; i <= m ; i ++ , cout << endl ) for ( int j = 1 ; j <= n ; j ++ ) System . out . println ( mat [ i ] [ j ] + " ▁ " ) ; ; Driver code to check above method
function findSubsequenceCount ( S , T ) { let m = T . length ; let n = S . length ; if ( m > n ) return 0 ; let mat = new Array ( m + 1 ) ; for ( let i = 0 ; i <= m ; i ++ ) { mat [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; j ++ ) { mat [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= m ; i ++ ) mat [ i ] [ 0 ] = 0 ; for ( let j = 0 ; j <= n ; j ++ ) mat [ 0 ] [ j ] = 1 ; for ( let i = 1 ; i <= m ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( T [ i - 1 ] != S [ j - 1 ] ) mat [ i ] [ j ] = mat [ i ] [ j - 1 ] ; else mat [ i ] [ j ] = mat [ i ] [ j - 1 ] + mat [ i - 1 ] [ j - 1 ] ; } } return mat [ m ] [ n ] ; } let T = " " ; let S = " " ; document . write ( findSubsequenceCount ( S , T ) ) ;
3 | javascript program for 3 - way quick sort ; * This function partitions a in three parts a ) a [ l . . i ] contains all elements * smaller than pivot b ) a [ i + 1. . j - 1 ] contains all occurrences of pivot c ) * a [ j . . r ] contains all elements greater than pivot ; From left , find the first element greater than or equal to v . This loop will definitely terminate as v is last element ; From right , find the first element smaller than or equal to v ; If i and j cross , then we are done ; Swap , so that smaller goes on left greater goes on right ; Move all same left occurrence of pivot to beginning of array and keep count using p ; Move all same right occurrence of pivot to end of array and keep count using q ; Move pivot element to its correct index ; Move all left same occurrences from beginning to adjacent to arr [ i ] ; Move all right same occurrences from end to adjacent to arr [ i ] ; 3 - way partition based quick sort ; Note that i and j are passed as reference ; Recur ; A utility function to prvar an array ; Driver code ; Function Call
var i , j ; function partition ( a , l , r ) { i = l - 1 ; j = r ; var p = l - 1 , q = r ; var v = a [ r ] ; while ( true ) { while ( a [ ++ i ] < v ) ; while ( v < a [ -- j ] ) if ( j == l ) break ; if ( i >= j ) break ; var temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; if ( a [ i ] == v ) { p ++ ; temp = a [ i ] ; a [ i ] = a [ p ] ; a [ p ] = temp ; } if ( a [ j ] == v ) { q -- ; temp = a [ q ] ; a [ q ] = a [ j ] ; a [ j ] = temp ; } } var temp = a [ i ] ; a [ i ] = a [ r ] ; a [ r ] = temp ; j = i - 1 ; for ( k = l ; k < p ; k ++ , j -- ) { temp = a [ k ] ; a [ k ] = a [ j ] ; a [ j ] = temp ; } i = i + 1 ; for ( k = r - 1 ; k > q ; k -- , i ++ ) { temp = a [ i ] ; a [ i ] = a [ k ] ; a [ k ] = temp ; } } function quicksort ( a , l , r ) { if ( r <= l ) return ; i = 0 ; j = 0 ; partition ( a , l , r ) ; quicksort ( a , l , j ) ; quicksort ( a , i , r ) ; } function printarr ( a , n ) { for ( i = 0 ; i < n ; ++ i ) document . write ( " " + a [ i ] ) ; document . write ( " " ) ; } var a = [ 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 ] ; var size = a . length ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ;
Check if a Regular Bracket Sequence can be formed with concatenation of given strings | Function to check possible RBS from the given strings ; Stores the values { sum , min_prefix } ; Iterate over the range ; Stores the total sum ; Stores the minimum prefix ; Check for minimum prefix ; Store these values in vector ; Make two pair vectors pos and neg ; Store values according to the mentioned approach ; Sort the positive vector ; Stores the extra count of open brackets ; No valid bracket sequence can be formed ; Sort the negative vector ; Stores the count of the negative elements ; No valid bracket sequence can be formed ; Check if open is equal to negative ; Driver Code
function checkRBS ( S ) { let N = S . length ; let v = new Array ( N ) ; for ( let i = 0 ; i < N ; ++ i ) { let s = S [ i ] ; let sum = 0 ; let pre = 0 ; for ( let c of s ) { if ( c == " " ) { ++ sum ; } else { -- sum ; } pre = Math . min ( sum , pre ) ; } v [ i ] = [ sum , pre ] ; } let pos = [ ] ; let neg = [ ] ; for ( let i = 0 ; i < N ; ++ i ) { if ( v [ i ] [ 0 ] >= 0 ) { pos . push ( [ - v [ i ] [ 1 ] , v [ i ] [ 0 ] ] ) ; } else { neg . push ( [ v [ i ] [ 0 ] - v [ i ] [ 1 ] , - v [ i ] [ 0 ] ] ) ; } } pos . sort ( ( a , b ) => a - b ) ; let open = 0 ; for ( let p of pos ) { if ( open - p [ 0 ] >= 0 ) { open += p [ 1 ] ; } else { document . write ( " " + " " ) ; return 0 ; } } neg . sort ( ( a , b ) => a - b ) ; let negative = 0 ; for ( let p of neg ) { if ( negative - p [ 0 ] >= 0 ) { negative += p [ 1 ] ; } else { document . write ( " " ) ; return 0 ; } } if ( open != negative ) { document . write ( " " ) ; return 0 ; } document . write ( " " ) ; return 0 ; } let arr = [ " " , " " ] ; checkRBS ( arr ) ;
Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code
function maximumUniqueSquares ( N ) { var ans = 0 ; var i ; if ( N < 4 ) { return 0 ; } var len = Math . sqrt ( N ) ; N -= len * len ; for ( i = 1 ; i < len ; i ++ ) { ans += i * i ; } if ( N >= len ) { N -= len ; for ( i = 1 ; i < len ; i ++ ) { ans += i ; } } for ( i = 1 ; i < N ; i ++ ) { ans += i ; } return ans ; } var N = 9 ; document . write ( maximumUniqueSquares ( N ) ) ;
Lexicographically smallest permutation number up to K having given array as a subsequence | Function to find the lexicographically smallest permutation such that the given array is a subsequence of it ; Stores the missing elements in arr in the range [ 1 , K ] ; Stores if the ith element is present in arr or not ; Loop to mark all integers present in the array as visited ; Loop to insert all the integers not visited into missing ; Append INT_MAX at end in order to prevent going out of bounds ; Pointer to the current element ; Pointer to the missing element ; Stores the required permutation ; Loop to construct the permutation using greedy approach ; If missing element is smaller that the current element insert missing element ; Insert current element ; Print the required Permutation ; Driver Code ; Function Call
const findPermutation = ( K , arr ) => { let missing = [ ] ; let visited = new Array ( K + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { visited [ arr [ i ] ] = 1 ; } for ( let i = 1 ; i <= K ; i ++ ) { if ( ! visited [ i ] ) { missing . push ( i ) ; } } const INT_MAX = 2147483647 ; arr . push ( INT_MAX ) ; missing . push ( INT_MAX ) ; let p1 = 0 ; let p2 = 0 ; let ans = [ ] ; while ( ans . length < K ) { if ( arr [ p1 ] < missing [ p2 ] ) { ans . push ( arr [ p1 ] ) ; p1 ++ ; } else { ans . push ( missing [ p2 ] ) ; p2 ++ ; } } for ( let i = 0 ; i < K ; i ++ ) { document . write ( ` ${ ans [ i ] } ` ) ; } } let K = 7 ; let arr = [ 6 , 4 , 2 , 1 ] ; findPermutation ( K , arr ) ;
Last element remaining after repeated removal of Array elements at perfect square indices | Function to find last remaining index after repeated removal of perfect square indices ; Initialize the ans variable as N ; Iterate a while loop and discard the possible values ; Total discarded values ; Check if this forms a perfect square ; Decrease answer by 1 ; Subtract the value from the current value of N ; Return the value remained ; Function to find last remaining element after repeated removal of array element at perfect square indices ; Find the remaining index ; Print the element at that index as the result ; Given input
function findRemainingIndex ( N ) { let ans = N ; while ( N > 1 ) { let discard = Math . floor ( Math . sqrt ( N ) ) ; if ( discard * discard == N ) { ans -- ; } N -= discard ; } return ans ; } function findRemainingElement ( arr , N ) { let remainingIndex = findRemainingIndex ( N ) ; document . write ( arr [ remainingIndex - 1 ] ) ; } let arr = [ 2 , 3 , 4 , 4 , 2 , 4 , - 3 , 1 , 1 ] ; let N = arr . length ; findRemainingElement ( arr , N ) ;
Count of subsets whose product is multiple of unique primes | Function to return gcd of a and b ; Function to check number has distinct prime ; While N has factors of two ; Traversing till sqrt ( N ) ; If N has a factor of i ; While N has a factor of i ; Covering case , N is Prime ; Function to check wheather num can be added to the subset ; Recursive Function to count subset ; Base Case ; If currSubset is empty ; If Unique [ pos ] can be added to the Subset ; Function to count the subsets ; Initialize unique ; Check it is a product of distinct primes ; Count frequency of unique element ; Function Call ; Given Input ; Function Call
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function checkDistinctPrime ( n ) { let original = n ; let product = 1 ; if ( n % 2 == 0 ) { product *= 2 ; while ( n % 2 == 0 ) { n = parseInt ( n / 2 , 10 ) ; } } for ( let i = 3 ; i < parseInt ( Math . sqrt ( n ) , 10 ) ; i += 2 ) { if ( n % i == 0 ) { product = product * i ; while ( n % i == 0 ) { n = parseInt ( n / i , 10 ) ; } } } if ( n > 2 ) { product = product * n ; } return product == original ; } function check ( pos , subset , unique ) { for ( let num = 0 ; num < subset . length ; num ++ ) { if ( gcd ( subset [ num ] , unique [ pos ] ) != 1 ) { return false ; } } return true ; } function countPrime ( pos , currSubset , unique , frequency ) { if ( pos == unique . length ) { if ( currSubset . length == 0 ) return 0 ; count = 1 ; for ( let element = 0 ; element < currSubset . length ; element ++ ) { count *= frequency [ currSubset [ element ] ] ; } return count ; } if ( check ( pos , currSubset , unique ) ) { return countPrime ( pos + 1 , currSubset , unique , frequency ) + countPrime ( pos + 1 , currSubset + [ unique [ pos ] ] , unique , frequency ) ; } else { return countPrime ( pos + 1 , currSubset , unique , frequency ) ; } } function countSubsets ( arr , N ) { let unique = new Set ( ) ; for ( let element = 0 ; element < arr . length ; element ++ ) { return 5 ; if ( checkDistinctPrime ( element ) ) { unique . add ( element ) ; } } unique = Array . from ( unique ) ; let frequency = new Map ( ) ; for ( let element = 0 ; element < unique . length ; element ++ ) { let freq = 0 ; for ( let i = 0 ; i < unique . length ; i ++ ) { if ( unique [ element ] == unique [ i ] ) { freq ++ ; } } frequency [ element ] = freq ; } let ans = countPrime ( 0 , [ ] , unique , frequency ) ; return ans ; } let arr = [ 2 , 4 , 7 , 10 ] ; let N = arr . length ; let ans = countSubsets ( arr , N ) ; document . write ( ans ) ;
Lexicographically largest possible by merging two strings by adding one character at a time | Recursive bfunction for finding the lexicographically largest string ; If either of the string length is 0 , return the other string ; If s1 is lexicographically larger than s2 ; Take first character of s1 and call the function ; Take first character of s2 and recursively call function for remaining string ; Given Input ; Function Call
const largestMerge = ( s1 , s2 ) => { if ( s1 . length == 0 s2 . length == 0 ) return s1 + s2 ; if ( s1 > s2 ) { return s1 [ 0 ] + largestMerge ( s1 . substr ( 1 ) , s2 ) ; } return s2 [ 0 ] + largestMerge ( s1 , s2 . substr ( 1 ) ) ; } s1 = " " ; s2 = " " ; document . write ( largestMerge ( s1 , s2 ) ) ;
Minimum distance between duplicates in a String | This function is used to find minimum distance between same repeating characters ; Define a hashmap and an arraylist ; Temporary variable ; Traverse through string ; If character present in dictionary ; Difference between current position and last stored value ; Updating current position ; Storing difference in list ; If character not in dictionary assign it with initial of its position ; If no element inserted in list i . e . no repeating characterss ; Return minimum distance ; Given input ; Function call
function shortestDistance ( S , N ) { var dic = new Map ( ) ; var dis = new Array ( ) ; var var1 ; for ( i = 0 ; i < N ; i ++ ) { if ( dic [ S [ i ] ] != null ) { var1 = i - dic [ S [ i ] ] ; dic [ S [ i ] ] = i ; dis . push ( var1 ) ; } else { dic [ S [ i ] ] = i ; } } if ( dis . length == 0 ) return - 1 ; else return dis . reduce ( function ( previous , current ) { return previous < current ? previous : current } ) - 1 ; } var S = " " ; var N = 13 ; document . write ( shortestDistance ( S , N ) ) ;
Number of open doors | TCS Coding Question | Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code
function countOpenDoors ( N ) { let doorsOpen = parseInt ( Math . sqrt ( N ) ) ; return doorsOpen ; } let N = 100 ; document . write ( countOpenDoors ( N ) ) ;
Minimum number of candies required to distribute among children based on given conditions | Function to count the minimum number of candies that is to be distributed ; Stores total count of candies ; Stores the amount of candies allocated to a student ; If the value of N is 1 ; Initialize with 1 all array element ; Traverse the array arr [ ] ; If arr [ i + 1 ] is greater than arr [ i ] and ans [ i + 1 ] is at most ans [ i ] ; Assign ans [ i ] + 1 to ans [ i + 1 ] ; Iterate until i is atleast 0 ; If arr [ i ] is greater than arr [ i + 1 ] and ans [ i ] is at most ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; If arr [ i ] is equals arr [ i + 1 ] and ans [ i ] is less than ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; Increment the sum by ans [ i ] ; Return the resultant sum ; Driver Code
function countCandies ( arr , n ) { let sum = 0 ; let ans = new Array ( n ) ; if ( n == 1 ) { return 1 ; } for ( let i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i + 1 ] > arr [ i ] && ans [ i + 1 ] <= ans [ i ] ) { ans [ i + 1 ] = ans [ i ] + 1 ; } } for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] && ans [ i ] <= ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } else if ( arr [ i ] == arr [ i + 1 ] && ans [ i ] < ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } sum += ans [ i ] ; } sum += ans [ n - 1 ] ; return sum ; } let arr = [ 1 , 0 , 2 ] ; let N = arr . length ; document . write ( countCandies ( arr , N ) )
Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier | Function to find the maximum profit by selling M number of products ; Initialize a Max - Heap to keep track of the maximum value ; Stores the maximum profit ; Traverse the array and push all the elements in max_heap ; Iterate a loop until M > 0 ; Decrement the value of M by 1 ; Pop the maximum element from the heap ; Update the maxProfit ; Push ( X - 1 ) to max heap ; Print the result ; Driver Code
function findMaximumProfit ( arr , M , N ) { let max_heap = [ ] ; let maxProfit = 0 ; for ( let i = 0 ; i < N ; i ++ ) max_heap . push ( arr [ i ] ) ; max_heap . sort ( function ( a , b ) { return a - b } ) ; max_heap . reverse ( ) ; while ( M > 0 ) { M -- ; let X = max_heap [ 0 ] ; max_heap . shift ( ) ; maxProfit += X ; max_heap . push ( X - 1 ) ; max_heap . sort ( function ( a , b ) { return a - b } ) ; max_heap . reverse ( ) ; } document . write ( maxProfit ) ; } let arr = [ 4 , 6 ] ; let M = 4 ; let N = arr . length ; findMaximumProfit ( arr , M , N ) ;
Generate an array with K positive numbers such that arr [ i ] is either | Function to generate the resultant sequence of first N natural numbers ; Initialize an array of size N ; Stores the sum of positive and negative elements ; Mark the first N - K elements as negative ; Update the value of arr [ i ] ; Update the value of sumNeg ; Mark the remaining K elements as positive ; Update the value of arr [ i ] ; Update the value of sumPos ; If the sum of the sequence is negative , then print - 1 ; Print the required sequence ; Driver Code
function findSequence ( n , k ) { let arr = new Array ( n ) ; let sumPos = 0 , sumNeg = 0 ; for ( let i = 0 ; i < n - k ; i ++ ) { arr [ i ] = - ( i + 1 ) ; sumNeg += arr [ i ] ; } for ( let i = n - k ; i < n ; i ++ ) { arr [ i ] = i + 1 ; sumPos += arr [ i ] ; } if ( Math . abs ( sumNeg ) >= sumPos ) { document . write ( - 1 ) ; return ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let N = 10 , K = 6 ; findSequence ( N , K ) ;
Print the indices for every row of a grid from which escaping from the grid is possible | Function to find the row index for every row of a matrix from which one can exit the grid after entering from left ; Iterate over the range [ 0 , M - 1 ] ; Stores the direction from which a person enters a cell ; Row index from which one enters the grid ; Column index from which one enters the grid ; Iterate until j is atleast N - 1 ; If Mat [ i ] [ j ] is equal to 1 ; If entry is from left cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from upper cell ; Decrement j by 1 ; Assign ' R ' to dir ; If entry is from right cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from bottom cell ; Increment j by 1 ; Assign ' L ' to dir ; Otherwise , ; If entry is from left cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from upper cell ; Increment j by 1 ; Assign ' L ' to dir ; If entry is from right cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from lower cell ; Decrement j by 1 ; Assign ' R ' to dir ; If i or j is less than 0 or i is equal to M or j is equal to N ; If j is equal to N ; Otherwise ; Input ; Function call
function findPath ( arr , M , N ) { for ( let row = 0 ; row < M ; row ++ ) { let dir = ' ' ; let i = row ; let j = 0 ; while ( j < N ) { if ( arr [ i ] [ j ] == 1 ) { if ( dir == ' ' ) { i -- ; dir = ' ' ; } else if ( dir == ' ' ) { j -- ; dir = ' ' ; } else if ( dir == ' ' ) { i ++ ; dir = ' ' ; } else if ( dir == ' ' ) { j ++ ; dir = ' ' ; } } else { if ( dir == ' ' ) { i ++ ; dir = ' ' ; } else if ( dir == ' ' ) { j ++ ; dir = ' ' ; } else if ( dir == ' ' ) { i -- ; dir = ' ' ; } else if ( dir == ' ' ) { j -- ; dir = ' ' ; } } if ( i < 0 i == M j < 0 j == N ) break ; } if ( j == N ) document . write ( i + " " ) ; else document . write ( - 1 + " " ) ; } } let arr = [ [ 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ] ; let M = arr . length ; let N = arr [ 0 ] . length ; findPath ( arr , M , N ) ;
Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code
function getSum ( n ) { let ans = 0 ; let arr = n . toString ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { ans += arr [ i ] ; } return ans ; } function getMax ( arr , n ) { let maxRem0 = 0 ; let rem1 = 0 ; let rem2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let digitSum = getSum ( arr [ i ] ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . max ( maxRem0 , arr [ i ] ) ; else if ( digitSum % 3 == 1 ) rem1 += arr [ i ] ; else rem2 += arr [ i ] ; } document . write ( maxRem0 + Math . max ( rem1 , rem2 ) ) } let arr = [ 23 , 12 , 43 , 3 , 56 ] ; let n = arr . length ; getMax ( arr , n ) ;
Check if 2 * K + 1 non | Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code
function checkString ( s , k ) { let n = s . length ; if ( 2 * k + 1 > n ) { document . write ( " " ) ; return ; } let a = s . substr ( 0 , k ) ; let b = s . substr ( n - k , k ) ; b . split ( " " ) . reverse ( ) . join ( " " ) if ( a == b ) document . write ( " " ) ; else document . write ( " " ) ; } let S = " " ; let K = 1 ; checkString ( S , K ) ;
Minimum number of socks required to picked to have at least K pairs of the same color | Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver code
function findMin ( arr , N , k ) { let pairs = 0 ; for ( let i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return - 1 ; else return 2 * k + N - 1 ; } let arr = [ 4 , 5 , 6 ] ; let K = 3 ; let N = arr . length ; document . write ( findMin ( arr , N , K ) ) ;
Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | javascript program for the above approach ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code
function minimumFlips ( A , B , C ) { var res = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { var x = 0 , y = 0 , z = 0 ; if ( ( A & ( 1 << i ) ) != 0 ) { x = 1 ; } if ( ( B & ( 1 << i ) ) != 0 ) { y = 1 ; } if ( ( C & ( 1 << i ) ) != 0 ) { z = 1 ; } if ( z == 0 ) { if ( x == 1 ) { res ++ ; } if ( y == 1 ) { res ++ ; } } if ( z == 1 ) { if ( x == 0 && y == 0 ) { res ++ ; } } } return res ; } var A = 2 , B = 6 , C = 5 ; document . write ( minimumFlips ( A , B , C ) ) ;