codeparrot/xlcost-text-to-code huggingface dataset:
Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | C ++ program for the above approach ; Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int num ) { bool flag = false ; if ( num > 1 ) { for ( int i = 2 ; i < num ; i ++ ) { if ( ( num % i ) == 0 ) { flag = true ; break ; } } } if ( flag ) return false ; else return true ; } int order ( int x ) { int n = 0 ; while ( x != 0 ) { n = n + 1 ; x = x / 10 ; } return n ; } bool isArmstrong ( int x ) { int n = order ( x ) ; int temp = x ; int sum1 = 0 ; while ( temp != 0 ) { int r = temp % 10 ; sum1 = sum1 + pow ( r , n ) ; temp = temp / 10 ; } return ( sum1 == x ) ; } int count_armstrong ( vector < string > li ) { int c = 0 ; for ( string ele : li ) { int val = 0 ; for ( char che : ele ) val += che ; if ( isArmstrong ( val ) ) c += 1 ; } return c ; } int count_prime ( vector < string > li ) { int c = 0 ; for ( string ele : li ) { int val = 0 ; for ( char che : ele ) val += che ; if ( isPrime ( val ) ) c += 1 ; } return c ; } int main ( ) { vector < string > arr = { " geeksforgeeks " , " a " , " computer " , " science " , " portal " , " for " , " geeks " } ; cout << " Number ▁ of ▁ Armstrong ▁ Strings ▁ are : ▁ " << count_armstrong ( arr ) << endl ; cout << " Number ▁ of ▁ Prime ▁ Strings ▁ are : ▁ " << count_prime ( arr ) << endl ; }
Minimize cost of flipping or swaps to make a Binary String balanced | C ++ program for the above approach ; Function to find the minimum cost to convert the given string into balanced string ; Stores count of 1 ' s ▁ and ▁ 0' s in the string ; Traverse the string ; Increment count1 ; Increment count 0 ; Stores absolute difference of counts of 0 ' s ▁ and ▁ 1' s ; If string consists of only 0 ' s ▁ and ▁ 1' s ; Print minimum cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumCost ( string s , int N ) { int count_1 = 0 , count_0 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) count_1 ++ ; else count_0 ++ ; } int k = abs ( count_0 - count_1 ) ; if ( count_1 == N count_0 == N ) cout << -1 << endl ; else cout << k / 2 << endl ; } int main ( ) { string S = "110110" ; int N = S . length ( ) ; findMinimumCost ( S , N ) ; return 0 ; }
Minimum time required to complete all tasks with alteration of their order allowed | C ++ program for the above approach ; Function to find the minimum time required to complete the tasks if the order of tasks can be changed ; If there is no task , print 0 ; Store the maximum occurring character and its frequency ; Stores the frequency of each character ; Traverse the string S ; Increment the frequency of the current character ; Update maxfreq and maxchar ; Store the number of empty slots ; Traverse the hashmap , um ; Fill the empty slots ; Store the required result ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumTime ( string & S , int N , int K ) { if ( N == 0 ) { cout << 0 ; return ; } int maxfreq = INT_MIN ; char maxchar ; unordered_map < char , int > um ; for ( int i = 0 ; i < N ; i ++ ) { um [ S [ i ] ] ++ ; if ( um [ S [ i ] ] > maxfreq ) { maxfreq = um [ S [ i ] ] ; maxchar = S [ i ] ; } } int emptySlots = ( maxfreq - 1 ) * K ; for ( auto & it : um ) { if ( it . first == maxchar ) continue ; emptySlots -= min ( it . second , maxfreq - 1 ) ; } int ans = N + max ( 0 , emptySlots ) ; cout << ans ; } int main ( ) { string S = " AAABBB " ; int K = 2 ; int N = S . length ( ) ; findMinimumTime ( S , N , K ) ; return 0 ; }
Check if all characters of a string can be made equal by increments or decrements | C ++ program for the above approach ; Function to check if it is possible to make all characters of string S same or not ; Length of string ; Stores the sum of ASCII value ; Traverse the string S ; Update the weightOfString ; If the sum is divisible by N then print " Yes " ; Otherwise print " No " ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canMakeEqual ( string S ) { int N = S . size ( ) ; int weightOfString = 0 ; for ( int i = 0 ; i < N ; i ++ ) { weightOfString += S [ i ] - ' a ' + 1 ; } if ( weightOfString % N == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " beb " ; canMakeEqual ( S ) ; return 0 ; }
Modify a string by circularly shifting each character to the right by respective frequencies | C ++ program for the above approach ; Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addFrequencyToCharacter ( string S ) { int frequency [ 26 ] = { 0 } ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < N ; i ++ ) { int add = frequency [ S [ i ] - ' a ' ] % 26 ; if ( int ( S [ i ] ) + add <= int ( ' z ' ) ) S [ i ] = char ( int ( S [ i ] ) + add ) ; else { add = ( int ( S [ i ] ) + add ) - ( int ( ' z ' ) ) ; S [ i ] = char ( int ( ' a ' ) + add - 1 ) ; } } cout << S ; } int main ( ) { string S = " geeksforgeeks " ; addFrequencyToCharacter ( S ) ; return 0 ; }
Minimize length of a string by removing pairs of consecutive increasing or decreasing digits | C ++ program for the above approach ; Function to find the minimum length of the string possible after removing pairs of consecutive digits ; Initialize the stack st ; Traverse the string S ; If the stack is empty ; Otherwise ; Get the top character of the stack ; If cha and top are consecutive digits ; Otherwise , push the character ch ; Print the size of the stack ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( string S ) { stack < char > st ; for ( auto ch : S ) { if ( st . empty ( ) ) st . push ( ch ) ; else { char top = st . top ( ) ; if ( abs ( ch - top ) == 1 ) st . pop ( ) ; else { st . push ( ch ) ; } } } return st . size ( ) ; } int main ( ) { string S = "12213" ; cout << minLength ( S ) ; return 0 ; }
Minimum time required to complete all tasks without altering their order | C ++ implementation of the above approach ; Function to find the minimum time required to complete tasks without changing their order ; Keeps track of the last time instant of each task ; Stores the required result ; Traverse the given string ; Check last time instant of task , if it exists before ; Increment the time if task is within the K units of time ; Update the time of the current task in the map ; Increment the time by 1 ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumTime ( string tasks , int K ) { unordered_map < char , int > map ; int curr_time = 0 ; for ( char c : tasks ) { if ( map . find ( c ) != map . end ( ) ) { if ( curr_time - map <= K ) { curr_time += K - ( curr_time - map ) + 1 ; } } map = curr_time ; curr_time ++ ; } cout << curr_time ; } int main ( ) { string S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; return 0 ; }
Check if a string is a subsequence of another string ( using Stacks ) | C ++ Program for the above approach ; Function to check if target is a subsequence of string S ; Declare a stack ; Push the characters of target into the stack ; Traverse the string S in reverse ; If the stack is empty ; if S [ i ] is same as the top of the stack ; Pop the top of stack ; Stack s is empty ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkforSubsequence ( string S , string target ) { stack < char > s ; for ( int i = 0 ; i < target . size ( ) ; i ++ ) { s . push ( target [ i ] ) ; } for ( int i = ( int ) S . size ( ) - 1 ; i >= 0 ; i -- ) { if ( s . empty ( ) ) { cout << " Yes " << endl ; return ; } if ( S [ i ] == s . top ( ) ) { s . pop ( ) ; } } if ( s . empty ( ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { string S = " KOTTAYAM " ; string target = " KOTA " ; checkforSubsequence ( S , target ) ; return 0 ; }
Program to construct a DFA to check if a given integer is unsigned or not | C ++ program for the above approach ; Function to construct DFA as per the given conditions ; If at state 0 and a digit has occurred then set it to state 1 ; Similarly for all other states ; Function to build and connect the DFA states ; Connect all the states to the dead state ; Function call to make DFA as per the given conditions ; Function call to check whether an integer in the form of string is unsigned integer or not ; Build the DFA ; Stores the current state ; Traverse the string ; If at a certain state a digit occurs then change the current state according to the DFA ; Or + / - sign ; Or decimal occurred ; Or any other character ; Or e / E or exponent sign ; State 1 , 4 , 8 will give the final answer ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; string digits = "0123456789" , sign = " + - " ; string dot = " . " , ex = " eE " ; int dfa [ 11 ] [ 5 ] ; void makeDFA ( ) { dfa [ 0 ] [ 0 ] = 1 ; dfa [ 1 ] [ 0 ] = 1 ; dfa [ 1 ] [ 2 ] = 3 ; dfa [ 1 ] [ 3 ] = 2 ; dfa [ 1 ] [ 4 ] = 6 ; dfa [ 3 ] [ 0 ] = 4 ; dfa [ 4 ] [ 0 ] = 4 ; dfa [ 4 ] [ 3 ] = 5 ; dfa [ 4 ] [ 4 ] = 6 ; dfa [ 6 ] [ 0 ] = 8 ; dfa [ 6 ] [ 1 ] = 7 ; dfa [ 7 ] [ 0 ] = 8 ; dfa [ 8 ] [ 0 ] = 8 ; dfa [ 8 ] [ 3 ] = 9 ; } void buildDFA ( ) { for ( int i = 0 ; i < 11 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) dfa [ i ] [ j ] = 10 ; makeDFA ( ) ; } void checkDFA ( string s ) { buildDFA ( ) ; int currentstate = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( digits . find ( s [ i ] ) != digits . npos ) currentstate = dfa [ currentstate ] [ 0 ] ; else if ( sign . find ( s [ i ] ) != sign . npos ) currentstate = dfa [ currentstate ] [ 1 ] ; else if ( dot . find ( s [ i ] ) != dot . npos ) currentstate = dfa [ currentstate ] [ 2 ] ; else if ( ex . find ( s [ i ] ) != ex . npos ) currentstate = dfa [ currentstate ] [ 4 ] ; else currentstate = dfa [ currentstate ] [ 3 ] ; } if ( currentstate == 1 currentstate == 4 currentstate == 8 ) { cout << " Unsigned ▁ integer " ; } else { cout << " Not ▁ an ▁ unsigned ▁ integer " ; } } int main ( ) { string S = "1729" ; checkDFA ( S ) ; return 0 ; }
Convert a Mobile Numeric Keypad sequence to equivalent sentence | C ++ program for the above approach ; Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSentence ( string str ) { char nums [ ] [ 5 ] = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; int i = 0 ; while ( str [ i ] != ' \0' ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( str [ i + 1 ] && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( str [ i ] == ' \0' ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { cout << nums [ str [ i ] - 48 ] [ count % 4 ] ; } else { cout << nums [ str [ i ] - 48 ] [ count % 3 ] ; } i ++ ; } } int main ( ) { string str = "234" ; printSentence ( str ) ; return 0 ; }
Lexicographically largest string with sum of characters equal to N | C ++ program for the above approach ; Function to construct the lexicographically largest string having sum of characters as N ; Stores the resulting string ; Iterate until N is at least 26 ; Append ' z ' to the string ans ; Decrement N by 26 ; Append character at index ( N + ' a ' ) ; Return the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getString ( int N ) { string ans = " " ; while ( N >= 26 ) { ans += ' z ' ; N -= 26 ; } ans += char ( N + ' a ' - 1 ) ; return ans ; } int main ( ) { int N = 30 ; cout << getString ( N ) ; return 0 ; }
Length of all prefixes that are also the suffixes of given string | C ++ program for the above approach ; Function to find the length of all prefixes of the given string that are also suffixes of the same string ; Stores the prefix string ; Traverse the string S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSamePrefixSuffix ( string s , int n ) { string prefix = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix += s [ i ] ; string suffix = s . substr ( n - 1 - i , n - 1 ) ; if ( prefix == suffix ) { cout << prefix . size ( ) << " ▁ " ; } } } int main ( ) { string S = " ababababab " ; int N = S . size ( ) ; countSamePrefixSuffix ( S , N ) ; return 0 ; }
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | C ++ program for the above approach ; Function to count newly created pairs by swapping the first characters of any pairs of strings ; Stores the count all possible pair of strings ; Push all the strings into the Unordered Map ; Generate all possible pairs of strings from the array arr [ ] ; Store the current pair of strings ; Swap the first character ; Check if both string are not present in map ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countStringPairs ( string a [ ] , int n ) { int ans = 0 ; unordered_map < string , int > s ; for ( int i = 0 ; i < n ; i ++ ) { s [ a [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { string p = a [ i ] ; string q = a [ j ] ; if ( p [ 0 ] != q [ 0 ] ) { swap ( p [ 0 ] , q [ 0 ] ) ; if ( s . find ( p ) == s . end ( ) && s . find ( q ) == s . end ( ) ) { ans ++ ; } } } } cout << ans ; } int main ( ) { string arr [ ] = { " good " , " bad " , " food " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countStringPairs ( arr , N ) ; return 0 ; }
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | C ++ program for the above approach ; Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ' ) ' ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumMoves ( string s , int n , int k ) { if ( n & 1 ) { cout << " No " ; return ; } int countOpen = count ( s . begin ( ) , s . end ( ) , ' ( ' ) ; int countClose = count ( s . begin ( ) , s . end ( ) , ' ) ' ) ; if ( countOpen != countClose ) { cout << " No " ; return ; } int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == ' ( ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " ) ( " ; int K = 1 ; minimumMoves ( S , S . length ( ) , K ) ; return 0 ; }
Generate a random Binary String of length N | C ++ program for the above approach ; Function to find a random number between 0 or 1 ; Generate the random number ; Return the generated number ; Function to generate a random binary string of length N ; Stores the empty string ; Iterate over the range [ 0 , N - 1 ] ; Store the random number ; Append it to the string ; Print the resulting string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRandom ( ) { int num = ( ( int ) rand ( ) % 2 ) ; return num ; } void generateBinaryString ( int N ) { srand ( time ( NULL ) ) ; string S = " " ; for ( int i = 0 ; i < N ; i ++ ) { int x = findRandom ( ) ; S += to_string ( x ) ; } cout << S ; } int main ( ) { int N = 7 ; generateBinaryString ( N ) ; return 0 ; }
Partition a string into palindromic strings of at least length 2 with every character present in a single string | C ++ program for the above approach ; Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void checkPalindrome ( string & s ) { int a [ 26 ] = { 0 } ; int o = 0 , e = 0 ; for ( int i = 0 ; s [ i ] != ' \0' ; i ++ ) a [ ( int ) s [ i ] - 97 ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( a [ i ] == 1 ) o ++ ; else if ( a [ i ] % 2 == 0 and a [ i ] != 0 ) e += ( a [ i ] / 2 ) ; } if ( e >= o ) cout << " Yes " ; else { o = o - e ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( o <= 0 ) break ; if ( o > 0 and a [ i ] % 2 == 1 and a [ i ] > 2 ) { int k = o ; o = o - a [ i ] / 2 ; if ( o > 0 or 2 * k + 1 == a [ i ] ) { o ++ ; a [ i ] = 1 ; } } } if ( o <= 0 ) cout << " Yes " ; else cout << " No " ; } } int main ( ) { string S = " abbbaddzcz " ; checkPalindrome ( S ) ; return 0 ; }
Maximize time by replacing ' _ ' in a given 24 Hour format time | C ++ program for the above approach ; Function to find the maximum time possible by replacing each ' _ ' with any digit ; If the first character is ' _ ' ; If s [ 1 ] is ' _ ' or s [ 1 ] is less than 4 ; Update s [ 0 ] as 2 ; Otherwise , update s [ 0 ] = 1 ; If s [ 1 ] is equal to ' _ ' ; If s [ 0 ] is equal to '2' ; Otherwise ; If S [ 3 ] is equal to ' _ ' ; If s [ 4 ] is equal to ' _ ' ; Return the modified string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string maximumTime ( string s ) { if ( s [ 0 ] == ' _ ' ) { if ( ( s [ 1 ] == ' _ ' ) || ( s [ 1 ] >= '0' && s [ 1 ] < '4' ) ) { s [ 0 ] = '2' ; } else { s [ 0 ] = '1' ; } } if ( s [ 1 ] == ' _ ' ) { if ( s [ 0 ] == '2' ) { s [ 1 ] = '3' ; } else { s [ 1 ] = '9' ; } } if ( s [ 3 ] == ' _ ' ) { s [ 3 ] = '5' ; } if ( s [ 4 ] == ' _ ' ) { s [ 4 ] = '9' ; } return s ; } int main ( ) { string S = "0 _ : 4 _ " ; cout << maximumTime ( S ) ; return 0 ; }
Count distinct emails present in a given array | C ++ program for the above approach ; Function to count all the distinct emails after preprocessing according to the given rules ; Traverse the given array of strings arr [ ] ; Stores the position of ' @ ' in the string ; If pos2 < x . size ( ) ; Erases all the occurrences of ' . ' before pos2 ; Stores the position of the first ' + ' ; Update the position pos2 ; If ' + ' exists then erase characters after ' + ' and before ' @ ' ; Insert all the updated strings inside the set ; Return the size of set ans ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctEmails ( vector < string > & emails ) { for ( auto & x : emails ) { auto pos2 = x . find ( ' @ ' ) ; if ( pos2 < x . size ( ) ) x . erase ( remove ( x . begin ( ) , x . begin ( ) + pos2 , ' . ' ) , x . begin ( ) + pos2 ) ; auto pos1 = x . find ( ' + ' ) ; pos2 = x . find ( ' @ ' ) ; if ( pos1 < x . size ( ) and pos2 < x . size ( ) ) { x . erase ( pos1 , pos2 - pos1 ) ; } } unordered_set < string > ans ( emails . begin ( ) , emails . end ( ) ) ; return ans . size ( ) ; } int main ( ) { vector < string > arr = { " raghav . agg @ geeksforgeeks . com " , " raghavagg @ geeksforgeeks . com " } ; cout << distinctEmails ( arr ) ; return 0 ; }
Nearest power of 2 of frequencies of each digit of a given number | C ++ program for the above approach ; Function to find the nearest power of 2 for all frequencies in the Map freq ; Traverse the Map ; Calculate log of the current array element ; Find the nearest power of 2 for the current frequency ; Function to find nearest power of 2 for frequency of each digit of num ; Length of string ; Stores the frequency of each character in the string ; Traverse the string S ; Function call to generate nearest power of 2 for each frequency ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestPowerOfTwoUtil ( unordered_map < char , int > & freq ) { for ( auto & it : freq ) { cout << it . first << " ▁ - > ▁ " ; int lg = log2 ( it . second ) ; int a = pow ( 2 , lg ) ; int b = pow ( 2 , lg + 1 ) ; if ( ( it . second - a ) < ( b - it . second ) ) { cout << a << endl ; } else { cout << b << endl ; } } } void nearestPowerOfTwo ( string & S ) { int N = S . size ( ) ; unordered_map < char , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ S [ i ] ] ++ ; } nearestPowerOfTwoUtil ( freq ) ; } int main ( ) { string N = "16333331163" ; nearestPowerOfTwo ( N ) ; return 0 ; }
Smallest string divisible by two given strings | C ++ program for the above approach ; Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int lcm ( int a , int b ) { return ( a / gcd ( a , b ) ) * b ; } void findSmallestString ( string s , string t ) { int n = s . length ( ) , m = t . length ( ) ; int l = lcm ( n , m ) ; string s1 = " " , t1 = " " ; for ( int i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( int i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 == t1 ) cout << s1 ; else cout << -1 ; } int main ( ) { string S = " baba " , T = " ba " ; findSmallestString ( S , T ) ; return 0 ; }