codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to generate an array having distinct elements at M consecutive indices | C ++ program for the above approach ; Modular function to calculate factorial ; Stores factorial of N ; Iterate over the range [ 1 , N ] ; Update result ; Function to count ways to replace array elements having 0 s with non - zero elements such that any M consecutive elements are distinct ; Store m consecutive distinct elements such that arr [ i ] is equal to B [ i % M ] ; Stores frequency of array elements ; Traverse the array arr [ ] ; If arr [ i ] is non - zero ; If B [ i % M ] is equal to 0 ; Update B [ i % M ] ; Update frequency of arr [ i ] ; If a duplicate element found in M consecutive elements ; Handling the case of inequality ; Stores count of 0 s in B [ ] ; Traverse the array , B [ ] ; If B [ i ] is 0 ; Update cnt ; Calculate factorial ; Driver Code ; Given M ; Given array ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int Fact ( int N ) { long long int result = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { result = ( result * i ) ; } return result ; } void numberOfWays ( int M , int arr [ ] , int N ) { int B [ M ] = { 0 } ; int counter [ M + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) { if ( B [ i % M ] == 0 ) { B [ i % M ] = arr [ i ] ; counter [ arr [ i ] ] ++ ; if ( counter [ arr [ i ] ] > 1 ) { cout << 0 << endl ; return ; } } else if ( B [ i % M ] != arr [ i ] ) { cout << 0 << endl ; return ; } } } int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( B [ i ] == 0 ) { cnt ++ ; } } cout << Fact ( cnt ) << endl ; } int main ( ) { int M = 4 ; int arr [ ] = { 1 , 0 , 3 , 0 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; numberOfWays ( M , arr , N ) ; }
Maximize sum by traversing diagonally from each cell of a given Matrix | C ++ program for the above approach ; Function to find the maximum sum ; Loop to traverse through the upper triangular matrix and update the maximum sum to ans ; Traverse through the lower triangular matrix ; Driver Code ; Given matrix ; Given dimension
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaximumSum ( vector < vector < int > > & arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = 0 , y = i , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ ] [ y ++ ] ; } if ( sum > ans ) ans = sum ; } for ( int i = 1 ; i < n ; i ++ ) { int x = i , y = 0 , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ ] [ y ++ ] ; } if ( sum > ans ) ans = sum ; } return ans ; } int main ( ) { vector < vector < int > > arr ; arr = { { 1 , 2 , 3 } , { 3 , 5 , 10 } , { 1 , 3 , 5 } } ; int n = arr . size ( ) ; cout << MaximumSum ( arr , n ) ; return 0 ; }
Count array elements exceeding all previous elements as well as the next array element | C ++ program for the above approach ; Function to count array elements satisfying the given condition ; If there is only one array element ; Traverse the array ; Update the maximum element encountered so far ; Count the number of array elements strictly greater than all previous and immediately next elements ; Print the count ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfIntegers ( int arr [ ] , int N ) { int cur_max = 0 , count = 0 ; if ( N == 1 ) { count = 1 ; } else { for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] > cur_max ) { cur_max = arr [ i ] ; if ( arr [ i ] > arr [ i + 1 ] ) { count ++ ; } } } if ( arr [ N - 1 ] > cur_max ) count ++ ; } cout << count ; } int main ( ) { int arr [ ] = { 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; numberOfIntegers ( arr , N ) ; return 0 ; }
Count ways to represent N as sum of powers of 2 | C ++ program for above implementation ; Base Cases ; Check if 2 ^ k can be used as one of the numbers or not ; Otherwise ; Count number of ways to N using 2 ^ k - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfWays ( int n , int k ) { if ( n == 0 ) return 1 ; if ( k == 0 ) return 1 ; if ( n >= pow ( 2 , k ) ) { int curr_val = pow ( 2 , k ) ; return numberOfWays ( n - curr_val , k ) + numberOfWays ( n , k - 1 ) ; } else return numberOfWays ( n , k - 1 ) ; } int main ( ) { int n = 4 ; int k = log2 ( n ) ; cout << numberOfWays ( n , k ) << endl ; }
Count ways to obtain triplets with positive product consisting of at most one negative element | C ++ Program to implement the above approach ; Function to calculate possible number of triplets ; counting frequency of positive numbers in array ; If current array element is positive ; Increment frequency ; Select a triplet from freq elements such that i < j < k . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int possibleTriplets ( int arr [ ] , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { freq ++ ; } } return ( freq * 1LL * ( freq - 1 ) * ( freq - 2 ) ) / 6 ; } int main ( ) { int arr [ ] = { 2 , 5 , -9 , -3 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << possibleTriplets ( arr , N ) ; return 0 ; }
Maximize difference between sum of even and odd | C ++ program to implement the above approach ; Function to find the maximum possible difference between sum of even and odd indices ; Convert arr [ ] into 1 - based indexing ; Reverse the array ; Convert arr [ ] into 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPossibleDiff ( vector < int > & arr , int N ) { arr . push_back ( -1 ) ; reverse ( arr . begin ( ) , arr . end ( ) ) ; arr . push_back ( -1 ) ; reverse ( arr . begin ( ) , arr . end ( ) ) ; int maxDiff = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { maxDiff += arr [ i ] ; } if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { maxDiff -= arr [ i ] ; } } cout << maxDiff ; } int main ( ) { vector < int > arr = { 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 } ; int N = arr . size ( ) ; maxPossibleDiff ( arr , N ) ; return 0 ; }
Reverse all elements of given circular array starting from index K | C ++ program for the above approach ; Function to print the array arr [ ] ; Print the array ; Function to reverse elements of given circular array starting from index k ; Initialize two variables as start = k and end = k - 1 ; Initialize count = N / 2 ; Loop while count > 0 ; Swap the elements at index ( start % N ) and ( end % N ) ; Update the start and end ; If end equals to - 1 set end = N - 1 ; Print the circular array ; Driver Code ; Function Call
#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 reverseCircularArray ( int arr [ ] , int N , int K ) { int start = K , end = K - 1 ; int count = N / 2 ; while ( count -- ) { int temp = arr [ start % N ] ; arr [ start % N ] = arr [ end % N ] ; arr [ end % N ] = temp ; start ++ ; end -- ; if ( end == -1 ) { end = N - 1 ; } } printArray ( arr , N ) ; } int main ( ) { int arr [ ] = { 3 , 5 , 2 , 4 , 1 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; reverseCircularArray ( arr , N , K ) ; return 0 ; }
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix | C ++ program for the above approach ; Function to find the matrix , A [ ] [ ] satisfying the given conditions ; Store the final matrix ; Initialize all the elements of the matrix A with 1 ; Traverse the matrix B [ ] [ ] row - wise ; If B [ i ] [ j ] is equal to 0 ; Mark all the elements of ith row of A [ ] [ ] as 0 ; Mark all the elements of jth column of A [ ] [ ] as 0 ; Check if the matrix B [ ] [ ] can be made using matrix A [ ] [ ] ; Store the bitwise OR of all elements of A [ ] [ ] in ith row and jth column ; Traverse through ith row ; Traverse through jth column ; If B [ i ] [ j ] is not equal to c , print " Not ▁ Possible " ; Print the final matrix A [ ] [ ] ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findOriginalMatrix ( vector < vector < int > > B , int N , int M ) { int A [ N ] [ M ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { A [ i ] [ j ] = 1 ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { if ( B [ i ] [ j ] == 0 ) { for ( int k = 0 ; k < M ; ++ k ) { A [ i ] [ k ] = 0 ; } for ( int k = 0 ; k < N ; ++ k ) { A [ k ] [ j ] = 0 ; } } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { int c = 0 ; for ( int k = 0 ; k < M ; ++ k ) { if ( c == 1 ) break ; c += A [ i ] [ k ] ; } for ( int k = 0 ; k < N ; ++ k ) { if ( c == 1 ) break ; c += A [ k ] [ j ] ; } if ( c != B [ i ] [ j ] ) { cout << " Not ▁ Possible " ; return ; } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { cout << A [ i ] [ j ] << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { vector < vector < int > > B { { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int N = B . size ( ) ; int M = B [ 0 ] . size ( ) ; findOriginalMatrix ( B , N , M ) ; return 0 ; }
Minimize cost to make X and Y equal by given increments | C ++ program for the above approach ; Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
#include <iostream> NEW_LINE using namespace std ; int gcd ( int x , int y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } int lcm ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } int minimumCost ( int x , int y ) { int lcm_ = lcm ( x , y ) ; int costx = ( lcm_ - x ) / x ; int costy = ( lcm_ - y ) / y ; return costx + costy ; } int main ( ) { int x = 5 , y = 17 ; cout << minimumCost ( x , y ) << endl ; }
Find GCD between the sum of two given integers raised to the power of N and their difference | C ++ program for the above approach ; Function to find the value of ( a ^ n ) % d ; Stores the value of ( a ^ n ) % d ; Calculate the value of ( a ^ n ) % d ; If n is odd ; Update res ; Update a ; Update n ; Function to find the GCD of ( p ^ n + q ^ n ) and p - q mod d ; If p and q are equal ; Stores GCD of ( p ^ n + q ^ n ) and ( p - q ) mod d ; Stores the value of ( p - q ) ; Stores square root of num ; Find the divisors of num . ; If i divides num ; Stores power of ( p ^ n ) mod i ; Stores power of ( q ^ n ) mod i ; Stores power of ( p ^ n + q ^ n ) mod i ; If ( p ^ n + q ^ n ) is divisible by i ; Calculate the largest divisor . ; If i divides num , ( num / i ) also divides num . Hence , calculate temp . ; If ( p ^ n + q ^ n ) is divisible by ( num / i ) ; Calculate the largest divisor . ; Driver Code ; Given p , q and n ; Function Call
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; long long int power ( long long a , long long n , long long int d ) { long long int res = 1 ; while ( n ) { if ( n % 2 ) { res = ( ( res % d ) * ( a % d ) ) % d ; } a = ( ( a % d ) * ( a % d ) ) % d ; n /= 2 ; } return res ; } long long int gcd ( long long p , long long q , long long n ) { if ( p == q ) { return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; } long long int candidate = 1 ; long long int num = p - q ; long long int sq = sqrt ( num ) ; for ( long long i = 1 ; i <= sq ; ++ i ) { if ( num % i == 0 ) { long long int X = power ( p , n , i ) ; long long int Y = power ( q , n , i ) ; long long int temp = ( X + Y ) % i ; if ( temp == 0 ) { candidate = max ( candidate , i ) ; } temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; if ( temp == 0 ) { candidate = max ( candidate , num / i ) ; } } } return candidate % mod ; } int main ( ) { long long int p , q , n ; p = 10 ; q = 6 ; n = 5 ; cout << gcd ( p , q , n ) ; return 0 ; }
Split given isosceles triangle of height H into N equal parts | C ++ Code for above approach ; Function to divide the isosceles triangle in equal parts by making N - 1 cuts parallel to the base ; Iterate over the range [ 1 , n - 1 ] ; Driver code ; Given N ; Given H ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPoint ( int n , int h ) { for ( int i = 1 ; i < n ; i ++ ) printf ( " % .2f ▁ " , sqrt ( i / ( n * 1.0 ) ) * h ) ; } int main ( ) { int n = 3 ; int h = 2 ; findPoint ( n , h ) ; return 0 ; }
Count all possible values of K less than Y such that GCD ( X , Y ) = GCD ( X + K , Y ) | C ++ program for the above approach ; Function to find the gcd of a and b ; Function to find the number of Ks ; Find gcd ; Calculating value of totient function for n ; Driver Code ; Given X and Y
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int calculateK ( int x , int y ) { int g = gcd ( x , y ) ; int n = y / g ; int res = n ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { res -= ( res / i ) ; while ( n % i == 0 ) n /= i ; } } if ( n != 1 ) res -= ( res / n ) ; return res ; } int main ( ) { int x = 3 , y = 15 ; cout << calculateK ( x , y ) << endl ; }
Check if a Float value is equivalent to an Integer value | C ++ program to implement the above approach ; Function to check if N is equivalent to an integer ; Convert float value of N to integer ; If N is not equivalent to any integer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isInteger ( double N ) { int X = N ; double temp2 = N - X ; if ( temp2 > 0 ) { return false ; } return true ; } int main ( ) { double N = 1.5 ; if ( isInteger ( N ) ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; }
Find the nearest power of 2 for every array element | C ++ program to implement the above approach ; Function to find nearest power of two for every element in the given array ; Traverse the array ; Calculate log of the current array element ; Find the nearest ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestPowerOfTwo ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int lg = log2 ( arr [ i ] ) ; int a = pow ( 2 , lg ) ; int b = pow ( 2 , lg + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) cout << a << " ▁ " ; else cout << b << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 2 , 7 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestPowerOfTwo ( arr , N ) ; return 0 ; }
Flip bits of the sum of count of set bits of two given numbers | C ++ program for the above approach ; Function to count number of set bits in integer ; Variable for counting set bits ; Function to invert bits of a number ; Calculate number of bits of N - 1 ; ; Function to invert the sum of set bits in A and B ; Stores sum of set bits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } int invertBits ( int n ) { int x = log2 ( n ) ; int m = 1 << x ; m = m | m - 1 ; n = n ^ m ; return n ; } void invertSum ( int A , int B ) { int temp = countSetBits ( A ) + countSetBits ( B ) ; cout << invertBits ( temp ) << endl ; } int main ( ) { int A = 5 ; int B = 7 ; invertSum ( A , B ) ; return 0 ; }
Sum of Bitwise XOR of each array element with all other array elements | C ++ program for the above approach ; Function to calculate for each array element , sum of its Bitwise XOR with all other array elements ; Declare an array of size 64 to store count of each bit ; Traversing the array ; Check if bit is present of not ; Increase the bit position ; Reduce the number to half ; Traverse the array ; Stores the bit position ; Stores the sum of Bitwise XOR ; Check if bit is present of not ; Reduce the number to its half ; Print the sum for A [ i ] ; Driver Code ; Given array ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void XOR_for_every_i ( int A [ ] , int N ) { int frequency_of_bits [ 32 ] { } ; for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int M = A [ i ] ; while ( M ) { if ( M & 1 ) { frequency_of_bits [ bit_position ] += 1 ; } bit_position += 1 ; M >>= 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { int M = A [ i ] ; int value_at_that_bit = 1 ; int XOR_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( M & 1 ) { XOR_sum += ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } else { XOR_sum += ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } M >>= 1 ; value_at_that_bit <<= 1 ; } cout << XOR_sum << ' ▁ ' ; } return ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; XOR_for_every_i ( A , N ) ; return 0 ; }
Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half | C ++ program for the above approach ; Function to minimize the maximum difference between any pair of elements of the array by the given operations ; Traverse the array ; If current element is even ; Insert it into even ; Otherwise ; Make it even by multiplying by 2 and insert it into set ; Calculate difference between first and the last element of the set ; Iterate until difference is minimized ; Erase the current element ; Reduce current element by half and insert it into the Set ; Update difference ; Return the resultant difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMaxDiff ( vector < int > & nums ) { set < int > s ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) { if ( nums [ i ] % 2 == 0 ) s . insert ( nums [ i ] ) ; else s . insert ( nums [ i ] * 2 ) ; } int res = * s . rbegin ( ) - * s . begin ( ) ; while ( * s . rbegin ( ) % 2 == 0 ) { int x = * s . rbegin ( ) ; s . erase ( x ) ; s . insert ( x / 2 ) ; res = min ( res , * s . rbegin ( ) - * s . begin ( ) ) ; } return res ; } int main ( ) { vector < int > arr = { 1 , 2 , 5 , 9 } ; cout << minimumMaxDiff ( arr ) ; }
XOR of all even numbers from a given range | C ++ Implementation of the above approach ; Function to calculate XOR of numbers in the range [ 1 , n ] ; If n is divisible by 4 ; If n mod 4 is equal to 1 ; If n mod 4 is equal to 2 ; Function to find XOR of even numbers in the range [ l , r ] ; Stores XOR of even numbers in the range [ 1 , l - 1 ] ; Stores XOR of even numbers in the range [ 1 , r ] ; Update xor_r ; Update xor_l ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bitwiseXorRange ( int n ) { if ( n % 4 == 0 ) return n ; if ( n % 4 == 1 ) return 1 ; if ( n % 4 == 2 ) return n + 1 ; return 0 ; } int evenXorRange ( int l , int r ) { int xor_l ; int xor_r ; xor_r = 2 * bitwiseXorRange ( r / 2 ) ; xor_l = 2 * bitwiseXorRange ( ( l - 1 ) / 2 ) ; return xor_l ^ xor_r ; } int main ( ) { int l = 10 ; int r = 20 ; cout << evenXorRange ( l , r ) ; return 0 ; }
Program to calculate Variance of first N Natural Numbers | C ++ Program to implement the above approach ; Function to calculate Variance of first N natural numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long double find_Variance ( int n ) { long long int numerator = n * n - 1 ; long double ans = ( numerator * 1.0 ) / 12 ; return ans ; } int main ( ) { int N = 5 ; cout << fixed << setprecision ( 6 ) << find_Variance ( N ) ; }
Count digits present in each element of a given Matrix | C ++ program for the above approach ; Function to count the number of digits in each element of the given matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Count the number of digits ; Print the result ; Driver Code ; Given matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int M = 3 ; const int N = 3 ; void countDigit ( int arr [ M ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int X = arr [ i ] [ j ] ; int d = floor ( log10 ( X ) * 1.0 ) + 1 ; cout << d << " ▁ " ; } cout << endl ; } } int main ( ) { int arr [ ] [ 3 ] = { { 27 , 173 , 5 } , { 21 , 6 , 624 } , { 5 , 321 , 49 } } ; countDigit ( arr ) ; return 0 ; }
Difference between sum of odd and even frequent elements in an Array | C ++ program to find absolute difference between the sum of all odd frequenct and even frequent elements in an array ; Function to find the sum of all even and odd frequent elements in an array ; Stores the frequency of array elements ; Traverse the array ; Update frequency of current element ; Stores sum of odd and even frequent elements ; Traverse the map ; If frequency is odd ; Add sum of all occurrences of current element to sum_odd ; If frequency is even ; Add sum of all occurrences of current element to sum_even ; Calculate difference between their sum ; Return diff ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int arr [ ] , int N ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } int sum_odd = 0 , sum_even = 0 ; for ( auto itr = mp . begin ( ) ; itr != mp . end ( ) ; itr ++ ) { if ( itr -> second % 2 != 0 ) sum_odd += ( itr -> first ) * ( itr -> second ) ; if ( itr -> second % 2 == 0 ) sum_even += ( itr -> first ) * ( itr -> second ) ; } int diff = sum_even - sum_odd ; return diff ; } int main ( ) { int arr [ ] = { 1 , 5 , 5 , 2 , 4 , 3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findSum ( arr , N ) ; return 0 ; }
Count N | C ++ program for the above approach ; Function to find the number of arrays following the given condition ; Initialize answer ; Calculate nPm ; Print ans ; Driver Code ; Given N and M ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; void noOfArraysPossible ( ll N , ll M ) { ll ans = 1 ; for ( ll i = 0 ; i < N ; ++ i ) { ans = ans * ( M - i ) ; } cout << ans ; } int main ( ) { ll N = 2 , M = 3 ; noOfArraysPossible ( N , M ) ; return 0 ; }
Permutations of an array having sum of Bitwise AND of adjacent elements at least K | C ++ program for the above approach ; Function to print all permutations of arr [ ] such that the sum of Bitwise AND of all adjacent element is at least K ; To check if any permutation exists or not ; Sort the given array ; Find all possible permutations ; Stores the sum of bitwise AND of adjacent elements of the current permutation ; Traverse the current permutation of arr [ ] ; Update the sum ; If the sum is at least K , then print the current permutation ; Set the flag variable ; Print the current permutation ; If flag is unset , then print - 1 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int arr [ ] , int n , int k ) { bool flag = false ; sort ( arr , arr + n ) ; do { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] & arr [ i + 1 ] ; } if ( sum >= k ) { flag = true ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; } } while ( next_permutation ( arr , arr + n ) ) ; if ( flag == false ) { cout << " - 1" ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 8 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPermutation ( arr , N , K ) ; return 0 ; }