codeparrot/xlcost-text-to-code huggingface dataset:
Array element with minimum sum of absolute differences | Set 2 | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumDiff ( int arr [ ] , int N ) { int res = arr [ 0 ] , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int min_diff = sum ; for ( int i = 0 ; i < N ; i ++ ) { if ( abs ( sum - ( arr [ i ] * N ) ) < min_diff ) { min_diff = abs ( sum - ( arr [ i ] * N ) ) ; res = arr [ i ] ; } } cout << res ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumDiff ( arr , N ) ; return 0 ; }
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int arr [ ] , int N , int K ) { int sum = 0 ; vector < int > v ; for ( int i = 0 ; i < N ; i ++ ) { v . push_back ( arr [ i ] ) ; } bool flag = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( ! flag && v [ i ] % K == 0 ) v . push_back ( v [ i ] / K ) ; else { flag = 1 ; } sum = sum + v [ i % N ] ; } return sum ; } int main ( ) { int arr [ ] = { 4 , 6 , 8 , 2 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sum ( arr , N , K ) ; return 0 ; }
Count array elements whose count of divisors is a prime number | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int primeDivisors ( int arr [ ] , int N ) { int K = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { K = max ( K , arr [ i ] ) ; } int prime [ K + 1 ] = { 0 } ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( int i = 2 ; i < K + 1 ; i ++ ) { if ( ! prime [ i ] ) { for ( int j = 2 * i ; j < K + 1 ; j += i ) { prime [ j ] = 1 ; } } } int factor [ K + 1 ] = { 0 } ; factor [ 0 ] = 0 ; factor [ 1 ] = 1 ; for ( int i = 2 ; i < K + 1 ; i ++ ) { factor [ i ] += 1 ; for ( int j = i ; j < K + 1 ; j += i ) { factor [ j ] += 1 ; } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( prime [ factor [ arr [ i ] ] ] == 0 ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 10 , 13 , 17 , 25 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << primeDivisors ( arr , N ) ; return 0 ; }
Count prime numbers up to N that can be represented as a sum of two prime numbers | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( int n , bool prime [ ] ) { prime [ 0 ] = 0 ; prime [ 1 ] = 0 ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } void countPrime ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( n , prime ) ; int dp [ n + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] += dp [ i - 1 ] ; if ( prime [ i ] == 1 && prime [ i - 2 ] == 1 ) { dp [ i ] ++ ; } } cout << dp [ n ] ; } int main ( ) { int N = 6 ; countPrime ( N ) ; return 0 ; }
Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | C ++ program of the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinSum ( vector < vector < int > > mat , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i != j ) { res |= mat [ i ] [ j ] ; } } sum += res ; } return sum ; } int main ( ) { vector < vector < int > > mat = { { -1 , 2 , 3 } , { 9 , -1 , 7 } , { 4 , 5 , -1 } } ; int N = mat . size ( ) ; cout << findMinSum ( mat , N ) ; return 0 ; }
LCM of unique elements present in an array | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findGCD ( int a , int b ) { if ( b == 0 ) return a ; return findGCD ( b , a % b ) ; } int findLCM ( int a , int b ) { return ( a * b ) / findGCD ( a , b ) ; } int uniqueElementsLCM ( int arr [ ] , int N ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } int lcm = 1 ; for ( auto i : freq ) { if ( i . second == 1 ) { lcm = findLCM ( lcm , i . first ) ; } } if ( lcm == 1 ) lcm = -1 ; cout << lcm ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 3 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; uniqueElementsLCM ( arr , N ) ; return 0 ; }
Minimize maximum difference between adjacent elements possible by removing a single array element | C ++ Program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAdjacentDifference ( vector < int > A ) { int diff = 0 ; for ( int i = 1 ; i < ( int ) A . size ( ) ; i ++ ) { diff = max ( diff , A [ i ] - A [ i - 1 ] ) ; } return diff ; } int MinimumValue ( int arr [ ] , int N ) { int MinValue = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { vector < int > new_arr ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; new_arr . push_back ( arr [ j ] ) ; } MinValue = min ( MinValue , maxAdjacentDifference ( new_arr ) ) ; } return MinValue ; } int main ( ) { int arr [ ] = { 1 , 3 , 7 , 8 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << MinimumValue ( arr , N ) ; return 0 ; }
Number of Antisymmetric Relations on a set of N elements | C ++ 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
#include <iostream> NEW_LINE using namespace std ; const int mod = 1000000007 ; int power ( long long x , unsigned int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } int antisymmetricRelation ( int N ) { return ( power ( 2 , N ) * 1LL * power ( 3 , ( N * N - N ) / 2 ) ) % mod ; } int main ( ) { int N = 2 ; cout << antisymmetricRelation ( N ) ; return 0 ; }
Number of Asymmetric Relations on a set of N elements | C ++ 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
#include <iostream> NEW_LINE using namespace std ; const int mod = 1000000007 ; int power ( long long x , unsigned int y ) { int 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 ; } int asymmetricRelation ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } int main ( ) { int N = 2 ; cout << asymmetricRelation ( N ) ; return 0 ; }
Check if two vectors are collinear or not | C ++ program for the above approach ; 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 ; Driver Code ; Given coordinates of the two vectors
#include <bits/stdc++.h> NEW_LINE using namespace std ; void crossProduct ( int vect_A [ ] , int vect_B [ ] , int 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 ] ; } void checkCollinearity ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { int A [ 3 ] = { x1 , y1 , z1 } ; int B [ 3 ] = { x2 , y2 , z2 } ; int cross_P [ 3 ] ; crossProduct ( A , B , cross_P ) ; if ( cross_P [ 0 ] == 0 && cross_P [ 1 ] == 0 && cross_P [ 2 ] == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int x1 = 4 , y1 = 8 , z1 = 12 ; int x2 = 8 , y2 = 16 , z2 = 24 ; checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) ; return 0 ; }
Program to calculate Kinetic Energy and Potential Energy | C ++ Program to implement the above approach ; Function to calculate Kinetic Energy ; Stores the Kinetic Energy ; Function to calculate Potential Energy ; Stores the Potential Energy ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float kineticEnergy ( float M , float V ) { float KineticEnergy ; KineticEnergy = 0.5 * M * V * V ; return KineticEnergy ; } float potentialEnergy ( float M , float H ) { float PotentialEnergy ; PotentialEnergy = M * 9.8 * H ; return PotentialEnergy ; } int main ( ) { float M = 5.5 , H = 23.5 , V = 10.5 ; cout << " Kinetic ▁ Energy ▁ = ▁ " << kineticEnergy ( M , V ) << endl ; cout << " Potential ▁ Energy ▁ = ▁ " << potentialEnergy ( M , H ) << endl ; return 0 ; }
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N ) { int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( N - even >= 1 ) { cout << even ; return ; } cout << 0 ; } int main ( ) { int arr [ ] = { 5 , 4 , 7 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Distribute M objects starting from Sth person such that every ith person gets arr [ i ] objects | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void distribute ( int N , int K , int M , int arr [ ] ) { int distribution [ N ] = { 0 } ; int ptr = K - 1 ; int 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 ( int i = 0 ; i < N ; i ++ ) { cout << distribution [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 2 , 1 , 4 } ; int M = 11 , S = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; distribute ( N , S , M , arr ) ; return 0 ; }
Sum of squares of differences between all pairs of an array | C ++ program for the above approach ; 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 ; Driver Code ; Given array ; Size of the array ; Function call to find sum of square of differences of all possible pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfSquaredDifferences ( int arr [ ] , int N ) { int ans = 0 ; int sumA = 0 , sumB = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sumA += ( arr [ i ] * arr [ i ] ) ; sumB += arr [ i ] ; } sumA = N * sumA ; sumB = ( sumB * sumB ) ; ans = sumA - sumB ; cout << ans ; } int main ( ) { int arr [ ] = { 2 , 8 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sumOfSquaredDifferences ( arr , N ) ; return 0 ; }
Count ways to remove objects such that exactly M equidistant objects remain | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void waysToRemove ( int n , int m ) { int ans = 0 ; if ( m == 1 ) { cout << n ; return ; } for ( int d = 0 ; d >= 0 ; d ++ ) { int len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } cout << ans ; } int main ( ) { int N = 5 , M = 3 ; waysToRemove ( N , M ) ; return 0 ; }
Count unique stairs that can be reached by moving given number of steps forward or backward | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countStairs ( int n , int x , int a , int b ) { int vis [ n + 1 ] = { 0 } ; int moves [ ] = { + a , - a , + b , - b } ; queue < int > q ; q . push ( x ) ; vis [ x ] = 1 ; while ( ! q . empty ( ) ) { int currentStair = q . front ( ) ; q . pop ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int newStair = currentStair + moves [ j ] ; if ( newStair > 0 && newStair <= n && ! vis [ newStair ] ) { q . push ( newStair ) ; vis [ newStair ] = 1 ; } } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( vis [ i ] == 1 ) cnt ++ ; cout << cnt ; } int main ( ) { int N = 10 , S = 2 , A = 5 , B = 7 ; countStairs ( N , S , A , B ) ; return 0 ; }
Count smaller elements present in the array for each array element | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallerNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { count ++ ; } } cout << count << " ▁ " ; } } int main ( ) { int arr [ ] = { 3 , 4 , 1 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smallerNumbers ( arr , N ) ; return 0 ; }
Minimize divisions by 2 , 3 , or 5 required to make two given integers equal | C ++ program for the above approach ; 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 print - 1 ; Increment count by 1 ; Print the value of count as the minimum number of operations ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } void minimumOperations ( int X , int Y ) { int GCD = gcd ( X , Y ) ; X = X / GCD ; Y = Y / GCD ; int count = 0 ; while ( X != Y ) { if ( Y > X ) { swap ( X , Y ) ; } if ( X % 2 == 0 ) { X = X / 2 ; } else if ( X % 3 == 0 ) { X = X / 3 ; } else if ( X % 5 == 0 ) { X = X / 5 ; } else { cout << " - 1" ; return ; } count ++ ; } cout << count ; } int main ( ) { int X = 15 , Y = 20 ; minimumOperations ( X , Y ) ; return 0 ; }
Minimum increments or decrements required to convert a sorted array into a power sequence | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int a [ ] , int n ) { int ans = accumulate ( a , a + n , 0 ) - n ; for ( int x = 1 ; ; x ++ ) { int curPow = 1 , curCost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curCost += abs ( a [ i ] - curPow ) ; curPow *= x ; } if ( curPow / x > ans + a [ n - 1 ] ) break ; ans = min ( ans , curCost ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 5 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperations ( arr , N ) ; return 0 ; }
Modulo Operations in Programming With Negative Results | C ++ program to illustrate modulo operations using the above equation ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int floorMod ( int a , int n ) { int q = ( int ) floor ( ( double ) a / n ) ; return a - n * q ; } int main ( ) { int a , b ; a = 9 ; b = 4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << floorMod ( a , b ) << " STRNEWLINE " ; a = -9 ; b = 4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << floorMod ( a , b ) << " STRNEWLINE " ; a = 9 ; b = -4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << floorMod ( a , b ) << " STRNEWLINE " ; a = -9 ; b = -4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << floorMod ( a , b ) << " STRNEWLINE " ; return 0 ; }
Find the absolute difference between the nearest powers of two given integers for every array element | C ++ program for the above approach ; Function to print the array ; Traverse the array ; Function to modify array elements by absolute difference of the nearest perfect power of a and b ; Traverse the array arr [ ] ; Find the log a of arr [ i ] ; Find the power of a less than and greater than a ; Find the log b of arr [ i ] ; Find the power of b less than and greater than b ; Update arr [ i ] with absolute difference of log_a & log _b ; Print the modified array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } void nearestPowerDiff ( int arr [ ] , int N , int a , int b ) { for ( int i = 0 ; i < N ; i ++ ) { int log_a = log ( arr [ i ] ) / log ( a ) ; int A = pow ( a , log_a ) ; int B = pow ( a , log_a + 1 ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_a = A ; else log_a = B ; int log_b = log ( arr [ i ] ) / log ( b ) ; A = pow ( b , log_b ) ; B = pow ( b , log_b + 1 ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_b = A ; else log_b = B ; arr [ i ] = abs ( log_a - log_b ) ; } printArray ( arr , N ) ; } int main ( ) { int arr [ ] = { 5 , 12 , 25 } ; int A = 2 , B = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestPowerDiff ( arr , N , A , B ) ; return 0 ; }