codeparrot/xlcost-text-to-code huggingface dataset:
Count N | C ++ program for the above approach ; Function to calculate the power of n ^ k % p ; Update x if it is more than or equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to count the number of arrays satisfying required conditions ; Calculating N ^ K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( long long x , unsigned int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int countArrays ( int n , int k ) { int mod = 1000000007 ; int ans = power ( n , k , mod ) ; return ans ; } int main ( ) { int n = 3 , k = 5 ; int ans = countArrays ( n , k ) ; cout << ans << endl ; return 0 ; }
Count ways to replace ' ? ' in a Binary String to make the count of 0 s and 1 s same as that of another string | C ++ program for the above approach ; Function to find the factorial of the given number N ; Stores the factorial ; Iterate over the range [ 2 , N ] ; Return the resultant result ; Function to find the number of ways of choosing r objects from n distinct objects ; Function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string S and T ; Traverse the string s ; If the current character is 1 ; Update the value of the sum1 ; Otherwise ; Traverse the string t ; If the current character is 1 , then update the value of sum2 ; If the current character is 0 ; Otherwise , update the value of K ; Check if P is greater than K or if K - P is odd ; Print the count of ways ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res = res * i ; } return res ; } int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } void countWays ( string s , string t ) { int n = s . length ( ) ; int sum1 = 0 , sum2 = 0 , K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { sum1 ++ ; } else sum1 -- ; } int m = t . length ( ) ; for ( int i = 0 ; i < m ; i ++ ) { if ( t [ i ] == '1' ) { sum2 ++ ; } else if ( t [ i ] == '0' ) { sum2 -- ; } else K ++ ; } int P = abs ( sum1 - sum2 ) ; if ( P > K or ( K - P ) % 2 ) { cout << 0 ; return ; } cout << nCr ( K , ( P + K ) / 2 ) ; } int main ( ) { string S1 = "1010" ; string S2 = "10 ? ? " ; countWays ( S1 , S2 ) ; return 0 ; }
Count pairs of indices having sum of indices same as the sum of elements at those indices | C ++ program for the above approach ; Function to find all possible pairs of the given array such that the sum of arr [ i ] + arr [ j ] is i + j ; Stores the total count of pairs ; Iterate over the range ; Iterate over the range ; Print the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N ) { int answer = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] + arr [ j ] == i + j ) { answer ++ ; } } } cout << answer ; } int main ( ) { int arr [ ] = { 0 , 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Distribute R , B beans such that each packet has at least 1 R and 1 B bean with absolute difference at most D | C ++ program for the above approach ; Function to check if it is possible to distribute R red and B blue beans in packets such that the difference between the beans in each packet is atmost D ; Check for the condition to distributing beans ; Print the answer ; Distribution is not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkDistribution ( int R , int B , int D ) { if ( max ( R , B ) <= min ( R , B ) * ( D + 1 ) ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int R = 1 , B = 1 , D = 0 ; checkDistribution ( R , B , D ) ; return 0 ; }
Generate a sequence with product N such that for every pair of indices ( i , j ) and i < j , arr [ j ] is divisible by arr [ i ] | C ++ program for the above approach ; Function to calculate the prime factors of N with their count ; Initialize a vector , say v ; Initialize the count ; Count the number of divisors ; Divide the value of N by 2 ; For factor 2 divides it ; Find all prime factors ; Count their frequency ; Push it to the vector ; Push N if it is not 1 ; Function to print the array that have the maximum size ; Stores the all prime factor and their powers ; Traverse the vector and find the maximum power of prime factor ; If max size is less than 2 ; Otherwise ; Print the maximum size of sequence ; Print the final sequence ; Print the prime factor ; Print the last value of the sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < int , int > > primeFactor ( int N ) { vector < pair < int , int > > v ; int count = 0 ; while ( ! ( N % 2 ) ) { N >>= 1 ; count ++ ; } if ( count ) v . push_back ( { 2 , count } ) ; for ( int i = 3 ; i <= sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } if ( count ) { v . push_back ( { i , count } ) ; } } if ( N > 2 ) v . push_back ( { N , 1 } ) ; return v ; } void printAnswer ( int n ) { vector < pair < int , int > > v = primeFactor ( n ) ; int maxi_size = 0 , prime_factor = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( maxi_size < v [ i ] . second ) { maxi_size = v [ i ] . second ; prime_factor = v [ i ] . first ; } } if ( maxi_size < 2 ) { cout << 1 << ' ▁ ' << n ; } else { int product = 1 ; cout << maxi_size << endl ; for ( int i = 0 ; i < maxi_size - 1 ; i ++ ) { cout << prime_factor << " ▁ " ; product *= prime_factor ; } cout << ( n / product ) ; } } int main ( ) { int N = 360 ; printAnswer ( N ) ; return 0 ; }
Check if it is possible to reach destination in even number of steps in an Infinite Matrix | C ++ Program for the above approach ; Function to check destination can be reached from source in even number of steps ; Coordinates differences ; minimum number of steps required ; Minsteps is even ; Minsteps is odd ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void IsEvenPath ( int Source [ ] , int Destination [ ] ) { int x_dif = abs ( Source [ 0 ] - Destination [ 0 ] ) ; int y_dif = abs ( Source [ 1 ] - Destination [ 1 ] ) ; int minsteps = x_dif + y_dif ; if ( minsteps % 2 == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int Source [ ] = { 2 , 1 } ; int Destination [ ] = { 1 , 4 } ; IsEvenPath ( Source , Destination ) ; return 0 ; }
Find the closest Fraction to given fraction having minimum absolute difference | C ++ program for the above approach ; Function to find the absolute value of x ; Function to find the fraction with minimum absolute difference ; Initialize the answer variables ; Iterate over the range ; Nearest fraction ; x / y - d / i < x / y - A / B ( B * x - y * A ) * ( i * y ) > ( i * x - y * d ) * ( B * y ) ; Check for d + 1 ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long ABS ( long long x ) { return max ( x , - x ) ; } void findFraction ( long long x , long long y , long long n ) { long long A = -1 , B = -1 ; for ( long long i = 1 ; i <= n ; i ++ ) { long long d = ( i * x ) / y ; if ( d >= 0 && ( A == -1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d , B = i ; d ++ ; if ( d >= 0 && ( A == -1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d , B = i ; } cout << A << " / " << B << endl ; } int main ( ) { long long x = 3 , y = 7 , n = 6 ; findFraction ( x , y , n ) ; return 0 ; }
Represent a number N in base | C ++ Program for above approach ; Function to convert N to equivalent representation in base - 2 ; Stores the required answer ; Iterate until N is not equal to zero ; If N is Even ; Add char '0' in front of string ; Add char '1' in front of string ; Decrement N by 1 ; Divide N by - 2 ; If string is empty , that means N is zero ; Put '0' in string s ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string BaseConversion ( int N ) { string s = " " ; while ( N != 0 ) { if ( N % 2 == 0 ) { s = "0" + s ; } else { s = "1" + s ; N -- ; } N /= -2 ; } if ( s == " " ) { s = "0" ; } return s ; } int main ( ) { int N = -9 ; cout << BaseConversion ( N ) ; return 0 ; }
Find instances at end of time frame after auto scaling | C ++ program for the above approach ; Function to find the number of instances after compintion ; Traverse the array , arr [ ] ; If current element is less than 25 ; Divide instances by 2 and take ceil value ; If the current element is greater than 60 ; Double the instances ; Print the instances at the end of the traversal ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalInstances ( int instances , int arr [ ] , int N ) { int i = 0 ; while ( i < N ) { if ( arr [ i ] < 25 && instances > 1 ) { double temp = ( instances / 2.0 ) ; instances = ( int ) ( ceil ( temp ) ) ; i = i + 10 ; } else if ( arr [ i ] > 60 && instances <= ( 2 * pow ( 10 , 8 ) ) ) { instances = instances * 2 ; i = i + 10 ; } i = i + 1 ; } cout << instances ; } int main ( ) { int instances = 2 ; int arr [ ] = { 25 , 23 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 76 , 80 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; finalInstances ( instances , arr , N ) ; }
Modify array by making all array elements equal to 0 by subtracting K ^ i from an array element in every i | C ++ program for the above approach ; Function to check whether all array elements can be made zero or not ; Stores if a power of K has already been subtracted or not ; Stores all the Kth power ; Iterate until X is less than INT_MAX ; Traverse the array arr [ ] ; Iterate over the range [ 0 , M ] ; If MP [ V [ j ] ] is 0 and V [ j ] is less than or equal to arr [ i ] ; If arr [ i ] is not 0 ; If i is less than N ; Otherwise , ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isMakeZero ( int arr [ ] , int N , int K ) { map < int , int > MP ; vector < int > V ; int X = 1 ; int i ; while ( X > 0 && X < INT_MAX ) { V . push_back ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( int j = V . size ( ) - 1 ; j >= 0 ; j -- ) { if ( MP [ V [ j ] ] == 0 && V [ j ] <= arr [ i ] ) { arr [ i ] -= V [ j ] ; MP [ V [ j ] ] = 1 ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " No " ; else return " Yes " ; } int main ( ) { int arr [ ] = { 8 , 0 , 3 , 4 , 80 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << isMakeZero ( arr , N , K ) ; return 0 ; }
Check whether the product of every subsequence is a perfect square or not | C ++ program for the above approach ; Function to check if the product of every subsequence of the array is a perfect square or not ; Traverse the given array ; If arr [ i ] is a perfect square or not ; Return " Yes " ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string perfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int p = sqrt ( arr [ i ] ) ; if ( p * p != arr [ i ] ) { return " No " ; } } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 4 , 100 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << perfectSquare ( arr , N ) ; return 0 ; }
Count of subarrays with average K | C ++ implementation of above approach ; Function to count subarray having average exactly equal to K ; To Store the final answer ; Calculate all subarrays ; Calculate required average ; Check if average is equal to k ; Required average found ; Increment res ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countKAverageSubarrays ( int arr [ ] , int n , int k ) { int res = 0 ; for ( int L = 0 ; L < n ; L ++ ) { int sum = 0 ; for ( int R = L ; R < n ; R ++ ) { sum += arr [ R ] ; int len = ( R - L + 1 ) ; if ( sum % len == 0 ) { int avg = sum / len ; if ( avg == k ) res ++ ; } } } return res ; } int main ( ) { int K = 6 ; int arr [ ] = { 12 , 5 , 3 , 10 , 4 , 8 , 10 , 12 , -6 , -1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countKAverageSubarrays ( arr , N , K ) ; }
Maximize minimum array element possible by exactly K decrements | C ++ program for the above approach ; Function to find the maximized minimum element of the array after performing given operation exactly K times ; Stores the minimum element ; Traverse the given array ; Update the minimum element ; Stores the required operations to make all elements equal to the minimum element ; Update required operations ; If reqOperations < K ; Decrement the value of K by reqOperations ; Update minElement ; Return minimum element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumElement ( int arr [ ] , int N , int K ) { int minElement = arr [ 0 ] ; for ( int i = 0 ; i < N ; ++ i ) { minElement = min ( minElement , arr [ i ] ) ; } int reqOperations = 0 ; for ( int i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= ( K + N - 1 ) / N ; } return minElement ; } int main ( ) { int arr [ ] = { 10 , 10 , 10 , 10 } ; int K = 7 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumElement ( arr , N , K ) ; return 0 ; }
Split the fraction into sum of multiple fractions having numerator as 1 | C ++ program for the above approach ; Function to split the fraction into distinct unit fraction ; To store answer ; While numerator is positive ; Finding x = ceil ( d / n ) ; Add 1 / x to list of ans ; Update fraction ; Driver Code ; Given Input ; Function Call ; Print Answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > FractionSplit ( long long n , long long d ) { vector < string > UnitFactions ; while ( n > 0 ) { long long x = ( d + n - 1 ) / n ; string s = "1 / " + to_string ( x ) ; UnitFactions . push_back ( s ) ; n = n * x - d ; d = d * x ; } return UnitFactions ; } int main ( ) { long long n = 13 , d = 18 ; auto res = FractionSplit ( n , d ) ; for ( string s : res ) cout << s << " , ▁ " ; return 0 ; }
Find permutation of [ 1 , N ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum | Function to generate the permutation of the first N natural numbers having sum of absolute difference between element and indices as minimum ; Initialize array arr [ ] from 1 to N ; Swap alternate positions ; Check N is greater than 1 and N is odd ; Swapping last two positions ; Print the permutation ; Driver code
#include <iostream> NEW_LINE using namespace std ; void swap ( int & a , int & b ) { int temp = a ; a = b ; b = temp ; } void findPermutation ( int N ) { int arr [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } for ( int i = 1 ; i < N ; i += 2 ) { swap ( arr [ i ] , arr [ i - 1 ] ) ; } if ( N % 2 == 1 && N > 1 ) { swap ( arr [ N - 1 ] , arr [ N - 2 ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int N = 7 ; findPermutation ( N ) ; return 0 ; }
Find Array obtained after adding terms of AP for Q queries | C ++ program for the above approach ; Function to find array after performing the given query to the array elements ; Traverse the given query ; Traverse the given array ; Update the value of A [ i ] ; Update the value of curr ; Print the array elements ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; void addAP ( int A [ ] , int Q , int operations [ 2 ] [ 4 ] ) { for ( int j = 0 ; j < 2 ; ++ j ) { int L = operations [ j ] [ 0 ] , R = operations [ j ] [ 1 ] , a = operations [ j ] [ 2 ] , d = operations [ j ] [ 3 ] ; int curr = a ; for ( int i = L - 1 ; i < R ; i ++ ) { A [ i ] += curr ; curr += d ; } } for ( int i = 0 ; i < 4 ; ++ i ) cout << A [ i ] << " ▁ " ; } int main ( ) { int A [ ] = { 5 , 4 , 2 , 8 } ; int Q = 2 ; int Query [ 2 ] [ 4 ] = { { 1 , 2 , 1 , 3 } , { 1 , 4 , 4 , 1 } } ; addAP ( A , Q , Query ) ; return 0 ; }
Maximize the number N by inserting given digit at any position | C ++ program for the above approach ; Function to find the maximum value of N after inserting the digit K ; Convert it into N to string ; Stores the maximum value of N after inserting K ; Iterate till all digits that are not less than K ; Add the current digit to the string result ; Add digit ' K ' to result ; Iterate through all remaining characters ; Add current digit to result ; Print the maximum number formed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximizeNumber ( int N , int K ) { string s = to_string ( N ) ; int L = s . length ( ) ; string result ; int i = 0 ; while ( ( i < L ) && ( K <= ( s [ i ] - '0' ) ) ) { result . push_back ( s [ i ] ) ; ++ i ; } result . push_back ( char ( K + '0' ) ) ; while ( i < L ) { result . push_back ( s [ i ] ) ; ++ i ; } cout << result ; } int main ( ) { int N = 6673 , K = 6 ; maximizeNumber ( N , K ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Function to find the value of X to the power Y ; Stores the value of X ^ Y ; If y is odd , multiply x with result ; Update the value of y and x ; Return the result ; Function to count number of arrays having element over the range [ 0 , 2 ^ K - 1 ] with Bitwise AND value 0 having maximum possible sum ; Print the value of N ^ K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , unsigned int y ) { int res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } void countArrays ( int N , int K ) { cout << int ( power ( N , K ) ) ; } int main ( ) { int N = 5 , K = 6 ; countArrays ( N , K ) ; return 0 ; }
Minimum absolute value of ( K – arr [ i ] ) for all possible values of K over the range [ 0 , N – 1 ] | C ++ program for the above approach ; Function to find the minimum absolute value of ( K - arr [ i ] ) for each possible value of K over the range [ 0 , N - 1 ] | ; Traverse the given array arr [ ] ; Stores the mininimum distance to any array element arr [ i ] ; Check if there is no safe position smaller than i ; Check if the current position is between two safe positions ; Take the minimum of two distances ; Check if the current index is a safe position ; Check if there is no safe position greater than i ; Print the minimum distance ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumDistance ( vector < int > arr , int N ) { int ind = 0 ; int prev = arr [ ind ] ; int s = arr . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int distance = INT_MAX ; if ( i < arr [ 0 ] ) { distance = arr [ 0 ] - i ; } else if ( i >= prev && ind + 1 < s && i <= arr [ ind + 1 ] ) { distance = min ( i - prev , arr [ ind + 1 ] - i ) ; if ( i == arr [ ind + 1 ] ) { distance = 0 ; prev = arr [ ind + 1 ] ; ind ++ ; } } else { distance = i - prev ; } cout << distance << " ▁ " ; } } int main ( ) { int N = 5 ; vector < int > arr = { 0 , 4 } ; minimumDistance ( arr , N ) ; return 0 ; }
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | C ++ program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Iterate over the range [ 0 , N ) ; Iterate over the range ; Check for the given condition ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfPairs ( int arr [ ] , int N , int X ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( ( arr [ i ] ^ arr [ j ] ) & X ) == 0 ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countOfPairs ( arr , N , X ) ; return 0 ; }
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | C ++ program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Initializing the map M ; Populating the map ; Count number of pairs for every element in map using mathematical concept of combination ; As nC2 = n * ( n - 1 ) / 2 ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfPairs ( int arr [ ] , int N , int X ) { int count = 0 ; unordered_map < int , int > M ; for ( int i = 0 ; i < N ; i ++ ) { M [ ( arr [ i ] & X ) ] ++ ; } for ( auto m : M ) { int p = m . second ; count += p * ( p - 1 ) / 2 ; } return count ; } int main ( ) { int arr [ ] = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countOfPairs ( arr , N , X ) ; return 0 ; }
Count of square submatrices with average at least K | C ++ program for the above approach ; Function to count submatrixes with average greater than or equals to K ; Stores count of submatrices ; Stores the prefix sum of matrix ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Update the prefix sum ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Iterate until l and r are greater than 0 ; Update count ; Stores sum of submatrix with bottom right corner as ( i , j ) and top left corner as ( l , r ) ; If sum1 is less than or equal to sum2 ; Increment cnt by 1 ; Return cnt as the answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE int cntMatrices ( vector < vector < int > > arr , int N , int M , int K ) { int cnt = 0 ; vector < vector < int > > pre ( N + 1 , vector < int > ( M + 1 , 0 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { pre [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + pre [ i - 1 ] [ j ] + pre [ i ] [ j - 1 ] - pre [ i - 1 ] [ j - 1 ] ; } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { for ( int l = i , r = j ; l > 0 && r > 0 ; l -- , r -- ) { int sum1 = ( K * ( i - l + 1 ) * ( i - r + 1 ) ) ; int sum2 = pre [ i ] [ j ] - pre [ l - 1 ] [ r ] - pre [ l ] [ r - 1 ] + pre [ l - 1 ] [ r - 1 ] ; if ( sum1 <= sum2 ) cnt ++ ; } } } return cnt ; } int main ( ) { vector < vector < int > > arr = { { 2 , 2 , 3 } , { 3 , 4 , 5 } , { 4 , 5 , 5 } } ; int K = 4 ; int N = arr . size ( ) ; int M = arr [ 0 ] . size ( ) ; cout << cntMatrices ( arr , N , M , K ) ; return 0 ; }
Concatenate the Array of elements into a single element | C ++ program for the above approach ; Function to find the integer value obtained by joining array elements together ; Stores the resulting integer value ; Traverse the array arr [ ] ; Stores the count of digits of arr [ i ] ; Update ans ; Increment ans by arr [ i ] ; Return the ans ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ConcatenateArr ( int arr [ ] , int N ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { int l = floor ( log10 ( arr [ i ] ) + 1 ) ; ans = ans * pow ( 10 , l ) ; ans += arr [ i ] ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 23 , 456 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ConcatenateArr ( arr , N ) ; return 0 ; }
Count of integers K in range [ 0 , N ] such that ( K XOR K + 1 ) equals ( K + 2 XOR K + 3 ) | C ++ program for the above approach ; Function to count all the integers less than N satisfying the given condition ; Store the count of even numbers less than N + 1 ; Return the count ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countXor ( int N ) { int cnt = N / 2 + 1 ; return cnt ; } int main ( ) { int N = 4 ; cout << countXor ( N ) ; return 0 ; }