codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of operations to convert array A to array B by adding an integer into a subarray | C ++ implementation to find the minimum number of operations in which the array A can be converted to another array B ; Function to find the minimum number of operations in which array A can be converted to array B ; Loop to iterate over the array ; if both elements are equal then move to next element ; Calculate the difference between two elements ; loop while the next pair of elements have same difference ; Increase the number of operations by 1 ; Print the number of operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkArray ( int a [ ] , int b [ ] , int n ) { int operations = 0 ; int i = 0 ; while ( i < n ) { if ( a [ i ] - b [ i ] == 0 ) { i ++ ; continue ; } int diff = a [ i ] - b [ i ] ; i ++ ; while ( i < n && a [ i ] - b [ i ] == diff ) { i ++ ; } operations ++ ; } cout << operations << " STRNEWLINE " ; } int main ( ) { int a [ ] = { 3 , 7 , 1 , 4 , 1 , 2 } ; int b [ ] = { 3 , 7 , 3 , 6 , 3 , 2 } ; int size = sizeof ( a ) / sizeof ( a [ 0 ] ) ; checkArray ( a , b , size ) ; return 0 ; }
Perfect Cube | C ++ program to check if a number is a perfect cube using prime factors ; Inserts the prime factor in HashMap if not present if present updates it 's frequency ; A utility function to find all prime factors of a given number N ; Insert the number of 2 s that divide n ; n must be odd at this point So we can skip one element ; while i divides n , insert i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check if a number is perfect cube ; Iteration in Map ; Driver Code ; Function to check if N is perfect cube or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , int > insertPF ( map < int , int > primeFact , int fact ) { if ( primeFact . find ( fact ) != primeFact . end ( ) ) { primeFact [ fact ] ++ ; } else { primeFact [ fact ] = 1 ; } return primeFact ; } map < int , int > primeFactors ( int n ) { map < int , int > primeFact ; while ( n % 2 == 0 ) { primeFact = insertPF ( primeFact , 2 ) ; n /= 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { primeFact = insertPF ( primeFact , i ) ; n /= i ; } } if ( n > 2 ) primeFact = insertPF ( primeFact , n ) ; return primeFact ; } string perfectCube ( int n ) { map < int , int > primeFact ; primeFact = primeFactors ( n ) ; for ( auto x : primeFact ) { if ( x . second % 3 != 0 ) return " No " ; } return " Yes " ; } int main ( ) { int N = 216 ; cout << perfectCube ( N ) ; return 0 ; }
Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3 | C ++ implementation to find the number the number of ways to reach Nth stair by taking 1 or 2 steps at a time and 3 rd step exactly once ; Single line to find factorial ; Function to find the number of ways ; Base Case ; Count of 2 - steps ; Count of 1 - steps ; Initial length of sequence ; Expected count of 2 - steps ; Loop to find the ways for every possible sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { return ( n == 1 n == 0 ) ? 1 : n * factorial ( n - 1 ) ; } int ways ( int n ) { if ( n < 3 ) { return 0 ; } int c2 = 0 ; int c1 = n - 3 ; int l = c1 + 1 ; int s = 0 ; int exp_c2 = c1 / 2 ; while ( exp_c2 >= c2 ) { int f1 = factorial ( l ) ; int f2 = factorial ( c1 ) ; int f3 = factorial ( c2 ) ; int f4 = ( f2 * f3 ) ; s += f1 / f4 ; c2 += 1 ; c1 -= 2 ; l -= 1 ; } return s ; } int main ( ) { int n = 7 ; int ans = ways ( n ) ; cout << ans << endl ; return 0 ; }
Find N from the value of N ! | C ++ program to find a number such that the factorial of that number is given ; Map to precompute and store the factorials of the numbers ; Function to precompute factorial ; Calculating the factorial for each i and storing in a map ; Driver code ; Precomputing the factorials
#include " bits / stdc + + . h " NEW_LINE #define ll  long long int NEW_LINE using namespace std ; map < ll , ll > m ; int precompute ( ) { ll fact = 1 ; for ( ll i = 1 ; i <= 18 ; i ++ ) { fact = fact * i ; m [ fact ] = i ; } } int main ( ) { precompute ( ) ; int K = 120 ; cout << m [ K ] << endl ; K = 6 ; cout << m [ K ] << endl ; return 0 ; }
Count of Leap Years in a given year range | C ++ implementation to find the count of leap years in given range of the year ; Function to calculate the number of leap years in range of ( 1 , year ) ; Function to calculate the number of leap years in given range ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calNum ( int year ) { return ( year / 4 ) - ( year / 100 ) + ( year / 400 ) ; } void leapNum ( int l , int r ) { l -- ; int num1 = calNum ( r ) ; int num2 = calNum ( l ) ; cout << num1 - num2 << endl ; } int main ( ) { int l1 = 1 , r1 = 400 ; leapNum ( l1 , r1 ) ; int l2 = 400 , r2 = 2000 ; leapNum ( l2 , r2 ) ; return 0 ; }
Ternary representation of Cantor set | C ++ implementation to find the cantor set for n levels and for a given start_num and end_num ; The Linked List Structure for the Cantor Set ; Function to initialize the Cantor Set List ; Function to propogate the list by adding new nodes for the next levels ; Modifying the start and end values for the next level ; Changing the pointers to the next node ; Recursively call the function to generate the Cantor Set for the entire level ; Function to print a level of the Set ; Function to build and display the Cantor Set for each level ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef struct cantor { double start , end ; struct cantor * next ; } Cantor ; Cantor * startList ( Cantor * head , double start_num , double end_num ) { if ( head == NULL ) { head = new Cantor ; head -> start = start_num ; head -> end = end_num ; head -> next = NULL ; } return head ; } Cantor * propagate ( Cantor * head ) { Cantor * temp = head ; if ( temp != NULL ) { Cantor * newNode = new Cantor ; double diff = ( ( ( temp -> end ) - ( temp -> start ) ) / 3 ) ; newNode -> end = temp -> end ; temp -> end = ( ( temp -> start ) + diff ) ; newNode -> start = ( newNode -> end ) - diff ; newNode -> next = temp -> next ; temp -> next = newNode ; propagate ( temp -> next -> next ) ; } return head ; } void print ( Cantor * temp ) { while ( temp != NULL ) { printf ( " [ % lf ] ▁ - - ▁ [ % lf ] TABSYMBOL " , temp -> start , temp -> end ) ; temp = temp -> next ; } cout << endl ; } void buildCantorSet ( int A , int B , int L ) { Cantor * head = NULL ; head = startList ( head , A , B ) ; for ( int i = 0 ; i < L ; i ++ ) { cout << " Level _ " << i << " ▁ : ▁ " ; print ( head ) ; propagate ( head ) ; } cout << " Level _ " << L << " ▁ : ▁ " ; print ( head ) ; } int main ( ) { int A = 0 ; int B = 9 ; int L = 2 ; buildCantorSet ( A , B , L ) ; return 0 ; }
Find sum of f ( s ) for all the chosen sets from the given array | C ++ implementation of the approach ; To store the factorial and the factorial mod inverse of a number ; Function to find ( a ^ m1 ) % mod ; Function to find factorial of all the numbers ; Function to find the factorial mod inverse of all the numbers ; Function to return nCr ; Function to find sum of f ( s ) for all the chosen sets from the given array ; Sort the given array ; Calculate the factorial and modinverse of all elements ; For all the possible sets Calculate max ( S ) and min ( S ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE #define mod  (int)(1e9 + 7) NEW_LINE int factorial [ N ] , modinverse [ N ] ; int power ( int a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1LL * a * a ) % mod ; else if ( m1 & 1 ) return ( 1LL * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( 1LL * factorial [ i - 1 ] * i ) % mod ; } void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( 1LL * modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } int binomial ( int n , int r ) { if ( r > n ) return 0 ; int a = ( 1LL * factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( 1LL * a * modinverse [ r ] ) % mod ; return a ; } int max_min ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; factorialfun ( ) ; modinversefun ( ) ; long long ans = 0 ; k -- ; for ( int i = 0 ; i < n ; i ++ ) { int x = n - i - 1 ; if ( x >= k ) ans -= binomial ( x , k ) * a [ i ] % mod ; int y = i ; if ( y >= k ) ans += binomial ( y , k ) * a [ i ] % mod ; ans = ( ans + mod ) % mod ; } return ( int ) ( ans ) ; } int main ( ) { int a [ ] = { 1 , 1 , 3 , 4 } , k = 2 ; int n = sizeof ( a ) / sizeof ( int ) ; cout << max_min ( a , n , k ) ; return 0 ; }
Length of Smallest subarray in range 1 to N with sum greater than a given value | C ++ implementation of the above implementation ; Function to return the count of minimum elements such that the sum of those elements is > S . ; Initialize currentSum = 0 ; Loop from N to 1 to add the numbers and check the condition . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumber ( int N , int S ) { int countElements = 0 ; int currSum = 0 ; while ( currSum <= S ) { currSum += N ; N -- ; countElements ++ ; } return countElements ; } int main ( ) { int N , S ; N = 5 ; S = 11 ; int count = countNumber ( N , S ) ; cout << count << endl ; return 0 ; }
Next Number with distinct digits | C ++ program to find next consecutive Number with all distinct digits ; Function to count distinct digits in a number ; To count the occurrence of digits in number from 0 to 9 ; Iterate over the digits of the number Flag those digits as found in the array ; Traverse the array arr and count the distinct digits in the array ; Function to return the total number of digits in the number ; Iterate over the digits of the number ; Function to return the next number with distinct digits ; Count the distinct digits in N + 1 ; Count the total number of digits in N + 1 ; Return the next consecutive number ; Increment Number by 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDistinct ( int n ) { int arr [ 10 ] = { 0 } ; int count = 0 ; while ( n ) { int r = n % 10 ; arr [ r ] = 1 ; n /= 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] ) count ++ ; } return count ; } int countDigit ( int n ) { int c = 0 ; while ( n ) { int r = n % 10 ; c ++ ; n /= 10 ; } return c ; } int nextNumberDistinctDigit ( int n ) { while ( n < INT_MAX ) { int distinct_digits = countDistinct ( n + 1 ) ; int total_digits = countDigit ( n + 1 ) ; if ( distinct_digits == total_digits ) { return n + 1 ; } else n ++ ; } return -1 ; } int main ( ) { int n = 2019 ; cout << nextNumberDistinctDigit ( n ) ; return 0 ; }
Minimum possible sum of array B such that AiBi = AjBj for all 1 â ‰¤ i < j â ‰¤ N | C ++ implementation of the approach ; To store least prime factors of all the numbers ; Function to find the least prime factor of all the numbers ; Function to return the ( ( a ^ m1 ) % mod ) ; Function to return the sum of elements of array B ; Find the prime factors of all the numbers ; To store each prime count in lcm ; Current number ; Map to store the prime count of a single number ; Basic way to calculate all prime factors ; If it is the first number in the array ; Take the maximum count of prime in a number ; Calculate lcm of given array ; Calculate sum of elements of array B ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  (int)(1e9 + 7) NEW_LINE #define N  1000005 NEW_LINE int lpf [ N ] ; void least_prime_factor ( ) { for ( int i = 1 ; i < N ; i ++ ) lpf [ i ] = i ; for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] == i ) for ( int j = i * 2 ; j < N ; j += i ) if ( lpf [ j ] == j ) lpf [ j ] = i ; } int power ( int a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1LL * a * a ) % mod ; else if ( m1 & 1 ) return ( 1LL * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } long long sum_of_elements ( int a [ ] , int n ) { least_prime_factor ( ) ; map < int , int > prime_factor ; for ( int i = 0 ; i < n ; i ++ ) { int temp = a [ i ] ; map < int , int > single_number ; while ( temp > 1 ) { int x = lpf [ temp ] ; single_number [ x ] ++ ; temp /= x ; } if ( i == 0 ) prime_factor = single_number ; else { for ( auto x : single_number ) prime_factor [ x . first ] = max ( x . second , prime_factor [ x . first ] ) ; } } long long ans = 0 , lcm = 1 ; for ( auto x : prime_factor ) lcm = ( lcm * power ( x . first , x . second ) ) % mod ; for ( int i = 0 ; i < n ; i ++ ) ans = ( ans + ( lcm * power ( a [ i ] , mod - 2 ) ) % mod ) % mod ; return ans ; } int main ( ) { int a [ ] = { 2 , 3 , 4 } ; int n = sizeof ( a ) / sizeof ( int ) ; cout << sum_of_elements ( a , n ) ; return 0 ; }
Find Number of Even cells in a Zero Matrix after Q queries | C ++ program find Number of Even cells in a Zero Matrix after Q queries ; Function to find the number of even cell in a 2D matrix ; Maintain two arrays , one for rows operation and one for column operation ; Increment operation on row [ i ] ; Increment operation on col [ i ] ; Count odd and even values in both arrays and multiply them ; Count of rows having even numbers ; Count of rows having odd numbers ; Count of columns having even numbers ; Count of columns having odd numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfEvenCells ( int n , int q [ ] [ 2 ] , int size ) { int row [ n ] = { 0 } ; int col [ n ] = { 0 } ; for ( int i = 0 ; i < size ; i ++ ) { int x = q [ i ] [ 0 ] ; int y = q [ i ] [ 1 ] ; row [ x - 1 ] ++ ; col [ y - 1 ] ++ ; } int r1 = 0 , r2 = 0 ; int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( row [ i ] % 2 == 0 ) { r1 ++ ; } if ( row [ i ] % 2 == 1 ) { r2 ++ ; } if ( col [ i ] % 2 == 0 ) { c1 ++ ; } if ( col [ i ] % 2 == 1 ) { c2 ++ ; } } int count = r1 * c1 + r2 * c2 ; return count ; } int main ( ) { int n = 2 ; int q [ ] [ 2 ] = { { 1 , 1 } , { 1 , 2 } , { 2 , 1 } } ; int size = sizeof ( q ) / sizeof ( q [ 0 ] ) ; cout << findNumberOfEvenCells ( n , q , size ) ; return 0 ; }
Find maximum unreachable height using two ladders | C ++ implementation of the approach ; Function to return the maximum height which can 't be reached ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxHeight ( int h1 , int h2 ) { return ( ( h1 * h2 ) - h1 - h2 ) ; } int main ( ) { int h1 = 7 , h2 = 5 ; cout << max ( 0 , maxHeight ( h1 , h2 ) ) ; return 0 ; }
Fermat 's Factorization Method | C ++ implementation of fermat 's factorization ; This function finds the value of a and b and returns a + b and a - b ; since fermat 's factorization applicable  for odd positive integers only ; check if n is a even number ; if n is a perfect root , then both its square roots are its factors ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FermatFactors ( int n ) { if ( n <= 0 ) { cout << " [ " << n << " ] " ; return ; } if ( ( n & 1 ) == 0 ) { cout << " [ " << n / 2.0 << " , " << 2 << " ] " ; return ; } int a = ceil ( sqrt ( n ) ) ; if ( a * a == n ) { cout << " [ " << a << " , " << a << " ] " ; return ; } int b ; while ( true ) { int b1 = a * a - n ; b = ( int ) sqrt ( b1 ) ; if ( b * b == b1 ) break ; else a += 1 ; } cout << " [ " << ( a - b ) << " , " << ( a + b ) << " ] " ; return ; } int main ( ) { FermatFactors ( 6557 ) ; return 0 ; }
Append two elements to make the array satisfy the given condition | C ++ implementation of the approach ; Function to find the required numbers ; Find the sum and xor ; Print the required elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNums ( int arr [ ] , int n ) { int S = 0 , X = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += arr [ i ] ; X ^= arr [ i ] ; } cout << X << " ▁ " << ( X + S ) ; } int main ( ) { int arr [ ] = { 1 , 7 } ; int n = sizeof ( arr ) / sizeof ( int ) ; findNums ( arr , n ) ; return 0 ; }
Satisfy the parabola when point ( A , B ) and the equation is given | C ++ implementation of the approach ; Function to find the required values ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int A , int B ) { double p = B / 2.0 ; int M = ceil ( 4 * p ) ; int N = 1 ; int O = - 2 * A ; int Q = ceil ( A * A + 4 * p * p ) ; cout << M << " ▁ " << N << " ▁ " << O << " ▁ " << Q ; } int main ( ) { int a = 1 ; int b = 1 ; solve ( a , b ) ; }
Largest number dividing maximum number of elements in the array | C ++ implementation of the approach ; Function to return the largest number that divides the maximum elements from the given array ; Finding gcd of all the numbers in the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLargest ( int * arr , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( arr [ i ] , gcd ) ; return gcd ; } int main ( ) { int arr [ ] = { 3 , 6 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findLargest ( arr , n ) ; return 0 ; }
Check if the sum of digits of N is palindrome | C ++ implementation of the approach ; Function to return the sum of digits of n ; Function that returns true if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function that returns true if the digit sum of n is palindrome ; Sum of the digits of n ; If the digit sum is palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } return sum ; } bool isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } bool isDigitSumPalindrome ( int n ) { int sum = digitSum ( n ) ; if ( isPalindrome ( sum ) ) return true ; return false ; } int main ( ) { int n = 56 ; if ( isDigitSumPalindrome ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the value of N XOR 'ed to itself K times | C ++ implementation of the approach ; Function to return n ^ n ^ ... k times ; If k is odd the answer is the number itself ; Else the answer is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorK ( int n , int k ) { if ( k % 2 == 1 ) return n ; return 0 ; } int main ( ) { int n = 123 , k = 3 ; cout << xorK ( n , k ) ; return 0 ; }
Find the sum of the costs of all possible arrangements of the cells | C ++ implementation of the approach ; To store the factorials and factorial mod inverse of the numbers ; Function to return ( a ^ m1 ) % mod ; Function to find the factorials of all the numbers ; Function to find factorial mod inverse of all the numbers ; Function to return nCr ; Function to return the sum of the costs of all the possible arrangements of the cells ; For all possible X 's ; For all possible Y 's ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE #define mod  (int)(1e9 + 7) NEW_LINE int factorial [ N ] , modinverse [ N ] ; int power ( int a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1LL * a * a ) % mod ; else if ( m1 & 1 ) return ( 1LL * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( 1LL * factorial [ i - 1 ] * i ) % mod ; } void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( 1LL * modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } int binomial ( int n , int r ) { if ( r > n ) return 0 ; int a = ( 1LL * factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( 1LL * a * modinverse [ r ] ) % mod ; return a ; } int arrange ( int n , int m , int k ) { factorialfun ( ) ; modinversefun ( ) ; long long ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) ans += ( 1LL * i * ( n - i ) * m * m ) % mod ; for ( int i = 1 ; i < m ; i ++ ) ans += ( 1LL * i * ( m - i ) * n * n ) % mod ; ans = ( ans * binomial ( n * m - 2 , k - 2 ) ) % mod ; return ( int ) ans ; } int main ( ) { int n = 2 , m = 2 , k = 2 ; cout << arrange ( n , m , k ) ; return 0 ; }
Find the Nth digit in the proper fraction of two numbers | C ++ implementation of the approach ; Function to print the Nth digit in the fraction ( p / q ) ; To store the resultant digit ; While N > 0 compute the Nth digit by dividing p and q and store the result into variable res and go to next digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNthDigit ( int p , int q , int N ) { int res ; while ( N > 0 ) { N -- ; p *= 10 ; res = p / q ; p %= q ; } return res ; } int main ( ) { int p = 1 , q = 2 , N = 1 ; cout << findNthDigit ( p , q , N ) ; return 0 ; }
Sum of the updated array after performing the given operation | C ++ implementation of the approach ; Utility function to return the sum of the array ; Function to return the sum of the modified array ; Subtract the subarray sum ; Sum of subarray arr [ i ... n - 1 ] ; Return the sum of the modified array ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } int sumModArr ( int arr [ ] , int n ) { int subSum = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int curr = arr [ i ] ; arr [ i ] -= subSum ; subSum += curr ; } return sumArr ( arr , n ) ; } int main ( ) { int arr [ ] = { 40 , 25 , 12 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumModArr ( arr , n ) ; return 0 ; }
Find closest integer with the same weight | C ++ implementation of the approach ; Function to return the number closest to x which has equal number of set bits as x ; Loop for each bit in x and compare with the next bit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int NumUnsignBits = 64 ; unsigned long findNum ( unsigned long x ) { for ( int i = 0 ; i < NumUnsignBits - 1 ; i ++ ) { if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) { x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; return x ; } } } int main ( ) { int n = 92 ; cout << findNum ( n ) ; return 0 ; }
Cake Distribution Problem | C ++ implementation of the approach ; Function to return the remaining count of cakes ; Sum for 1 cycle ; no . of full cycle and remainder ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntCakes ( int n , int m ) { int sum = ( n * ( n + 1 ) ) / 2 ; int quo = m / sum ; int rem = m % sum ; double ans = m - quo * sum ; double x = ( -1 + pow ( ( 8 * rem ) + 1 , 0.5 ) ) / 2 ; ans = ans - x * ( x + 1 ) / 2 ; return int ( ans ) ; } int main ( ) { int n = 3 ; int m = 8 ; int ans = cntCakes ( n , m ) ; cout << ( ans ) ; }
Find the number of squares inside the given square grid | C ++ implementation of the approach ; Function to return the number of squares inside an n * n grid ; Driver code
#include <iostream> NEW_LINE using namespace std ; int cnt_squares ( int n ) { return n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ; } int main ( ) { cout << cnt_squares ( 4 ) << endl ; return 0 ; }