codeparrot/xlcost-text-to-code huggingface dataset:
Maximum count of “ 010. . ” subsequences that can be removed from given Binary String | C ++ program for the above approach ; Function to count the maximum number of operations performed on the string ; Size of the string ; Stores the maximum number of operations performed ; Stores the count of ' X ' occurred so far ; Traverse the string ; Check if current char is ' X ' ; Check if the value of cnt is greater than 0 ; Decrement the value of cnt ; Increment the value of ans ; Print the value of ans ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void countOperations ( string S ) { int n = S . length ( ) ; int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == '0' ) { cnt ++ ; } else { if ( cnt > 0 ) { cnt -- ; ans ++ ; } } } cout << ans ; } int main ( ) { string S = "110011010" ; countOperations ( S ) ; return 0 ; }
Check if given String can be split only into subsequences ABC | C ++ program for the above approach ; Function to check if the given string can be splitted into subsequences " ABC " ; Stores the length of the string ; Stores the indices of ' A ' ; Stores the indices of ' B ' ; Stores the indices of ' C ' ; Traverse the string S ; If S [ i ] is equal to ' A ' ; Push the index i in A ; Else if S [ i ] is equal to ' B ' ; Push the index i in B ; Else if S [ i ] is equal to ' C ' ; Push the index i in C ; If N is not multiple of 3 or count of characters ' A ' , ' B ' and ' C ' are not equal ; Return " No " ; Iterate over the deque B ; If A is not empty and B [ i ] is greater than A [ 0 ] ; Removes the front element of A ; Else return " No " ; Iterate over the deque B in reverse ; If C is not empty and last element of C is greater thab B [ i ] ; Removes the last element of C ; Else return " No " ; If none of the above cases satisfy return "Yes' ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( string S ) { int N = S . length ( ) ; deque < int > A ; deque < int > B ; deque < int > C ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' A ' ) { A . push_back ( i ) ; } else if ( S [ i ] == ' B ' ) { B . push_back ( i ) ; } else { C . push_back ( i ) ; } } if ( N % 3 || A . size ( ) != B . size ( ) || A . size ( ) != C . size ( ) ) { return " No " ; } for ( int i = 0 ; i < B . size ( ) ; i ++ ) { if ( ! A . empty ( ) && B [ i ] > A [ 0 ] ) { A . pop_front ( ) ; } else { return " No " ; } } for ( int i = B . size ( ) - 1 ; i >= 0 ; i -- ) { if ( ! C . empty ( ) && B [ i ] < C . back ( ) ) { C . pop_back ( ) ; } else { return " No " ; } } return " Yes " ; } int main ( ) { string S = " ABABCC " ; cout << check ( S ) << endl ; return 0 ; }
Check if summation of two words is equal to target word | C ++ program for the above approach ; Function to check weather summation of two words equal to target word ; Store the length of each string ; Reverse the strings A , B and C ; Stores the remainder ; Iterate in the range [ 0 , max ( L , max ( M , N ) ) ] ; Stores the integer at ith position from the right in the sum of A and B ; If i is less than L ; If i is less than M ; Update rem and curr ; If i is less than N and curr is not equal to C [ i ] - ' a ' , return " No " ; If rem is greater than 0 , return " No " ; Otherwise , return " Yes " ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isSumEqual ( string A , string B , string C ) { int L = A . length ( ) ; int M = B . length ( ) ; int N = A . length ( ) ; reverse ( A . begin ( ) , A . end ( ) ) ; reverse ( B . begin ( ) , B . end ( ) ) ; reverse ( C . begin ( ) , C . end ( ) ) ; int rem = 0 ; for ( int i = 0 ; i < max ( L , max ( M , N ) ) ; i ++ ) { int curr = rem ; if ( i < L ) curr += A [ i ] - ' a ' ; if ( i < M ) curr += B [ i ] - ' a ' ; rem = curr / 10 ; curr %= 10 ; if ( i < N && curr != C [ i ] - ' a ' ) { return " No " ; } } if ( rem ) return " No " ; else return " Yes " ; } int main ( ) { string A = " acb " , B = " cba " , C = " cdb " ; cout << isSumEqual ( A , B , C ) ; return 0 ; }
Minimum K such that every substring of length at least K contains a character c | Set | C ++ program for the above approach ; Function to find minimum value of K such that there exist atleast one K amazing character ; Stores the answer ; Update the string S ; Iterate over the characters in range [ a , z ] ; Stores the last index where c appears ; Stores the maximum possible length ; Update string S ; Iterate over characters of string S ; If S [ i ] is equal to c ; Stores the distance between positions of two same c ; Update max_len ; Update the value of prev ; Update the value of ans ; Return the answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumLengthSubstring ( string S , int N ) { int ans = N ; S = "0" + S + "0" ; for ( char c = ' a ' ; c <= ' z ' ; ++ c ) { int prev = 0 ; int max_len = 0 ; S [ 0 ] = c ; S [ N + 1 ] = c ; for ( int i = 1 ; i <= N + 1 ; ++ i ) { if ( S [ i ] == c ) { int len = i - prev ; max_len = max ( max_len , len ) ; prev = i ; } } ans = min ( ans , max_len ) ; } return ans ; } int main ( ) { string S = " abcde " ; int N = S . length ( ) ; cout << MinimumLengthSubstring ( S , N ) ; }
Minimize hamming distance in Binary String by setting only one K size substring bits | C ++ program for the above approach ; Function to find minimum Hamming Distance after atmost one operation ; Store the size of the string ; Store the prefix sum of 1 s ; Create Prefix Sum array ; Initialize cnt as number of ones in string S ; Store the required result ; Traverse the string , S ; Store the number of 1 s in the substring S [ i , i + K - 1 ] ; Update the answer ; Return the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumHammingDistance ( string S , int K ) { int n = S . size ( ) ; int pref [ n ] ; pref [ 0 ] = S [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + ( S [ i ] - '0' ) ; int cnt = pref [ n - 1 ] ; int ans = cnt ; for ( int i = 0 ; i < n - K ; i ++ ) { int value = pref [ i + K - 1 ] - ( i - 1 >= 0 ? pref [ i - 1 ] : 0 ) ; ans = min ( ans , cnt - value + ( K - value ) ) ; } return ans ; } int main ( ) { string s = "101" ; int K = 2 ; cout << minimumHammingDistance ( s , K ) ; return 0 ; }
Check if a permutation of S2 can be obtained by adding or removing characters from S1 | C ++ program for the above approach ; Function to check if the given number is prime or not ; If the number is less than 2 ; If the number is 2 ; If N is a multiple of 2 ; Otherwise , check for the odds values ; Function to check if S1 can be a permutation of S2 by adding or removing characters from S1 ; Initialize a frequency array ; Decrement the frequency for occurrence in s1 ; Increment the frequency for occurence in s2 ; If frequency of current char is same in s1 and s2 ; Check the frequency for the current char is not prime ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; else if ( n == 2 ) return true ; else if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { if ( n % i == 0 ) return false ; } return true ; } void checkPermutation ( string s1 , string s2 ) { int freq [ 26 ] = { 0 } ; for ( char ch : s1 ) { freq [ ch - ' a ' ] -- ; } for ( char ch : s2 ) { freq [ ch - ' a ' ] ++ ; } bool isAllChangesPrime = true ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) { continue ; } else if ( ! isPrime ( abs ( freq [ i ] ) ) ) { isAllChangesPrime = false ; break ; } } if ( isAllChangesPrime ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { string S1 = " gekforgk " ; string S2 = " geeksforgeeks " ; checkPermutation ( S1 , S2 ) ; }
Count ways to split a string into two subsets that are reverse of each other | C ++ program for the above approach ; Function to find the total number of ways to partitiaon the string into two subset satisfying the conditions ; Stores the resultant number of ways of splitting ; Iterate over the range [ 0 , 2 ^ N ] ; Traverse the string S ; If ith bit is set , then append the character S [ i ] to X ; Otherwise , append the character S [ i ] to Y ; Reverse the second string ; If X is equal to Y ; Return the total number of ways ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( string S , int N ) { int ans = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { string X , Y ; for ( int i = 0 ; i < N ; i ++ ) { if ( mask >> i & 1 ) { X += S [ i ] ; } else { Y += S [ i ] ; } } reverse ( Y . begin ( ) , Y . end ( ) ) ; if ( X == Y ) { ans ++ ; } } return ans ; } int main ( ) { string S = " mippiisssisssiipsspiim " ; int N = S . length ( ) ; cout << countWays ( S , N ) ; return 0 ; }
Check if String formed by first and last X characters of a String is a Palindrome | C ++ implementation for the above approach ; Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isEqualSubstring ( string str , int x ) { int n = str . length ( ) ; int i = 0 ; while ( i < n && i < x ) { if ( str [ i ] != str [ n - i - 1 ] ) { cout << " false " ; return ; } i ++ ; } cout << " true " ; } int main ( ) { string str = " GeeksforGeeks " ; int x = 3 ; isEqualSubstring ( str , x ) ; }
Flip the String by either swapping given characters or rotating it horizontally for Q queries | C ++ program for the above approach ; Function to find final string after applying all Queries on it ; Traverse the Queries array ; convert A , B to zero indexing ; Query of 1 st type ; swap ath and bth characters ; Query of 2 nd type ; swap first N characters with last N characters ; Print answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string S , int N , vector < vector < int > > Queries , int Q ) { for ( int i = 0 ; i < Q ; i ++ ) { int T = Queries [ i ] [ 0 ] , A = Queries [ i ] [ 1 ] , B = Queries [ i ] [ 2 ] ; A -- ; B -- ; if ( T == 1 ) { swap ( S [ A ] , S [ B ] ) ; } else { for ( int j = 0 ; j < N ; j ++ ) { swap ( S [ j ] , S [ j + N ] ) ; } } } cout << S << endl ; } int main ( ) { string S = " ABCD " ; int N = S . length ( ) / 2 ; vector < vector < int > > Queries = { { 2 , 0 , 0 } , { 1 , 1 , 3 } , { 2 , 0 , 0 } } ; int Q = Queries . size ( ) ; solve ( S , N , Queries , Q ) ; return 0 ; }
Minimum number of replacement done of substring "01" with "110" to remove it completely | C ++ program for the above approach ; Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumOperations ( string S , int N ) { int ans = 0 ; int cntOne = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '0' ) { ans += cntOne ; cntOne *= 2 ; } else cntOne ++ ; } cout << ans ; } int main ( ) { string S = "001" ; int N = S . length ( ) ; minimumOperations ( S , N ) ; return 0 ; }
Count different Bitwise OR values of equal length strings S1 and S2 by swapping exactly one pair of characters from the first string | C ++ program for the above approach ; Function to find the number of ways to obtain different Bitwise OR ; Stores the count of pairs t00 , t10 , t01 , t11 ; Traverse the characters of the string S1 and S2 ; Count the pair ( 0 , 0 ) ; Count the pair ( 1 , 0 ) ; Count the pair ( 1 , 1 ) ; Count the pair ( 0 , 1 ) ; Number of ways to calculate the different bitwise OR ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void differentBitwiseOR ( string s1 , string s2 ) { int n = s1 . size ( ) ; int t00 = 0 , t10 = 0 , t01 = 0 , t11 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == '0' && s2 [ i ] == '0' ) { t00 ++ ; } if ( s1 [ i ] == '1' && s2 [ i ] == '0' ) { t10 ++ ; } if ( s1 [ i ] == '1' && s2 [ i ] == '1' ) { t11 ++ ; } if ( s1 [ i ] == '0' && s2 [ i ] == '1' ) { t01 ++ ; } } int ans = t00 * t10 + t01 * t10 + t00 * t11 ; cout << ans ; } int main ( ) { string S1 = "01001" ; string S2 = "11011" ; differentBitwiseOR ( S1 , S2 ) ; return 0 ; }
Flip all 0 s in given Binary Strings K times with different neighbours | C ++ program for the above approach ; Function to modify the given string K number of times by flipping 0 s having different adjacent characters ; Size of the string ; Stores modified string after each iteration ; Iterate over the range [ 0 k ] ; Traverse the string S ; If '0' is present at 0 th index then replace it with 1 st index ; If '0' is present at the last index then replace it with last index - 1 character ; Otherwise , convert 0 s to 1 if the adjacent characters are different ; If during this iteration there is no change in the string then break this loop ; Update the string S ; Print the updated string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convertString ( string S , int k ) { int n = S . length ( ) ; string temp = S ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 && S [ j ] == '0' ) { temp [ j ] = S [ j + 1 ] ; } else if ( j == n - 1 && S [ j ] == '0' ) { temp [ j ] = S [ j - 1 ] ; } else if ( S [ j - 1 ] != S [ j + 1 ] && S [ j ] == '0' ) { temp [ j ] = '1' ; } } if ( S == temp ) { break ; } S = temp ; } cout << S ; } int main ( ) { string S = "10010001" ; int K = 1 ; convertString ( S , K ) ; return 0 ; }
Minimum number of removals required such that no subsequence of length 2 occurs more than once | C ++ program for the above approach ; Function to remove the minimum count of characters from the string such that no subsequence of length 2 repeats ; Initialize the final string ; Stores if any character occurs in the final string or not ; Store the index of the last character added in the string ; Traverse the string ; Add all the unique characters ; Check if S [ 0 ] appears in the range [ pos + 1 , N - 1 ] ; If the characters are the same ; Print the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void RemoveCharacters ( string s ) { string ans = " " ; bool c [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) c [ i ] = 0 ; int pos = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( c [ s [ i ] - ' a ' ] == 0 ) { c [ s [ i ] - ' a ' ] = 1 ; pos = i ; ans += s [ i ] ; } } for ( int i = pos + 1 ; i < ( int ) s . size ( ) ; i ++ ) { if ( s [ i ] == s [ 0 ] ) { ans += s [ i ] ; break ; } } cout << ans ; } int main ( ) { string S = " abcaadbcd " ; RemoveCharacters ( S ) ; return 0 ; }
Check if a numeric string can be split into substrings having difference between consecutive numbers equal to K | C ++ program for the above approach ; Function to check if a numeric string can be split into substrings such that the difference between the consecutive substrings is K ; Stores the size of the string ; Iterate over the range [ 1 , N ] and try each possible starting number ; Convert the number to string ; Build up a sequence starting with the number ; Compare it with the original string s ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isPossible ( string s , int K ) { bool valid = false ; long firstx = -1 ; int n = s . length ( ) ; for ( int i = 1 ; i <= n / 2 ; ++ i ) { long x = stol ( s . substr ( 0 , i ) ) ; firstx = x ; string test = to_string ( x ) ; while ( test . length ( ) < s . length ( ) ) { x -= K ; test += to_string ( x ) ; } if ( test == s ) { valid = true ; break ; } } cout << ( ( valid == true ) ? " Yes ▁ " : " No " ) ; } int main ( ) { string S = "8642" ; int K = 2 ; isPossible ( S , K ) ; return 0 ; }
Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | C ++ program for the above approach ; Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , print " YES " else print " NO " ; Driver code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void changeCharacters ( string S , int N , int K ) { int flag = 1 ; int count = 0 ; stack < char > st ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . empty ( ) ) { if ( S [ i ] == '0' ) { flag = 0 ; break ; } count = 0 ; st . push ( S [ i ] ) ; } else { if ( S [ i ] == '0' ) { count ++ ; if ( count == K ) { st . pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { string S = "100100" ; int N = S . length ( ) ; int K = 2 ; changeCharacters ( S , N , K ) ; return 0 ; }
Check if all prefixes of a number is divisible by remaining count of digits | C ++ program for the above approach ; Function to check if all prefixes of a number is divisible by remaining count of digits or not ; Traverse and check divisibility for each updated number ; Update the original number ; Driver Code ; Given Input ; Function Call
#include <iostream> NEW_LINE using namespace std ; bool prefixDivisble ( int n ) { int i = 1 ; while ( n > 0 ) { if ( n % i != 0 ) return false ; n = n / 10 ; i ++ ; } return true ; } int main ( ) { int n = 52248 ; if ( prefixDivisble ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Length of the longest substring that contains even number of vowels | C ++ program for the above approach ; Function to find the length of the longest substring having even number of vowels ; Create two hashmaps ; Keep the track of frequencies of the vowels ; Stores the maximum length ; Traverse the given string S ; Find character in the map ; If it is a vowel , then update the frequency ; Find the index of occurence of the string evenOdd in map ; Update the maximum length ; Print the maximum length ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubstring ( string s ) { unordered_map < string , int > indexes ; unordered_map < char , int > chars ( { { ' a ' , 0 } , { ' e ' , 1 } , { ' i ' , 2 } , { ' o ' , 3 } , { ' u ' , 4 } } ) ; string evenOdd = "00000" ; indexes [ evenOdd ] = -1 ; int length = 0 ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { char c = s [ i ] ; auto it = chars . find ( c ) ; if ( it != chars . end ( ) ) { evenOdd [ it -> second ] = evenOdd [ it -> second ] == '0' ? '1' : '0' ; } auto lastIndex = indexes . find ( evenOdd ) ; if ( lastIndex == indexes . end ( ) ) { indexes [ evenOdd ] = i ; } else { length = max ( length , i - lastIndex -> second ) ; } } cout << length ; } int main ( ) { string S = " bcbcbc " ; longestSubstring ( S ) ; return 0 ; }
Minimum number whose binary form is not a subsequence of given binary string | C ++ program for the above approach ; Function to check if string str1 is a subsequence of string str2 ; Store index of str1 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 ; If matched , move ahead in str1 ; If all characters of str1 were found in str2 ; Function to find the minimum number which is not a subsequence of the given binary string in its binary form ; Store the decimal number of string , S ; Initialize the required result ; Iterate in the range [ 0 , R ] ; Convert integer i to binary string ; Check if the string is not a subsequence ; Update ans and break ; Print the required result ; Driver Code ; Function Call ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSubsequence ( string str1 , string str2 , int m , int n ) { int j = 0 ; for ( int i = 0 ; i < n && j < m ; i ++ ) if ( str1 [ j ] == str2 [ i ] ) j ++ ; return ( j == m ) ; } void findMinimumNumber ( string s ) { int r = stoi ( s , 0 , 2 ) ; int ans = r + 1 ; for ( int i = 0 ; i <= r ; i ++ ) { string p = " " ; int j = i ; while ( j > 0 ) { p += to_string ( j % 2 ) ; j = j / 2 ; } int m = p . length ( ) ; int n = s . length ( ) ; reverse ( p . begin ( ) , p . end ( ) ) ; if ( ! isSubsequence ( p , s , m , n ) ) { ans = i ; break ; } } cout << ans ; } int main ( ) { string s = "10101" ; findMinimumNumber ( s ) ; return 0 ; }
Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings | C ++ program for the above approach ; Function to calculate minimum cost to convert all the characters of S to '1' ; Stores the result ; Stores the number of groups that have 0 as characters ; Traverse the string S ; If current character is '0' ; If count is greater than 0 ; Set the count to 0 ; If the last few consecutive characters are '0' ; If string contains all characters as '1' ; Minimum Cost ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinimumCost ( string S , int A , int B ) { int count = 0 ; int group = 0 ; for ( int i = 0 ; i < S . size ( ) ; i ++ ) { if ( S [ i ] == '0' ) { count += 1 ; } else { if ( count > 0 ) { group += 1 ; } count = 0 ; } } if ( count > 0 ) group += 1 ; if ( group == 0 ) { cout << 0 << endl ; } else { cout << min ( A , B ) * ( group - 1 ) + B ; } } int main ( ) { int A = 1 ; int B = 5 ; string S = "01100" ; MinimumCost ( S , A , B ) ; return 0 ; }
Largest Prime Number possible from a subsequence of a Binary String | C ++ Program to implement the above approach ; Function to check if a number is prime or not ; Return not prime ; If prime return true ; Function to find the largest prime number possible from a subsequence ; Stores pairs of subsequences and their respective decimal value ; Stores the answer ; Traverse the string ; Stores the size of the vector ; Traverse the vector ; Extract the current pair ; Get the binary string from the pair ; Stores equivalent decimal values ; If the current character is '1' ; Add the character to the subsequence ; Update the value by left shifting the current value and adding 1 to it ; If s [ i ] = = '0' ; Add the character to the subsequence ; Update the value by left shifting the current value and adding 0 to it ; Store the subsequence in the vector ; Check if the decimal representation of current subsequence is prime or not ; If prime ; Update the answer with the largest one ; If no prime number could be obtained ; Driver Code ; Input String
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; bool isPrime ( int x ) { if ( x <= 1 ) return false ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return false ; } return true ; } void largestPrime ( string s ) { vector < pair < string , int > > vec { { " " , 0 } } ; int ans = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int n = vec . size ( ) ; for ( int j = 0 ; j < n ; j ++ ) { pair < string , int > temp = vec [ j ] ; string str = temp . first ; int val = temp . second ; if ( s [ i ] == '1' ) { temp . first = str + '1' ; temp . second = ( ( val << 1 ) + 1 ) ; } else { temp . first = str + '0' ; temp . second = ( ( val << 1 ) + 0 ) ; } vec . push_back ( temp ) ; int check = temp . second ; if ( isPrime ( check ) ) { ans = max ( ans , check ) ; } } } if ( ans == 0 ) cout << -1 << endl ; else cout << ans << endl ; } int main ( ) { string s = "110" ; largestPrime ( s ) ; return 0 ; }