codeparrot/xlcost-text-to-code huggingface dataset:
Find GCD of each subtree of a given node in an N | C ++ program to find GCD of each subtree for a given node by Q queries ; Maximum Number of nodes ; Tree represented as adjacency list ; for storing value associates with node ; for storing GCD of every subarray ; number of nodes ; Function to find GCD of two numbers using Euclidean algo ; if b == 0 then simply return a ; DFS function to traverse the tree ; initializing answer with GCD of this node . ; iterate over each child of current node ; skipping the parent ; call DFS for each child ; taking GCD of the answer of the child to find node 's GCD ; Calling DFS from the root ( 1 ) for precomputing answers ; Function to find and print GCD for Q queries ; doing preprocessing ; iterate over each given query ; Driver code ; Tree : 1 ( 2 ) / \ 2 ( 3 ) 3 ( 4 ) / \ 4 ( 8 ) 5 ( 16 ) ; making a undirected tree ; values associated with nodes ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1e5 + 5 ; vector < vector < int > > v ( N ) ; vector < int > val ( N ) ; vector < int > answer ( N ) ; int n ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void DFS ( int node , int parent ) { answer [ node ] = val [ node ] ; for ( int child : v [ node ] ) { if ( child == parent ) continue ; DFS ( child , node ) ; answer [ node ] = gcd ( answer [ node ] , answer [ child ] ) ; } } void preprocess ( ) { DFS ( 1 , -1 ) ; } void findGCD ( int queries [ ] , int q ) { preprocess ( ) ; for ( int i = 0 ; i < q ; i ++ ) { int GCD = answer [ queries [ i ] ] ; cout << " For ▁ subtree ▁ of ▁ " << queries [ i ] << " , ▁ GCD ▁ = ▁ " << GCD << endl ; } } int main ( ) { n = 5 ; v [ 1 ] . push_back ( 2 ) ; v [ 2 ] . push_back ( 1 ) ; v [ 1 ] . push_back ( 3 ) ; v [ 3 ] . push_back ( 1 ) ; v [ 3 ] . push_back ( 4 ) ; v [ 4 ] . push_back ( 3 ) ; v [ 3 ] . push_back ( 5 ) ; v [ 5 ] . push_back ( 3 ) ; val [ 1 ] = 2 ; val [ 2 ] = 3 ; val [ 3 ] = 4 ; val [ 4 ] = 8 ; val [ 5 ] = 16 ; int queries [ ] = { 2 , 3 , 1 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; findGCD ( queries , q ) ; return 0 ; }
Minimize cost to convert given two integers to zero using given operations | C ++ implementation to find the minimum cost to make the two integers equal to zero using given operations ; Function to find out the minimum cost to make two number X and Y equal to zero ; If x is greater than y then swap ; Cost of making y equal to x ; Cost if we choose 1 st operation ; Cost if we choose 2 nd operation ; Total cost ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int makeZero ( int x , int y , int a , int b ) { if ( x > y ) x = y , y = x ; int tot_cost = ( y - x ) * a ; int cost1 = 2 * x * a ; int cost2 = x * b ; tot_cost += min ( cost1 , cost2 ) ; cout << tot_cost ; } int main ( ) { int X = 1 , Y = 3 ; int cost1 = 391 , cost2 = 555 ; makeZero ( X , Y , cost1 , cost2 ) ; }
Find N fractions that sum upto a given fraction N / D | C ++ implementation to split the fraction into N parts ; Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void splitFraction ( int n , int d ) { int ar [ n ] ; int first = d + n - 1 ; ar [ 0 ] = first ; for ( int i = 1 ; i < n ; i ++ ) { int temp = -- first ; first ++ ; ar [ i ] = first * temp ; -- first ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ar [ i ] % n == 0 ) { cout << "1 / " << ar [ i ] / n << " , ▁ " ; } else { cout << n << " / " << ar [ i ] << " , ▁ " ; } } } int main ( ) { int N = 4 ; int D = 2 ; splitFraction ( N , D ) ; }
Count of pairs ( i , j ) in the array such that arr [ i ] is a factor of arr [ j ] | C ++ Program to find the number of pairs ( i , j ) such that arr [ i ] is a factor of arr [ j ] ; Function to return the count of Pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) ans ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numPairs ( arr , n ) << endl ; return 0 ; }
Check if array can be converted into strictly decreasing sequence | C ++ implementation to check that array can be converted into a strictly decreasing sequence ; Function to check that array can be converted into a strictly decreasing sequence ; Loop to check that each element is greater than the ( N - index ) ; If element is less than ( N - index ) ; If array can be converted ; Driver Code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int * arr , int n ) { bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < n - i ) { flag = false ; } } if ( flag ) { return true ; } else { return false ; } } int main ( ) { int arr1 [ ] = { 11 , 11 , 11 , 11 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; if ( check ( arr1 , n1 ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } }
Maximum Balanced String Partitions | C ++ program to find a maximum number X , such that a given string can be partitioned into X substrings that are each balanced ; Function to find a maximum number X , such that a given string can be partitioned into X substrings that are each balanced ; If the size of the string is 0 , then answer is zero ; variable that represents the number of ' R ' s and ' L ' s ; To store maximum number of possible partitions ; increment the variable r if the character in the string is ' R ' ; increment the variable l if the character in the string is ' L ' ; if r and l are equal , then increment ans ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int BalancedPartition ( string str , int n ) { if ( n == 0 ) return 0 ; int r = 0 , l = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' R ' ) { r ++ ; } else if ( str [ i ] = ' L ' ) { l ++ ; } if ( r == l ) { ans ++ ; } } return ans ; } int main ( ) { string str = " LLRRRLLRRL " ; int n = str . size ( ) ; cout << BalancedPartition ( str , n ) << endl ; return 0 ; }
Minimize the non | C ++ implementation to minimize the non - zero elements in the array ; Function to minimize the non - zero elements in the given array ; To store the min pos needed ; Loop to iterate over the elements of the given array ; If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOccupiedPosition ( int A [ ] , int n ) { int minPos = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] > 0 ) { ++ minPos ; i += 2 ; } } return minPos ; } int main ( ) { int A [ ] = { 8 , 0 , 7 , 0 , 0 , 6 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minOccupiedPosition ( A , n ) ; return 0 ; }
Find minimum number K such that sum of array after multiplication by K exceed S | C ++ implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; store sum of array elements ; Calculate the sum after ; return minimum possible K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimumK ( int a [ ] , int n , int S ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return ceil ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) ; } int main ( ) { int a [ ] = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int S = 200 ; cout << findMinimumK ( a , n , S ) ; return 0 ; }
Find the largest number smaller than integer N with maximum number of set bits | C ++ implementation to Find the largest number smaller than integer N with maximum number of set bits ; Function to return the largest number less than N ; Iterate through all possible values ; Multiply the number by 2 i times ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestNum ( int n ) { int num = 0 ; for ( int i = 0 ; i <= 32 ; i ++ ) { int x = ( 1 << i ) ; if ( ( x - 1 ) <= n ) num = ( 1 << i ) - 1 ; else break ; } return num ; } int main ( ) { int N = 345 ; cout << largestNum ( N ) ; return 0 ; }
Find the String having each substring with exactly K distinct characters | C ++ Program to Find the String having each substring with exactly K distinct characters ; Function to find the required output string ; Each element at index i is modulus of K ; Driver code ; initialise integers N and K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findString ( int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { cout << char ( ' A ' + i % K ) ; } } int main ( ) { int N = 10 ; int K = 3 ; findString ( N , K ) ; return 0 ; }
Find total no of collisions taking place between the balls in which initial direction of each ball is given | C ++ implementation to Find total no of collisions taking place between the balls in which initial direction of each ball is given ; Function to count no of collision ; length of the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string s ) { int N , i , cnt = 0 , ans = 0 ; N = s . length ( ) ; for ( i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' R ' ) cnt ++ ; if ( s [ i ] == ' L ' ) ans += cnt ; } return ans ; } int main ( ) { string s = " RRLL " ; cout << count ( s ) << endl ; return 0 ; }
Maximum number on 7 | C ++ implementation to find the maximum number that can be using the N segments in N segments display ; Function to find the maximum number that can be displayed using the N segments ; Condition to check base case ; Condition to check if the number is even ; Condition to check if the number is odd ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void segments ( int n ) { if ( n == 1 n == 0 ) { return ; } if ( n % 2 == 0 ) { cout << "1" ; segments ( n - 2 ) ; } else if ( n % 2 == 1 ) { cout << "7" ; segments ( n - 3 ) ; } } int main ( ) { int n ; n = 11 ; segments ( n ) ; return 0 ; }
Minimum number of subsequences required to convert one string to another using Greedy Algorithm | C ++ implementation for minimum number of subsequences required to convert one string to another ; Function to find the minimum number of subsequences required to convert one string to another S2 == A and S1 == B ; At least 1 subsequence is required Even in best case , when A is same as B ; size of B ; size of A ; Create an 2D array next [ ] [ ] of size 26 * sizeOfB to store the next occurrence of a character ( ' a ' to ' z ' ) as an index [ 0 , sizeOfA - 1 ] ; Array Initialization with infinite ; Loop to Store the values of index ; If the value of next [ i ] [ j ] is infinite then update it with next [ i ] [ j + 1 ] ; Greedy algorithm to obtain the maximum possible subsequence of B to cover the remaining string of A using next subsequence ; Loop to iterate over the string A ; Condition to check if the character is not present in the string B ; Condition to check if there is an element in B matching with character A [ i ] on or next to B [ pos ] given by next [ A [ i ] - ' a ' ] [ pos ] ; Condition to check if reached at the end of B or no such element exists on or next to A [ pos ] , thus increment number by one and reinitialise pos to zero ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findMinimumSubsequences ( string A , string B ) { int numberOfSubsequences = 1 ; int sizeOfB = B . size ( ) ; int sizeOfA = A . size ( ) ; int inf = 1000000 ; int next [ 26 ] [ sizeOfB ] ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < sizeOfB ; j ++ ) { next [ i ] [ j ] = inf ; } } for ( int i = 0 ; i < sizeOfB ; i ++ ) { next [ B [ i ] - ' a ' ] [ i ] = i ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = sizeOfB - 2 ; j >= 0 ; j -- ) { if ( next [ i ] [ j ] == inf ) { next [ i ] [ j ] = next [ i ] [ j + 1 ] ; } } } int pos = 0 ; int i = 0 ; while ( i < sizeOfA ) { if ( pos == 0 && next [ A [ i ] - ' a ' ] [ pos ] == inf ) { numberOfSubsequences = -1 ; break ; } else if ( pos < sizeOfB && next [ A [ i ] - ' a ' ] [ pos ] < inf ) { int nextIndex = next [ A [ i ] - ' a ' ] [ pos ] + 1 ; pos = nextIndex ; i ++ ; } else { numberOfSubsequences ++ ; pos = 0 ; } } return numberOfSubsequences ; } int main ( ) { string A = " aacbe " ; string B = " aceab " ; cout << findMinimumSubsequences ( A , B ) ; return 0 ; }
Vertical and Horizontal retrieval ( MRT ) on Tapes | C ++ program to print Horizontal filling ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; vector is used because n number of records can insert in one tape with size constraint ; Null vector obtained to use fresh vector ' v ' ; initialize variables to 0 for each iteration ; sum is used for checking whether i 'th tape is full or not ; check sum less than size of tape ; increment in j for next record ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . size ( ) is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
#include <bits/stdc++.h> NEW_LINE using namespace std ; void horizontalFill ( int records [ ] , int tape [ ] , int nt ) { int sum = 0 ; int Retrieval_Time = 0 ; double Mrt ; int current = 0 ; vector < int > v ; for ( int i = 0 ; i < nt ; i ++ ) { v . clear ( ) ; Retrieval_Time = 0 ; sum = 0 ; cout << " tape ▁ " << i + 1 << " ▁ : ▁ [ ▁ " ; sum += records [ current ] ; while ( sum <= tape [ i ] ) { cout << records [ current ] << " ▁ " ; v . push_back ( records [ current ] ) ; current ++ ; sum += records [ current ] ; } cout << " ] " ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { Retrieval_Time += v [ i ] * ( v . size ( ) - i ) ; } Mrt = ( double ) Retrieval_Time / v . size ( ) ; cout << " TABSYMBOL MRT ▁ : ▁ " << Mrt << endl ; } } int main ( ) { int records [ ] = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 } ; int tape [ ] = { 25 , 80 , 160 } ; int n = sizeof ( records ) / sizeof ( records [ 0 ] ) ; int m = sizeof ( tape ) / sizeof ( tape [ 0 ] ) ; sort ( records , records + n ) ; horizontalFill ( records , tape , m ) ; }
Circular Convolution using Matrix Method | C ++ program to compute circular convolution of two arrays ; Function to find circular convolution ; Finding the maximum size between the two input sequence sizes ; Copying elements of x to row_vec and padding zeros if size of x < maxSize ; Copying elements of h to col_vec and padding zeros if size of h is less than maxSize ; Generating 2D matrix of circularly shifted elements ; Computing result by matrix multiplication and printing results ; Driver program
#include <iostream> NEW_LINE using namespace std ; #define MAX_SIZE  10 NEW_LINE void convolution ( int * x , int * h , int n , int m ) { int row_vec [ MAX_SIZE ] , col_vec [ MAX_SIZE ] ; int out [ MAX_SIZE ] = { 0 } ; int circular_shift_mat [ MAX_SIZE ] [ MAX_SIZE ] ; int maxSize = n > m ? n : m ; for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } int k = 0 , d = 0 ; for ( int i = 0 ; i < maxSize ; i ++ ) { int curIndex = k - d ; for ( int j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( int i = 0 ; i < maxSize ; i ++ ) { for ( int j = 0 ; j < maxSize ; j ++ ) { out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; } cout << out [ i ] << " ▁ " ; } } int main ( ) { int x [ ] = { 5 , 7 , 3 , 2 } ; int n = sizeof ( x ) / sizeof ( int ) ; int h [ ] = { 1 , 5 } ; int m = sizeof ( h ) / sizeof ( int ) ; convolution ( x , h , n , m ) ; return 0 ; }
Maximize the number of palindromic Strings | C ++ program for the above approach ; To check if there is any string of odd length ; If there is at least 1 string of odd length . ; If all the strings are of even length . ; Count of 0 's in all  the strings ; Count of 1 's in  all the strings ; If z is even and o is even then ans will be N . ; Otherwise ans will be N - 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_palindrome ( string s [ ] , int n ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] . size ( ) % 2 != 0 ) { flag = 1 ; } } if ( flag == 1 ) { return n ; } int z = 0 , o = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < s [ i ] . size ( ) ; j ++ ) { if ( s [ i ] [ j ] == '0' ) z ++ ; else o ++ ; } } if ( o % 2 == 0 && z % 2 == 0 ) { return n ; } else { return n - 1 ; } } int main ( ) { int n = 3 ; string s [ n ] = { "1110" , "100110" , "010101" } ; cout << max_palindrome ( s , n ) ; return 0 ; }
Minimum possible travel cost among N cities | C ++ implementation of the approach ; Function to return the minimum cost to travel from the first city to the last ; To store the total cost ; Start from the first city ; If found any city with cost less than that of the previous boarded bus then change the bus ; Calculate the cost to travel from the currently boarded bus till the current city ; Update the currently boarded bus ; Finally calculate the cost for the last boarding bus till the ( N + 1 ) th city ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( vector < int > & cost , int n ) { int totalCost = 0 ; int boardingBus = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( cost [ boardingBus ] > cost [ i ] ) { totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) ; boardingBus = i ; } } totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) ; return totalCost ; } int main ( ) { vector < int > cost { 4 , 7 , 8 , 3 , 4 } ; int n = cost . size ( ) ; cout << minCost ( cost , n ) ; return 0 ; }
Minimum cells to be flipped to get a 2 * 2 submatrix with equal elements | C ++ implementation of the approach ; Function to return the minimum flips required such that the submatrix from mat [ i ] [ j ] to mat [ i + 1 ] [ j + 1 ] contains all equal elements ; Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements ; To store the result ; For every submatrix of size 2 * 2 ; Update the count of flips required for the current submatrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minFlipsSub ( string mat [ ] , int i , int j ) { int cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] [ j ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] [ j + 1 ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] [ j ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] [ j + 1 ] == '1' ) cnt1 ++ ; else cnt0 ++ ; return min ( cnt0 , cnt1 ) ; } int minFlips ( string mat [ ] , int r , int c ) { int res = INT_MAX ; for ( int i = 0 ; i < r - 1 ; i ++ ) { for ( int j = 0 ; j < c - 1 ; j ++ ) { res = min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } int main ( ) { string mat [ ] = { "0101" , "0101" , "0101" } ; int r = sizeof ( mat ) / sizeof ( string ) ; int c = mat [ 0 ] . length ( ) ; cout << minFlips ( mat , r , c ) ; return 0 ; }
Count set bits in the Kth number after segregating even and odd from N natural numbers | C ++ implementation of the approach ; Function to return the kth element of the Odd - Even sequence of length n ; Finding the index from where the even numbers will be stored ; Return the kth element ; Function to return the count of set bits in the kth number of the odd even sequence of length n ; Required kth number ; Return the count of set bits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findK ( int n , int k ) { int pos ; if ( n % 2 == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } int countSetBits ( int n , int k ) { int kth = findK ( n , k ) ; return __builtin_popcount ( kth ) ; } int main ( ) { int n = 18 , k = 12 ; cout << countSetBits ( n , k ) ; return 0 ; }
Minimum cost to convert str1 to str2 with the given operations | C ++ implementation of the approach ; Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( string str1 , string str2 , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { swap ( str1 [ i ] , str1 [ i + 1 ] ) ; cost ++ ; } else { cost ++ ; } } } return cost ; } int main ( ) { string str1 = " abb " , str2 = " bba " ; int n = str1 . length ( ) ; cout << minCost ( str1 , str2 , n ) ; return 0 ; }
Partition first N natural number into two sets such that their sum is not coprime | C ++ implementation of the approach ; Function to find the required sets ; Impossible case ; Sum of first n - 1 natural numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_set ( int n ) { if ( n <= 2 ) { cout << " - 1" ; return ; } int sum1 = ( n * ( n - 1 ) ) / 2 ; int sum2 = n ; cout << sum1 << " ▁ " << sum2 ; } int main ( ) { int n = 8 ; find_set ( n ) ; return 0 ; }
Count common elements in two arrays containing multiples of N and M | C ++ implementation of the above approach ; Recursive function to find gcd using euclidean algorithm ; Function to find lcm of two numbers using gcd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int lcm ( int n , int m ) { return ( n * m ) / gcd ( n , m ) ; } int main ( ) { int n = 2 , m = 3 , k = 5 ; cout << k / lcm ( n , m ) << endl ; return 0 ; }
Check whether a subsequence exists with sum equal to k if arr [ i ] > 2 * arr [ i | C ++ implementation of above approach ; Function to check whether sum of any set of the array element is equal to k or not ; Traverse the array from end to start ; if k is greater than arr [ i ] then subtract it from k ; If there is any subsequence whose sum is equal to k ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool CheckForSequence ( int arr [ ] , int n , int k ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( k >= arr [ i ] ) k -= arr [ i ] ; } if ( k != 0 ) return false ; else return true ; } int main ( ) { int A [ ] = { 1 , 3 , 7 , 15 , 31 } ; int n = sizeof ( A ) / sizeof ( int ) ; cout << ( CheckForSequence ( A , n , 18 ) ? " True " : " False " ) << endl ; return 0 ; }