codeparrot/xlcost-text-to-code huggingface dataset:
Minimum product modulo N possible for any pair from a given range | C ++ program for the above approach ; Function to return the minimum possible value of ( i * j ) % N ; Stores the minimum remainder ; Iterate from L to R ; Iterate from L to R ; Print the minimum value of remainder ; If R - L >= N ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; void minModulo ( int L , int R , int N ) { if ( R - L < N ) { int ans = INT_MAX ; for ( ll i = L ; i <= R ; i ++ ) for ( ll j = L ; j <= R ; j ++ ) if ( i != j ) ans = min ( 0ll + ans , ( i * j ) % N ) ; cout << ans ; } else { cout << 0 ; } } int main ( ) { int L = 6 , R = 10 , N = 2019 ; minModulo ( L , R , N ) ; return 0 ; }
Count numbers having GCD with N equal to the number itself | C ++ program for the above approach ; Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { count ++ ; if ( N / i != i ) { count ++ ; } } } return count ; } int main ( ) { int N = 10 ; cout << countNumbers ( N ) ; return 0 ; }
Maximum length of all possible K equal length ropes generated by cutting N ropes | C ++ program for the above approach ; Function to find the maximum size of ropes having frequency at least K by cutting the given ropes ; Stores the left and the right boundaries ; Stores the maximum length of rope possible ; Iterate while low is less than or equal to high ; Stores the mid value of the range [ low , high ] ; Stores the count of ropes of length mid ; Traverse the array arr [ ] ; If count is at least K ; Assign mid to ans ; Update the value of low ; Otherwise , update the value of high ; Return the value of ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSize ( int a [ ] , int k , int n ) { int low = 1 ; int high = * max_element ( a , a + n ) ; int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int count = 0 ; for ( int c = 0 ; c < n ; c ++ ) { count += a / mid ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 9 } ; int K = 6 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( maximumSize ( arr , K , n ) ) ; }
Quadratic equation whose roots are K times the roots of given equation | C ++ program for the above approach ; Function to find the quadratic equation whose roots are K times the roots of the given equation ; Print quadratic equation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findEquation ( int A , int B , int C , int K ) { cout << A << " ▁ " << K * B << " ▁ " << K * K * C ; } int main ( ) { int A = 1 , B = 2 , C = 1 , K = 2 ; findEquation ( A , B , C , K ) ; return 0 ; }
Maximum the value of a given expression for any pair of coordinates on a 2D plane | C ++ program for the above approach ; Function to find the maximum value of the given expression possible for any pair of co - ordinates ; Stores the differences between pairs ; Stores the maximum value ; Traverse the array arr [ ] [ ] ; While pq is not empty and difference between point [ 0 ] and pq . top ( ) [ 1 ] > K ; Removes the top element ; If pq is not empty ; Update the value res ; Push pair { point [ 1 ] - point [ 0 ] , point [ 0 ] } in pq ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxValueOfEquation ( vector < vector < int > > & arr , int K ) { priority_queue < vector < int > > pq ; int res = INT_MIN ; for ( auto point : arr ) { while ( ! pq . empty ( ) && point [ 0 ] - pq . top ( ) [ 1 ] > K ) { pq . pop ( ) ; } if ( ! pq . empty ( ) ) { res = max ( res , pq . top ( ) [ 0 ] + point [ 0 ] + point [ 1 ] ) ; } pq . push ( { point [ 1 ] - point [ 0 ] , point [ 0 ] } ) ; } cout << res ; } int main ( ) { vector < vector < int > > arr = { { 1 , 3 } , { 2 , 0 } , { 5 , 10 } , { 6 , -10 } } ; int K = 1 ; findMaxValueOfEquation ( arr , K ) ; return 0 ; }
Minimum increments required to make absolute difference of all pairwise adjacent array elements even | C ++ program for the above approach ; Function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; Stores the count of odd and even elements ; Traverse the array ; Increment odd count ; Increment even count ; Return the minimum number of operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int arr [ ] , int n ) { int oddcount = 0 , evencount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) oddcount ++ ; else evencount ++ ; } return min ( oddcount , evencount ) ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 1 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperations ( arr , N ) ; return 0 ; }
Count numbers up to C that can be reduced to 0 by adding or subtracting A or B | C ++ program for the above approach ; Function to calculate GCD of the two numbers a and b ; Base Case ; Recursively find the GCD ; Function to count the numbers up to C that can be reduced to 0 by adding or subtracting A or B ; Stores GCD of A and B ; Stores the count of multiples of g in the range ( 0 , C ] ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long gcd ( long long a , long long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void countDistinctNumbers ( long long A , long long B , long long C ) { long long g = gcd ( A , B ) ; long long count = C / g ; cout << count ; } int main ( ) { long long A = 2 , B = 3 , C = 5 ; countDistinctNumbers ( A , B , C ) ; return 0 ; }
Find the last element after repeatedly removing every second element from either end alternately | C ++ program for the above approach ; Function to find the last element remaining in the array after performing the given operations ; Checks if traversal is from left to right or vice versa ; Store the elements currently present in the array ; Store the distance between 2 consecutive array elements ; Store the first element of the remaining array ; Iterate while elements are greater than 1 ; If left to right turn ; Update head ; Otherwise , check if the remaining elements are odd ; If true , update head ; Eleminate half of the array elements ; Double the steps after each turn ; Alter the turn ; Print the remaining element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printLastElement ( int arr [ ] , int N ) { bool leftTurn = true ; int remainElements = N ; int step = 1 ; int head = 1 ; while ( remainElements > 1 ) { if ( leftTurn ) { head = head + step ; } else { if ( remainElements % 2 == 1 ) head = head + step ; } remainElements = remainElements / 2 ; step = step * 2 ; leftTurn = ! leftTurn ; } cout << arr [ head - 1 ] ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printLastElement ( arr , N ) ; return 0 ; }
Distributed C candies among N boys such that difference between maximum and minimum candies received is K | C ++ program for the above approach ; Function to calculate the maximum and minimum number of candies a boy can possess ; All candies will be given to one boy ; All the candies will be given to 1 boy ; Give K candies to 1 st boy initially ; Count remaining candies ; If the last candy of remaining candies is given to the last boy , i . e Nth boy ; Increase minimum count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_min ( int N , int C , int K ) { int maximum , minimum ; if ( N == 1 ) { maximum = minimum = C ; } else if ( K >= C ) { maximum = C ; minimum = 0 ; } else { maximum = K ; minimum = 0 ; int remain_candy = C - K ; maximum += remain_candy / N ; minimum = remain_candy / N ; if ( remain_candy % N == N - 1 ) { minimum ++ ; } } cout << " Maximum ▁ = ▁ " << maximum << endl ; cout << " Minimum ▁ = ▁ " << minimum ; return 0 ; } int main ( ) { int N = 4 ; int C = 12 ; int K = 3 ; max_min ( N , C , K ) ; return 0 ; }
Length of smallest subarray required to be removed to make remaining elements consecutive | C ++ program for the above approach ; Function to find the length of the smallest subarray to be removed to make remaining array elements consecutive ; Store the ending index of the longest prefix consecutive array ; Traverse the array to find the longest prefix consecutive sequence ; A [ 0. . . left_index ] is the prefix consecutive sequence ; Store the starting index of the longest suffix consecutive sequence ; Traverse the array to find the longest suffix consecutive sequence ; A [ right_index ... N - 1 ] is the consecutive sequence ; Store the smallest subarray required to be removed ; Check if subarray from the middle can be removed ; Update the right index s . t . A [ 0 , N - 1 ] is consecutive ; If updated_right < N , then update the minimumLength ; Print the required result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void shortestSubarray ( int * A , int N ) { int i ; int left_index ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] + 1 != A [ i + 1 ] ) break ; } left_index = i ; int right_index ; for ( i = N - 1 ; i >= 1 ; i -- ) { if ( A [ i ] != A [ i - 1 ] + 1 ) break ; } right_index = i ; int updated_right ; int minLength = min ( N - left_index - 1 , right_index ) ; if ( A [ right_index ] <= A [ left_index ] + 1 ) { updated_right = right_index + A [ left_index ] - A [ right_index ] + 1 ; if ( updated_right < N ) minLength = min ( minLength , updated_right - left_index - 1 ) ; } cout << minLength ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 7 , 4 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; shortestSubarray ( arr , N ) ; return 0 ; }
Check if a string can be split into 3 substrings such that one of them is a substring of the other two | C ++ program for the above approach ; Function to check if string S contains any character with frequency >= 3 or not ; Stores frequency of characters ; Iterate over the string ; Update the frequency of current character ; Iterate over the hash array ; If any character has frequency >= 3 ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string freqCheck ( string S , int N ) { int hash [ 26 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { hash [ S [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hash [ i ] > 2 ) { return " Yes " ; } } return " No " ; } int main ( ) { string S = " geekseekforgeeks " ; int N = S . length ( ) ; cout << freqCheck ( S , N ) ; return 0 ; }
Minimize length of a string by removing suffixes and prefixes of same characters | C ++ program for the above approach ; Function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; Initialize two pointers ; Traverse the string S ; Current char on left pointer ; Shift i towards right ; Shift j towards left ; Return the minimum possible length of string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( string s ) { int i = 0 , j = s . length ( ) - 1 ; for ( ; i < j && s [ i ] == s [ j ] ; ) { char d = s [ i ] ; while ( i <= j && s [ i ] == d ) i ++ ; while ( i <= j && s [ j ] == d ) j -- ; } return j - i + 1 ; } int main ( ) { string S = " aacbcca " ; cout << minLength ( S ) ; }
Number of Binary Search Trees of height H consisting of H + 1 nodes | C ++ program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the value of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of of BSTs of height H consisting of ( H + 1 ) nodes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1000000007 ; int power ( long long x , unsigned int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } int CountBST ( int H ) { return power ( 2 , H ) ; } int main ( ) { int H = 2 ; cout << CountBST ( H ) ; return 0 ; }
Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | C ++ program for the above approach ; Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1' ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rotateString ( int n , int m , string s ) { vector < int > v ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { v . push_back ( i ) ; } } for ( int i = 1 ; i < ( int ) v . size ( ) ; i ++ ) { if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) { cnt ++ ; } } if ( v . size ( ) >= 2 && ( n - ( v . back ( ) - v [ 0 ] ) - 1 ) > m ) { cnt ++ ; } if ( cnt <= 1 ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { string S = "101001" ; int M = 1 ; int N = S . size ( ) ; rotateString ( N , M , S ) ; return 0 ; }
Number obtained by reducing sum of digits of 2 N into a single digit | C ++ program for the above approach ; Function to find the number obtained by reducing sum of digits of 2 ^ N into a single digit ; Stores answers for different values of N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int N ) { int ans [ 6 ] = { 1 , 2 , 4 , 8 , 7 , 5 } ; return ans [ N % 6 ] ; } int main ( ) { int N = 6 ; cout << findNumber ( N ) << endl ; return 0 ; }
Check if two piles of coins can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other | C ++ program for the above approach ; Function to check if two given piles can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other ; If maximum of A & B exceeds the twice of minimum of A & B ; Not possible to empty the piles ; If sum of both the coins is divisible by 3 , then print Yes ; Otherwise , print " No " ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canBeEmptied ( int A , int B ) { if ( max ( A , B ) > 2 * min ( A , B ) ) { cout << " No " ; return ; } if ( ( A + B ) % 3 == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int A = 1 , B = 2 ; canBeEmptied ( A , B ) ; return 0 ; }
Maximum index a pointer can reach in N steps by avoiding a given index B | Set 2 | C ++ program for the above approach ; Function to find the maximum index the pointer can reach ; Initialize two pointers ; Stores number of steps ; Stores sum of first N natural numbers ; Increment i with j ; Increment j with 1 ; Increment count ; If i points to B ; Break ; Print the pointer index ; Driver Code ; Given value of N & B ; Function call to find maximum index the pointer can reach
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumIndex ( int N , int B ) { int i = 0 , j = 1 ; int cnt = 0 ; int sum = N * ( N + 1 ) / 2 ; bool flag = false ; while ( cnt < N ) { i += j ; j ++ ; cnt ++ ; if ( i == B ) { flag = true ; break ; } } if ( ! flag ) { cout << sum ; } else cout << sum - 1 ; } int main ( ) { int N = 4 , B = 6 ; maximumIndex ( N , B ) ; return 0 ; }
Generate longest possible array with product K such that each array element is divisible by its previous adjacent element | C ++ program for the above approach ; Function to construct longest array with product K such that each element is divisible by its previous element ; Stores the prime factors of K ; Stores the power to which primefactor i is raised ; Sort prime factors in descending order ; Stores the final array ; Multiply the last element by K ; Print the constructed array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLongestArray ( int K ) { vector < pair < int , int > > primefactors ; int K_temp = K ; for ( int i = 2 ; i * i <= K ; i ++ ) { int count = 0 ; while ( K_temp % i == 0 ) { K_temp /= i ; count ++ ; } if ( count > 0 ) primefactors . push_back ( { count , i } ) ; } if ( K_temp != 1 ) primefactors . push_back ( { 1 , K_temp } ) ; sort ( primefactors . rbegin ( ) , primefactors . rend ( ) ) ; vector < int > answer ( primefactors [ 0 ] . first , primefactors [ 0 ] . second ) ; answer . back ( ) *= K ; for ( int i = 0 ; i < primefactors [ 0 ] . first ; i ++ ) { answer . back ( ) /= primefactors [ 0 ] . second ; } cout << " { " ; for ( int i = 0 ; i < ( int ) answer . size ( ) ; i ++ ) { if ( i == answer . size ( ) - 1 ) cout << answer [ i ] << " } " ; else cout << answer [ i ] << " , ▁ " ; } } int main ( ) { int K = 4 ; findLongestArray ( K ) ; }
Minimum number of steps required to place all 1 s at a single index | C ++ implementation of the above approach ; Function to print minimum steps required to shift all 1 s to a single index in a binary array ; Size of array ; Used to store cumulative sum ; Initialize count ; Traverse the array in forward direction ; Steps needed to store all previous ones to ith index ; Count number of 1 s present till i - th index ; Initialize count ; Traverse the array in backward direction ; Steps needed to store all 1 s to the right of i at current index ; Count number of 1 s present after i - th index ; Print the number of steps required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minsteps ( vector < int > & A ) { int n = A . size ( ) ; vector < int > left ( n , 0 ) , right ( n , 0 ) , res ( n , 0 ) ; int count = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { left [ i ] = left [ i - 1 ] + count ; count += A [ i ] ; } count = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] + count ; count += A [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { res [ i ] = left [ i ] + right [ i ] ; cout << res [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; } int main ( ) { vector < int > A = { 1 , 0 , 1 , 0 } ; minsteps ( A ) ; }
Minimize flips on K | C ++ program for the above approach ; Function to find the minimum number K - length subarrays required to be flipped to make all array elements 1 ; Stores whether an element can be flipped or not ; Store the required number of flips ; Traverse the array , A [ ] ; Find the prefix sum for the indices i > 0 ; Check if the current element is required to be flipped ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumOperations ( vector < int > & A , int K ) { vector < int > isflipped ( A . size ( ) , 0 ) ; int ans = 0 ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { if ( i > 0 ) { isflipped [ i ] += isflipped [ i - 1 ] ; isflipped [ i ] %= 2 ; } if ( A [ i ] == 0 && ! isflipped [ i ] ) { if ( ( A . size ( ) - i + 1 ) <= K ) { cout << -1 ; return ; } ans ++ ; isflipped [ i ] ++ ; isflipped [ i + K ] -- ; } else if ( A [ i ] == 1 && isflipped [ i ] ) { if ( ( A . size ( ) - i + 1 ) <= K ) { cout << -1 ; return ; } ans ++ ; isflipped [ i ] ++ ; isflipped [ i + K ] -- ; } } cout << ans ; } int main ( ) { vector < int > arr = { 0 , 1 , 0 } ; int K = 1 ; minimumOperations ( arr , K ) ; return 0 ; }
Calculate sum of scores after N days based on given conditions | C ++ program for the above approach ; Function to c sum of calculate sum of scores after n days ; Store the required sum ; Store the score on previous monday and current day respectively ; Iterate over the range [ 1 , n ] ; If the current day is monday ; Increment score of prev_monday by 1 ; Update score of current day ; Add score of current day and increment score for next day ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findScoreSum ( int n ) { int total = 0 ; int prev_monday = 0 , curr_day = 0 ; for ( int day = 1 ; day <= n ; day ++ ) { if ( day % 7 == 1 ) { prev_monday ++ ; curr_day = prev_monday ; } total += curr_day ++ ; } cout << total ; } int main ( ) { int N = 8 ; findScoreSum ( N ) ; return 0 ; }
Calculate sum of scores after N days based on given conditions | C ++ program for the above approach ; Function to calculate sum of scores after n days ; Store the number of full weeks ; Stores the remaining days in the last week ; Store the sum of scores in the first F full weeks ; Store the sum of scores in the last week ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findScoreSum ( int n ) { int F = n / 7 ; int D = n % 7 ; int fullWeekScore = ( 49 + 7 * F ) * F / 2 ; int lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / 2 ; cout << fullWeekScore + lastNonFullWeekScore ; } int main ( ) { int N = 8 ; findScoreSum ( N ) ; return 0 ; }
Largest number up to N whose modulus with X is equal to Y modulo X | C ++ program for the above approach ; Function to print the largest number upto N whose modulus with X is same as Y % X ; Stores the required number ; Update num as the result ; Return the resultant number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long maximumNum ( long long X , long long Y , long long N ) { long long num = 0 ; if ( N - N % X + Y <= N ) { num = N - N % X + Y ; } else { num = N - N % X - ( X - Y ) ; } return num ; } int main ( ) { long long X = 10 ; long long Y = 5 ; long long N = 15 ; cout << maximumNum ( X , Y , N ) ; return 0 ; }
Generate an N | C ++ program for the above approach ; Function to calculate GCD of two integers ; Function to calculate GCD of a given array ; Utility function to check for all the possible combinations ; If an N - length sequence is obtained ; If GCD of the sequence is K ; Otherwise ; Add an element from the first array ; Recursively proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Add an element from the second array ; Recursive proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Function to check all the possible combinations ; Stores the subsequence ; If GCD is not equal to K for any combination ; Driver Code ; Given arrays ; Given value of K ; Function call to generate the required subsequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GCD ( int a , int b ) { if ( ! b ) return a ; return GCD ( b , a % b ) ; } int GCDArr ( vector < int > a ) { int ans = a [ 0 ] ; for ( int i : a ) ans = GCD ( ans , i ) ; return ans ; } bool findSubseqUtil ( vector < int > a , vector < int > b , vector < int > & ans , int k , int i ) { if ( ans . size ( ) == a . size ( ) ) { if ( GCDArr ( ans ) == k ) { cout << " [ " ; int m = ans . size ( ) ; for ( int i = 0 ; i < m - 1 ; i ++ ) cout << ans [ i ] << " , ▁ " ; cout << ans [ m - 1 ] << " ] " ; return true ; } else return false ; } ans . push_back ( a [ i ] ) ; bool temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . pop_back ( ) ; ans . push_back ( b [ i ] ) ; temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . pop_back ( ) ; return false ; } void findSubseq ( vector < int > A , vector < int > B , int K , int i ) { vector < int > ans ; findSubseqUtil ( A , B , ans , K , i ) ; if ( ! ans . size ( ) ) cout << -1 ; } int main ( ) { vector < int > A = { 5 , 3 , 6 , 2 , 9 } ; vector < int > B = { 21 , 7 , 14 , 12 , 28 } ; int K = 3 ; findSubseq ( A , B , K , 0 ) ; return 0 ; }