codeparrot/xlcost-text-to-code huggingface dataset:
Count subarrays having even Bitwise OR | C ++ program for the above approach ; Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bitOr ( int arr [ ] , int N ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += ( ( length ) * ( length + 1 ) ) / 2 ; } length = 0 ; } } count += ( ( length ) * ( length + 1 ) ) / 2 ; return count ; } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 2 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << bitOr ( arr , N ) ; return 0 ; }
Generate an array having sum of Bitwise OR of same | C ++ program for the above approach ; Function to print the resultant array ; Stores the sum of the array ; Calculate sum of the array ; If sum > K ; Not possible to construct the array ; Update K ; Stores the resultant array ; Traverse the array ; Stores the current element ; If jth bit is not set and value of 2 ^ j is less than K ; Update curr ; Update K ; Push curr into B ; If K is greater than 0 ; Print the vector B ; Otherwise ; Driver Code ; Input ; Size of the array ; Given input ; Function call to print the required array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArr ( int A [ ] , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; } if ( sum > K ) { cout << -1 << " STRNEWLINE " ; return ; } K -= sum ; vector < int > B ; for ( int i = 0 ; i < N ; i ++ ) { int curr = A [ i ] ; for ( int j = 32 ; j >= 0 and K ; j -- ) { if ( ( curr & ( 1LL << j ) ) == 0 and ( 1LL << j ) <= K ) { curr |= ( 1LL << j ) ; K -= ( 1LL << j ) ; } } B . push_back ( curr ) ; } if ( ! K ) { for ( auto it : B ) { cout << it << " ▁ " ; } cout << " STRNEWLINE " ; } else { cout << " - 1" << " STRNEWLINE " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 32 ; constructArr ( arr , N , K ) ; }
Find all pairs raised to power K differs by exactly N | C ++ program for the above approach ; Function to print pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ValidPairs ( int X , int K ) { long long int count = 0 ; for ( int A = -1000 ; A <= 1000 ; A ++ ) { for ( int B = -1000 ; B <= 1000 ; B ++ ) { if ( pow ( A , K ) - pow ( B , K ) == X ) { count ++ ; cout << A << " ▁ " << B << endl ; } } } if ( count == 0 ) { cout << " - 1" ; } } int main ( ) { long long int X = 33 ; int K = 5 ; ValidPairs ( X , K ) ; return 0 ; }
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | C ++ program for the above approach ; Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumAND ( int arr [ ] , int n , int m ) { int tot = 1 << n ; int mx = 0 ; for ( int bm = 0 ; bm < tot ; bm ++ ) { int andans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( bm >> i ) & 1 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = max ( mx , andans ) ; } } return mx ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 2 ; cout << maximumAND ( arr , N , M ) ; return 0 ; }
Calculate sum of the array generated by given operations | C ++ program for the above approach ; Function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; If the size of array is 0 ; Stores the required sum ; Traverse the array ops [ ] ; If the character is C , remove the top element from the stack ; If the character is D , then push 2 * top element into stack ; If the character is + , add sum of top two elements from the stack ; Otherwise , push x and add it to ans ; Print the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTotalSum ( vector < string > & ops ) { if ( ops . empty ( ) ) { cout << 0 ; return ; } stack < int > pts ; int ans = 0 ; for ( int i = 0 ; i < ops . size ( ) ; i ++ ) { if ( ops [ i ] == " C " ) { ans -= pts . top ( ) ; pts . pop ( ) ; } else if ( ops [ i ] == " D " ) { pts . push ( pts . top ( ) * 2 ) ; ans += pts . top ( ) ; } else if ( ops [ i ] == " + " ) { int a = pts . top ( ) ; pts . pop ( ) ; int b = pts . top ( ) ; pts . push ( a ) ; ans += ( a + b ) ; pts . push ( a + b ) ; } else { int n = stoi ( ops [ i ] ) ; ans += n ; pts . push ( n ) ; } } cout << ans ; } int main ( ) { vector < string > arr = { "5" , " - 2" , " C " , " D " , " + " } ; findTotalSum ( arr ) ; return 0 ; }
XOR of major diagonal elements of a 3D Matrix | C ++ program for the above approach ; Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; If element is part of major diagonal ; Print the resultant Bitwise XOR ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findXOR ( vector < vector < vector < int > > > & mat , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) { if ( ( i == j && j == k ) ) { XOR ^= mat [ i ] [ j ] [ k ] ; XOR ^= mat [ i ] [ j ] [ N - k - 1 ] ; } } } } cout << XOR << " STRNEWLINE " ; } int main ( ) { vector < vector < vector < int > > > mat = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . size ( ) ; findXOR ( mat , N ) ; return 0 ; }
XOR of major diagonal elements of a 3D Matrix | C ++ program for the above approach ; Function to find the Bitwise XOR of both diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; Print the resultant Bitwise XOR ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findXOR ( vector < vector < vector < int > > > & mat , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XOR ^= mat [ i ] [ i ] [ i ] ; XOR ^= mat [ i ] [ i ] [ N - i - 1 ] ; } cout << XOR << " STRNEWLINE " ; } int main ( ) { vector < vector < vector < int > > > mat = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . size ( ) ; findXOR ( mat , N ) ; return 0 ; }
Count of subtrees possible from an N | C ++ program of the above approach ; Adjacency list to represent the graph ; Stores the count of subtrees possible from given N - ary Tree ; Utility function to count the number of subtrees possible from given N - ary Tree ; Stores the count of subtrees when cur node is the root ; Traverse the adjacency list ; Iterate over every ancestor ; Calculate product of the number of subtrees for each child node ; Update the value of ans ; Return the resultant count ; Function to count the number of subtrees in the given tree ; Initialize an adjacency matrix ; Add the edges ; Function Call to count the number of subtrees possible ; Print count of subtrees ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define MAX  300004 NEW_LINE using namespace std ; vector < int > graph [ MAX ] ; int mod = 1e9 + 7 ; int ans = 0 ; int countSubtreesUtil ( int cur , int par ) { int res = 1 ; for ( int i = 0 ; i < graph [ cur ] . size ( ) ; i ++ ) { int v = graph [ cur ] [ i ] ; if ( v == par ) continue ; res = ( res * ( countSubtreesUtil ( v , cur ) + 1 ) ) % mod ; } ans = ( ans + res ) % mod ; return res ; } void countSubtrees ( int N , vector < pair < int , int > > & adj ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = adj [ i ] . first ; int b = adj [ i ] . second ; graph [ a ] . push_back ( b ) ; graph [ b ] . push_back ( a ) ; } countSubtreesUtil ( 1 , 1 ) ; cout << ans + 1 ; } int main ( ) { int N = 3 ; vector < pair < int , int > > adj = { { 0 , 1 } , { 1 , 2 } } ; countSubtrees ( N , adj ) ; return 0 ; }
Find the player who wins the game of placing alternate + and | C ++ program for the above approach ; Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkWinner ( int arr [ ] , int N ) { int diff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { cout << " A " ; } else { cout << " B " ; } } int main ( ) { int arr [ ] = { 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkWinner ( arr , N ) ; return 0 ; }
Generate an array having sum of Euler Totient Function of all elements equal to N | C ++ program for the above approach ; Function to construct the array such the sum of values of Euler Totient functions of all array elements is N ; Stores the resultant array ; Find divisors in sqrt ( N ) ; If N is divisible by i ; Push the current divisor ; If N is not a perfect square ; Push the second divisor ; Print the resultant array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArray ( int N ) { vector < int > ans ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { ans . push_back ( i ) ; if ( N != ( i * i ) ) { ans . push_back ( N / i ) ; } } } for ( auto it : ans ) { cout << it << " ▁ " ; } } int main ( ) { int N = 12 ; constructArray ( N ) ; return 0 ; }
Place N boys and M girls in different rows such that count of persons placed in each row is maximized | C ++ Program to implement the above approach ; Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Driver Code ; Input ; Function to count maximum persons that can be placed in a row
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int maximumRowValue ( int n , int m ) { return gcd ( n , m ) ; } int main ( ) { int N = 4 ; int M = 2 ; cout << maximumRowValue ( N , M ) ; }
Program to generate an array having convolution of two given arrays | C ++ program for the above approach ; Function to generate a convolution array of two given arrays ; Stores the size of arrays ; Stores the final array ; Traverse the two given arrays ; Update the convolution array ; Print the convolution array c [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; constexpr int MOD = 998244353 ; void findConvolution ( const vector < int > & a , const vector < int > & b ) { int n = a . size ( ) , m = b . size ( ) ; vector < long long > c ( n + m - 1 ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { c [ i + j ] += 1LL * ( a [ i ] * b [ j ] ) % MOD ; } } for ( int k = 0 ; k < c . size ( ) ; ++ k ) { c [ k ] %= MOD ; cout << c [ k ] << " ▁ " ; } } int main ( ) { vector < int > A = { 1 , 2 , 3 , 4 } ; vector < int > B = { 5 , 6 , 7 , 8 , 9 } ; findConvolution ( A , B ) ; return 0 ; }
Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | C ++ program for the above approach ; Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat [ x ] [ y ] is equal to 1 ; Increase count by 1 ; Return the total count of 1 s ; Function to find the minimum number of 1 s present in a sub - matrix of size A * B or B * A ; Stores the minimum count of 1 s ; Iterate i from 0 to N ; Iterate j from 0 to M ; If a valid sub matrix of size A * B from ( i , j ) is possible ; Count the number of 1 s present in the sub matrix of size A * B from ( i , j ) ; Update minimum if count is less than the current minimum ; If a valid sub matrix of size B * A from ( i , j ) is possible ; Count the number of 1 s in the sub matrix of size B * A from ( i , j ) ; Update minimum if count is less than the current minimum ; Return minimum as the final result ; Driver Code ; Given Input ; Function call to find the minimum number of 1 s in a submatrix of size A * B or B * A
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define P  51 NEW_LINE int count1s ( int start_i , int start_j , int end_i , int end_j , int mat [ ] [ P ] ) { int count = 0 ; for ( int x = start_i ; x < end_i ; x ++ ) { for ( int y = start_j ; y < end_j ; y ++ ) { if ( mat [ x ] [ y ] == 1 ) count ++ ; } } return count ; } int findMinimumCount ( int N , int M , int A , int B , int mat [ ] [ P ] ) { int minimum = 1e9 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + A <= N && j + B <= M ) { int count = count1s ( i , j , i + A , j + B , mat ) ; minimum = min ( count , minimum ) ; } if ( i + B <= N && j + A <= M ) { int count = count1s ( i , j , i + B , j + A , mat ) ; minimum = min ( count , minimum ) ; } } } return minimum ; } int main ( ) { int A = 2 , B = 2 ; int N = 3 , M = 4 ; int mat [ P ] [ P ] = { { 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; cout << findMinimumCount ( N , M , A , B , mat ) ; }
Program to check if a number can be expressed as an even power of 2 or not | C ++ program for the above approach ; Function to check if N can be expressed as an even power of 2 ; Iterate till x is N ; if x is even then return true ; Increment x ; If N is not a power of 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEvenPower ( int n ) { int x = 0 ; while ( x < n ) { int value = pow ( 2 , x ) ; if ( value == n ) { if ( x % 2 == 0 ) return true ; else return false ; } x ++ ; } return false ; } int main ( ) { int N = 4 ; cout << ( checkEvenPower ( N ) ? " Yes " : " No " ) ; }
Program to check if a number can be expressed as an even power of 2 or not | C ++ program for the above approach ; Function to check if N can be expressed as an even power of 2 or not ; Iterate until low > high ; Calculate mid ; If 2 ^ mid is equal to n ; If mid is odd ; Update the value of low ; Update the value of high ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkEvenPower ( int n ) { int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int value = pow ( 2 , mid ) ; if ( value == n ) { if ( mid % 2 == 1 ) return " No " ; else return " Yes " ; } else if ( value < n ) low = mid + 1 ; else high = mid - 1 ; } return " No " ; } int main ( ) { int N = 4 ; cout << checkEvenPower ( N ) ; return 0 ; }
Program to check if a number can be expressed as an even power of 2 or not | C ++ program for the above approach ; Function to check if N can be expressed as an even power of 2 or not ; If N is not a power of 2 ; Bitwise AND operation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEvenPower ( long long int N ) { if ( ( N & ( N - 1 ) ) != 0 ) return false ; N = N & 0x55555555 ; return ( N > 0 ) ; } int main ( ) { int N = 4 ; cout << checkEvenPower ( N ) ; return 0 ; }
Sum of quotients of division of N by powers of K not exceeding N | C ++ program for the above approach ; Function to calculate sum of quotients obtained by dividing N by powers of K <= N ; Store the required sum ; Iterate until i exceeds N ; Update sum ; Multiply i by K to obtain next power of K ; Print the result ; Driver Code ; Given N and K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int N , int K ) { int ans = 0 ; int i = 1 ; while ( i <= N ) { ans += N / i ; i = i * K ; } cout << ans ; } int main ( ) { int N = 10 , K = 2 ; findSum ( N , K ) ; return 0 ; }
Count Arithmetic Progressions having sum S and common difference equal to D | C ++ program for the above approach ; Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countAPs ( int S , int D ) { S = S * 2 ; int answer = 0 ; for ( int i = 1 ; i <= sqrt ( S ) ; i ++ ) { if ( S % i == 0 ) { if ( ( ( S / i ) - D * i + D ) % 2 == 0 ) answer ++ ; if ( ( D * i - ( S / i ) + D ) % 2 == 0 ) answer ++ ; } } return answer ; } int main ( ) { int S = 12 , D = 1 ; cout << countAPs ( S , D ) ; return 0 ; }
Minimize insertions to make sum of every pair of consecutive array elements at most K | C ++ program for the above approach ; Function to count minimum number of insertions required to make sum of every pair of adjacent elements at most K ; Stores if it is possible to make sum of each pair of adjacent elements at most K ; Stores the count of insertions ; Stores the previous value to index i ; Traverse the array ; If arr [ i ] is greater than or equal to K ; Mark possible 0 ; If last + arr [ i ] is greater than K ; Increment res by 1 ; Assign arr [ i ] to last ; If possible to make the sum of pairs of adjacent elements at most K ; Otherwise print " - 1" ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumInsertions ( int arr [ ] , int N , int K ) { bool possible = 1 ; int res = 0 ; int last = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= K ) { possible = 0 ; break ; } if ( last + arr [ i ] > K ) res ++ ; last = arr [ i ] ; } if ( possible ) { cout << res ; } else { cout << " - 1" ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumInsertions ( arr , N , K ) ; return 0 ; }
Maximum sum of K | C ++ program for the above approach ; Function to count the number of distinct elements present in the array ; Insert all elements into the Set ; Return the size of set ; Function that finds the maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find a subarray of size K ; Initialize Set ; Calculate sum of the distinct elements ; If the set size is same as the count of distinct elements ; Update the maximum value ; Driver code ; Stores the count of distinct elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinct ( int arr [ ] , int n ) { map < int , int > mpp ; for ( int i = 0 ; i < n ; i ++ ) { mpp [ arr [ i ] ] = 1 ; } return mpp . size ( ) ; } int maxSubSum ( int arr [ ] , int n , int k , int totalDistinct ) { if ( k > n ) return 0 ; int maxm = 0 , sum = 0 ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { sum = 0 ; set < int > st ; for ( int j = i ; j < i + k ; j ++ ) { sum += arr [ j ] ; st . insert ( arr [ j ] ) ; } if ( ( int ) st . size ( ) == totalDistinct ) maxm = max ( sum , maxm ) ; } return maxm ; } int main ( ) { int arr [ ] = { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int totalDistinct = distinct ( arr , N ) ; cout << ( maxSubSum ( arr , N , K , totalDistinct ) ) ; return 0 ; }
Maximum sum of K | C ++ program for the above approach ; Function to count the number of distinct elements present in the array ; Insert array elements into set ; Return the st size ; Function to calculate maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find an subarray of length K from an N - sized array , if K > N ; Traverse the array ; Update the mp ; If i >= K , then decrement arr [ i - K ] element 's one  occurence ; If frequency of any element is 0 then remove the element ; If mp size is same as the count of distinct elements of array arr [ ] then update maximum sum ; Function that finds the maximum sum of K - length subarray having same number of distinct elements as the original array ; Size of array ; Stores count of distinct elements ; Print maximum subarray sum ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinct ( vector < int > arr , int N ) { set < int > st ; for ( int i = 0 ; i < N ; i ++ ) { st . insert ( arr [ i ] ) ; } return st . size ( ) ; } int maxSubarraySumUtil ( vector < int > arr , int N , int K , int totalDistinct ) { if ( K > N ) return 0 ; int mx = 0 ; int sum = 0 ; map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] += 1 ; sum += arr [ i ] ; if ( i >= K ) { mp [ arr [ i - K ] ] -= 1 ; sum -= arr [ i - K ] ; if ( mp [ arr [ i - K ] ] == 0 ) mp . erase ( arr [ i - K ] ) ; } if ( mp . size ( ) == totalDistinct ) mx = max ( mx , sum ) ; } return mx ; } void maxSubarraySum ( vector < int > arr , int K ) { int N = arr . size ( ) ; int totalDistinct = distinct ( arr , N ) ; cout << maxSubarraySumUtil ( arr , N , K , totalDistinct ) ; } int main ( ) { vector < int > arr { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; maxSubarraySum ( arr , K ) ; }
Number of Irreflexive Relations on a Set | C ++ program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of irreflixive relations in a set consisting of N elements ; Return the resultant count ; Driver Code
#include <iostream> NEW_LINE using namespace std ; const int mod = 1000000007 ; int power ( long long x , unsigned int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } int irreflexiveRelation ( int N ) { return power ( 2 , N * N - N ) ; } int main ( ) { int N = 2 ; cout << irreflexiveRelation ( N ) ; return 0 ; }
Count Arithmetic Progressions having sum N and common difference equal to 1 | C ++ program for the above approach ; Function to count all possible AP series with common difference 1 and sum of elements equal to N ; Stores the count of AP series ; Traverse through all factors of 2 * N ; Check for the given conditions ; Increment count ; Print count - 1 ; Driver Code ; Given value of N ; Function call to count required number of AP series
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countAPs ( long long int N ) { long long int count = 0 ; for ( long long int i = 1 ; i * i <= 2 * N ; i ++ ) { long long int res = 2 * N ; if ( res % i == 0 ) { long long int op = res / i - i + 1 ; if ( op % 2 == 0 ) { count ++ ; } if ( i * i != res and ( i - res / i + 1 ) % 2 == 0 ) { count ++ ; } } } cout << count - 1 << " STRNEWLINE " ; } int main ( ) { long long int N = 963761198400 ; countAPs ( N ) ; }