codeparrot/xlcost-text-to-code huggingface dataset:
Smallest vertex in the connected components of all the vertices in given undirect graph | C ++ program for the above approach ; Stores the parent and size of the set of the ith element ; Function to initialize the ith set ; Function to find set of ith vertex ; Base Case ; Recursive call to find set ; Function to unite the set that includes a and the set that includes b ; If a and b are not from same set ; Function to find the smallest vertex in the connected component of the ith vertex for all i in range [ 1 , N ] ; Loop to initialize the ith set ; Loop to unite all vertices connected by edges into the same set ; Stores the minimum vertex value for ith set ; Loop to iterate over all vertices ; If current vertex does not exist in minVal initialize it with i ; Update the minimum value of the set having the ith vertex ; Loop to print required answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int maxn = 100 ; int parent [ maxn ] , Size [ maxn ] ; void make_set ( int v ) { parent [ v ] = v ; Size [ v ] = 1 ; } int find_set ( int v ) { if ( v == parent [ v ] ) { return v ; } return parent [ v ] = find_set ( parent [ v ] ) ; } void union_sets ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; if ( a != b ) { if ( Size [ a ] < Size [ b ] ) swap ( a , b ) ; parent [ b ] = a ; Size [ a ] += Size [ b ] ; } } void findMinVertex ( int N , vector < pair < int , int > > edges ) { for ( int i = 1 ; i <= N ; i ++ ) { make_set ( i ) ; } for ( int i = 0 ; i < edges . size ( ) ; i ++ ) { union_sets ( edges [ i ] . first , edges [ i ] . second ) ; } unordered_map < int , int > minVal ; for ( int i = 1 ; i <= N ; i ++ ) { if ( minVal [ find_set ( i ) ] == 0 ) { minVal [ find_set ( i ) ] = i ; } else { minVal [ find_set ( i ) ] = min ( minVal [ find_set ( i ) ] , i ) ; } } for ( int i = 1 ; i <= N ; i ++ ) { cout << minVal [ find_set ( i ) ] << " ▁ " ; } } int main ( ) { int N = 6 ; vector < pair < int , int > > edges = { { 1 , 3 } , { 2 , 4 } } ; findMinVertex ( N , edges ) ; return 0 ; }
Count of pairs in given range having their ratio equal to ratio of product of their digits | C ++ program for the above approach ; Function to find the product of digits of the given number ; Function to find the count of pairs ( a , b ) such that a : b = ( product ofdigits of a ) : ( product of digits of b ) ; Stores the count of the valid pairs ; Loop to iterate over all unordered pairs ( a , b ) ; Stores the product of digits of a ; Stores the product of digits of b ; If x != 0 and y != 0 and a : b is equivalent to x : y ; Increment valid pair count ; Return Answer ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } int countPairs ( int L , int R ) { int cntPair = 0 ; for ( int a = L ; a <= R ; a ++ ) { for ( int b = a + 1 ; b <= R ; b ++ ) { int x = getProduct ( a ) ; int y = getProduct ( b ) ; if ( x && y && ( a * y ) == ( b * x ) ) { cntPair ++ ; } } } return cntPair ; } int main ( ) { int L = 1 ; int R = 100 ; cout << countPairs ( 1 , 100 ) ; return 0 ; }
Find numbers in range [ L , R ] that are coprime with given Array elements | C ++ program for the above approach ; Function to find all the elements in the range [ L , R ] which are co prime with all array elements ; Store all the divisors of array element in S ; Find the divisors ; Stores all possible required number satisfying the given criteria ; Insert all element [ L , R ] ; Traverse the set ; Remove the multiples of ele ; Print the resultant numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void elementsCoprimeWithArr ( int A [ ] , int N , int L , int R ) { unordered_set < int > S ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = A [ i ] ; for ( int j = 1 ; j <= sqrt ( curr_ele ) + 1 ; j ++ ) { if ( curr_ele % j == 0 ) { S . insert ( j ) ; S . insert ( curr_ele / j ) ; } } } unordered_set < int > store ; for ( int i = L ; i <= R ; i ++ ) store . insert ( i ) ; S . erase ( 1 ) ; for ( auto it : S ) { int ele = it ; int index = 1 ; while ( index * ele <= R ) { store . erase ( index * ele ) ; index ++ ; } } for ( auto i : store ) { cout << i << " ▁ " ; } } int main ( ) { int arr [ ] = { 3 , 5 } ; int L = 1 , R = 10 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; elementsCoprimeWithArr ( arr , N , L , R ) ; return 0 ; }
Maximize matrix sum by flipping the sign of any adjacent pairs | C ++ program for the above approach ; Function to find the maximum sum of matrix element after flipping the signs of adjacent matrix elements ; Initialize row and column ; Stores the sum of absolute matrix element ; Find the minimum absolute value in the matrix ; Store count of negatives ; Find the smallest absolute value in the matrix ; Increment the count of negative numbers ; Find the absolute sum ; If the count of negatives is even then print the sum ; Subtract minimum absolute element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( vector < vector < int > > & matrix ) { int r = matrix . size ( ) ; int c = matrix [ 0 ] . size ( ) ; int sum = 0 ; int mini = INT_MAX ; int count = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { int k = matrix [ i ] [ j ] ; mini = min ( mini , abs ( k ) ) ; if ( k < 0 ) count ++ ; sum += abs ( k ) ; } } if ( count % 2 == 0 ) { return sum ; } else { return ( sum - 2 * mini ) ; } } int main ( ) { vector < vector < int > > matrix = { { 2 , -2 } , { -2 , 2 } } ; cout << maxSum ( matrix ) ; return 0 ; }
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements | C ++ program for the above approach ; Function to calculate last positive element of the array ; Return the first element if N = 1 ; Stores the greatest and the second greatest element ; Traverse the array A [ ] ; If current element is greater than the greatest element ; If current element is greater than second greatest element ; Return the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastPositiveElement ( vector < int > arr ) { int N = arr . size ( ) ; if ( N == 1 ) return arr [ 0 ] ; int greatest = -1 , secondGreatest = -1 ; for ( int x : arr ) { if ( x >= greatest ) { secondGreatest = greatest ; greatest = x ; } else if ( x >= secondGreatest ) { secondGreatest = x ; } } return greatest - secondGreatest ; } int main ( ) { vector < int > arr = { 3 , 5 , 4 , 7 } ; cout << lastPositiveElement ( arr ) ; return 0 ; }
Reduce given array by replacing subarrays with values less than K with their sum | C ++ program for the above approach ; Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void updateArray ( vector < int > & arr , int K ) { int sum = 0 ; vector < int > res ; for ( int i = 0 ; i < ( int ) arr . size ( ) ; i ++ ) { if ( arr [ i ] < K ) { sum += arr [ i ] ; } else { if ( sum != 0 ) { res . push_back ( sum ) ; } sum = 0 ; res . push_back ( arr [ i ] ) ; } } if ( sum != 0 ) res . push_back ( sum ) ; for ( auto & it : res ) cout << it << ' ▁ ' ; } int main ( ) { vector < int > arr = { 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 } ; int K = 100 ; updateArray ( arr , K ) ; return 0 ; }
Find if path length is even or odd between given Tree nodes for Q queries | C ++ program for the above approach ; Stores the input tree ; Stores the set number of all nodes ; Function to add an edge in the tree ; Function to convert the given tree into a bipartite graph using BFS ; Set the set number to - 1 for all node of the given tree ; Stores the current node during the BFS traversal of the tree ; Initialize the set number of 1 st node and enqueue it ; BFS traversal of the given tree ; Current node ; Traverse over all neighbours of the current node ; If the set is not assigned ; Assign set number to node u ; Function to find if the path length between node A and B is even or odd ; If the set number of both nodes is same , path length is odd else even ; Driver Code ; Function to convert tree into bipartite
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > adj ( 100000 ) ; vector < int > setNum ( 100000 ) ; void addEdge ( int a1 , int a2 ) { adj [ a1 ] . push_back ( a2 ) ; adj [ a2 ] . push_back ( a1 ) ; } void toBipartite ( int N ) { setNum . assign ( N , -1 ) ; queue < int > q ; q . push ( 0 ) ; setNum [ 0 ] = 0 ; while ( ! q . empty ( ) ) { int v = q . front ( ) ; q . pop ( ) ; for ( int u : adj [ v ] ) { if ( setNum [ u ] == -1 ) { setNum [ u ] = setNum [ v ] ^ 1 ; q . push ( u ) ; } } } } void pathLengthQuery ( int A , int B ) { if ( setNum [ A ] == setNum [ B ] ) { cout << " Odd " << endl ; } else { cout << " Even " << endl ; } } int main ( ) { int N = 7 ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 4 ) ; addEdge ( 3 , 5 ) ; addEdge ( 2 , 6 ) ; toBipartite ( N ) ; pathLengthQuery ( 4 , 2 ) ; pathLengthQuery ( 0 , 4 ) ; return 0 ; }
Minimum size of set having either element in range [ 0 , X ] or an odd power of 2 with sum N | CPP program for the above approach ; Function to find the highest odd power of 2 in the range [ 0 , N ] ; If P is even , subtract 1 ; Function to find the minimum operations to make N ; If N is odd and X = 0 , then no valid set exist ; Stores the minimum possible size of the valid set ; Loop to subtract highest odd power of 2 while X < N , step 2 ; If N > 0 , then increment the value of answer by 1 ; Return the resultant size of set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int highestPowerof2 ( int n ) { int p = int ( log2 ( n ) ) ; if ( p % 2 == 0 ) p -= 1 ; return int ( pow ( 2 , p ) ) ; } int minStep ( int N , int X ) { if ( N % 2 and X == 0 ) return -1 ; int size = 0 ; while ( X < N ) { N -= highestPowerof2 ( N ) ; size += 1 ; } if ( N ) size += 1 ; return size ; } int main ( ) { int N = 11 ; int X = 2 ; cout << ( minStep ( N , X ) ) ; }
Check if all the digits of the given number are same | C ++ Program to implement the above approach ; Function to check if all the digits in the number N is the same or not ; Find the last digit ; Find the current last digit ; Update the value of N ; If there exists any distinct digit , then return No ; Otherwise , return Yes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkSameDigits ( int N ) { int digit = N % 10 ; while ( N != 0 ) { int current_digit = N % 10 ; N = N / 10 ; if ( current_digit != digit ) { return " No " ; } } return " Yes " ; } int main ( ) { int N = 222 ; cout << ( checkSameDigits ( N ) ) ; return 0 ; }
Find number formed by K times alternatively reducing X and adding Y to 0 | C ++ program for the above approach ; Function to find the value obtained after alternatively reducing X and adding Y to 0 total K number of times ; Stores the final result after adding only Y to 0 ; Stores the final number after reducing only X from 0 ; Return the result obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int positionAfterKJumps ( int X , int Y , int K ) { int addY = Y * ( K / 2 ) ; int reduceX = -1 * X * ( K / 2 + K % 2 ) ; return addY + reduceX ; } int main ( ) { int X = 2 , Y = 5 , K = 3 ; cout << positionAfterKJumps ( X , Y , K ) ; return 0 ; }
Maximize the largest number K such that bitwise and of K till N is 0 | C ++ program for above approach ; Function to find maximum value of k which makes bitwise AND zero . ; Take k = N initially ; Start traversing from N - 1 till 0 ; Whenever we get AND as 0 we stop and return ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxK ( int N ) { int K = N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { K &= i ; if ( K == 0 ) { return i ; } } return 0 ; } int main ( ) { int N = 5 ; cout << findMaxK ( N ) ; }
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | C ++ program of the above approach ; Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all possible pairs ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
#include <bits/stdc++.h> 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 ( ( arr [ j ] % arr [ i ] == 0 ) && ( j + 1 ) % ( i + 1 ) == 0 && ( arr [ j ] / arr [ i ] == ( j + 1 ) / ( i + 1 ) ) ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 5 , -2 , 4 , 20 , 25 , -6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | C ++ program of the above approach ; Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all values of x in range [ 1 , N ] . ; Iterating over all values of y that are divisible by x in the range [ 1 , N ] . ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int x = 1 ; x <= n ; x ++ ) { for ( int y = 2 * x ; y <= n ; y += x ) { if ( ( arr [ y - 1 ] % arr [ x - 1 ] == 0 ) && ( arr [ y - 1 ] / arr [ x - 1 ] == y / x ) ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 5 , -2 , 4 , 20 , 25 , -6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Number of minimum length paths between 1 to N including each node | C ++ program for the above approach ; Function to calculate the distances from node 1 to N ; Stores the number of edges ; Storing the edges in vector ; Initialize queue ; BFS from 1 st node using queue ; Pop from queue ; Traversing the adjacency list ; Initialize queue ; BFS from last node ; Pop from queue ; Traverse the adjacency list ; Print the count of minimum distance ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE void countMinDistance ( int n , int m , int edges [ ] [ 2 ] ) { vector < ll > g [ 10005 ] ; for ( int i = 0 ; i < m ; i ++ ) { int a = edges [ i ] [ 0 ] - 1 ; int b = edges [ i ] [ 1 ] - 1 ; g [ a ] . push_back ( b ) ; g [ b ] . push_back ( a ) ; } queue < pair < ll , ll > > queue1 ; queue1 . push ( { 0 , 0 } ) ; vector < int > dist ( n , 1e9 ) ; vector < int > ways1 ( n , 0 ) ; dist [ 0 ] = 0 ; ways1 [ 0 ] = 1 ; while ( ! queue1 . empty ( ) ) { auto up = queue1 . front ( ) ; queue1 . pop ( ) ; int x = up . first ; int dis = up . second ; if ( dis > dist [ x ] ) continue ; if ( x == n - 1 ) continue ; for ( ll y : g [ x ] ) { if ( dist [ y ] > dis + 1 ) { dist [ y ] = dis + 1 ; ways1 [ y ] = ways1 [ x ] ; queue1 . push ( { y , dis + 1 } ) ; } else if ( dist [ y ] == dis + 1 ) { ways1 [ y ] += ways1 [ x ] ; } } } queue < pair < ll , ll > > queue2 ; queue2 . push ( { n - 1 , 0 } ) ; vector < int > dist1 ( n , 1e9 ) ; vector < int > ways2 ( n , 0 ) ; dist1 [ n - 1 ] = 0 ; ways2 [ n - 1 ] = 1 ; while ( ! queue2 . empty ( ) ) { auto up = queue2 . front ( ) ; queue2 . pop ( ) ; int x = up . first ; int dis = up . second ; if ( dis > dist1 [ x ] ) continue ; if ( x == 0 ) continue ; for ( ll y : g [ x ] ) { if ( dist1 [ y ] > dis + 1 ) { dist1 [ y ] = dis + 1 ; ways2 [ y ] = ways2 [ x ] ; queue2 . push ( { y , dis + 1 } ) ; } else if ( dist1 [ y ] == 1 + dis ) { ways2 [ y ] += ways2 [ x ] ; } } } for ( int i = 0 ; i < n ; i ++ ) { cout << ways1 [ i ] * ways2 [ i ] << " ▁ " ; } } int main ( ) { int N = 5 , M = 5 ; int edges [ M ] [ 2 ] = { { 1 , 2 } , { 1 , 4 } , { 1 , 3 } , { 2 , 5 } , { 2 , 4 } } ; countMinDistance ( N , M , edges ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Function to find the count of odd and even integers having N bits and K set bits ; Find the count of even integers ; Find the count of odd integers ; Print the total count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long factorial ( int n ) { long long ans = 1 ; while ( n >= 1 ) { ans *= n ; n -- ; } return ans ; } void Binary_Num ( int n , int k ) { long long num_even , num_odd ; if ( n - k - 1 >= 0 && k - 1 >= 0 ) { num_even = factorial ( n - 2 ) / ( factorial ( k - 1 ) * factorial ( n - k - 1 ) ) ; } else { num_even = 0 ; } if ( k - 2 >= 0 ) { num_odd = factorial ( n - 2 ) / ( factorial ( k - 2 ) * factorial ( n - k ) ) ; } else { num_odd = 0 ; } cout << num_even << " ▁ " << num_odd << endl ; } int main ( ) { int N = 9 , K = 6 ; Binary_Num ( N , K ) ; return 0 ; }
Sum of all subsets of a given size ( = K ) | C ++ program for the above approach ; Function to find the sum of all sub - sets of size K ; Frequency of each array element in summation equation . ; calculate factorial of n - 1 ; calculate factorial of k - 1 ; calculate factorial of n - k ; Calculate sum of array . ; Sum of all subsets of size k . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSumOfAllSubsets ( int arr [ ] , int n , int k ) { int factorial_N = 1 , factorial_d = 1 , factorial_D = 1 ; for ( int i = 1 ; i <= n - 1 ; i ++ ) factorial_N *= i ; for ( int i = 1 ; i <= k - 1 ; i ++ ) factorial_d *= i ; for ( int i = 1 ; i <= n - k ; i ++ ) factorial_D *= i ; int freq = factorial_N / ( factorial_d * factorial_D ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum = sum * freq ; cout << " Sum ▁ of ▁ all ▁ subsets ▁ of ▁ size ▁ = ▁ " << k << " ▁ is ▁ = > ▁ " << sum << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 5 } ; int n = 4 , k = 2 ; findSumOfAllSubsets ( arr , n , k ) ; return 0 ; }
Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | C ++ Program to implement the above approach ; Function to calculate maximum triplet XOR form 3 ranges ; Initialize a variable to store the answer ; create minimum number that have a set bit at ith position ; Check for each number and try to greedily choose the bit if possible If A >= cur then A also have a set bit at ith position ; Increment the answer ; Subtract this value from A ; Check for B ; Increment the answer ; Subtract this value from B ; Check for C ; Increment the answer ; Subtract this value from C ; If any of the above conditions is not satisfied then there is no way to turn that bit ON . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumTripletXOR ( int A , int B , int C ) { int ans = 0 ; for ( int i = 30 ; i >= 0 ; i -- ) { int cur = 1 << i ; if ( A >= cur ) { ans += cur ; A -= cur ; } else if ( B >= cur ) { ans += cur ; B -= cur ; } else if ( C >= cur ) { ans += cur ; C -= cur ; } } return ans ; } int main ( ) { int A = 6 ; int B = 2 ; int C = 10 ; cout << maximumTripletXOR ( A , B , C ) ; }
Count of integers in given range having their last K digits are equal | C ++ Program of the above approach ; Function to return the count of integers from 1 to X having the last K digits as equal ; Stores the total count of integers ; Loop to iterate over all possible values of z ; Terminate the loop when z > X ; Add count of integers with last K digits equal to z ; Return count ; Function to return the count of integers from L to R having the last K digits as equal ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int intCount ( int X , int K ) { int ans = 0 ; for ( int z = 0 ; z < pow ( 10 , K ) ; z += ( pow ( 10 , K ) - 1 ) / 9 ) { if ( z > X ) break ; ans += ( ( X - z ) / pow ( 10 , K ) + 1 ) ; } return ans ; } int intCountInRange ( int L , int R , int K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } int main ( ) { int L = 49 ; int R = 101 ; int K = 2 ; cout << intCountInRange ( L , R , K ) ; return 0 ; }
Count subsequence of length 4 having product of the first three elements equal to the fourth element | C ++ program for the above approach ; Function to find the total number of subsequences satisfying the given criteria ; Stores the count of quadruplets ; Stores the frequency of product of the triplet ; Traverse the given array arr [ ] ; Consider arr [ i ] as fourth element of subsequences ; Generate all possible pairs of the array [ 0 , i - 1 ] ; Increment the frequency of the triplet ; Return the total count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countQuadruples ( int A [ ] , int N ) { int ans = 0 ; unordered_map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { ans += freq [ A [ i ] ] ; for ( int j = 0 ; j < i ; j ++ ) { for ( int k = 0 ; k < j ; k ++ ) { freq [ A [ i ] * A [ j ] * A [ k ] ] ++ ; } } } return ans ; } int main ( ) { int arr [ ] = { 10 , 2 , 2 , 7 , 40 , 160 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countQuadruples ( arr , N ) ; return 0 ; }
Maximum times X and Y can be reduced to near 0 using numbers A or B | C ++ Program to implement the above approach ; Helper function to check if we can perform Mid number of moves # define MAXN 10000000 MAXN = 1e7 ; Remove atleast Mid * B from both X and Y ; If any value is negative return false . ; Calculate remaining values ; If val >= Mid then it is possible to perform this much moves ; else return false ; Initialize a variable to store the answer ; Fix the left and right range ; Binary Search over the answer ; Check for the middle value as the answer ; It is possible to perform this much moves ; Maximise the answer ; Return answer ; Driver Code ; Generalise that A >= B
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool can ( int Mid , int X , int Y , int A , int B ) { int p1 = X - Mid * B ; int p2 = Y - Mid * B ; if ( p1 < 0 p2 < 0 ) { return false ; } int k = A - B ; if ( k == 0 ) { return true ; } int val = p1 / k + p2 / k ; if ( val >= Mid ) { return true ; } return false ; } int maxPossibleMoves ( int X , int Y , int A , int B ) { int ans = 0 ; int L = 1 , R = MAXN ; while ( L <= R ) { int Mid = ( L + R ) / 2 ; if ( can ( Mid , X , Y , A , B ) ) { L = Mid + 1 ; ans = max ( ans , Mid ) ; } else { R = Mid - 1 ; } } return ans ; } int main ( ) { int X = 10 , Y = 12 , A = 2 , B = 5 ; if ( A < B ) { swap ( A , B ) ; } cout << maxPossibleMoves ( X , Y , A , B ) ; }
Find two proper factors of N such that their sum is coprime with N | C ++ Program for the above approach ; Function to find two proper factors of N such that their sum is coprime with N ; Find factors in sorted order ; Find largest value of d2 such that d1 and d2 are co - prime ; Check if d1 and d2 are proper factors of N ; Print answer ; No such factors exist ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFactors ( int n ) { for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { int d1 = i , d2 = n ; while ( d2 % d1 == 0 ) { d2 = d2 / d1 ; } if ( d1 > 1 && d2 > 1 ) { cout << d1 << " , ▁ " << d2 ; return ; } } } cout << -1 ; } int main ( ) { int N = 10 ; printFactors ( N ) ; return 0 ; }