codeparrot/xlcost-text-to-code huggingface dataset:
Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | C ++ program for the above approach ; Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; Iterator to store first element of B ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Driver Code ; Given array ; Given size ; Print the required smallest integer
#include <iostream> NEW_LINE #include <set> NEW_LINE using namespace std ; void findMinKToMakeAllEqual ( int N , int A [ ] ) { set < int > B ; for ( int i = 0 ; i < N ; i ++ ) B . insert ( A [ i ] ) ; int M = B . size ( ) ; set < int > :: iterator itr = B . begin ( ) ; if ( M > 3 ) printf ( " - 1" ) ; else if ( M == 3 ) { int B_1 = * itr ; int B_2 = * ( ++ itr ) ; int B_3 = * ( ++ itr ) ; if ( B_2 - B_1 == B_3 - B_2 ) printf ( " % d " , B_2 - B_1 ) ; else printf ( " - 1" ) ; } else if ( M == 2 ) { int B_1 = * itr ; int B_2 = * ( ++ itr ) ; if ( ( B_2 - B_1 ) % 2 == 0 ) printf ( " % d " , ( B_2 - B_1 ) / 2 ) ; else printf ( " % d " , B_2 - B_1 ) ; } else printf ( " % d " , 0 ) ; } int main ( ) { int A [ ] = { 1 , 3 , 5 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findMinKToMakeAllEqual ( N , A ) ; }
Maximum number of times a given string needs to be concatenated to form a substring of another string | C ++ program for the above approach ; Function to find lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Iterate string to calculate lps [ i ] ; If the current character of the pattern matches ; Otherwise ; Otherwise ; Function to implement KMP algorithm ; Stores the longest prefix and suffix values for pattern ; Preprocess the pattern and find the lps [ ] array ; Index for txt [ ] ; Index for pat [ ] ; If pattern is found return 1 ; Mismatch after j matches ; Don 't match lps[0, lps[j - 1]]  characters they will  match anyway ; Return 0 if the pattern is not found ; Function to find the maximum value K for which string S2 concatenated K times is a substring of string S1 ; Store the required maximum number ; Create a temporary string to store string word ; Store the maximum number of times string S2 can occur in string S1 ; Traverse in range [ 0 , numWords - 1 ] ; If curWord is found in sequence ; Concatenate word to curWord ; Increment resCount by 1 ; Otherwise break the loop ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void computeLPSArray ( string pat , int M , int * lps ) { int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < M ) { if ( pat [ i ] == pat [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } } int KMPSearch ( string pat , string txt ) { int M = pat . size ( ) ; int N = txt . size ( ) ; int lps [ M ] ; computeLPSArray ( pat , M , lps ) ; int i = 0 ; int j = 0 ; while ( i < N ) { if ( pat [ j ] == txt [ i ] ) { j ++ ; i ++ ; } if ( j == M ) { return 1 ; j = lps [ j - 1 ] ; } else if ( i < N && pat [ j ] != txt [ i ] ) { if ( j != 0 ) j = lps [ j - 1 ] ; else i = i + 1 ; } } return 0 ; } void maxRepeating ( string seq , string word ) { int resCount = 0 ; string curWord = word ; int numWords = seq . length ( ) / word . length ( ) ; for ( int i = 0 ; i < numWords ; i ++ ) { if ( KMPSearch ( curWord , seq ) ) { curWord += word ; resCount ++ ; } else break ; } cout << resCount ; } int main ( ) { string S1 = " ababc " , S2 = " ab " ; maxRepeating ( S1 , S2 ) ; return 0 ; }
Count all possible strings that can be generated by placing spaces | C ++ Program to implement the above approach ; Function to count the number of strings that can be generated by placing spaces between pair of adjacent characters ; Length of the string ; Count of positions for spaces ; Count of possible strings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int countNumberOfStrings ( string s ) { int length = s . length ( ) ; int n = length - 1 ; long long int count = pow ( 2 , n ) ; return count ; } int main ( ) { string S = " ABCD " ; cout << countNumberOfStrings ( S ) ; return 0 ; }
Minimum time required to reach a given score | C ++ program for the above approach ; Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Print the time required ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumTime ( int * p , int n , int target ) { unordered_map < int , int > um ; for ( int i = 0 ; i < n ; i ++ ) { um [ p [ i ] ] ++ ; } int time = 0 ; int sum = 0 ; while ( sum < target ) { sum = 0 ; time ++ ; for ( auto & it : um ) { sum += it . second * ( time / it . first ) ; } } cout << time ; } int main ( ) { int arr [ ] = { 1 , 3 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int target = 10 ; findMinimumTime ( arr , N , target ) ; return 0 ; }
Construct MEX array from the given array | C ++ program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Print the array B ; Driver Code ; Given array ; Given size ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  100001 NEW_LINE void constructMEX ( int arr [ ] , int N ) { int hash [ MAXN ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] = 1 ; } int MexOfArr ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( hash [ i ] == 0 ) { MexOfArr = i ; break ; } } int B [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < MexOfArr ) B [ i ] = arr [ i ] ; else B [ i ] = MexOfArr ; } for ( int i = 0 ; i < N ; i ++ ) cout << B [ i ] << ' ▁ ' ; } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; constructMEX ( arr , N ) ; return 0 ; }
Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | C ++ program to implement the above approach ; Function to print the smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of arr [ ] in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array arr [ ] ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print the element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print all the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print current pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallestNumberLeftInPQ ( int arr [ ] , int N ) { priority_queue < int > pq ; vector < pair < int , int > > pairsArr ; for ( int i = 0 ; i < N ; i ++ ) { pq . push ( arr [ i ] ) ; } while ( pq . size ( ) > 1 ) { int X = pq . top ( ) ; pq . pop ( ) ; int Y = pq . top ( ) ; pq . pop ( ) ; pq . push ( ( X + Y + 1 ) / 2 ) ; pairsArr . push_back ( { X , Y } ) ; } cout << " { " << pq . top ( ) << " } , ▁ " ; int sz = pairsArr . size ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { if ( i == 0 ) { cout << " { ▁ " ; } cout << " ( " << pairsArr [ i ] . first << " , ▁ " << pairsArr [ i ] . second << " ) " ; if ( i != sz - 1 ) { cout << " , ▁ " ; } if ( i == sz - 1 ) { cout << " ▁ } " ; } } } int main ( ) { int arr [ ] = { 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smallestNumberLeftInPQ ( arr , N ) ; }
Move weighting scale alternate under given constraints | C ++ program to print weights for alternating the weighting scale ; DFS method to traverse among states of weighting scales ; If we reach to more than required steps , return true ; Try all possible weights and choose one which returns 1 afterwards ; Try this weight only if it is greater than current residueand not same as previous chosen weight ; assign this weight to array and recur for next state ; if any weight is not possible , return false ; method prints weights for alternating scale and if not possible prints ' not ▁ possible ' ; call dfs with current residue as 0 and current steps as 0 ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool dfs ( int residue , int curStep , int wt [ ] , int arr [ ] , int N , int steps ) { if ( curStep > steps ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > residue && arr [ i ] != wt [ curStep - 1 ] ) { wt [ curStep ] = arr [ i ] ; if ( dfs ( arr [ i ] - residue , curStep + 1 , wt , arr , N , steps ) ) return true ; } } return false ; } void printWeightsOnScale ( int arr [ ] , int N , int steps ) { int wt [ steps ] ; if ( dfs ( 0 , 0 , wt , arr , N , steps ) ) { for ( int i = 0 ; i < steps ; i ++ ) cout << wt [ i ] << " ▁ " ; cout << endl ; } else cout << " Not ▁ possible STRNEWLINE " ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int steps = 10 ; printWeightsOnScale ( arr , N , steps ) ; return 0 ; }
Minimum removals required such that sum of remaining array modulo M is X | C ++ program for the above approach ; Function to find the minimum elements having sum x ; Initialize dp table ; Pre - compute subproblems ; If mod is smaller than element ; Minimum elements with sum j upto index i ; Return answer ; Function to find minimum number of removals to make sum % M in remaining array is equal to X ; Sum of all elements ; Sum to be removed ; Print answer ; Driver Code ; Given array ; Given size ; Given mod and x ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( vector < int > S , int n , int x ) { vector < vector < int > > table ( n + 1 , vector < int > ( x + 1 , 0 ) ) ; for ( int i = 1 ; i <= x ; i ++ ) { table [ 0 ] [ i ] = INT_MAX - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= x ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return ( table [ n ] [ x ] > n ) ? -1 : table [ n ] [ x ] ; } void minRemovals ( vector < int > arr , int n , int m , int x ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int requied_Sum = 0 ; if ( sum % m < x ) requied_Sum = m + sum % m - x ; else requied_Sum = sum % m - x ; cout << findSum ( arr , n , requied_Sum ) ; } int main ( ) { vector < int > arr = { 3 , 2 , 1 , 2 } ; int n = arr . size ( ) ; int m = 4 , x = 2 ; minRemovals ( arr , n , m , x % m ) ; return 0 ; }
Count minimum character replacements required such that given string satisfies the given conditions | C ++ program for the above approach ; Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minChange ( string s , int n ) { int L [ 26 ] = { 0 } ; int R [ 26 ] = { 0 } ; for ( int i = 0 ; i < n / 2 ; i ++ ) { char ch = s [ i ] ; L [ ch - ' a ' ] ++ ; } for ( int i = n / 2 ; i < n ; i ++ ) { char ch = s [ i ] ; R [ ch - ' a ' ] ++ ; } int count = n ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { count = min ( count , n - L [ ch - ' a ' ] - R [ ch - ' a ' ] ) ; } int change = n / 2 ; for ( int d = 0 ; d + 1 < 26 ; d ++ ) { change -= L [ d ] ; change += R [ d ] ; count = min ( count , change ) ; } change = n / 2 ; for ( int d = 0 ; d + 1 < 26 ; d ++ ) { change -= R [ d ] ; change += L [ d ] ; count = min ( change , count ) ; } return count ; } int main ( ) { string S = " aababc " ; int N = S . length ( ) ; cout << minChange ( S , N ) << " STRNEWLINE " ; }
Rearrange string to obtain Longest Palindromic Substring | C ++ program to implement the above approach ; Function to rearrange the string to get the longest palindromic substring ; Stores the length of str ; Store the count of occurrence of each character ; Traverse the string , str ; Count occurrence of each character ; Store the left half of the longest palindromic substring ; Store the right half of the longest palindromic substring ; Traverse the array , hash [ ] ; Append half of the characters to res1 ; Append half of the characters to res2 ; reverse string res2 to make res1 + res2 palindrome ; Store the remaining characters ; Check If any odd character appended to the middle of the resultant string or not ; Append all the character which occurs odd number of times ; If count of occurrence of characters is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string longestPalinSub ( string str ) { int N = str . length ( ) ; int hash [ 256 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { hash [ str [ i ] ] ++ ; } string res1 = " " ; string res2 = " " ; for ( int i = 0 ; i < 256 ; i ++ ) { for ( int j = 0 ; j < hash [ i ] / 2 ; j ++ ) { res1 . push_back ( i ) ; } for ( int j = ( hash [ i ] + 1 ) / 2 ; j < hash [ i ] ; j ++ ) { res2 . push_back ( i ) ; } } reverse ( res2 . begin ( ) , res2 . end ( ) ) ; string res3 ; bool f = false ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % 2 ) { if ( ! f ) { res1 . push_back ( i ) ; f = true ; } else { res3 . push_back ( i ) ; } } } return ( res1 + res2 + res3 ) ; } int main ( ) { string str = " geeksforgeeks " ; cout << longestPalinSub ( str ) ; }
Maximize subsequences having array elements not exceeding length of the subsequence | C ++ program for the above approach ; Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from x . first ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of arr [ ] ; Update the frequency ; Print the number of subsequences ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int No_Of_subsequences ( map < int , int > mp ) { int count = 0 ; int left = 0 ; for ( auto x : mp ) { x . second += left ; count += ( x . second / x . first ) ; left = x . second % x . first ; } return count ; } void maximumsubsequences ( int arr [ ] , int n ) { map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; cout << No_Of_subsequences ( mp ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumsubsequences ( arr , N ) ; return 0 ; }
Check if concatenation of any permutation of given list of arrays generates the given array | C ++ program for the above approach ; Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Driver Code ; Given target list ; Given array of list ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < int > & arr , vector < vector < int > > & pieces ) { unordered_map < int , int > m ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) m [ arr [ i ] ] = i + 1 ; for ( int i = 0 ; i < pieces . size ( ) ; i ++ ) { if ( pieces [ i ] . size ( ) == 1 && m [ pieces [ i ] [ 0 ] ] != 0 ) { continue ; } else if ( pieces [ i ] . size ( ) > 1 && m [ pieces [ i ] [ 0 ] ] != 0 ) { int idx = m [ pieces [ i ] [ 0 ] ] - 1 ; idx ++ ; if ( idx >= arr . size ( ) ) return false ; for ( int j = 1 ; j < pieces [ i ] . size ( ) ; j ++ ) { if ( arr [ idx ] == pieces [ i ] [ j ] ) { idx ++ ; if ( idx >= arr . size ( ) && j < pieces [ i ] . size ( ) - 1 ) return false ; } else { return false ; } } } else { return false ; } } return true ; } int main ( ) { vector < int > arr = { 1 , 2 , 4 , 3 } ; vector < vector < int > > pieces { { 1 } , { 4 , 3 } , { 2 } } ; if ( check ( arr , pieces ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Modify given array to make sum of odd and even indexed elements same | C ++ program for the above approach ; Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Driver Code ; Given array arr [ ] ; Function Call
#include <iostream> NEW_LINE using namespace std ; void makeArraySumEqual ( int a [ ] , int N ) { int count_0 = 0 , count_1 = 0 ; int odd_sum = 0 , even_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; if ( ( i + 1 ) % 2 == 0 ) even_sum += a [ i ] ; else if ( ( i + 1 ) % 2 > 0 ) odd_sum += a [ i ] ; } if ( odd_sum == even_sum ) { for ( int i = 0 ; i < N ; i ++ ) cout << " ▁ " << a [ i ] ; } else { if ( count_0 >= N / 2 ) { for ( int i = 0 ; i < count_0 ; i ++ ) cout << "0 ▁ " ; } else { int is_Odd = count_1 % 2 ; count_1 -= is_Odd ; for ( int i = 0 ; i < count_1 ; i ++ ) cout << "1 ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; makeArraySumEqual ( arr , N ) ; return 0 ; }
Minimum replacement of pairs by their LCM required to reduce given array to its LCM | C ++ program for the above approach ; Boolean array to set or unset prime non - prime indices ; Stores the prefix sum of the count of prime numbers ; Function to check if a number is prime or not from 0 to N ; If p is a prime ; Set its multiples as non - prime ; Function to store the count of prime numbers ; Function to count the operations to reduce the array to one element by replacing each pair with its LCM ; Generating Prime Number ; Corner Case ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int maxm = 10001 ; bool prime [ maxm ] ; int prime_number [ maxm ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p < maxm ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < maxm ; i += p ) prime [ i ] = false ; } } prime [ 0 ] = false ; prime [ 1 ] = false ; } void num_prime ( ) { prime_number [ 0 ] = 0 ; for ( int i = 1 ; i <= maxm ; i ++ ) prime_number [ i ] = prime_number [ i - 1 ] + prime [ i ] ; } void min_steps ( int arr [ ] , int n ) { SieveOfEratosthenes ( ) ; num_prime ( ) ; if ( n == 1 ) cout << "0 STRNEWLINE " ; else if ( n == 2 ) cout << "1 STRNEWLINE " ; else cout << prime_number [ n ] - 1 + ( n - 2 ) ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; min_steps ( arr , N ) ; return 0 ; }
Find the winner of a game of removing any number of stones from the least indexed non | C ++ program for the above approach ; Function to find the winner of game between A and B ; win = 1 means B is winner win = 0 means A is winner ; If size is even , winner is B ; If size is odd , winner is A ; Stone will be removed by B ; B will take n - 1 stones from current pile having n stones and force A to pick 1 stone ; Stone will be removed by A ; A will take n - 1 stones from current pile having n stones and force B to pick 1 stone ; Print the winner accordingly ; Driver Code ; Given piles of stone ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( int a [ ] , int n ) { int win = 0 ; if ( n % 2 == 0 ) win = 1 ; else win = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( i % 2 == 1 ) { if ( win == 0 && a [ i ] > 1 ) win = 1 ; } else { if ( win == 1 && a [ i ] > 1 ) win = 0 ; } } if ( win == 0 ) cout << " A " ; else cout << " B " ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findWinner ( arr , N ) ; return 0 ; }
Check if a destination is reachable from source with two movements allowed | Set 2 | C ++ program to implement the above approach ; Check if ( x2 , y2 ) can be reached from ( x1 , y1 ) ; Reduce x2 by y2 until it is less than or equal to x1 ; Reduce y2 by x2 until it is less than or equal to y1 ; If x2 is reduced to x1 ; Check if y2 can be reduced to y1 or not ; If y2 is reduced to y1 ; Check if x2 can be reduced to x1 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isReachable ( long long x1 , long long y1 , long long x2 , long long y2 ) { while ( x2 > x1 && y2 > y1 ) { if ( x2 > y2 ) x2 %= y2 ; else y2 %= x2 ; } if ( x2 == x1 ) return ( y2 - y1 ) >= 0 && ( y2 - y1 ) % x1 == 0 ; else if ( y2 == y1 ) return ( x2 - x1 ) >= 0 && ( x2 - x1 ) % y1 == 0 ; else return 0 ; } int main ( ) { long long source_x = 2 , source_y = 10 ; long long dest_x = 26 , dest_y = 12 ; if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the player who wins the game by removing the last of given N cards | C ++ Program to implement the above approach ; Function to check which player can win the game ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkWinner ( int N , int K ) { if ( N % ( K + 1 ) ) { cout << " A " ; } else { cout << " B " ; } } int main ( ) { int N = 50 ; int K = 10 ; checkWinner ( N , K ) ; }