codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subset of maximum sum possible by splitting array into two subsets | Function to split array elements into two subsets having sum of the smaller subset maximized ; Stores the size of the array ; Stores the frequency of array elements ; Stores the total sum of the array ; Stores the sum of the resultant set ; Stores if it is possible to split the array that satisfies the conditions ; Stores the elements of the first subseta ; Traverse the array arr [ ] ; Increment total sum ; Increment count of arr [ i ] ; Sort the array arr [ ] ; Stores the index of the last element of the array ; Traverse the array arr [ ] ; Stores the frequency of arr [ i ] ; If frq + ans . size ( ) is at most remaining size ; Append arr [ i ] to ans ; Decrement totSum by arr [ i ] ; Increment s by arr [ i ] ; Otherwise , decrement i by frq ; If s is greater than totSum ; Mark flag 1 ; If flag is equal to 1 ; Print the arrList ans ; Otherwise , print " - 1" ; Driver Code
function findSubset ( arr ) { var N = arr . length ; var mp = new Map ( ) ; var totSum = 0 ; var s = 0 ; var flag = 0 ; var ans = [ ] ; for ( var i = 0 ; i < arr . length ; i ++ ) { totSum += arr [ i ] ; if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) ; } arr . sort ( ( a , b ) => a - b ) var i = N - 1 ; while ( i >= 0 ) { var frq = mp . get ( arr [ i ] ) ; if ( ( frq + ans . length ) < ( N - ( frq + ans . length ) ) ) { for ( var k = 0 ; k < frq ; k ++ ) { ans . push ( arr [ i ] ) ; totSum -= arr [ i ] ; s += arr [ i ] ; i -- ; } } else { i -= frq ; } if ( s > totSum ) { flag = 1 ; break ; } } if ( flag == 1 ) { for ( i = ans . length - 1 ; i >= 0 ; i -- ) { document . write ( ans [ i ] + " " ) ; } } else { document . write ( - 1 ) ; } } var arr = [ 5 , 3 , 2 , 4 , 1 , 2 ] ; findSubset ( arr ) ;
Minimize Bitwise XOR of array elements with 1 required to make sum of array at least K | Function to find minimum number of Bitwise XOR of array elements with 1 required to make sum of the array at least K ; Stores the count of even array elements ; Stores sum of the array ; Traverse the array arr [ ] ; Increment sum ; If array element is even ; Increase count of even ; If S is at least K ; If S + E is less than K ; Otherwise , moves = K - S ; Driver Code
function minStepK ( arr , N , K ) { var E = 0 ; var S = 0 ; for ( var i = 0 ; i < N ; i ++ ) { S += arr [ i ] ; if ( arr [ i ] % 2 === 0 ) E += 1 ; } if ( S >= K ) return 0 ; else if ( S + E < K ) return - 1 ; else return K - S ; } var arr = [ 0 , 1 , 1 , 0 , 1 ] ; var N = arr . length ; var K = 4 ; document . write ( minStepK ( arr , N , K ) ) ;
Maximize difference between odd and even | Function to find maximum and minimum value of a number that can be obtained by rotating bits ; Stores the value of N ; Stores the maximum value ; Stores the minimum value ; If temp is odd ; Update the maximum and the minimum value ; If flag is 1 , then return the maximum value ; Otherwise , return the maximum value ; Function to find the maximum difference between the sum of odd and even - indexed array elements possible by rotating bits ; Stores the maximum difference ; Stores the sum of elements present at odd indices ; Stores the sum of elements present at even indices ; Traverse the given array ; If the index is even ; Update the caseOne ; Stores the maximum difference ; Stores the sum of elements placed at odd positions ; Stores the sum of elements placed at even positions ; Traverse the array ; If the index is even ; Update the caseTwo ; Return the maximum of caseOne and caseTwo ; Driver code
function Rotate ( n , f ) { let temp = n ; let maxi = n ; let mini = n ; for ( let idx = 0 ; idx < 7 ; idx ++ ) { if ( temp % 2 == 1 ) { temp >>= 1 ; temp += Math . pow ( 2 , 7 ) ; } else temp >>= 1 ; mini = Math . min ( mini , temp ) ; maxi = Math . max ( maxi , temp ) ; } if ( f == 1 ) return ( maxi ) ; else return ( mini ) ; } function calcMinDiff ( arr , n ) { let caseOne = 0 ; let sumOfodd = 0 ; let sumOfeven = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 0 ) ; else sumOfeven += Rotate ( arr [ i ] , 1 ) ; } caseOne = Math . abs ( sumOfodd - sumOfeven ) ; let caseTwo = 0 ; sumOfodd = 0 ; sumOfeven = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 1 ) ; else sumOfeven += Rotate ( arr [ i ] , 0 ) ; } caseTwo = Math . abs ( sumOfodd - sumOfeven ) ; return Math . max ( caseOne , caseTwo ) ; } let arr = [ 123 , 86 , 234 , 189 ] ; let n = arr . length ; document . write ( ( calcMinDiff ( arr , n ) ) ) ;
Array element with minimum sum of absolute differences | Set 2 | Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update res that gives the minimum sum ; If the current difference is less than the previous difference ; Update min_diff and res ; Print the resultant value ; Driver Code
function minimumDiff ( arr , N ) { let res = arr [ 0 ] , sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; let min_diff = sum ; for ( let i = 0 ; i < N ; i ++ ) { if ( Math . abs ( sum - ( arr [ i ] * N ) ) < min_diff ) { min_diff = Math . abs ( sum - ( arr [ i ] * N ) ) ; res = arr [ i ] ; } } document . write ( res ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; minimumDiff ( arr , N ) ;
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Stores the array elements ; Traverse the array ; Stores if the operation should be formed or not ; Traverse the vector V ; If flag is false and if v [ i ] is divisible by K ; Otherwise , set flag as true ; Increment the sum by v [ i % N ] ; Return the resultant sum ; Driver Code
function sum ( arr , N , K ) { var sum = 0 ; var i ; var v = [ ] ; for ( i = 0 ; i < N ; i ++ ) { v . push ( arr [ i ] ) ; } var flag = 0 ; for ( i = 0 ; i < v . length ; i ++ ) { if ( ! flag && v [ i ] % K == 0 ) v . push ( v [ i ] / K ) ; else { flag = 1 ; } sum = sum + v [ i % N ] ; } return sum ; } var arr = [ 4 , 6 , 8 , 2 ] ; var K = 2 ; var N = arr . length ; document . write ( sum ( arr , N , K ) ) ;
Count array elements whose count of divisors is a prime number | Function to count the array elements whose count of divisors is prime ; Stores the maximum element ; Find the maximum element ; Store if i - th element is prime ( 0 ) or non - prime ( 1 ) ; Base Case ; If i is a prime number ; Mark all multiples of i as non - prime ; Stores the count of divisors ; Base Case ; Iterate to count factors ; Stores the count of array elements whose count of divisors is a prime number ; Traverse the array arr [ ] ; If count of divisors is prime ; Return the resultant count ; Driver Code
function primeDivisors ( arr , N ) { var K = arr [ 0 ] ; var i , j ; for ( i = 1 ; i < N ; i ++ ) { K = Math . max ( K , arr [ i ] ) ; } var prime = Array ( K + 1 ) . fill ( 0 ) ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( i = 2 ; i < K + 1 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( j = 2 * i ; j < K + 1 ; j += i ) { prime [ j ] = 1 ; } } } var factor = Array ( K + 1 ) . fill ( 0 ) ; factor [ 0 ] = 0 ; factor [ 1 ] = 1 ; for ( i = 2 ; i < K + 1 ; i ++ ) { factor [ i ] += 1 ; for ( j = i ; j < K + 1 ; j += i ) { factor [ j ] += 1 ; } } var count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( prime [ factor [ arr [ i ] ] ] == 0 ) count ++ ; } return count ; } var arr = [ 10 , 13 , 17 , 25 ] ; var N = arr . length ; document . write ( primeDivisors ( arr , N ) ) ;
Count prime numbers up to N that can be represented as a sum of two prime numbers | Function to store all prime numbers up to N using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is prime ; Set all multiples of p as non - prime ; Function to count prime numbers up to N that can be represented as the sum of two prime numbers ; Stores all the prime numbers ; Update the prime array ; Create a dp array of size n + 1 ; Update dp [ 1 ] = 0 ; Iterate over the range [ 2 , N ] ; Add the previous count value ; Increment dp [ i ] by 1 if i and ( i - 2 ) are both prime ; Print the result ; Driver code
function SieveOfEratosthenes ( n , prime ) { prime [ 0 ] = 0 ; prime [ 1 ] = 0 ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == Boolean ( true ) ) { for ( var i = p * p ; i <= n ; i += p ) { prime [ i ] = Boolean ( false ) ; } } } } function countPrime ( n ) { var prime = new Array ( n + 1 ) ; var x = new Boolean ( true ) ; prime . fill ( x ) ; SieveOfEratosthenes ( n , prime ) ; var dp = new Array ( n + 1 ) ; dp . fill ( 0 ) ; dp [ 1 ] = 0 ; for ( var i = 2 ; i <= n ; i ++ ) { dp [ i ] += dp [ i - 1 ] ; if ( prime [ i ] == Boolean ( true ) && prime [ i - 2 ] == Boolean ( true ) ) { dp [ i ] ++ ; } } document . write ( dp [ n ] ) ; } var n = 6 ; countPrime ( n ) ;
Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | Function to find the minimum sum of the array such that Bitwise AND of arr [ i ] ana arr [ j ] is mat [ i ] [ j ] ; Stores the minimum possible sum ; Traverse the range [ 0 , N - 1 ] ; Stores the Bitwise OR of all the element of a row ; Traverse the range [ 0 , N - 1 ] ; If i not equal to j ; Update the value of res ; Increment the sum by res ; Return minimum possible sum ; Driver Code
function findMinSum ( mat , N ) { var sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var res = 0 ; for ( var j = 0 ; j < N ; j ++ ) { if ( i != j ) { res |= mat [ i ] [ j ] ; } } sum += res ; } return sum ; } var mat = [ [ - 1 , 2 , 3 ] , [ 9 , - 1 , 7 ] , [ 4 , 5 , - 1 ] ] ; var N = mat . length ; document . write ( findMinSum ( mat , N ) ) ;
LCM of unique elements present in an array | Function to find GCD of two numbers ; Base Case ; Recursively find the GCD ; Function to find LCM of two numbers ; Function to find LCM of unique elements present in the array ; Stores the frequency of each number of the array ; Store the frequency of each element of the array ; Store the required result ; Traverse the map freq ; If the frequency of the current element is 1 , then update ans ; If there is no unique element , set lcm to - 1 ; Print the result ; Driver Code ; Function Call
function findGCD ( a , b ) { if ( b == 0 ) return a ; return findGCD ( b , a % b ) ; } function findLCM ( a , b ) { return ( a * b ) / findGCD ( a , b ) ; } function uniqueElementsLCM ( arr , N ) { var freq = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } var lcm = 1 ; freq . forEach ( ( value , key ) => { if ( value == 1 ) { lcm = findLCM ( lcm , key ) ; } } ) ; if ( lcm == 1 ) lcm = - 1 ; document . write ( lcm ) ; } var arr = [ 1 , 2 , 1 , 3 , 3 , 4 ] ; var N = arr . length ; uniqueElementsLCM ( arr , N ) ;
Minimize maximum difference between adjacent elements possible by removing a single array element | Function to find maximum difference between adjacent array elements ; Store the maximum difference ; Traverse the array ; Update maximum difference ; Function to calculate the minimum of maximum difference between adjacent array elements possible by removing a single array element ; Stores the required minimum ; Stores the updated array ; Skip the i - th element ; Update MinValue ; return MinValue ; Driver code
function maxAdjacentDifference ( A ) { let diff = 0 ; for ( let i = 1 ; i < A . length ; i ++ ) { diff = Math . max ( diff , A [ i ] - A [ i - 1 ] ) ; } return diff ; } function MinimumValue ( arr , N ) { let MinValue = Number . MAX_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let new_arr = [ ] ; for ( let j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; new_arr . push ( arr [ j ] ) ; } MinValue = Math . min ( MinValue , maxAdjacentDifference ( new_arr ) ) ; } return MinValue ; } let arr = [ 1 , 3 , 7 , 8 ] ; let N = arr . length ; document . write ( MinimumValue ( arr , N ) ) ;
Number of Antisymmetric Relations on a set of N elements | Javascript program for the above approach ; Function to calculate the value of x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of antisymmetric relations in a set consisting of N elements ; Print the count of antisymmetric relations ; Driver Code
var mod = 1000000007 ; function power ( x , y ) { var res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } function antisymmetricRelation ( N ) { return ( power ( 2 , N ) * power ( 3 , ( N * N - N ) / 2 ) ) % mod ; } var N = 2 ; document . write ( antisymmetricRelation ( N ) ) ;
Number of Asymmetric Relations on a set of N elements | Javascript program for the above approach ; Function to calculate x ^ y modulo ( 10 ^ 9 + 7 ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the final value of x ^ y ; Function to count the number of asymmetric relations in a set consisting of N elements ; Return the resultant count ; Driver Code
var mod = 1000000007 ; function power ( x , y ) { var res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } function asymmetricRelation ( N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } var N = 2 ; document . write ( asymmetricRelation ( N ) ) ;
Check if two vectors are collinear or not | Function to calculate cross product of two vectors ; Update cross_P [ 0 ] ; Update cross_P [ 1 ] ; Update cross_P [ 2 ] ; Function to check if two given vectors are collinear or not ; Store the first and second vectors ; Store their cross product ; Calculate their cross product ; Check if their cross product is a NULL Vector or not ; Given coordinates of the two vectors
function crossProduct ( vect_A , vect_B , cross_P ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } function checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) { let A = [ x1 , y1 , z1 ] ; let B = [ x2 , y2 , z2 ] ; let cross_P = [ ] ; crossProduct ( A , B , cross_P ) ; if ( cross_P [ 0 ] == 0 && cross_P [ 1 ] == 0 && cross_P [ 2 ] == 0 ) document . write ( " " ) else document . write ( " " ) } let x1 = 4 , y1 = 8 , z1 = 12 ; let x2 = 8 , y2 = 16 , z2 = 24 ; checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) ;
Program to calculate Kinetic Energy and Potential Energy | Function to calculate Kinetic Energy ; Stores the Kinetic Energy ; Function to calculate Potential Energy ; Stores the Potential Energy ; Driver Code
function kineticEnergy ( M , V ) { let KineticEnergy ; KineticEnergy = 0.5 * M * V * V ; return KineticEnergy ; } function potentialEnergy ( M , H ) { let PotentialEnergy ; PotentialEnergy = M * 9.8 * H ; return PotentialEnergy ; } let M = 5.5 , H = 23.5 , V = 10.5 ; document . write ( " " + kineticEnergy ( M , V ) ) document . write ( " " ) ; document . write ( " " + potentialEnergy ( M , H ) )
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR | Function to count the number of pairs required to be removed from the array and replaced by their Bitwise OR values ; Stores the count of even array elements ; Traverse the given array ; Increment the count of even array elements ; If the array contains at least one odd array element ; Otherwise , print 0 ; Driver Code
function countPairs ( arr , N ) { let even = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( N - even >= 1 ) { document . write ( even ) ; return ; } document . write ( 0 ) ; } let arr = [ 5 , 4 , 7 , 2 ] ; let N = arr . length ; countPairs ( arr , N ) ;
Distribute M objects starting from Sth person such that every ith person gets arr [ i ] objects | Function to find distribution of M objects among all array elements ; Stores the distribution of M objects ; Stores the indices of distribution ; Stores the remaining objects ; Iterate until rem is positive ; If the number of remaining objects exceeds required the number of objects ; Increase the number of objects for the index ptr by arr [ ptr ] ; Decrease remaining objects by arr [ ptr ] ; Increase the number of objects for the index ptr by rem ; Decrease remaining objects to 0 ; Increase ptr by 1 ; Print the final distribution ; Driver Code
function distribute ( N , K , M , arr ) { let distribution = new Array ( N ) for ( let i = 0 ; i < N ; i ++ ) { distribution [ i ] = 0 } let ptr = K - 1 ; let rem = M ; while ( rem > 0 ) { if ( rem >= arr [ ptr ] ) { distribution [ ptr ] += arr [ ptr ] ; rem -= arr [ ptr ] ; } else { distribution [ ptr ] += rem ; rem = 0 ; } ptr = ( ptr + 1 ) % N ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( distribution [ i ] + " " ) } } let arr = [ 2 , 3 , 2 , 1 , 4 ] ; let M = 11 , S = 2 ; let N = arr . length distribute ( N , S , M , arr ) ;
Sum of squares of differences between all pairs of an array | Function to calculate sum of squares of differences of all possible pairs ; Stores the final sum ; Stores temporary values ; Traverse the array ; Final sum ; Print the answer ; Given array ; Size of the array ; Function call to find sum of square of differences of all possible pairs
function sumOfSquaredDifferences ( arr , N ) { let ans = 0 ; let sumA = 0 , sumB = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sumA += ( arr [ i ] * arr [ i ] ) ; sumB += arr [ i ] ; } sumA = N * sumA ; sumB = ( sumB * sumB ) ; ans = sumA - sumB ; document . write ( ans ) ; } let arr = [ 2 , 8 , 4 ] ; let N = arr . length ; sumOfSquaredDifferences ( arr , N ) ;
Count ways to remove objects such that exactly M equidistant objects remain | Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iterate until len <= n and increment the distance in each iteration ; Total length if adjacent objects are d distance apart ; If len > n ; Update the number of ways ; Print the result ; Driver Code
function waysToRemove ( n , m ) { var ans = 0 ; if ( m == 1 ) { document . write ( n ) ; return ; } for ( var d = 0 ; d >= 0 ; d ++ ) { var len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } document . write ( ans ) ; } var N = 5 , M = 3 ; waysToRemove ( N , M ) ;
Count unique stairs that can be reached by moving given number of steps forward or backward | Function to count the number of unique stairs visited ; Checks whether the current stair is visited or not ; Store the possible moves from the current position ; Initialize a queue ; / Push the starting position ; Mark the starting position S as visited ; Iterate until queue is not empty ; Store the current stair number ; Pop it from the queue ; Check for all possible moves from the current stair ; Store the new stair number ; If it is valid and unvisited ; Push it into queue ; Mark the stair as visited ; Store the result ; Count the all visited stairs ; Print the result ; Driver Code
function countStairs ( n , x , a , b ) { var vis = Array ( n + 1 ) . fill ( 0 ) ; var moves = [ + a , - a , + b , - b ] ; var q = [ ] ; q . push ( x ) ; vis [ x ] = 1 ; while ( q . length != 0 ) { var currentStair = q [ 0 ] ; q . shift ( ) ; for ( var j = 0 ; j < 4 ; j ++ ) { var newStair = currentStair + moves [ j ] ; if ( newStair > 0 && newStair <= n && ! vis [ newStair ] ) { q . push ( newStair ) ; vis [ newStair ] = 1 ; } } } var cnt = 0 ; for ( var i = 1 ; i <= n ; i ++ ) if ( vis [ i ] == 1 ) cnt ++ ; document . write ( cnt ) ; } var N = 10 , S = 2 , A = 5 , B = 7 ; countStairs ( N , S , A , B ) ;
Count smaller elements present in the array for each array element | Function to count for each array element , the number of elements that are smaller than that element ; Traverse the array ; Stores the count ; Traverse the array ; Increment count ; Print the count of smaller elements for the current element ; Driver Code
function smallerNumbers ( arr , N ) { var i ; for ( i = 0 ; i < N ; i ++ ) { var count = 0 ; for ( j = 0 ; j < N ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { count += 1 ; } } document . write ( count + " " ) ; } } var arr = [ 3 , 4 , 1 , 1 , 2 ] var N = arr . length ; smallerNumbers ( arr , N ) ;
Minimize divisions by 2 , 3 , or 5 required to make two given integers equal | Function to calculate GCD of two numbers ; Base Case ; Calculate GCD recursively ; Function to count the minimum number of divisions required to make X and Y equal ; Calculate GCD of X and Y ; Divide X and Y by their GCD ; Stores the number of divisions ; Iterate until X != Y ; Maintain the order X <= Y ; If X is divisible by 2 , then divide X by 2 ; If X is divisible by 3 , then divide X by 3 ; If X is divisible by 5 , then divide X by 5 ; If X is not divisible by 2 , 3 , or 5 , then prvar - 1 ; Increment count by 1 ; Print the value of count as the minimum number of operations ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } function minimumOperations ( X , Y ) { var GCD = gcd ( X , Y ) ; X = X / GCD ; Y = Y / GCD ; var count = 0 ; while ( X != Y ) { if ( Y > X ) { var t = X ; X = Y ; Y = t ; } if ( X % 2 == 0 ) { X = X / 2 ; } else if ( X % 3 == 0 ) { X = X / 3 ; } else if ( X % 5 == 0 ) { X = X / 5 ; } else { document . write ( " " ) ; return ; } count += 1 ; } document . write ( count ) ; } var X = 15 , Y = 20 ; minimumOperations ( X , Y ) ;
Minimum increments or decrements required to convert a sorted array into a power sequence | Function to find the minimum number of increments or decrements required to convert array into a power sequence ; Initialize the count to f ( X ) for X = 1 ; Calculate the value of f ( X ) X ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ; Calculate F ( x ) ; Check if X ^ ( n - 1 ) > f ( 1 ) + a [ n - 1 ] ; Update ans to store the minimum of ans and F ( x ) ; Return the minimum number of operations required ; Driver Code
function minOperations ( a , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { ans += a [ i ] ; } ans -= n ; for ( x = 1 ; ; x ++ ) { var curPow = 1 , curCost = 0 ; for ( i = 0 ; i < n ; i ++ ) { curCost += Math . abs ( a [ i ] - curPow ) ; curPow *= x ; } if ( curPow / x > ans + a [ n - 1 ] ) break ; ans = Math . min ( ans , curCost ) ; } return ans ; } var arr = [ 1 , 5 , 7 ] ; var N = arr . length ; document . write ( minOperations ( arr , N ) ) ;
Modulo Operations in Programming With Negative Results | Function to calculate and return the remainder of a % n ; Type casting is necessary as ( int ) / ( int ) will give int result , i . e . - 3 / 2 will give - 1 and not - 1.5 ; Return the resultant remainder ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
function floorMod ( a , n ) { let q = Math . floor ( Math . floor ( a / n ) ) ; return a - n * q ; } let a , b ; a = 9 ; b = 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ; a = - 9 ; b = 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ; a = 9 ; b = - 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ; a = - 9 ; b = - 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ;