codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number can be represented as product of two positive perfect cubes | C ++ program for the above approach ; Function to check if the number N can be represented as the product of two perfect cubes or not ; If cube of cube_root is N ; Otherwise , print No ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void productOfTwoPerfectCubes ( int N ) { int cube_root ; cube_root = round ( cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { cout << " Yes " ; return ; } else { cout << " No " ; return ; } } int main ( ) { int N = 216 ; productOfTwoPerfectCubes ( N ) ; return 0 ; }
Size of smallest square that contains N non | CPP program for the above approach ; Function to check if side of square X can pack all the N rectangles or not ; Find the number of rectangle it can pack ; If val is atleast N , then return true ; Otherwise , return false ; Function to find the size of the smallest square that can contain N rectangles of dimensions W * H ; Stores the lower bound ; Stores the upper bound ; Iterate until i is less than j ; Calculate the mid value ; If the current size of square cam contain N rectangles ; Otherwise , update i ; Return the minimum size of the square required ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool bound ( int w , int h , int N , int x ) { int val = ( x / w ) * ( x / h ) ; if ( val >= N ) return true ; else return false ; } int FindSquare ( int N , int W , int H ) { int i = 1 ; int j = W * H * N ; while ( i < j ) { int mid = i + ( j - i ) / 2 ; if ( bound ( W , H , N , mid ) ) j = mid ; else i = mid + 1 ; } return j ; } int main ( ) { int W = 2 ; int H = 3 ; int N = 10 ; cout << FindSquare ( N , W , H ) ; }
Count integers up to N that are equal to at least 2 nd power of any integer exceeding 1 | C ++ program for the above approach ; Function to count the integers up to N that can be represented as a ^ b , where a & b > 1 ; Initialize a HashSet ; Iterating over the range [ 2 , sqrt ( N ) ] ; Generate all possible power of x ; Multiply x by i ; If the generated number lies in the range [ 1 , N ] then insert it in HashSet ; Print the total count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNumberOfPairs ( int N ) { unordered_set < int > st ; for ( int i = 2 ; i * i <= N ; i ++ ) { int x = i ; while ( x <= N ) { x *= i ; if ( x <= N ) { st . insert ( x ) ; } } } cout << st . size ( ) ; } int main ( ) { int N = 10000 ; printNumberOfPairs ( N ) ; return 0 ; }
Maximize product of lengths of strings having no common characters | C ++ program for the above approach ; Function to count the number of set bits in the integer n ; Stores the count of set bits in n ; Return the count ; Function to find the maximum product of pair of strings having no common characters ; Stores the integer equivalent of the strings ; Traverse the array of strings ; Traverse the current string ; Store the current bit position in bits [ i ] ; Store the required result ; Traverse the array , bits [ ] to get all unique pairs ( i , j ) ; Check whether the strings have no common characters ; Update the overall maximum product ; Print the maximum product ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } void maximumProduct ( vector < string > words ) { vector < int > bits ( words . size ( ) , 0 ) ; for ( int i = 0 ; i < words . size ( ) ; i ++ ) { for ( int j = 0 ; j < words [ i ] . length ( ) ; j ++ ) { bits [ i ] = bits [ i ] | 1 << ( words [ i ] [ j ] - ' a ' ) ; } } int result = 0 ; for ( int i = 0 ; i < bits . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < bits . size ( ) ; j ++ ) { if ( ( bits [ i ] & bits [ j ] ) == 0 ) { int L = countSetBits ( bits [ i ] ) ; int R = countSetBits ( bits [ j ] ) ; result = max ( L * R , result ) ; } } } cout << result ; } int main ( ) { vector < string > arr = { " abcw " , " baz " , " foo " , " bar " , " xtfn " , " abcdef " } ; maximumProduct ( arr ) ; return 0 ; }
Modify a sentence by reversing order of occurrences of all Palindrome Words | Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver Code
function palindrome ( str ) { var st = 0 ; var ed = str . length - 1 ; while ( st < ed ) { if ( str [ st ] == str [ ed ] ) { st ++ ; ed -- ; } else return false ; } return true ; } function printReverse ( sentence ) { var newlist = [ ] ; var lis = [ ] ; var temp = " " ; for ( var i = 0 ; i < sentence . length ; i ++ ) { if ( sentence [ i ] == ' ▁ ' ) { lis . push ( temp ) ; temp = " " ; } else temp += sentence [ i ] ; } lis . push ( temp ) ; for ( var i = 0 ; i < lis . length ; i ++ ) { if ( palindrome ( lis [ i ] ) ) newlist . push ( lis [ i ] ) ; } newlist . reverse ( ) ; var j = 0 ; for ( var i = 0 ; i < lis . length ; i ++ ) { if ( palindrome ( lis [ i ] ) ) { lis [ i ] = newlist [ j ] ; j = j + 1 ; } } for ( var i = 0 ; i < lis . length ; i ++ ) { document . write ( lis [ i ] + " ▁ " ) ; } } var sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " ; printReverse ( sentence ) ;
Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index | C ++ program for the above approach ; Function to count the minimum number of operations required to convert the array A [ ] into array B [ ] ; Stores the index in the first permutation A [ ] which is same as the subsequence in B [ ] ; Find the first i elements in A [ ] which is a subsequence in B [ ] ; If element A [ i ] is same as B [ j ] ; Return the count of operations required ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int minCount ( int A [ ] , int B [ ] , int N ) { int i = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] == B [ j ] ) { i ++ ; } } return N - i ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int B [ ] = { 1 , 5 , 2 , 3 , 4 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minCount ( A , B , N ) ; return 0 ; }
Minimize maximum array element possible by at most K splits on the given array | C ++ program for the above approach ; Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int possible ( int A [ ] , int N , int mid , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += ( A [ i ] - 1 ) / mid ; } return count <= K ; } int minimumMaximum ( int A [ ] , int N , int K ) { int lo = 1 ; int hi = * max_element ( A , A + N ) ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } int main ( ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumMaximum ( arr , N , K ) ; return 0 ; }
Maximum value of X such that difference between any array element and X does not exceed K | C ++ program for the above approach ; Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumNumber ( int arr [ ] , int N , int K ) { int minimum = * min_element ( arr , arr + N ) ; int ans = minimum + K ; for ( int i = 0 ; i < N ; i ++ ) { if ( abs ( arr [ i ] - ans ) > K ) { ans = -1 ; break ; } } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumNumber ( arr , N , K ) ; return 0 ; }
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | C ++ program for the above approach ; Function to check if a number is prime or not ; Base Cases ; Check if n is divisible by 2 or 3 ; Iterate over every 6 number from the range [ 5 , sqrt ( N ) ] ; If n is found to be non - prime ; Otherwise , return true ; Function to check if a number can be represented as the sum of a prime number and a perfect square or not ; Stores all perfect squares less than N ; Store the perfect square in the array ; Iterate over all perfect squares ; Store the difference of perfect square from n ; If difference is prime ; Update flag ; Break out of the loop ; If N is the sum of a prime number and a perfect square ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } void sumOfPrimeSquare ( int n ) { int i = 0 ; vector < int > squares ; while ( i * i < n ) { squares . push_back ( i * i ) ; i ++ ; } bool flag = false ; for ( i = 0 ; i < squares . size ( ) ; i ++ ) { int difference = n - squares [ i ] ; if ( isPrime ( difference ) ) { flag = true ; break ; } } if ( flag ) { cout << " Yes " ; } else cout << " No " ; } int main ( ) { int N = 27 ; sumOfPrimeSquare ( N ) ; return 0 ; }
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | C ++ program for the above approach ; Function to store all prime numbers less than or equal to N ; Update prime [ 0 ] and prime [ 1 ] as false ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all multiples of p which are <= n as non - prime ; Function to check whether a number can be represented as the sum of a prime number and a perfect square ; Stores all the prime numbers less than or equal to n ; Update the array prime [ ] ; Iterate over the range [ 0 , n ] ; If current number is non - prime ; Update difference ; If difference is a perfect square ; If true , update flag and break out of loop ; If N can be expressed as sum of prime number and perfect square ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool prime [ ] , int n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } void sumOfPrimeSquare ( int n ) { bool flag = false ; bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , n ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) continue ; int dif = n - i ; if ( ceil ( ( double ) sqrt ( dif ) ) == floor ( ( double ) sqrt ( dif ) ) ) { flag = true ; break ; } } if ( flag ) { cout << " Yes " ; } else cout << " No " ; } int main ( ) { int N = 27 ; sumOfPrimeSquare ( N ) ; return 0 ; }
Check if any subarray of length M repeats at least K times consecutively or not | C ++ program for the above approach ; Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; bool check ( int arr [ ] , int M , int K , int ind ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < K ; j ++ ) { if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) { return false ; } } } return true ; } bool SubarrayRepeatsKorMore ( int arr [ ] , int N , int M , int K ) { for ( int ind = 0 ; ind <= N - M * K ; ind ++ ) { if ( check ( arr , M , K , ind ) ) { return true ; } } return false ; } int main ( ) { int arr [ ] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int M = 2 , K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Check if any subarray of length M repeats at least K times consecutively or not | C ++ program for the above approach ; Function to check if any subarray of length M repeats at least K times consecutively or not ; Stores the required count of repeated subarrays ; Check if the next continuous subarray has equal elements ; Check if K continuous subarray of length M are found or not ; If no subarrays are found ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; bool checkExists ( int arr [ ] , int N , int M , int K ) { int count = 0 ; for ( int i = 0 ; i < N - M ; i ++ ) { if ( arr [ i ] == arr [ i + M ] ) count ++ ; else count = 0 ; if ( count == M * ( K - 1 ) ) return true ; } return false ; } int main ( ) { int arr [ ] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 2 , K = 2 ; if ( checkExists ( arr , N , M , K ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Number of common digits present in two given numbers | C ++ program for the above approach ; Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CommonDigits ( int N , int M ) { int count = 0 ; int freq1 [ 10 ] = { 0 } ; int freq2 [ 10 ] = { 0 } ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = N / 10 ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = M / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } int main ( ) { int N = 748294 ; int M = 34298156 ; cout << CommonDigits ( N , M ) ; return 0 ; }
Smallest value of N such that the sum of all natural numbers from K to N is at least X | C ++ program for the above approach ; Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumNumber ( int K , int X ) { if ( K > X ) { cout << " - 1" ; return ; } int ans = 0 ; int sum = 0 ; for ( int i = K ; i <= X ; i ++ ) { sum += i ; if ( sum >= X ) { ans = i ; break ; } } cout << ans ; } int main ( ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; return 0 ; }
Smallest value of N such that the sum of all natural numbers from K to N is at least X | C ++ program for the above approach ; Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isGreaterEqual ( int N , int K , int X ) { return ( ( N * 1LL * ( N + 1 ) / 2 ) - ( ( K - 1 ) * 1LL * K / 2 ) ) >= X ; } void minimumNumber ( int K , int X ) { if ( K > X ) { cout << " - 1" ; return ; } int low = K , high = X , res = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , K , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } cout << res ; } int main ( ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; return 0 ; }
Count pairs with product of indices equal to the product of elements present at those indices | C ++ program for the above approach ; Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int CountPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 0 , 3 , 2 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountPairs ( arr , N ) ; return 0 ; }
Maximize remainder of sum of a pair of array elements with different parity modulo K | C ++ program for the above approach ; Function to find the maximum remainder of sum of a pair of array elements modulo K ; Stores all even numbers ; Stores all odd numbers ; Segregate remainders of even and odd numbers in respective sets ; Stores the maximum remainder obtained ; Find the complement of remainder of each even number in odd set ; Find the complement of remainder x ; Print the answer ; Driver code ; Given array ; Size of the array ; Given value of K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxRemainder ( int A [ ] , int N , int K ) { unordered_set < int > even ; set < int > odd ; for ( int i = 0 ; i < N ; i ++ ) { int num = A [ i ] ; if ( num % 2 == 0 ) even . insert ( num % K ) ; else odd . insert ( num % K ) ; } int max_rem = 0 ; for ( int x : even ) { int y = K - 1 - x ; auto it = odd . upper_bound ( y ) ; if ( it != odd . begin ( ) ) { it -- ; max_rem = max ( max_rem , x + * it ) ; } } cout << max_rem ; } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 11 , 6 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 7 ; maxRemainder ( arr , N , K ) ; return 0 ; }
Length of the longest path ending at vertex V in a Graph | C ++ program for the above approach ; Function to perform DFS Traversal from source node to the deepest node and update maximum distance to the deepest node ; Mark source as visited ; Update the maximum distance ; Traverse the adjacency list of the current source node ; Recursively call for the child node ; Backtracking step ; Function to calculate maximum length of the path ending at vertex V from any source node ; Stores the maximum length of the path ending at vertex V ; Stores the size of the matrix ; Stores the adjacency list of the given graph ; Traverse the matrix to create adjacency list ; Perform DFS Traversal to update the maximum distance ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( int src , vector < int > Adj [ ] , vector < bool > & visited , int level , int & distance ) { visited [ src ] = true ; distance = max ( distance , level ) ; for ( auto & child : Adj [ src ] ) { if ( child != src and visited [ child ] == false ) { dfs ( child , Adj , visited , level + 1 , distance ) ; } } visited [ src ] = false ; } int maximumLength ( vector < vector < int > > & mat , int V ) { int distance = 0 ; int N = ( int ) mat . size ( ) ; vector < int > Adj [ N ] ; vector < bool > visited ( N , false ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { Adj [ i ] . push_back ( j ) ; } } } dfs ( V , Adj , visited , 0 , distance ) ; return distance ; } int main ( ) { vector < vector < int > > mat = { { 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 } , { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } } ; int V = 2 ; cout << maximumLength ( mat , V ) ; return 0 ; }
Minimum removals required such that given string consists only of a pair of alternating characters | C ++ program for the above approach ; Function to find the maximum length of alternating occurrences of a pair of characters in a string s ; Stores the next character for alternating sequence ; Stores the length of alternating occurrences of a pair of characters ; Traverse the given string ; If current character is same as the required character ; Increase length by 1 ; Reset required character ; Return the length ; Function to find minimum characters required to be deleted from S to obtain an alternating sequence ; Stores maximum length of alternating sequence of two characters ; Stores length of the string ; Generate every pair of English alphabets ; Function call to find length of alternating sequence for current pair of characters ; Update len to store the maximum of len and newLen in len ; Return n - len as the final result ; Driver Code ; Given Input ; Function call to find minimum characters required to be removed from S to make it an alternating sequence of a pair of characters
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLength ( string s , char i , char j ) { char required = i ; int length = 0 ; for ( char curr : s ) { if ( curr == required ) { length += 1 ; if ( required == i ) required = j ; else required = i ; } } return length ; } int minimumDeletions ( string S ) { int len = 0 ; int n = S . length ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { for ( char j = i + 1 ; j <= ' z ' ; j ++ ) { int newLen = findLength ( S , i , j ) ; len = max ( len , newLen ) ; } } return n - len ; } int main ( ) { string S = " adebbeeaebd " ; cout << minimumDeletions ( S ) ; return 0 ; }
Smallest value of X satisfying the condition X % A [ i ] = B [ i ] for two given arrays | C ++ program for the above approach ; Function to calculate modulo inverse of a w . r . t m using Extended Euclid Algorithm ; Base Case ; Perform extended euclid algorithm ; q is quotient ; m is remainder now , process same as euclid 's algorithm ; If x1 is negative ; Make x1 positive ; Function to implement Chinese Remainder Theorem to find X ; Stores the product of array elements ; Traverse the array ; Update product ; Initialize the result ; Apply the above formula ; Function to calculate the product of all elements of the array a [ ] ; Stores product of all array elements ; Traverse the array ; Return the product ; Function to find the value of X that satisfies the given condition ; Stores the required smallest value using Chinese Remainder Theorem ; Stores the product of all array elements ; The equation is Y + K * M >= P Therefore , calculate K = ceil ( ( P - Y ) / M ) ; So , X = Y + K * M ; Print the resultant value of X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } int findMinX ( int A [ ] , int B [ ] , int N ) { int prod = 1 ; for ( int i = 0 ; i < N ; i ++ ) prod *= A [ i ] ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pp = prod / A [ i ] ; result += B [ i ] * inv ( pp , A [ i ] ) * pp ; } return result % prod ; } int product ( int a [ ] , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { ans *= a [ i ] ; } return ans ; } void findSmallestInteger ( int A [ ] , int B [ ] , int P , int n ) { int Y = findMinX ( A , B , n ) ; int M = product ( A , n ) ; int K = ceil ( ( ( double ) P - ( double ) Y ) / ( double ) M ) ; int X = Y + K * M ; cout << X ; } int main ( ) { int A [ ] = { 3 , 4 , 5 } ; int B [ ] = { 2 , 3 , 1 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int P = 72 ; findSmallestInteger ( A , B , P , n ) ; }