codeparrot/xlcost-text-to-code huggingface dataset:
Minimize remaining array element by removing pairs and replacing them by their absolute difference | C ++ program to implement the above approach ; Function to find minimize the remaining array element by removing pairs and replacing them by their absolute difference ; Stores sum of array elements ; Traverse the array ; Update totalSum ; Stores half of totalSum ; dp [ i ] : True if sum i can be obtained as a subset sum ; Base case ; Stores closest sum that can be obtained as a subset sum ; Traverse the array ; Iterate over all possible value of sum ; Update dp [ j ] ; If sum i can be obtained from array elements ; Update reach ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SmallestElementLeft ( int arr [ ] , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } int req = totalSum / 2 ; bool dp [ req + 1 ] ; memset ( dp , false , sizeof ( dp ) ) ; dp [ 0 ] = true ; int reach = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = req ; j - arr [ i ] >= 0 ; j -- ) { dp [ j ] = dp [ j ] || dp [ j - arr [ i ] ] ; if ( dp [ j ] ) { reach = max ( reach , j ) ; } } } return totalSum - ( 2 * reach ) ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << SmallestElementLeft ( arr , N ) ; return 0 ; }
Replace even | C ++ program to implement the above approach ; Function to count the minimum number of substrings of str1 such that replacing even - indexed characters of those substrings converts the string str1 to str2 ; Stores length of str1 ; Stores minimum count of operations to convert str1 to str2 ; Traverse both the given string ; If current character in both the strings are equal ; Stores current index of the string ; If current character in both the strings are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperationsReq ( string str1 , string str2 ) { int N = str1 . length ( ) ; int cntOp = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str1 [ i ] == str2 [ i ] ) { continue ; } int ptr = i ; while ( ptr < N && str1 [ ptr ] != str2 [ ptr ] ) { str1 [ ptr ] = str2 [ ptr ] ; ptr += 2 ; } cntOp ++ ; } return cntOp ; } int main ( ) { string str1 = " abcdef " ; string str2 = " ffffff " ; cout << minOperationsReq ( str1 , str2 ) ; return 0 ; }
Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | C ++ Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array a [ ] which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } void ifgcdFibonacci ( int a [ ] , int n , int k ) { vector < int > compositeset ; for ( int i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . push_back ( a [ i ] ) ; } } int gcd = compositeset [ 0 ] ; for ( int i = 1 ; i < compositeset . size ( ) ; i ++ ) { gcd = __gcd ( gcd , compositeset [ i ] ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { cout << " Yes " ; return ; } cout << " No " ; return ; } int main ( ) { int arr [ ] = { 34 , 2 , 4 , 8 , 5 , 7 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; ifgcdFibonacci ( arr , n , k ) ; return 0 ; }
Longest subarray in which all elements are a factor of K | C ++ program to implement the above approach ; Function to find the length of the longest subarray in which all elements are a factor of K ; Stores length of the longest subarray in which all elements are a factor of K ; Stores length of the current subarray ; Traverse the array arr [ ] ; If A [ i ] is a factor of K ; Update Len ; Update MaxLen ; If A [ i ] is not a factor of K ; Update Len ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_longest_subarray ( int A [ ] , int N , int K ) { int MaxLen = 0 ; int Len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( K % A [ i ] == 0 ) { Len ++ ; MaxLen = max ( MaxLen , Len ) ; } else { Len = 0 ; } } return MaxLen ; } int main ( ) { int A [ ] = { 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; ; int K = 60 ; cout << find_longest_subarray ( A , N , K ) ; return 0 ; }
Length of smallest meeting that can be attended | C ++ program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ ] [ ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; Driver Code ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minTime ( int meeting [ ] [ 2 ] , int n , vector < int > entrance , int m , vector < int > & exit , int p ) { int ans = INT_MAX ; sort ( entrance . begin ( ) , entrance . end ( ) ) ; sort ( exit . begin ( ) , exit . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int u = meeting [ i ] [ 0 ] ; int v = meeting [ i ] [ 1 ] ; auto it1 = upper_bound ( entrance . begin ( ) , entrance . end ( ) , u ) ; auto it2 = lower_bound ( exit . begin ( ) , exit . end ( ) , v ) ; int start = it1 - entrance . begin ( ) - 1 ; int end = it2 - exit . begin ( ) ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = min ( ans , exit [ end ] - entrance [ start ] ) ; } return ans >= INT_MAX ? -1 : ans ; } int main ( ) { int meeting [ ] [ 2 ] = { { 15 , 19 } , { 5 , 10 } , { 7 , 25 } } ; vector < int > entrance = { 4 , 13 , 25 , 2 } ; vector < int > exit = { 10 , 25 } ; int n = ( sizeof ( meeting ) ) / sizeof ( meeting [ 0 ] ) ; int m = entrance . size ( ) ; int p = exit . size ( ) ; cout << minTime ( meeting , n , entrance , m , exit , p ) << endl ; return 0 ; }
Divide array into two arrays which does not contain any pair with sum K | C ++ program for the above approach ; Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Driver Code ; Given K ; Given array ; Given size
#include <bits/stdc++.h> NEW_LINE using namespace std ; void splitArray ( int a [ ] , int n , int k ) { vector < int > first , second ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= k / 2 ) first . push_back ( a [ i ] ) ; else second . push_back ( a [ i ] ) ; } for ( int i = 0 ; i < first . size ( ) ; i ++ ) { cout << first [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; for ( int i = 0 ; i < second . size ( ) ; i ++ ) { cout << second [ i ] << " ▁ " ; } } int main ( ) { int k = 5 ; int a [ ] = { 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = sizeof ( a ) / sizeof ( int ) ; splitArray ( a , n , k ) ; return 0 ; }
Count numbers in a given range whose count of prime factors is a Prime Number | C ++ program to implement the above approach ; Function to find the smallest prime factor of all the numbers in range [ 0 , MAX ] ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; No smallest prime factor of 0 and 1 exists ; Traverse all the numbers in the range [ 1 , MAX ] ; Update spf [ i ] ; Update all the numbers whose smallest prime factor is 2 ; Traverse all the numbers in the range [ 1 , sqrt ( MAX ) ] ; Check if i is a prime number ; Update all the numbers whose smallest prime factor is i ; Check if j is a prime number ; Function to find count of prime factor of num ; Stores count of prime factor of num ; Calculate count of prime factor ; Update count ; Update num ; Function to precalculate the count of numbers in the range [ 0 , i ] whose count of prime factors is a prime number ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; Update sum [ 0 ] ; Traverse all the numbers in the range [ 1 , MAX ] ; Stores count of prime factor of i ; If count of prime factor is a prime number ; Update sum [ i ] ; Update sum [ i ] ; Driver Code ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; int N = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ;
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1001 NEW_LINE vector < int > sieve ( ) { vector < int > spf ( MAX ) ; spf [ 0 ] = spf [ 1 ] = -1 ; for ( int i = 2 ; i < MAX ; i ++ ) { spf [ i ] = i ; } for ( int i = 4 ; i < MAX ; i = i + 2 ) { spf [ i ] = 2 ; } for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) { spf [ j ] = i ; } } } } return spf ; } int countFactors ( vector < int > & spf , int num ) { int count = 0 ; while ( num > 1 ) { count ++ ; num = num / spf [ num ] ; } return count ; } vector < int > precalculateSum ( vector < int > & spf ) { vector < int > sum ( MAX ) ; sum [ 0 ] = 0 ; for ( int i = 1 ; i < MAX ; i ++ ) { int prime_factor = countFactors ( spf , i ) ; if ( spf [ prime_factor ] == prime_factor ) { sum [ i ] = sum [ i - 1 ] + 1 ; } else { sum [ i ] = sum [ i - 1 ] ; } } return sum ; } int main ( ) { vector < int > spf = sieve ( ) ; vector < int > sum = precalculateSum ( spf ) ; int Q [ ] [ 2 ] = { { 4 , 8 } , { 30 , 32 } } ; for ( int i = 0 ; i < 2 ; i ++ ) { cout << ( sum [ Q [ i ] [ 1 ] ] - sum [ Q [ i ] [ 0 ] - 1 ] ) << " ▁ " ; } return 0 ; }
Maximum number of Perfect Numbers present in a subarray of size K | C ++ program for the above approach ; Function to check a number is Perfect Number or not ; Stores sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to N ; Function to return maximum sum of a subarray of size K ; If k is greater than N ; Compute sum of first window of size K ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window ; return the answer ; Function to find all the perfect numbers in the array ; The given array is converted into binary array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPerfect ( int N ) { int sum = 1 ; for ( int i = 2 ; i < sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { sum += i ; } else { sum += i + N / i ; } } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } int maxSum ( int arr [ ] , int N , int K ) { if ( N < K ) { cout << " Invalid " ; return -1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = max ( res , curr_sum ) ; } return res ; } int max_PerfectNumbers ( int arr [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isPerfect ( arr [ i ] ) ? 1 : 0 ; } return maxSum ( arr , N , K ) ; } int main ( ) { int arr [ ] = { 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 } ; int K = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max_PerfectNumbers ( arr , N , K ) ; return 0 ; }
Count greater elements on the left side of every array element | C ++ program to implement the above approach ; Function to print the count of greater elements on left of each array element ; Function to get the count of greater elements on left of each array element ; Store distinct array elements in sorted order ; Stores the count of greater elements on the left side ; Traverse the array ; Insert array elements into the set ; Find previous greater element ; Find the distance between the previous greater element of arr [ i ] and last element of the set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void display ( int countLeftGreater [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << countLeftGreater [ i ] << " ▁ " ; } } void countGreater ( int arr [ ] , int N ) { set < int > St ; int countLeftGreater [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { St . insert ( arr [ i ] ) ; auto it = St . upper_bound ( arr [ i ] ) ; countLeftGreater [ i ] = distance ( it , St . end ( ) ) ; } display ( countLeftGreater , N ) ; } int main ( ) { int arr [ ] = { 12 , 1 , 2 , 3 , 0 , 11 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countGreater ( arr , N ) ; }
Count characters to be shifted from the start or end of a string to obtain another string | C ++ 14 program for the above approach ; Function to find the minimum cost to convert string A to string B ; Length of string ; Initialize maxlen as 0 ; Traverse the string A ; Stores the length of substrings of string A ; Traversing string B for each character of A ; Shift i pointer towards right and increment length , if A [ i ] equals B [ j ] ; If traverse till end ; Update maxlen ; Return minimum cost ; Driver Code ; Given two strings A and B ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( string A , string B ) { int n = A . size ( ) ; int i = 0 ; int maxlen = 0 ; while ( i < n ) { int length = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( A [ i ] == B [ j ] ) { ++ i ; ++ length ; if ( i == n ) break ; } } maxlen = max ( maxlen , length ) ; } return n - maxlen ; } int main ( ) { string A = " edacb " ; string B = " abcde " ; cout << minCost ( A , B ) << endl ; }
Lexicographic rank of a string among all its substrings | C ++ program for the above approach ; Function to find lexicographic rank of string among all its substring ; Length of string ; Traverse the given string and store the indices of each character ; Extract the index ; Store it in the vector ; Traverse the alphaIndex array lesser than the index of first character of given string ; If alphaIndex [ i ] size exceeds 0 ; Traverse over the indices ; Add count of substring equal to n - alphaIndex [ i ] [ j ] ; Store all substrings in a vector str starting with the first character of the given string ; Insert the current character to substring ; Store the substring formed ; Sort the substring in the lexicographical order ; Find the rank of given string ; increase the rank until the given string is same ; If substring is same as the given string ; Add 1 to rank of the given string ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lexicographicRank ( string s ) { int n = s . length ( ) ; vector < int > alphaIndex [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int x = s [ i ] - ' a ' ; alphaIndex [ x ] . push_back ( i ) ; } int rank = 0 ; for ( int i = 0 ; i < 26 && ' a ' + i < s [ 0 ] ; i ++ ) { if ( alphaIndex [ i ] . size ( ) > 0 ) { for ( int j = 0 ; j < alphaIndex [ i ] . size ( ) ; j ++ ) { rank = rank + ( n - alphaIndex [ i ] [ j ] ) ; } } } vector < string > str ; for ( int i = 0 ; i < alphaIndex [ s [ 0 ] - ' a ' ] . size ( ) ; i ++ ) { string substring ; int j = alphaIndex [ s [ 0 ] - ' a ' ] [ i ] ; for ( ; j < n ; j ++ ) { substring . push_back ( s [ j ] ) ; str . push_back ( substring ) ; } } sort ( str . begin ( ) , str . end ( ) ) ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( str [ i ] != s ) { rank ++ ; } else { break ; } } return rank + 1 ; } int main ( ) { string str = " enren " ; cout << lexicographicRank ( str ) ; return 0 ; }
Find H | C ++ implementation of the above approach ; Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Print the h - index ; Driver Code ; citations
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hIndex ( vector < int > citations , int n ) { int hindex = 0 ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( citations [ mid ] >= ( mid + 1 ) ) { low = mid + 1 ; hindex = mid + 1 ; } else { high = mid - 1 ; } } cout << hindex << endl ; return hindex ; } int main ( ) { int n = 5 ; vector < int > citations = { 5 , 3 , 3 , 2 , 2 } ; hIndex ( citations , n ) ; }
Check if all strings of an array can be made same by interchanging characters | C ++ Program to implement the above approach ; Function to check if all strings are equal after swap operations ; Stores the frequency of characters ; Stores the length of string ; Traverse the array ; Traverse each string ; Check if frequency of character is divisible by N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEqual ( string arr [ ] , int N ) { int hash [ 256 ] = { 0 } ; int M = arr [ 0 ] . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] ] ++ ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % N != 0 ) { return false ; } } return true ; } int main ( ) { string arr [ ] = { " fdd " , " fhh " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkEqual ( arr , N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Maximize sum of remaining elements after every removal of the array half with greater sum | C ++ program to implement the above approach ; Function to find the maximum sum ; Base Case ; Check if ( mapped key is found in the dictionary ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right values ; Store the value in dp array ; Return the final answer ; Function to print maximum sum ; Stores prefix sum ; Store results of subproblems ; Traversing the array ; Add prefix sum of array ; Print the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 100 ] ; int maxweight ( int s , int e , map < int , int > pre ) { if ( s == e ) return 0 ; if ( dp [ s ] [ e ] != -1 ) return dp [ s ] [ e ] ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre [ i ] - pre [ s - 1 ] ; int right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = max ( ans , ( int ) ( left + maxweight ( s , i , pre ) ) ) ; if ( left == right ) ans = max ( ans , max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; if ( left > right ) ans = max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; dp [ s ] [ e ] = ans ; } return dp [ s ] [ e ] ; } void maxSum ( int arr [ ] , int n ) { map < int , int > pre ; pre [ -1 ] = 0 ; pre [ 0 ] = arr [ 0 ] ; memset ( dp , -1 , sizeof dp ) ; for ( int i = 0 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + arr [ i ] ; cout << ( maxweight ( 0 , n - 1 , pre ) ) ; } int main ( ) { int arr [ ] = { 6 , 2 , 3 , 4 , 5 , 5 } ; maxSum ( arr , 6 ) ; }
Find K smallest leaf nodes from a given Binary Tree | C ++ program of the above approach ; Structure of binary tree node ; Function to create new node ; Utility function which calculates smallest three nodes of all leaf nodes ; Check if current root is a leaf node ; Traverse the left and right subtree ; Function to find the K smallest nodes of the Binary Tree ; Sorting the Leaf nodes array ; Loop to print the K smallest Leaf nodes of the array ; Driver Code ; Construct binary tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void storeLeaf ( Node * root , vector < int > & arr ) { if ( ! root ) return ; if ( ! root -> left and ! root -> right ) { arr . push_back ( root -> data ) ; return ; } storeLeaf ( root -> left , arr ) ; storeLeaf ( root -> right , arr ) ; } void KSmallest ( Node * root , int k ) { vector < int > arr ; storeLeaf ( root , arr ) ; sort ( arr . begin ( ) , arr . end ( ) ) ; for ( int i = 0 ; i < k ; i ++ ) { if ( i < arr . size ( ) ) { cout << arr [ i ] << " ▁ " ; } else { break ; } } } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> left -> left = newNode ( 21 ) ; root -> left -> right = newNode ( 5 ) ; root -> left -> right -> right = newNode ( 8 ) ; root -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> right -> right = newNode ( 19 ) ; KSmallest ( root , 3 ) ; return 0 ; }
Check if all the pairs of an array are coprime with each other | C ++ implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ;  ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool allCoprime ( int A [ ] , int n ) { bool all_coprime = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( __gcd ( A [ i ] , A [ j ] ) != 1 ) { all_coprime = false ; break ; } } } return all_coprime ; } ' ' Function return gcd of two number ' ' ' int main ( ) { int A [ ] = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( allCoprime ( A , arr_size ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Smallest power of 2 consisting of N digits | C ++ Program of the above approach ; Function to return smallest power of 2 consisting of N digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestNum ( int n ) { float power = log2 ( 10 ) ; cout << power ; return ceil ( ( n - 1 ) * power ) ; } int main ( ) { int n = 4 ; cout << smallestNum ( n ) ; return 0 ; }
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence | C ++ program for the above approach ; Function to find the subsequence of length K forming valid sequence ; Stores the resultant string ; Check whether character at index i is visited or not ; Traverse the string ; Push index of open bracket ; Pop and mark visited ; Increment count by 2 ; Append the characters and create the resultant string ; Return the resultant string ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; string findString ( string s , int k ) { int n = s . length ( ) ; string ans = " " ; stack < int > st ; vector < bool > vis ( n , false ) ; int count = 0 ; Vector < boolean > vis ( n , false ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == ' ( ' ) { st . push ( i ) ; } if ( count < k && s [ i ] == ' ) ' ) { vis [ st . top ( ) ] = 1 ; st . pop ( ) ; vis [ i ] = true ; count += 2 ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == true ) { ans += s [ i ] ; } } return ans ; } int main ( ) { string s = " ( ) ( ) ( ) " ; int K = 2 ; cout << findString ( s , K ) ; }