codeparrot/xlcost-text-to-code huggingface dataset:
Maximum size of subset such that product of all subset elements is a factor of N | Function to find the maximum size of the subset such that the product of subset elements is a factor of N ; Base Case ; Stores maximum size of valid subset ; Traverse the given array ; If N % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; Return Answer ; Driver Code
function maximizeSubset ( N , arr , M , x = 0 ) { if ( x == M ) { return 0 ; } let ans = 0 ; for ( let i = x ; i < M ; i ++ ) { if ( N % arr [ i ] == 0 ) { ans = Math . max ( ans , maximizeSubset ( Math . floor ( N / arr [ i ] ) , arr , M , x + 1 ) + 1 ) ; } } return ans ; } let N = 64 ; let arr = [ 1 , 2 , 4 , 8 , 16 , 32 ] ; let M = arr . length ; document . write ( maximizeSubset ( N , arr , M ) ) ;
Count of distinct sums formed by N numbers taken form range [ L , R ] | Function to find total number of different sums of N numbers in the range [ L , R ] ; To store minimum possible sum with N numbers with all as L ; To store maximum possible sum with N numbers with all as R ; All other numbers in between maxSum and minSum can also be formed so numbers in this range is the final answer ; Driver Code
function countDistinctSums ( N , L , R ) { let minSum = L * N ; let maxSum = R * N ; return maxSum - minSum + 1 ; } let N = 2 , L = 1 , R = 3 ; document . write ( countDistinctSums ( N , L , R ) ) ;
Make the Array sum 0 by using either ceil or floor on each element | Function to modify the array element such that the sum is close to 0 ; Stores the modified elements ; Stores the sum of array element ; Stores minimum size of the array ; Traverse the array and find the sum ; If sum is positive ; Find the minimum number of elements that must be changed ; Iterate until M elements are modified or the array end ; Update the current array elements to its floor ; Print the resultant array ; Driver Code
function setSumtoZero ( arr , N ) { let A = new Array ( N ) ; let sum = 0 ; let m = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { sum += Math . ceil ( arr [ i ] ) ; A [ i ] = Math . ceil ( arr [ i ] ) ; } if ( sum > 0 ) { m = Math . min ( sum , N ) ; for ( let i = 0 ; i < N && m > 0 ; i ++ ) { A [ i ] = Math . floor ( arr [ i ] ) ; if ( A [ i ] != Math . floor ( arr [ i ] ) ) m -- ; } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( A [ i ] + " " ) ; } } let arr = [ - 2 , - 2 , 4.5 ] ; let N = arr . length ; setSumtoZero ( arr , N ) ;
Find first undeleted integer from K to N in given unconnected Graph after performing Q queries | Function to perform th Get operation of disjoint set union ; Function to perform the union operation of dijoint set union ; Update the graph [ a ] as b ; Function to perform given queries on set of vertices initially not connected ; Stores the vertices ; Mark every vertices rightmost vertex as i ; Traverse the queries array ; Check if it is first type of the givan query ; Get the parent of a ; Print the answer for the second query ; Driver Code
function Get ( graph , a ) { return ( graph [ a ] = graph [ a ] == a ? a : Get ( graph , graph [ a ] ) ) ; } function Union ( graph , a , b ) { a = Get ( graph , a ) ; b = Get ( graph , b ) ; graph [ a ] = b ; } function Queries ( queries , N , M ) { let graph = new Array ( N ) . fill ( 2 ) ; for ( let i = 1 ; i <= N + 1 ; i ++ ) { graph [ i ] = i ; } for ( let query of queries ) { if ( query [ 0 ] == 1 ) { Union ( graph , query [ 1 ] , query [ 1 ] + 1 ) ; } else { let a = Get ( graph , query [ 1 ] ) ; if ( a == N + 1 ) document . write ( - 1 + " " ) ; else document . write ( graph [ a ] + " " ) ; } } } let N = 5 ; let queries = [ [ 2 , 1 ] , [ 1 , 1 ] , [ 2 , 1 ] , [ 2 , 3 ] , ] ; let Q = queries . length ; Queries ( queries , N , Q ) ;
Finding the Nth term in a sequence formed by removing digit K from natural numbers | Javascript implementation for the above approach ; Denotes the digit place ; Method to convert any number to binary equivalent ; denotes the current digits place ; If current digit is >= K increment its value by 1 ; Else add the digit as it is ; Move to the next digit ; Driver code
function convertToBase9 ( n ) { let ans = 0 ; let a = 1 ; while ( n > 0 ) { ans += a * ( n % 9 ) ; a *= 10 ; n = Math . floor ( n / 9 ) ; } return ans ; } function getNthnumber ( base9 , K ) { let ans = 0 ; let a = 1 ; while ( base9 > 0 ) { let cur = base9 % 10 ; if ( cur >= K ) { ans += a * ( cur + 1 ) ; } else { ans += a * cur ; } base9 = Math . floor ( base9 / 10 ) ; a *= 10 ; } return ans ; } let N = 10 , K = 1 ; let base9 = convertToBase9 ( N ) ; document . write ( getNthnumber ( base9 , K ) ) ;
Check if given array can be rearranged such that mean is equal to median | Function to return true or false if size of array is odd ; To prevent overflow ; If element is greater than mid , then it can only be present in right subarray ; If element is smaller than mid , then it can only be present in left subarray ; Else the element is present at the middle then return 1 ; when element is not present in array then return 0 ; Function to return true or false if size of array is even ; Calculating Candidate Median ; If Candidate Median if greater than Mean then decrement j ; If Candidate Median if less than Mean then increment i ; If Candidate Median if equal to Mean then return 1 ; when No candidate found for mean ; Function to return true if Mean can be equal to any candidate median otherwise return false ; Calculating Mean ; If N is Odd ; If N is even ; Driver Code
const binarySearch = ( arr , size , key ) => { let low = 0 , high = size - 1 , mid ; while ( low <= high ) { mid = low + parseInt ( ( high - low ) / 2 ) ; if ( key > arr [ mid ] ) low = mid + 1 ; else if ( key < arr [ mid ] ) high = mid - 1 ; else return 1 ; } return 0 ; } const twoPointers = ( arr , N , mean ) => { let i = 0 , j = N - 1 ; while ( i < j ) { let temp = ( arr [ i ] + arr [ j ] ) / 2 ; if ( temp > mean ) j -- ; else if ( temp < mean ) i ++ ; else return 1 ; } return 0 ; } const checkArray = ( arr , N ) => { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; let mean = sum / N ; if ( N & 1 ) return binarySearch ( arr , N , mean ) ; else return twoPointers ( arr , N , mean ) ; } let arr = [ 1.0 , 3.0 , 6.0 , 9.0 , 12.0 , 32.0 ] ; let N = arr . length ; if ( checkArray ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of distinct integers belonging to first N terms of at least one of given GPs | Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
function UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) { let S = new Set ( ) ; let p1 = a1 ; for ( let i = 0 ; i < N ; i ++ ) { S . add ( p1 ) ; p1 = ( p1 * r1 ) ; } let p2 = a2 ; for ( let i = 0 ; i < N ; i ++ ) { S . add ( p2 ) ; p2 = ( p2 * r2 ) ; } return S . size ; } let N = 5 ; let a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ; document . write ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) ;
Minimize operations to make all array elements | Function to find minimum the number of operations required to make all the array elements to - 1 ; Stores the array elements with their corresponding indices ; Push the array element and it 's index ; Sort the elements according to it 's first value ; Stores the minimum number of operations required ; Traverse until vp is not empty ; Stores the first value of vp ; Stores the second value of vp ; Update the minCnt ; Pop the back element from the vp until the first value is same as val and difference between indices is less than K ; Return the minCnt ; Driver Code
function minOperations ( arr , N , K ) { let vp = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { vp . push ( { " " : arr [ i ] , " " : i } ) ; } vp . sort ( function ( a , b ) { return a . first - b . first ; } ) ; let minCnt = 0 ; while ( vp . length > 0 ) { let val , ind ; val = vp [ vp . length - 1 ] . first ; ind = vp [ vp . length - 1 ] . second ; minCnt ++ ; while ( vp . length > 0 && ( vp [ vp . length - 1 ] . first == val ) && ( ind - vp [ vp . length - 1 ] . second + 1 <= K ) ) { vp . pop ( ) ; } } return minCnt ; } let arr = [ 18 , 11 , 18 , 11 , 18 ] ; let K = 3 ; let N = arr . length ; document . write ( minOperations ( arr , N , K ) ) ;
Find array sum after incrementing by K adjacent elements of every positive element M times | Function to find the nearest non - zero element in the left direction ; Stores the index of the first element greater than 0 from the right side ; Traverse the array in the range [ 1 , N ] ; Check arr [ i ] is greater than 0 ; Update the value of L ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of L ; Update the value of steps [ i ] ; Function to find the nearest non - zero element in the right direction ; Stores the index of the first element greater than 0 from the left side ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Traverse the array from the right side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Update the value of steps [ i ] ; Function to find the sum of the array after the given operation M times ; Stores the distance of the nearest non zero element . ; Stores sum of the initial array arr [ ] ; Traverse the array from the left side ; Update the value of sum ; Print the total sum of the array ; Driver Code
function nearestLeft ( arr , N , steps ) { let L = - N ; for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { L = - ( N - i ) ; break ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { L = i ; } steps [ i ] = i - L ; } } function nearestRight ( arr , N , steps ) { let R = 2 * N ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { R = N + i ; break ; } } for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { R = i ; } steps [ i ] = Math . min ( steps [ i ] , R - i ) ; } } function findSum ( arr , N , M , K ) { let steps = new Array ( N ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum = sum + arr [ i ] ; } if ( sum == 0 ) { return 0 ; } nearestLeft ( arr , N , steps ) ; nearestRight ( arr , N , steps ) ; for ( let i = 0 ; i < N ; i ++ ) sum += 2 * K * Math . max ( 0 , M - steps [ i ] ) ; return sum ; } let arr = [ 0 , 1 , 0 , 1 , 0 , 0 ] ; let N = arr . length ; let M = 2 ; let K = 1 ; document . write ( findSum ( arr , N , M , K ) ) ;
Count of pairs in given range having their ratio equal to ratio of product of their digits | Function to find the product of digits of the given number ; Function to find the count of pairs ( a , b ) such that a : b = ( product of digits of a ) : ( product of digits of b ) ; Stores the count of the valid pairs ; Loop to iterate over all unordered pairs ( a , b ) ; Stores the product of digits of a ; Stores the product of digits of b ; If x != 0 and y != 0 and a : b is equivalent to x : y ; Increment valid pair count ; Return Answer ; Driver code ; Function Call
function getProduct ( n ) { let product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = Math . floor ( n / 10 ) ; } return product ; } function countPairs ( L , R ) { let cntPair = 0 ; for ( let a = L ; a <= R ; a ++ ) { for ( let b = a + 1 ; b <= R ; b ++ ) { let x = getProduct ( a ) ; let y = getProduct ( b ) ; if ( x && y && ( a * y ) == ( b * x ) ) { cntPair ++ ; } } } return cntPair ; } let L = 1 ; let R = 100 ; document . write ( countPairs ( 1 , 100 ) ) ;
Maximize matrix sum by flipping the sign of any adjacent pairs | Function to find the maximum sum of matrix element after flipping the signs of adjacent matrix elements ; Initialize row and column ; Stores the sum of absolute matrix element ; Find the minimum absolute value in the matrix ; Store count of negatives ; Find the smallest absolute value in the matrix ; Increment the count of negative numbers ; Find the absolute sum ; If the count of negatives is even then print the sum ; Subtract minimum absolute element ; Driver Code
function maxSum ( matrix ) { let r = matrix . length ; let c = matrix [ 0 ] . length ; let sum = 0 ; let mini = Number . MAX_VALUE ; let count = 0 ; for ( let i = 0 ; i < r ; i ++ ) { for ( let j = 0 ; j < c ; j ++ ) { let k = matrix [ i ] [ j ] ; mini = Math . min ( mini , Math . abs ( k ) ) ; if ( k < 0 ) count ++ ; sum += Math . abs ( k ) ; } } if ( count % 2 == 0 ) { return sum ; } else { return ( sum - 2 * mini ) ; } } let matrix = [ [ 2 , - 2 ] , [ - 2 , 2 ] ] ; document . write ( maxSum ( matrix ) ) ;
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements | Function to calculate last positive element of the array ; Return the first element if N = 1 ; Stores the greatest and the second greatest element ; Traverse the array A [ ] ; If current element is greater than the greatest element ; If current element is greater than second greatest element ; Return the final answer ; Driver Code
function lastPositiveElement ( arr ) { let N = arr . length ; if ( N == 1 ) return arr [ 0 ] ; let greatest = - 1 , secondGreatest = - 1 ; for ( let x of arr ) { if ( x >= greatest ) { secondGreatest = greatest ; greatest = x ; } else if ( x >= secondGreatest ) { secondGreatest = x ; } } return greatest - secondGreatest ; } let arr = [ 3 , 5 , 4 , 7 ] ; document . write ( lastPositiveElement ( arr ) ) ;
Reduce given array by replacing subarrays with values less than K with their sum | Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Prlet the result ; Driver Code
function updateArray ( arr , K ) { let sum = 0 ; let res = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < K ) { sum += arr [ i ] ; } else { if ( sum != 0 ) { res . push ( sum ) ; } sum = 0 ; res . push ( arr [ i ] ) ; } } if ( sum != 0 ) res . push ( sum ) ; for ( let it of res ) document . write ( it + ' ' ) ; } let arr = [ 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 ] ; let K = 100 ; updateArray ( arr , K ) ;
Minimum size of set having either element in range [ 0 , X ] or an odd power of 2 with sum N | Function to find the highest odd power of 2 in the range [ 0 , N ] ; If P is even , subtract 1 ; Function to find the minimum operations to make N ; If N is odd and X = 0 , then no valid set exist ; Stores the minimum possible size of the valid set ; Loop to subtract highest odd power of 2 while X < N , step 2 ; If N > 0 , then increment the value of answer by 1 ; Return the resultant size of set ; Driver Code
function highestPowerof2 ( n ) { let p = Math . floor ( Math . log2 ( n ) ) ; if ( p % 2 == 0 ) { p -= 1 } return Math . pow ( 2 , p ) } function minStep ( N , X ) { if ( N % 2 != 0 && X == 0 ) return - 1 let size = 0 while ( X < N ) { N -= highestPowerof2 ( N ) size += 1 } if ( N != 0 ) size += 1 return size ; } let N = 11 let X = 2 document . write ( minStep ( N , X ) )
Check if all the digits of the given number are same | Function to check if all the digits in the number N is the same or not ; Find the last digit ; Find the current last digit ; Update the value of N ; If there exists any distinct digit , then return No ; Otherwise , return Yes ; Driver Code
function checkSameDigits ( N ) { var digit = N % 10 ; while ( N != 0 ) { var current_digit = N % 10 ; N = parseInt ( N / 10 ) ; if ( current_digit != digit ) { return " " ; } } return " " ; } var N = 222 ; document . write ( checkSameDigits ( N ) ) ;
Find number formed by K times alternatively reducing X and adding Y to 0 | Function to find the value obtained after alternatively reducing X and adding Y to 0 total K number of times ; Stores the final result after adding only Y to 0 ; Stores the final number after reducing only X from 0 ; Return the result obtained ; Driver Code
function positionAfterKJumps ( X , Y , K ) { let addY = Y * Math . floor ( K / 2 ) ; let reduceX = - 1 * X * ( Math . floor ( K / 2 ) + K % 2 ) ; return addY + reduceX ; } let X = 2 , Y = 5 , K = 3 ; document . write ( positionAfterKJumps ( X , Y , K ) ) ;
Maximize the largest number K such that bitwise and of K till N is 0 | Function to find maximum value of k which makes bitwise AND zero . ; Take k = N initially ; Start traversing from N - 1 till 0 ; Whenever we get AND as 0 we stop and return ; Driver Code
function findMaxK ( N ) { let K = N ; for ( let i = N - 1 ; i >= 0 ; i -- ) { K &= i ; if ( K == 0 ) { return i ; } } return 0 ; } let N = 5 ; document . write ( findMaxK ( N ) ) ;
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all possible pairs ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
function countPairs ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ j ] % arr [ i ] == 0 ) && ( j + 1 ) % ( i + 1 ) == 0 && ( arr [ j ] / arr [ i ] == ( j + 1 ) / ( i + 1 ) ) ) { count ++ ; } } } return count ; } let arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ] ; let n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Number of minimum length paths between 1 to N including each node | Function to calculate the distances from node 1 to N ; Stores the number of edges ; Storing the edges in vector ; Initialize queue ; BFS from 1 st node using queue ; Pop from queue ; Traversing the adjacency list ; Initialize queue ; BFS from last node ; Pop from queue ; Traverse the adjacency list ; Print the count of minimum distance ; Driver Code
function countMinDistance ( n , m , edges ) { let g = new Array ( 10005 ) . fill ( 0 ) . map ( ( ) => [ ] ) ; for ( let i = 0 ; i < m ; i ++ ) { let a = edges [ i ] [ 0 ] - 1 ; let b = edges [ i ] [ 1 ] - 1 ; g [ a ] . push ( b ) ; g [ b ] . push ( a ) ; } let queue1 = [ ] ; queue1 . push ( [ 0 , 0 ] ) ; let dist = new Array ( n ) . fill ( 1e9 ) ; let ways1 = new Array ( n ) . fill ( 0 ) ; dist [ 0 ] = 0 ; ways1 [ 0 ] = 1 ; while ( queue1 . length > 0 ) { let up = queue1 [ 0 ] ; queue1 . pop ( ) ; let x = up [ 0 ] ; let dis = up [ 1 ] ; if ( dis > dist [ x ] ) continue ; if ( x == n - 1 ) continue ; for ( let y of g [ x ] ) { if ( dist [ y ] > dis + 1 ) { dist [ y ] = dis + 1 ; ways1 [ y ] = ways1 [ x ] ; queue1 . push ( [ y , dis + 1 ] ) ; } else if ( dist [ y ] == dis + 1 ) ways1 [ y ] += ways1 [ x ] ; } } let queue2 = [ ] ; queue2 . push ( [ n - 1 , 0 ] ) ; let dist1 = new Array ( n ) . fill ( 1e9 ) ; let ways2 = new Array ( n ) . fill ( 0 ) ; dist1 [ n - 1 ] = 0 ; ways2 [ n - 1 ] = 1 ; while ( queue2 . length > 0 ) { let up = queue2 [ 0 ] ; queue2 . pop ( ) ; let x = up [ 0 ] ; let dis = up [ 1 ] ; if ( dis > dist1 [ x ] ) continue ; if ( x == 0 ) continue ; for ( let y of g [ x ] ) { if ( dist1 [ y ] > dis + 1 ) { dist1 [ y ] = dis + 1 ; ways2 [ y ] = ways2 [ x ] ; queue2 . push ( [ y , dis + 1 ] ) ; } else if ( dist1 [ y ] == 1 + dis ) ways2 [ y ] += ways2 [ x ] ; } } ways1 [ n - 1 ] = 1 ; ways2 [ n - 1 ] = 1 ; for ( let i = 0 ; i < n ; i ++ ) document . write ( ways1 [ i ] * ways2 [ i ] + " " ) ; } let N = 5 ; let M = 5 ; let edges = [ [ 1 , 2 ] , [ 1 , 4 ] , [ 1 , 3 ] , [ 2 , 5 ] , [ 2 , 4 ] , ] ; countMinDistance ( N , M , edges ) ;
Count of N | JavaScript Program to implement the above approach ; Function to find the count of odd and even integers having N bits and K set bits ; Find the count of even integers ; Find the count of odd integers ; Print the total count ; Driver code
function factorial ( n ) { let ans = 1 ; while ( n >= 1 ) { ans *= n ; n -- ; } return ans ; } function Binary_Num ( n , k ) { let num_even , num_odd ; if ( n - k - 1 >= 0 && k - 1 >= 0 ) { num_even = Math . floor ( factorial ( n - 2 ) / ( factorial ( k - 1 ) * factorial ( n - k - 1 ) ) ) ; } else { num_even = 0 ; } if ( k - 2 >= 0 ) { num_odd = Math . floor ( factorial ( n - 2 ) / ( factorial ( k - 2 ) * factorial ( n - k ) ) ) ; } else { num_odd = 0 ; } document . write ( num_even + " " + num_odd + " " ) ; } let N = 9 , K = 6 ; Binary_Num ( N , K ) ;
Sum of all subsets of a given size ( = K ) | Function to find the sum of all sub - sets of size K ; Frequency of each array element in summation equation . ; calculate factorial of n - 1 ; calculate factorial of k - 1 ; calculate factorial of n - k ; Calculate sum of array . ; Sum of all subsets of size k . ; Driver Code
function findSumOfAllSubsets ( arr , n , k ) { let factorial_N = 1 , factorial_d = 1 , factorial_D = 1 ; for ( let i = 1 ; i <= n - 1 ; i ++ ) factorial_N *= i ; for ( let i = 1 ; i <= k - 1 ; i ++ ) factorial_d *= i ; for ( let i = 1 ; i <= n - k ; i ++ ) factorial_D *= i ; let freq = factorial_N / ( factorial_d * factorial_D ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum = sum * freq ; document . write ( " " + k + " " + sum + " " ) ; } let arr = [ 1 , 2 , 4 , 5 ] ; let n = 4 , k = 2 ; findSumOfAllSubsets ( arr , n , k ) ;
Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | Function to calculate maximum triplet XOR form 3 ranges ; Initialize a variable to store the answer ; Create minimum number that have a set bit at ith position ; Check for each number and try to greedily choose the bit if possible If A >= cur then A also have a set bit at ith position ; Increment the answer ; Subtract this value from A ; Check for B ; Increment the answer ; Subtract this value from B ; Check for C ; Increment the answer ; Subtract this value from C ; If any of the above conditions is not satisfied then there is no way to turn that bit ON . ; Driver Code
function maximumTripletXOR ( A , B , C ) { let ans = 0 ; for ( let i = 30 ; i >= 0 ; i -- ) { let cur = 1 << i ; if ( A >= cur ) { ans += cur ; A -= cur ; } else if ( B >= cur ) { ans += cur ; B -= cur ; } else if ( C >= cur ) { ans += cur ; C -= cur ; } } return ans ; } let A = 6 ; let B = 2 ; let C = 10 ; document . write ( maximumTripletXOR ( A , B , C ) ) ;
Count of integers in given range having their last K digits are equal | Function to return the count of integers from 1 to X having the last K digits as equal ; Stores the total count of integers ; Loop to iterate over all possible values of z ; Terminate the loop when z > X ; Add count of integers with last K digits equal to z ; Return count ; Function to return the count of integers from L to R having the last K digits as equal ; Driver Code ; Function Call
function intCount ( X , K ) { let ans = 0 ; for ( let z = 0 ; z < Math . pow ( 10 , K ) ; z += Math . floor ( ( Math . pow ( 10 , K ) - 1 ) / 9 ) ) { if ( z > X ) break ; ans += Math . floor ( ( ( X - z ) / Math . pow ( 10 , K ) + 1 ) ) ; } return ans ; } function intCountInRange ( L , R , K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } let L = 49 ; let R = 101 ; let K = 2 ; document . write ( intCountInRange ( L , R , K ) ) ;