codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum possible size of array with given rules for removing elements | C ++ program to find size of minimum possible array after removing elements according to given rules ; dp [ i ] [ j ] denotes the minimum number of elements left in the subarray arr [ i . . j ] . ; If already evaluated ; If size of array is less than 3 ; Initialize result as the case when first element is separated ( not removed using given rules ) ; Now consider all cases when first element forms a triplet and removed . Check for all possible triplets ( low , i , j ) ; Check if this triplet follows the given rules of removal . And elements between ' low ' and ' i ' , and between ' i ' and ' j ' can be recursively removed . ; Insert value in table and return result ; This function mainly initializes dp table and calls recursive function minSizeRec ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE int dp [ MAX ] [ MAX ] ; int minSizeRec ( int arr [ ] , int low , int high , int k ) { if ( dp [ low ] [ high ] != -1 ) return dp [ low ] [ high ] ; if ( ( high - low + 1 ) < 3 ) return high - low + 1 ; int res = 1 + minSizeRec ( arr , low + 1 , high , k ) ; for ( int i = low + 1 ; i <= high - 1 ; i ++ ) { for ( int j = i + 1 ; j <= high ; j ++ ) { if ( arr [ i ] == ( arr [ low ] + k ) && arr [ j ] == ( arr [ low ] + 2 * k ) && minSizeRec ( arr , low + 1 , i - 1 , k ) == 0 && minSizeRec ( arr , i + 1 , j - 1 , k ) == 0 ) { res = min ( res , minSizeRec ( arr , j + 1 , high , k ) ) ; } } } return ( dp [ low ] [ high ] = res ) ; } int minSize ( int arr [ ] , int n , int k ) { memset ( dp , -1 , sizeof ( dp ) ) ; return minSizeRec ( arr , 0 , n - 1 , k ) ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 1 ; cout << minSize ( arr , n , k ) << endl ; return 0 ; }
Find number of solutions of a linear equation of n variables | A naive recursive C ++ program to find number of non - negative solutions for a given linear equation ; Recursive function that returns count of solutions for given rhs value and coefficients coeff [ start . . end ] ; Base case ; Initialize count of solutions ; One by subtract all smaller or equal coefficiants and recur ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSol ( int coeff [ ] , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } int main ( ) { int coeff [ ] = { 2 , 2 , 5 } ; int rhs = 4 ; int n = sizeof ( coeff ) / sizeof ( coeff [ 0 ] ) ; cout << countSol ( coeff , 0 , n - 1 , rhs ) ; return 0 ; }
Maximum weight transformation of a given string | C ++ program to find maximum weight transformation of a given string ; Returns weight of the maximum weight transformation ; Base case ; If this subproblem is already solved ; Don 't make pair, so  weight gained is 1 ; If we can make pair ; If elements are dissimilar , weight gained is 4 ; if elements are similar so for making a pair we toggle any of them . Since toggle cost is 1 so overall weight gain becomes 3 ; save and return maximum of above cases ; Initializes lookup table and calls getMaxRec ( ) ; Create and initialize lookup table ; Call recursive function ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxRec ( string & str , int i , int n , int lookup [ ] ) { if ( i >= n ) return 0 ; if ( lookup [ i ] != -1 ) return lookup [ i ] ; int ans = 1 + getMaxRec ( str , i + 1 , n , lookup ) ; if ( i + 1 < n ) { if ( str [ i ] != str [ i + 1 ] ) ans = max ( 4 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; else ans = max ( 3 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; } return lookup [ i ] = ans ; } int getMaxWeight ( string str ) { int n = str . length ( ) ; int lookup [ n ] ; memset ( lookup , -1 , sizeof lookup ) ; return getMaxRec ( str , 0 , str . length ( ) , lookup ) ; } int main ( ) { string str = " AAAAABB " ; cout << " Maximum ▁ weight ▁ of ▁ a ▁ transformation ▁ of ▁ " << str << " ▁ is ▁ " << getMaxWeight ( str ) ; return 0 ; }
Minimum steps to reach a destination | C ++ program to count number of steps to reach a point ; source -> source vertex step -> value of last step taken dest -> destination vertex ; base cases ; if we go on positive side ; if we go on negative side ; minimum of both cases ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int steps ( int source , int step , int dest ) { if ( abs ( source ) > ( dest ) ) return INT_MAX ; if ( source == dest ) return step ; int pos = steps ( source + step + 1 , step + 1 , dest ) ; int neg = steps ( source - step - 1 , step + 1 , dest ) ; return min ( pos , neg ) ; } int main ( ) { int dest = 11 ; cout << " No . ▁ of ▁ steps ▁ required ▁ to ▁ reach ▁ " << dest << " ▁ is ▁ " << steps ( 0 , 0 , dest ) ; return 0 ; }
Longest Common Substring | DP | C ++ implementation of the above approach ; Function to find the length of the longest LCS ; Create DP table ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LCSubStr ( string s , string t , int n , int m ) { int dp [ 2 ] [ m + 1 ] ; int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( s [ i - 1 ] == t [ j - 1 ] ) { dp [ i % 2 ] [ j ] = dp [ ( i - 1 ) % 2 ] [ j - 1 ] + 1 ; if ( dp [ i % 2 ] [ j ] > res ) res = dp [ i % 2 ] [ j ] ; } else dp [ i % 2 ] [ j ] = 0 ; } } return res ; } int main ( ) { string X = " OldSite : GeeksforGeeks . org " ; string Y = " NewSite : GeeksQuiz . com " ; int m = X . length ( ) ; int n = Y . length ( ) ; cout << LCSubStr ( X , Y , m , n ) ; return 0 ; cout << " GFG ! " ; return 0 ; }
Longest Common Substring | DP | C ++ program using to find length of the longest common substring recursion ; Returns length of function f or longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Driver code
#include <iostream> NEW_LINE using namespace std ; string X , Y ; int lcs ( int i , int j , int count ) { if ( i == 0 j == 0 ) return count ; if ( X [ i - 1 ] == Y [ j - 1 ] ) { count = lcs ( i - 1 , j - 1 , count + 1 ) ; } count = max ( count , max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) ; return count ; } int main ( ) { int n , m ; X = " abcdxyz " ; Y = " xyzabcd " ; n = X . size ( ) ; m = Y . size ( ) ; cout << lcs ( n , m , 0 ) ; return 0 ; }
Make Array elements equal by replacing adjacent elements with their XOR | C ++ Program of the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the XOR of all elements of array A [ ] ; Case 1 , check if the XOR of the array A [ ] is 0 ; Maintains the XOR till the current element ; Iterate over the array ; If the current XOR is equal to the total XOR increment the count and initialize current XOR as 0 ; Print Answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleEqualArray ( int A [ ] , int N ) { int tot_XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { tot_XOR ^= A [ i ] ; } if ( tot_XOR == 0 ) { cout << " YES " ; return ; } int cur_XOR = 0 ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_XOR ^= A [ i ] ; if ( cur_XOR == tot_XOR ) { cnt ++ ; cur_XOR = 0 ; } } if ( cnt > 2 ) { cout << " YES " ; } else { cout << " NO " ; } } int main ( ) { int A [ ] = { 0 , 2 , 2 } ; int N = sizeof ( A ) / sizeof ( int ) ; possibleEqualArray ( A , N ) ; return 0 ; }
Count of palindromes that can be obtained by concatenating equal length prefix and substrings | C ++ program the above approach ; Function to calculate the number of palindromes ; Calculation of Z - array ; Calculation of sigma ( Z [ i ] + 1 ) ; Return the count ; Driver Code ; Given String
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPalindromes ( string S ) { int N = ( int ) S . length ( ) ; vector < int > Z ( N ) ; int l = 0 , r = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( i <= r ) Z [ i ] = min ( r - i + 1 , Z [ i - l ] ) ; while ( i + Z [ i ] < N && S [ Z [ i ] ] == S [ i + Z [ i ] ] ) { Z [ i ] ++ ; } if ( i + Z [ i ] - 1 > r ) { l = i ; r = i + Z [ i ] - 1 ; } } int sum = 0 ; for ( int i = 0 ; i < Z . size ( ) ; i ++ ) { sum += Z [ i ] + 1 ; } return sum ; } int main ( ) { string S = " abab " ; cout << countPalindromes ( S ) ; return 0 ; }
Extract substrings between any pair of delimiters | C ++ Program to implement the above approach ; Function to print strings present between any pair of delimeters ; Stores the indices of ; If opening delimeter is encountered ; If closing delimeter is encountered ; Extract the position of opening delimeter ; Length of substring ; Extract the substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSubsInDelimeters ( string str ) { stack < int > dels ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( str [ i ] == ' [ ' ) { dels . push ( i ) ; } else if ( str [ i ] == ' ] ' && ! dels . empty ( ) ) { int pos = dels . top ( ) ; dels . pop ( ) ; int len = i - 1 - pos ; string ans = str . substr ( pos + 1 , len ) ; cout << ans << endl ; } } } int main ( ) { string str = " [ This ▁ is ▁ first ] ▁ ignored ▁ text ▁ [ This ▁ is ▁ second ] " ; printSubsInDelimeters ( str ) ; return 0 ; }
Print matrix elements from top | C ++ program for the above approach ; Function to traverse the matrix diagonally upwards ; Store the number of rows ; Initialize queue ; Push the index of first element i . e . , ( 0 , 0 ) ; Get the front element ; Pop the element at the front ; Insert the element below if the current element is in first column ; Insert the right neighbour if it exists ; Driver Code ; Given vector of vectors arr ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printDiagonalTraversal ( vector < vector < int > > & nums ) { int m = nums . size ( ) ; queue < pair < int , int > > q ; q . push ( { 0 , 0 } ) ; while ( ! q . empty ( ) ) { pair < int , int > p = q . front ( ) ; q . pop ( ) ; cout << nums [ p . first ] [ p . second ] << " ▁ " ; if ( p . second == 0 && p . first + 1 < m ) { q . push ( { p . first + 1 , p . second } ) ; } if ( p . second + 1 < nums [ p . first ] . size ( ) ) q . push ( { p . first , p . second + 1 } ) ; } } int main ( ) { vector < vector < int > > arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printDiagonalTraversal ( arr ) ; return 0 ; }
Find original sequence from Array containing the sequence merged many times in order | C ++ program for the above approach ; Function that returns the restored permutation ; Vector to store the result ; Map to mark the elements which are taken in result ; Check if the element is coming first time ; Push in result vector ; Mark it in the map ; Return the answer ; Function to print the result ; Driver Code ; Given Array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > restore ( int arr [ ] , int N ) { vector < int > result ; map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp [ arr [ i ] ] == 0 ) { result . push_back ( arr [ i ] ) ; mp [ arr [ i ] ] ++ ; } } return result ; } void print_result ( vector < int > result ) { for ( int i = 0 ; i < result . size ( ) ; i ++ ) cout << result [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print_result ( restore ( arr , N ) ) ; return 0 ; }
Find original sequence from Array containing the sequence merged many times in order | C ++ program for the above approach ; Function that returns the restored permutation ; Vector to store the result ; Set to insert unique elements ; Check if the element is coming first time ; Push in result vector ; Function to print the result ; Driver Code ; Given Array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > restore ( int arr [ ] , int N ) { vector < int > result ; int count1 = 1 ; set < int > s ; for ( int i = 0 ; i < N ; i ++ ) { s . insert ( arr [ i ] ) ; if ( s . size ( ) == count1 ) { result . push_back ( arr [ i ] ) ; count1 ++ ; } } return result ; } void print_result ( vector < int > result ) { for ( int i = 0 ; i < result . size ( ) ; i ++ ) cout << result [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print_result ( restore ( arr , N ) ) ; return 0 ; }
Program to print the pattern 1020304017018019020 * * 50607014015016 * * * * 809012013 * * * * * * 10011. . . | C ++ implementation to print the given pattern ; Function to find the sum of N integers from 1 to N ; Function to print the given pattern ; Iterate over [ 0 , N - 1 ] ; Sub - Pattern - 1 ; Sub - Pattern - 2 ; Count the number of element in rows and sub - pattern 2 and 3 will have same rows ; Increment Val to print the series 1 , 2 , 3 , 4 , 5 ... ; Finally , add the ( N - 1 ) th element i . e . , 5 and increment it by 1 ; Initial is used to give the initial value of the row in Sub - Pattern 3 ; Sub - Pattern 3 ; Skip printing zero at the last ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int n ) { return n * ( n - 1 ) / 2 ; } void BSpattern ( int N ) { int Val = 0 , Pthree = 0 , cnt = 0 , initial ; string s = " * * " ; for ( int i = 0 ; i < N ; i ++ ) { cnt = 0 ; if ( i > 0 ) { cout << s ; s += " * * " ; } for ( int j = i ; j < N ; j ++ ) { if ( i > 0 ) { cnt ++ ; } cout << ++ Val ; cout << 0 ; } if ( i == 0 ) { int Sumbeforelast = sum ( Val ) * 2 ; Pthree = Val + Sumbeforelast + 1 ; initial = Pthree ; } initial = initial - cnt ; Pthree = initial ; for ( int k = i ; k < N ; k ++ ) { cout << Pthree ++ ; if ( k != N - 1 ) { cout << 0 ; } } cout << " STRNEWLINE " ; } } int main ( ) { int N = 5 ; BSpattern ( N ) ; return 0 ; }
Check if a number starts with another number or not | C ++ program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into strings ; Find the lengths of strings s1 and s2 ; Base Case ; Traverse the strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Driver Code ; Given numbers ; Function Call ; If B is a prefix of A , then print " Yes "
#include " bits / stdc + + . h " NEW_LINE using namespace std ; bool checkprefix ( int A , int B ) { string s1 = to_string ( A ) ; string s2 = to_string ( B ) ; int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 < n2 ) { return false ; } for ( int i = 0 ; i < n2 ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { return false ; } } return true ; } int main ( ) { int A = 12345 , B = 12 ; bool result = checkprefix ( A , B ) ; if ( result ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Check if it is possible to reach ( x , y ) from origin in exactly Z steps using only plus movements | C ++ program for the above approach ; Function to check if it is possible to reach ( x , y ) from origin in exactly z steps ; Condition if we can 't reach in Z steps ; Driver Code ; Destination point coordinate ; Number of steps allowed ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleToReach ( int x , int y , int z ) { if ( z < abs ( x ) + abs ( y ) || ( z - abs ( x ) - abs ( y ) ) % 2 ) { cout << " Not ▁ Possible " << endl ; } else cout << " Possible " << endl ; } int main ( ) { int x = 5 , y = 5 ; int z = 11 ; possibleToReach ( x , y , z ) ; return 0 ; }
Number of cycles in a Polygon with lines from Centroid to Vertices | C ++ program to find number of cycles in a Polygon with lines from Centroid to Vertices ; Function to find the Number of Cycles ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nCycle ( int N ) { return ( N ) * ( N - 1 ) + 1 ; } int main ( ) { int N = 4 ; cout << nCycle ( N ) << endl ; return 0 ; }
Sum of consecutive bit differences of first N non | C ++ program for the above problem ; Recursive function to count the sum of bit differences of numbers from 1 to pow ( 2 , ( i + 1 ) ) - 1 ; base cases ; Recursion call if the sum of bit difference of numbers around i are not calculated ; return the sum of bit differences if already calculated ; Function to calculate the sum of bit differences up to N ; nearest smaller power of 2 ; remaining numbers ; calculate the count of bit diff ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long a [ 65 ] = { 0 } ; long long Count ( int i ) { if ( i == 0 ) return 1 ; else if ( i < 0 ) return 0 ; if ( a [ i ] == 0 ) { a [ i ] = ( i + 1 ) + 2 * Count ( i - 1 ) ; return a [ i ] ; } else return a [ i ] ; } long long solve ( long long n ) { long long i , sum = 0 ; while ( n > 0 ) { i = log2 ( n ) ; n = n - pow ( 2 , i ) ; sum = sum + ( i + 1 ) + Count ( i - 1 ) ; } return sum ; } int main ( ) { long long n = 7 ; cout << solve ( n ) << endl ; return 0 ; }
Count of total Heads and Tails after N flips in a coin | C ++ program to count total heads and tails after N flips in a coin ; Function to find count of head and tail ; Check if initially all the coins are facing towards head ; Check if initially all the coins are facing towards tail ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > count_ht ( char s , int N ) { pair < int , int > p ; if ( s == ' H ' ) { p . first = floor ( N / 2.0 ) ; p . second = ceil ( N / 2.0 ) ; } else if ( s == ' T ' ) { p . first = ceil ( N / 2.0 ) ; p . second = floor ( N / 2.0 ) ; } return p ; } int main ( ) { char C = ' H ' ; int N = 5 ; pair < int , int > p = count_ht ( C , N ) ; cout << " Head ▁ = ▁ " << ( p . first ) << " STRNEWLINE " ; cout << " Tail ▁ = ▁ " << ( p . second ) << " STRNEWLINE " ; }
Longest palindromic string possible after removal of a substring | C ++ Implementation of the above approach ; Function to find the longest palindrome from the start of the string using KMP match ; Append S ( reverse of C ) to C ; Use KMP algorithm ; Function to return longest palindromic string possible from the given string after removal of any substring ; Initialize three strings A , B AND F ; Loop to find longest substrings from both ends which are reverse of each other ; Proceed to third step of our approach ; Remove the substrings A and B ; Find the longest palindromic substring from beginning of C ; Find the longest palindromic substring from end of C ; Store the maximum of D and E in F ; Find the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findPalindrome ( string C ) { string S = C ; reverse ( S . begin ( ) , S . end ( ) ) ; C = C + " & " + S ; int n = C . length ( ) ; int longestPalindrome [ n ] ; longestPalindrome [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( C [ i ] == C [ len ] ) { len ++ ; longestPalindrome [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = longestPalindrome [ len - 1 ] ; } else { longestPalindrome [ i ] = 0 ; i ++ ; } } } string ans = C . substr ( 0 , longestPalindrome [ n - 1 ] ) ; return ans ; } string findAns ( string s ) { string A = " " ; string B = " " ; string F = " " ; int i = 0 ; int j = s . length ( ) - 1 ; int len = s . length ( ) ; while ( i < j && s [ i ] == s [ j ] ) { i = i + 1 ; j = j - 1 ; } if ( i > 0 ) { A = s . substr ( 0 , i ) ; B = s . substr ( len - i , i ) ; } if ( len > 2 * i ) { string C = s . substr ( i , s . length ( ) - 2 * i ) ; string D = findPalindrome ( C ) ; reverse ( C . begin ( ) , C . end ( ) ) ; string E = findPalindrome ( C ) ; if ( D . length ( ) > E . length ( ) ) { F = D ; } else { F = E ; } } string answer = A + F + B ; return answer ; } int main ( ) { string str = " abcdefghiedcba " ; cout << findAns ( str ) << endl ; }
Find Nth term of the series 2 , 3 , 10 , 15 , 26. ... | C ++ program to find Nth term of the series 2 , 3 , 10 , 15 , 26. ... ; Function to find Nth term ; Nth term ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthTerm ( int N ) { int nth = 0 ; if ( N % 2 == 1 ) nth = ( N * N ) + 1 ; else nth = ( N * N ) - 1 ; return nth ; } int main ( ) { int N = 5 ; cout << nthTerm ( N ) << endl ; return 0 ; }
Find the Nth term in series 12 , 35 , 81 , 173 , 357 , ... | C ++ program to find the Nth term in series 12 , 35 , 81 , 173 , 357 , ... ; Function to find Nth term ; Nth term ; Driver Method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthTerm ( int N ) { int nth = 0 , first_term = 12 ; nth = ( first_term * ( pow ( 2 , N - 1 ) ) ) + 11 * ( ( pow ( 2 , N - 1 ) ) - 1 ) ; return nth ; } int main ( ) { int N = 5 ; cout << nthTerm ( N ) << endl ; return 0 ; }
Find Nth term of the series 4 , 2 , 2 , 3 , 6 , ... | C ++ program to find Nth term of the series 4 , 2 , 2 , 3 , 6 , ... ; Function to find Nth term ; Nth term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthTerm ( int N ) { int nth = 0 , first_term = 4 ; int pi = 1 , po = 1 ; int n = N ; while ( n > 1 ) { pi *= n - 1 ; n -- ; po *= 2 ; } nth = ( first_term * pi ) / po ; return nth ; } int main ( ) { int N = 5 ; cout << nthTerm ( N ) << endl ; return 0 ; }
Find the final number obtained after performing the given operation | C ++ implementation of the approach ; Function to return the final number obtained after performing the given operation ; Find the gcd of the array elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int finalNum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result = __gcd ( result , arr [ i ] ) ; } return result ; } int main ( ) { int arr [ ] = { 3 , 9 , 6 , 36 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << finalNum ( arr , n ) ; return 0 ; }
Check whether all the substrings have number of vowels atleast as that of consonants | C ++ implementation of the approach ; Function that returns true if character ch is a vowel ; Compares two integers according to their digit sum ; Check if there are two consecutive consonants ; Check if there is any vowel surrounded by two consonants ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { switch ( ch ) { case ' a ' : case ' e ' : case ' i ' : case ' o ' : case ' u ' : return true ; } return false ; } bool isSatisfied ( string str , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i - 1 ] ) ) { return false ; } } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( isVowel ( str [ i ] ) && ! isVowel ( str [ i - 1 ] ) && ! isVowel ( str [ i + 1 ] ) ) { return false ; } } return true ; } int main ( ) { string str = " acaba " ; int n = str . length ( ) ; if ( isSatisfied ( str , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Print the longest prefix of the given string which is also the suffix of the same string | C ++ implementation of the approach ; Returns length of the longest prefix which is also suffix and the two do not overlap . This function mainly is copy of computeLPSArray ( ) in KMP Algorithm ; lps [ 0 ] is always 0 ; Length of the previous longest prefix suffix ; Loop to calculate lps [ i ] for i = 1 to n - 1 ; This is tricky . Consider the example . AAACAAAA and i = 7. The idea is similar to search step . ; Also , note that we do not increment i here ; If len = 0 ; Since we are looking for non overlapping parts ; Function that returns the prefix ; Get the length of the longest prefix ; Stores the prefix ; Traverse and add characters ; Returns the prefix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LengthlongestPrefixSuffix ( string s ) { int n = s . length ( ) ; int lps [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } int res = lps [ n - 1 ] ; return ( res > n / 2 ) ? n / 2 : res ; } string longestPrefixSuffix ( string s ) { int len = LengthlongestPrefixSuffix ( s ) ; string prefix = " " ; for ( int i = 0 ; i < len ; i ++ ) prefix += s [ i ] ; return prefix ; } int main ( ) { string s = " abcab " ; string ans = longestPrefixSuffix ( s ) ; if ( ans == " " ) cout << " - 1" ; else cout << ans ; return 0 ; }