codeparrot/xlcost-text-to-code huggingface dataset:
Longest sub | C ++ implementation of the approach ; Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubArray ( int arr [ ] , int n ) { bool isZeroPresent = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { isZeroPresent = true ; break ; } } if ( isZeroPresent ) return n ; return 0 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 0 , 1 , 2 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSubArray ( arr , n ) ; return 0 ; }
Smallest Even number with N digits | C ++ implementation of the approach ; Function to return smallest even number with n digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestEven ( int n ) { if ( n == 1 ) return 0 ; return pow ( 10 , n - 1 ) ; } int main ( ) { int n = 4 ; cout << smallestEven ( n ) ; return 0 ; }
Maximize profit when divisibility by two numbers have associated profits | C ++ implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } int main ( ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; cout << maxProfit ( n , a , b , x , y ) ; return 0 ; }
Series summation if T ( n ) is given and n is very large | C ++ implementation of the approach ; Function to return the sum of the given series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE #define MOD  1000000007 NEW_LINE int sumOfSeries ( int n ) { ll ans = ( ll ) pow ( n % MOD , 2 ) ; return ( ans % MOD ) ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Kth odd number in an array | C ++ implementation of the approach ; Function to return the kth odd element from the array ; Traverse the array ; If current element is odd ; If kth odd element is found ; Total odd elements in the array are < k ; Driver code
#include <iostream> NEW_LINE using namespace std ; int kthOdd ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n ; i ++ ) { if ( ( arr [ i ] % 2 ) == 1 ) k -- ; if ( k == 0 ) return arr [ i ] ; } return -1 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << ( kthOdd ( arr , n , k ) ) ; return 0 ; }
Find last five digits of a given five digit number raised to power five | CPP program to find last five digits of a five digit number raised to power five ; Function to find the last five digits of a five digit number raised to power five ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastFiveDigits ( int n ) { n = ( n / 10000 ) * 10000 + ( ( n / 100 ) % 10 ) * 1000 + ( n % 10 ) * 100 + ( ( n / 10 ) % 10 ) * 10 + ( n / 1000 ) % 10 ; int ans = 1 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans *= n ; ans %= 100000 ; } printf ( " % 05d " , ans ) ; } int main ( ) { int n = 12345 ; lastFiveDigits ( n ) ; return 0 ; }
Sum of ( maximum element | C ++ implementation of the above approach ; Function to return a ^ n % mod ; Compute sum of max ( A ) - min ( A ) for all subsets ; Sort the array . ; Maxs = 2 ^ i - 1 ; Mins = 2 ^ ( n - 1 - i ) - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; const int mod = 1000000007 ; ll power ( ll a , ll n ) { if ( n == 0 ) return 1 ; ll p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( n & 1 ) { p = ( p * a ) % mod ; } return p ; } ll computeSum ( int * arr , int n ) { sort ( arr , arr + n ) ; ll sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ll maxs = ( power ( 2 , i ) - 1 + mod ) % mod ; maxs = ( maxs * arr [ i ] ) % mod ; ll mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod ; mins = ( mins * arr [ i ] ) % mod ; ll V = ( maxs - mins + mod ) % mod ; sum = ( sum + V ) % mod ; } return sum ; } int main ( ) { int arr [ ] = { 4 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << computeSum ( arr , n ) ; return 0 ; }
Count of all N digit numbers such that num + Rev ( num ) = 10 ^ N | C ++ implementation of the approach ; Function to return the count of such numbers ; If n is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int n ) { if ( n % 2 == 1 ) return 0 ; return ( 9 * pow ( 10 , n / 2 - 1 ) ) ; } int main ( ) { int n = 2 ; cout << countNumbers ( n ) ; return 0 ; }
Count of numbers having only 1 set bit in the range [ 0 , n ] | C ++ implementation of the approach ; Function to return the required count ; To store the count of numbers ; Every power of 2 contains only 1 set bit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { int cnt = 0 ; int p = 1 ; while ( p <= n ) { cnt ++ ; p *= 2 ; } return cnt ; } int main ( ) { int n = 7 ; cout << count ( n ) ; return 0 ; }
Find the K | C ++ programme to find the K 'th minimum element from an array concatenated M times ; Function to find the K - th minimum element from an array concatenated M times ; Sort the elements in ascending order ; Return the K 'th Min element  present at ( (K-1) / M ) index ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int KthMinValAfterMconcatenate ( int A [ ] , int N , int M , int K ) { sort ( A , A + N ) ; return ( A [ ( ( K - 1 ) / M ) ] ) ; } int main ( ) { int A [ ] = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << KthMinValAfterMconcatenate ( A , N , M , K ) ; return 0 ; }
Sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | C ++ implementation of the approach ; Function to return the required sum ; Total odd numbers from 1 to n ; Sum of first n odd numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumN ( int n ) { n = ( n + 1 ) / 2 ; return ( n * n ) ; } int main ( ) { int n = 3 ; cout << sumN ( n ) ; return 0 ; }
Numbers that are not divisible by any number in the range [ 2 , 10 ] | C ++ implementation of the approach ; Function to return the count of numbers from 1 to N which are not divisible by any number in the range [ 2 , 10 ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int n ) { return n - n / 2 - n / 3 - n / 5 - n / 7 + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35 - n / 30 - n / 42 - n / 70 - n / 105 + n / 210 ; } int main ( ) { int n = 20 ; cout << countNumbers ( n ) ; return 0 ; }
Maximum Primes whose sum is equal to given N | C ++ program for above approach ; Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPrimes ( int n ) { return n / 2 ; } int main ( ) { int n = 17 ; cout << maxPrimes ( n ) ; return 0 ; }
Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | C ++ implementation of the approach ; Function to return sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int n ) { return n * ( n + 1 ) * ( n + 2 ) / 3 ; } int main ( ) { int n = 2 ; cout << sum ( n ) ; }
Find the shortest distance between any pair of two different good nodes | C ++ program to find the shortest pairwise distance between any two different good nodes . ; Function to add edges ; Function to find the shortest distance between any pair of two different good nodes ; Keeps minimum element on top ; To keep required answer ; If it is not good vertex ; Keep all vertices not visited and distance as MAXI ; Distance from ith vertex to ith is zero ; Make queue empty ; Push the ith vertex ; Count the good vertices ; Take the top element ; Remove it ; If it is already visited ; Count good vertices ; If distance from vth vertex is greater than ans ; If two good vertices are found ; Go to all adjacent vertices ; if distance is less ; Return the required answer ; Driver code ; Number of vertices and edges ; Function call to add edges ; Number of good nodes ; To keep good vertices
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE const int MAXI = 99999999 ; void add_edge ( vector < pair < int , int > > gr [ ] , int x , int y , int weight ) { gr [ x ] . push_back ( { y , weight } ) ; gr [ y ] . push_back ( { x , weight } ) ; } int minDistance ( vector < pair < int , int > > gr [ ] , int n , int dist [ ] , int vis [ ] , int a [ ] , int k ) { priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > q ; int ans = MAXI ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! a [ i ] ) continue ; for ( int j = 1 ; j <= n ; j ++ ) { dist [ j ] = MAXI ; vis [ j ] = 0 ; } dist [ i ] = 0 ; while ( ! q . empty ( ) ) q . pop ( ) ; q . push ( { 0 , i } ) ; int good = 0 ; while ( ! q . empty ( ) ) { int v = q . top ( ) . second ; q . pop ( ) ; if ( vis [ v ] ) continue ; vis [ v ] = 1 ; good += a [ v ] ; if ( dist [ v ] > ans ) break ; if ( good == 2 and a [ v ] ) { ans = min ( ans , dist [ v ] ) ; break ; } for ( int j = 0 ; j < gr [ v ] . size ( ) ; j ++ ) { int to = gr [ v ] [ j ] . first ; int weight = gr [ v ] [ j ] . second ; if ( dist [ v ] + weight < dist [ to ] ) { dist [ to ] = dist [ v ] + weight ; q . push ( { dist [ to ] , to } ) ; } } } } return ans ; } int main ( ) { int n = 5 , m = 5 ; vector < pair < int , int > > gr [ N ] ; add_edge ( gr , 1 , 2 , 3 ) ; add_edge ( gr , 1 , 2 , 3 ) ; add_edge ( gr , 2 , 3 , 4 ) ; add_edge ( gr , 3 , 4 , 1 ) ; add_edge ( gr , 4 , 5 , 8 ) ; int k = 3 ; int a [ N ] , vis [ N ] , dist [ N ] ; a [ 1 ] = a [ 3 ] = a [ 5 ] = 1 ; cout << minDistance ( gr , n , dist , vis , a , k ) ; return 0 ; }
Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | C ++ implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimum ( int n , int m ) { int mini = m ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int sec = n / i ; if ( __gcd ( m , i ) > 1 ) { return i ; } else if ( __gcd ( sec , m ) > 1 ) { mini = min ( sec , mini ) ; } } } if ( mini == m ) return -1 ; else return mini ; } int main ( ) { int n = 8 , m = 10 ; cout << findMinimum ( n , m ) ; return 0 ; }
Find Nth term of the series 1 , 5 , 32 , 288 ... | CPP code to generate ' Nth ' terms of this sequence ; Function to generate a fixed \ number ; Finding nth term ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthTerm ( int N ) { int nth = 0 , i ; for ( i = N ; i > 0 ; i -- ) { nth += pow ( i , i ) ; } return nth ; } int main ( ) { int N = 3 ; cout << nthTerm ( N ) << endl ; return 0 ; }
Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | C ++ implementation of the approach ; Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthSmallest ( int n , int k ) { return ( 2 * k ) ; } int main ( ) { int n = 8 , k = 4 ; cout << kthSmallest ( n , k ) ; return 0 ; }
Check if a number can be represented as sum of non zero powers of 2 | C ++ implementation of the approach ; Function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSumOfPowersOfTwo ( int n ) { if ( n % 2 == 1 ) return false ; else return true ; } int main ( ) { int n = 10 ; if ( isSumOfPowersOfTwo ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimize sum of adjacent difference with removal of one element from array | C ++ implementation of above approach ; Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinRemoval ( int arr [ ] , int n ) { int temp , value = 0 ; int maximum = 0 ; if ( n == 1 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 && i != n - 1 ) { value = value + abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = abs ( arr [ i ] - arr [ i + 1 ] ) + abs ( arr [ i ] - arr [ i - 1 ] ) - abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; } else if ( i == 0 ) { value = value + abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = abs ( arr [ i ] - arr [ i + 1 ] ) ; } else temp = abs ( arr [ i ] - arr [ i - 1 ] ) ; maximum = max ( maximum , temp ) ; } return ( value - maximum ) ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 2 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinRemoval ( arr , n ) << " STRNEWLINE " ; return 0 ; }
Time until distance gets equal to X between two objects moving in opposite direction | C ++ implementation of the approach ; Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double getTime ( int u , int v , int x ) { double speed = u + v ; double time = x / speed ; return time ; } int main ( ) { double u = 3 , v = 3 , x = 3 ; cout << getTime ( u , v , x ) ; return 0 ; }
Given number of matches played , find number of teams in tournament | C ++ implementation of the approach ; Function to return the number of teams ; To store both roots of the equation ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int number_of_teams ( int M ) { int N1 , N2 , sqr ; sqr = sqrt ( 1 + ( 8 * M ) ) ; N1 = ( 1 + sqr ) / 2 ; N2 = ( 1 - sqr ) / 2 ; if ( N1 > 0 ) return N1 ; return N2 ; } int main ( ) { int M = 45 ; cout << number_of_teams ( M ) ; return 0 ; }
Greatest number less than equal to B that can be formed from the digits of A | C ++ implementation of the approach ; Function to return the greatest number not gtreater than B that can be formed with the digits of A ; To store size of A ; To store the required answer ; Traverse from leftmost digit and place a smaller digit for every position . ; Keep all digits in A ; To avoid leading zeros ; For all possible values at ith position from largest value to smallest ; Take largest possible digit ; Keep duplicate of string a ; Remove the taken digit from s2 ; Sort all the remaining digits of s2 ; Add s2 to current s1 ; If s1 is less than B then it can be included in the answer . Note that stoll ( ) converts a string to lomg long int . ; change A to s2 ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Permute_Digits ( string a , long long b ) { int n = a . size ( ) ; string ans = " " ; for ( int i = 0 ; i < n ; i ++ ) { set < char > temp ( a . begin ( ) , a . end ( ) ) ; if ( i == 0 ) temp . erase ( 0 ) ; for ( auto j = temp . rbegin ( ) ; j != temp . rend ( ) ; ++ j ) { string s1 = ans + * j ; string s2 = a ; s2 . erase ( s2 . find ( * j ) , 1 ) ; sort ( s2 . begin ( ) , s2 . end ( ) ) ; s1 += s2 ; if ( stoll ( s1 ) <= b ) { ans += * j ; a = s2 ; break ; } } } return ans ; } int main ( ) { string a = "123" ; int b = 222 ; cout << Permute_Digits ( a , b ) ; return 0 ; }
Sum of numbers from 1 to N which are in Lucas Sequence | C ++ program to find sum of numbers from 1 to N which are in Lucas Sequence ; Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LucasSum ( int N ) { int sum = 0 ; int a = 2 , b = 1 , c ; sum += a ; while ( b <= N ) { sum += b ; int c = a + b ; a = b ; b = c ; } return sum ; } int main ( ) { int N = 20 ; cout << LucasSum ( N ) ; return 0 ; }
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | C ++ implementation of the approach ; Function to return the count of required numbers ; Count of numbers in range which are divisible by 6 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int l , int r ) { return ( ( r / 6 ) - ( l - 1 ) / 6 ) ; } int main ( ) { int l = 1000 , r = 6000 ; cout << countNumbers ( l , r ) ; return 0 ; }
Sum of minimum element of all sub | C ++ implementation of the above approach ; Function to find the sum of minimum of all subsequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinSum ( int arr [ ] , int n ) { int occ = n - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * pow ( 2 , occ ) ; occ -- ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinSum ( arr , n ) ; return 0 ; }
Number of trailing zeroes in base B representation of N ! | CPP program to find the number of trailing zeroes in base B representation of N ! ; To find the power of a prime p in factorial N ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of B ; Returns largest power of B that divides N ! ; calculating minimum power of all the prime factors of B ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPowerOfP ( int N , int p ) { int count = 0 ; int r = p ; while ( r <= N ) { count += ( N / r ) ; r = r * p ; } return count ; } vector < pair < int , int > > primeFactorsofB ( int B ) { vector < pair < int , int > > ans ; for ( int i = 2 ; B != 1 ; i ++ ) { if ( B % i == 0 ) { int count = 0 ; while ( B % i == 0 ) { B = B / i ; count ++ ; } ans . push_back ( make_pair ( i , count ) ) ; } } return ans ; } int largestPowerOfB ( int N , int B ) { vector < pair < int , int > > vec ; vec = primeFactorsofB ( B ) ; int ans = INT_MAX ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) ans = min ( ans , findPowerOfP ( N , vec [ i ] . first ) / vec [ i ] . second ) ; return ans ; } int main ( ) { cout << largestPowerOfB ( 5 , 2 ) << endl ; cout << largestPowerOfB ( 6 , 9 ) << endl ; return 0 ; }
Count numbers in range 1 to N which are divisible by X but not by Y | C ++ implementation of above approach ; Function to count total numbers divisible by x but not y in range 1 to N ; Check if Number is divisible by x but not Y if yes , Increment count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int X , int Y , int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ( i % X == 0 ) && ( i % Y != 0 ) ) count ++ ; } return count ; } int main ( ) { int X = 2 , Y = 3 , N = 10 ; cout << countNumbers ( X , Y , N ) ; return 0 ; }
Position of a person diametrically opposite on a circle | C ++ implementation of the approach ; Function to return the required position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPosition ( int n , int m ) { if ( m > ( n / 2 ) ) return ( m - ( n / 2 ) ) ; return ( m + ( n / 2 ) ) ; } int main ( ) { int n = 8 , m = 5 ; cout << getPosition ( n , m ) ; return 0 ; }
Minimum operations required to modify the array such that parity of adjacent elements is different | C ++ implementation of the approach ; Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int parity ( int a ) { return a % 3 ; } int solve ( int array [ ] , int size ) { int operations = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { if ( parity ( array [ i ] ) == parity ( array [ i + 1 ] ) ) { operations ++ ; if ( i + 2 < size ) { int pari1 = parity ( array [ i ] ) ; int pari2 = parity ( array [ i + 2 ] ) ; if ( pari1 == pari2 ) { if ( pari1 == 0 ) array [ i + 1 ] = 1 ; else if ( pari1 == 1 ) array [ i + 1 ] = 0 ; else array [ i + 1 ] = 1 ; } else { if ( ( pari1 == 0 && pari2 == 1 ) || ( pari1 == 1 && pari2 == 0 ) ) array [ i + 1 ] = 2 ; if ( ( pari1 == 1 && pari2 == 2 ) || ( pari1 == 2 && pari2 == 1 ) ) array [ i + 1 ] = 0 ; if ( ( pari1 == 2 && pari2 == 0 ) || ( pari1 == 0 && pari2 == 2 ) ) array [ i + 1 ] = 1 ; } } } } return operations ; } int main ( ) { int array [ ] = { 2 , 1 , 3 , 0 } ; int size = sizeof ( array ) / sizeof ( array [ 0 ] ) ; cout << solve ( array , size ) << endl ; return 0 ; }
Number of submatrices with OR value 1 | C ++ program to count number of submatrices with OR value 1 ; Function to find required prefix - count for each row from right to left ; Function to find the count of submatrices with OR value 1 ; Array to store prefix count of zeros from right to left for boolean array ; Variable to store the count of submatrices with OR value 0 ; Loop to evaluate each column of the prefix matrix uniquely . For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; First part of pair will be the value of inserted element . Second part will be the count of the number of elements pushed before with a greater value ; Variable to store the number of submatrices with all 0 s ; Return the final answer ; Driver Code
#include <iostream> NEW_LINE #include <stack> NEW_LINE #define n  3 NEW_LINE using namespace std ; void findPrefixCount ( int p_arr [ ] [ n ] , bool arr [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( int ) ( ! arr [ i ] [ j ] ) ; } } int matrixOrValueOne ( bool arr [ ] [ n ] ) { int p_arr [ n ] [ n ] = { 0 } ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; stack < pair < int , int > > q ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . size ( ) != 0 and q . top ( ) . first > p_arr [ i ] [ j ] ) { to_sum -= ( q . top ( ) . second + 1 ) * ( q . top ( ) . first - p_arr [ i ] [ j ] ) ; c += q . top ( ) . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . push ( { p_arr [ i ] [ j ] , c } ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } int main ( ) { bool arr [ ] [ n ] = { { 0 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 0 } } ; cout << matrixOrValueOne ( arr ) ; return 0 ; }
XOR of XORs of all sub | C ++ program to find the XOR of XOR 's of all submatrices ; Function to find to required XOR value ; Nested loop to find the number of sub - matrix each index belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
#include <iostream> NEW_LINE using namespace std ; #define n  3 NEW_LINE int submatrixXor ( int arr [ ] [ n ] ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; if ( ( top_left % 2 == 1 ) && ( bottom_right % 2 == 1 ) ) ans = ( ans ^ arr [ i ] [ j ] ) ; } } return ans ; } int main ( ) { int arr [ ] [ n ] = { { 6 , 7 , 13 } , { 8 , 3 , 4 } , { 9 , 7 , 6 } } ; cout << submatrixXor ( arr ) ; return 0 ; }