codeparrot/xlcost-text-to-code huggingface dataset:
Count substrings of same length differing by a single character from two given strings | C ++ pprogram for the above approach ; Function to count the number of substrings of equal length which differ by a single character ; Stores the count of pairs of substrings ; Traverse the string s ; Traverse the string t ; Different character ; Increment the answer ; Count equal substrings from next index ; Increment the count ; Increment q ; Check the condition ; Increment k ; Add q to count ; Decrement z ; Return the final count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string s , string t ) { int answ = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { for ( int j = 0 ; j < t . size ( ) ; j ++ ) { if ( t [ j ] != s [ i ] ) { answ += 1 ; int k = 1 ; int z = -1 ; int q = 1 ; while ( j + z >= 0 && 0 <= i + z && s [ i + z ] == t [ j + z ] ) { z -= 1 ; answ += 1 ; q += 1 ; } while ( s . size ( ) > i + k && j + k < t . size ( ) && s [ i + k ] == t [ j + k ] ) { k += 1 ; answ += q ; z = -1 ; } } } } return answ ; } int main ( ) { string S = " aba " ; string T = " baba " ; cout << ( countSubstrings ( S , T ) ) ; }
Minimum flips to make all 1 s in right and 0 s in left | C ++ program to implement the above approach ; Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and all 1 s on the right ; Stores count of 1 s on the left of each index ; Stores count of flips required to make string monotonically increasing ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumCntOfFlipsRequired ( string str ) { int n = str . length ( ) ; int zeros = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } int minFlips = INT_MAX ; int currOnes = 0 ; int flips ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = min ( minFlips , flips ) ; } return minFlips ; } int main ( ) { string str = "100101" ; cout << minimumCntOfFlipsRequired ( str ) ; return 0 ; }
Maximize length of longest non | C ++ program to implement the above approach ; Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start pointer ; Stores index of end pointer ; Stores previously inserted element into the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestNonDecreasing ( int A [ ] , int N ) { int res = 0 ; int start = 0 ; int end = N - 1 ; int prev = -1 ; while ( start <= end ) { if ( A [ start ] <= A [ end ] ) { if ( prev == -1 ) { prev = A [ start ] ; res ++ ; start ++ ; } else { if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else { break ; } } } else { if ( prev == -1 ) { prev = A [ end ] ; res ++ ; end -- ; } else { if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else { break ; } } } } return res ; } int main ( ) { int A [ ] = { 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findLongestNonDecreasing ( A , N ) ; return 0 ; }
Non | C ++ program for the above approach ; Function to print all pairs whose sum of Bitwise OR and AND is N ; Iterate from i = 0 to N ; Print pairs ( i , N - i ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int N ) { for ( int i = 0 ; i <= N ; i ++ ) { cout << " ( " << i << " , ▁ " << N - i << " ) , ▁ " ; } } int main ( ) { int N = 5 ; findPairs ( N ) ; return 0 ; }
Minimum index to split array into subarrays with co | C ++ program for the above approach ; Function to find the GCD of 2 numbers ; Base Case ; Find the GCD recursively ; Function to find the minimum partition index K s . t . product of both subarrays around that partition are co - prime ; Stores the prefix and suffix array product ; Update the prefix array ; Update the suffix array ; Iterate the given array ; Check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; If no index for partition exists , then return - 1 ; Driver Code ; Function call
#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 findPartition ( int nums [ ] , int N ) { int prefix [ N ] , suffix [ N ] , i , k ; prefix [ 0 ] = nums [ 0 ] ; for ( i = 1 ; i < N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] * nums [ i ] ; } suffix [ N - 1 ] = nums [ N - 1 ] ; for ( i = N - 2 ; i >= 0 ; i -- ) { suffix [ i ] = suffix [ i + 1 ] * nums [ i ] ; } for ( k = 0 ; k < N - 1 ; k ++ ) { if ( GCD ( prefix [ k ] , suffix [ k + 1 ] ) == 1 ) { return k ; } } return -1 ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findPartition ( arr , N ) ; return 0 ; }
Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | C ++ program for the above approach ; Function to count reversals to separate elements with same parity ; Traverse the given array ; Count size of subarray having integers with same parity only ; Otherwise ; Reversals required is equal to one less than subarray size ; Return the total reversals ; Function to print the array elements ; Function to count the minimum reversals required to make make sum of all adjacent elements odd ; Stores operations required for separating adjacent odd elements ; Stores operations required for separating adjacent even elements ; Maximum of two is the return ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int separate ( int arr [ ] , int n , int parity ) { int count = 1 , res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( ( arr [ i ] + parity ) & 1 ) && ( ( arr [ i - 1 ] + parity ) & 1 ) ) count ++ ; else { if ( count > 1 ) res += count - 1 ; count = 1 ; } } return res ; } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void requiredOps ( int arr [ ] , int N ) { int res1 = separate ( arr , N , 0 ) ; int res2 = separate ( arr , N , 1 ) ; cout << max ( res1 , res2 ) ; } int main ( ) { int arr [ ] = { 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; requiredOps ( arr , N ) ; return 0 ; }
Count prime pairs whose difference is also a Prime Number | C ++ program to implement the above approach ; Function to find all prime numbers in the range [ 1 , N ] ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to count pairs of prime numbers in the range [ 1 , N ] whose difference is prime ; Function to count pairs of prime numbers whose difference is also a prime number ; isPrime [ i ] : Stores if i is a prime number or not ; Iterate over the range [ 2 , N ] ; If i and i - 2 is a prime number ; Update cntPairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < bool > SieveOfEratosthenes ( int N ) { vector < bool > isPrime ( N , true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( isPrime [ p ] ) { for ( int i = p * p ; i <= N ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } int cntPairsdiffOfPrimeisPrime ( int N ) { int cntPairs = 0 ; vector < bool > isPrime = SieveOfEratosthenes ( N ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime [ i ] && isPrime [ i - 2 ] ) { cntPairs += 2 ; } } return cntPairs ; } int main ( ) { int N = 5 ; cout << cntPairsdiffOfPrimeisPrime ( N ) ; return 0 ; }
Length of longest subsequence consisting of distinct adjacent elements | C ++ program for the above approach ; Function that finds the length of longest subsequence having different adjacent elements ; Stores the length of the longest subsequence ; Traverse the array ; If previous and current element are not same ; Increment the count ; Print the maximum length ; Driver Code ; Size of Array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestSubsequence ( int arr [ ] , int N ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count ++ ; } } cout << count << endl ; } int main ( ) { int arr [ ] = { 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; longestSubsequence ( arr , N ) ; return 0 ; }
Count of substrings having the most frequent character in the string as first character | C ++ program for the above approach ; Function to find all substrings whose first character occurs maximum number of times ; Stores frequency of characters ; Stores character that appears maximum number of times ; Stores max frequency of character ; Updates frequency of characters ; Update maxfreq ; Character that occures maximum number of times ; Update the maximum frequency character ; Stores all count of substrings ; Traverse over string ; Get the current character ; Update count of substrings ; Return the count of all valid substrings ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int substringCount ( string s ) { vector < int > freq ( 26 , 0 ) ; char max_char = ' # ' ; int maxfreq = INT_MIN ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; if ( maxfreq < freq [ s [ i ] - ' a ' ] ) maxfreq = freq [ s [ i ] - ' a ' ] ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( maxfreq == freq [ i ] ) { max_char = ( char ) ( i + ' a ' ) ; break ; } } int ans = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { char ch = s [ i ] ; if ( max_char == ch ) { ans += ( s . size ( ) - i ) ; } } return ans ; } int main ( ) { string S = " abcab " ; cout << ( substringCount ( S ) ) ; }
Minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | C ++ program for the above approach ; Function to print the minimum sum of values assigned to each element of the array as per given conditions ; Initialize vectors with value 1 ; Traverse from left to right ; Update if ans [ i ] > ans [ i - 1 ] ; Traverse from right to left ; Update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; Find the minimum sum ; Print the sum ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minSum ( int * arr , int n ) { vector < int > ans ( n , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { ans [ i ] = max ( ans [ i ] , ans [ i - 1 ] + 1 ) ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { ans [ i ] = max ( ans [ i ] , ans [ i + 1 ] + 1 ) ; } } int s = 0 ; for ( auto x : ans ) { s = s + x ; } cout << s << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minSum ( arr , N ) ; return 0 ; }
Maximize sum of assigned weights by flipping at most K bits in given Binary String | C ++ program of the above approach ; Function to find maximum sum of weights of binary string after at most K flips ; Stores lengths of substrings of the form 1. . 00. . 1 s ; Stores the index of last 1 encountered in the string ; Stores the index of first 1 encountered ; Stores lengths of all substrings having of 0 s enclosed by 1 s at both ends ; Traverse the string ; If character is 0 ; If character is 1 First Priority ; Second Priority ; Add according to the first priority ; Stores length of the shortest substring of 0 s ; Convert shortest substrings of 0 s to 1 s ; Add according to the first priority ; Add according to the third priority ; If more 0 s can be made into 1 , then check for 0 s at ends ; Update the ans ; If K is non - zero , then flip 0 s at the beginning ; Return the final weights ; Driver Code ; Given string str ; Given K flips ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMax ( string s , int n , int k ) { int ans = 0 ; int l = 0 ; int ind = -1 ; int indf = -1 ; multiset < int > ls ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) l ++ ; else if ( s [ i ] == '1' && l > 0 && ans != 0 ) { ls . insert ( l ) ; l = 0 ; } if ( s [ i ] == '1' ) { ind = i ; l = 0 ; if ( indf == -1 ) indf = i ; if ( i > 0 && s [ i - 1 ] == '1' ) ans += 2 ; else ans += 1 ; } } int curr ; while ( k > 0 && ! ls . empty ( ) ) { curr = * ls . begin ( ) ; if ( k >= curr ) { ans += ( 2 * curr + 1 ) ; k -= curr ; } else { ans += ( 2 * k ) ; k = 0 ; } ls . erase ( ls . begin ( ) ) ; } if ( k > 0 ) { ans += ( 2 * min ( k , n - ( ind + 1 ) ) - 1 ) ; k -= min ( k , n - ( ind + 1 ) ) ; if ( ind > -1 ) ans ++ ; } if ( k > 0 ) { ans += ( min ( indf , k ) * 2 - 1 ) ; if ( indf > -1 ) ans ++ ; } return ans ; } int main ( ) { string str = "1110000101" ; int N = str . length ( ) ; int K = 3 ; cout << findMax ( str , N , K ) ; return 0 ; }
Minimum removal of consecutive similar characters required to empty a Binary String | C ++ program for the above approach ; Function to find minimum steps to make the string empty ; Stores the modified string ; Size of string ; Removing substring of same character from modified string ; Print the minimum steps required ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( string S ) { string new_str ; int N = S . length ( ) ; int i = 0 ; while ( i < N ) { new_str += S [ i ] ; int j = i ; while ( i < N && S [ i ] == S [ j ] ) ++ i ; } cout << ceil ( ( new_str . size ( ) + 1 ) / 2.0 ) ; } int main ( ) { string S = "0010100" ; minSteps ( S ) ; return 0 ; }
Count pairs with equal Bitwise AND and Bitwise OR value | C ++ program to implement the above approach ; Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int N ) { int cntPairs = 0 ; map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } for ( auto freq : mp ) { cntPairs += ( freq . second * ( freq . second - 1 ) ) / 2 ; } return cntPairs ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; }
Minimum increments required to make given matrix palindromic | C ++ program for the above approach ; Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int palindromeMatrix ( int N , int M , vector < vector < int > > arr ) { int ans = 0 ; for ( int i = 0 ; i < ( N + 1 ) / 2 ; i ++ ) { for ( int j = 0 ; j < ( M + 1 ) / 2 ; j ++ ) { set < pair < int , int > > s ; s . insert ( { i , j } ) ; s . insert ( { i , M - j - 1 } ) ; s . insert ( { N - i - 1 , j } ) ; s . insert ( { N - i - 1 , M - j - 1 } ) ; vector < int > values ; for ( pair < int , int > p : s ) { values . push_back ( arr [ p . first ] [ p . second ] ) ; } int max = * max_element ( values . begin ( ) , values . end ( ) ) ; for ( int k = 0 ; k < values . size ( ) ; k ++ ) { ans += max - values [ k ] ; } } } cout << ans ; } int main ( ) { int N = 3 , M = 3 ; vector < vector < int > > arr = { { 1 , 2 , 1 } , { 3 , 4 , 1 } , { 1 , 2 , 1 } } ; palindromeMatrix ( N , M , arr ) ; return 0 ; }
Minimum division by 10 and multiplication by 2 required to reduce given number to 1 | C ++ program for the above approach ; Function to find the minimum number operations required to reduce N to 1 ; Stores count of powers of 2 and 5 ; Calculating the primefactors 2 ; Calculating the primefactors 5 ; If n is 1 and cnt2 <= cnt5 ; Return the minimum operations ; Otherwise , n can 't be reduced ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMoves ( int n ) { int cnt2 = 0 , cnt5 = 0 ; while ( n % 2 == 0 ) { n /= 2 ; cnt2 ++ ; } while ( n % 5 == 0 ) { n /= 5 ; cnt5 ++ ; } if ( n == 1 && cnt2 <= cnt5 ) { return 2 * cnt5 - cnt2 ; } else return -1 ; } int main ( ) { int N = 25 ; cout << minimumMoves ( N ) ; return 0 ; }
Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non | C ++ Program to implement the above approach ; Function to check if a permutation of first N natural numbers exist with Bitwise AND of adjacent elements not equal to 0 ; If n is a power of 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int n ) { if ( ( n & n - 1 ) != 0 ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { int n = 5 ; check ( n ) ; return 0 ; }
Minimize adding odd and subtracting even numbers to make all array elements equal to K | C ++ program to implement the above approach ; Function to find the minimum operations required to make array elements equal to K ; Stores minimum count of operations ; Traverse the given array ; If K is greater than arr [ i ] ; If ( K - arr [ i ] ) is even ; Update cntOpe ; Update cntOpe ; If K is less than arr [ i ] ; If ( arr [ i ] - K ) is even ; Update cntOpe ; Update cntOpe ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinOperation ( int arr [ ] , int N , int K ) { int cntOpe = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( K > arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 2 ; } else { cntOpe += 1 ; } } else if ( K < arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 1 ; } else { cntOpe += 2 ; } } } return cntOpe ; } int main ( ) { int arr [ ] = { 8 , 7 , 2 , 1 , 3 } ; int K = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinOperation ( arr , N , K ) ; return 0 ; }
Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | C ++ program for the above approach ; Function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store frequency of elements ; Stores xor value ; Taking xor of all the values of both arrays ; Store frequency of B [ ] ; Find the array B [ ] after rearrangement ; If the updated value is present then decrement its frequency ; Otherwise return empty vector ; Utility function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store rearranged array B ; If rearrangement possible ; Otherwise return - 1 ; Driver Code ; Given vector A ; Given vector B ; Size of the vector ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > rearrangeArray ( vector < int > & A , vector < int > & B , int N ) { map < int , int > m ; int xor_value = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xor_value ^= A [ i ] ; xor_value ^= B [ i ] ; m [ B [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = A [ i ] ^ xor_value ; if ( m [ B [ i ] ] ) { m [ B [ i ] ] -- ; } else return vector < int > { } ; } return B ; } void rearrangeArrayUtil ( vector < int > & A , vector < int > & B , int N ) { vector < int > ans = rearrangeArray ( A , B , N ) ; if ( ans . size ( ) ) { for ( auto x : ans ) { cout << x << " ▁ " ; } } else { cout << " - 1" ; } } int main ( ) { vector < int > A = { 13 , 21 , 33 , 49 , 53 } ; vector < int > B = { 54 , 50 , 34 , 22 , 14 } ; int N = ( int ) A . size ( ) ; rearrangeArrayUtil ( A , B , N ) ; return 0 ; }
Count sequences of given length having non | C ++ program for the above approach ; Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Print the answer ; Driver Code ; Given M and X ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long int binCoff ( unsigned int n , unsigned int r ) { unsigned long int val = 1 ; int i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } void findWays ( int M ) { int n = M / 2 ; unsigned long int a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; cout << b ; } int main ( ) { int M = 4 , X = 5 ; findWays ( M ) ; return 0 ; }
XOR of array elements whose modular inverse with a given number exists | C ++ program for the above approach ; Function to return the gcd of a & b ; Base Case ; Recursively calculate GCD ; Function to print the Bitwise XOR of elements of arr [ ] if gcd ( arr [ i ] , M ) is 1 ; Initialize xor ; Traversing the array ; GCD of M and arr [ i ] ; If GCD is 1 , update xor ; Print xor ; Drive Code ; Given array arr [ ] ; Given number M ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } void countInverse ( int arr [ ] , int N , int M ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int gcdOfMandelement = gcd ( M , arr [ i ] ) ; if ( gcdOfMandelement == 1 ) { XOR ^= arr [ i ] ; } } cout << XOR << ' ▁ ' ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int M = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countInverse ( arr , N , M ) ; return 0 ; }