codeparrot/xlcost-text-to-code huggingface dataset:
Count of ordered triplets ( R , G , B ) in a given original string | C ++ code for the above program ; function to count the ordered triplets ( R , G , B ) ; count the B ( blue ) colour ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( string color ) { int result = 0 , Blue_Count = 0 ; int Red_Count = 0 ; for ( char c : color ) { if ( c == ' B ' ) Blue_Count ++ ; } for ( char c : color ) { if ( c == ' B ' ) Blue_Count -- ; if ( c == ' R ' ) Red_Count ++ ; if ( c == ' G ' ) result += Red_Count * Blue_Count ; } return result ; } int main ( ) { string color = " RRGGBBRGGBB " ; cout << countTriplets ( color ) ; return 0 ; }
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | C ++ program to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Variable to store the sum of cubes ; Loop to find the number K ; If C is just greater then N , then break the loop ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int naive_find_x ( int N ) { int c = 0 , i ; for ( i = 1 ; i < N ; i ++ ) { c += i * i * i ; if ( c >= N ) break ; } return i ; } int main ( ) { int N = 100 ; cout << naive_find_x ( N ) ; return 0 ; }
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | C ++ program to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Left bound ; Right bound ; Variable to store the answer ; Applying binary search ; Calculating mid value of the range ; If the sum of cubes of first mid natural numbers is greater than equal to N iterate the left half ; Sum of cubes of first mid natural numbers is less than N , then move to the right segment ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binary_searched_find_x ( int k ) { int l = 0 ; int r = k ; int ans = 0 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( pow ( ( ( mid * ( mid + 1 ) ) / 2 ) , 2 ) >= k ) { ans = mid ; r = mid - 1 ; } else { l = mid + 1 ; } } return ans ; } int main ( ) { int N = 100 ; cout << binary_searched_find_x ( N ) ; return 0 ; }
Length of the longest ZigZag subarray of the given array | C ++ implementation to find the length of longest zigzag subarray of the given array ; Function to find the length of longest zigZag contiguous subarray ; ' max ' to store the length of longest zigZag subarray ; ' len ' to store the lengths of longest zigZag subarray at different instants of time ; Traverse the array from the beginning ; Check if ' max ' length is less than the length of the current zigzag subarray . If true , then update ' max ' ; Reset ' len ' to 1 as from this element , again the length of the new zigzag subarray is being calculated ; comparing the length of the last zigzag subarray with ' max ' ; Return required maximum length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lenOfLongZigZagArr ( int a [ ] , int n ) { int max = 1 , len = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && ( a [ i ] < a [ i + 1 ] ) ) len ++ ; else if ( i % 2 == 1 && ( a [ i ] > a [ i + 1 ] ) ) len ++ ; else { if ( max < len ) max = len ; len = 1 ; } } if ( max < len ) max = len ; return max ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << lenOfLongZigZagArr ( arr , n ) ; return 0 ; }
Check if a given number is a Perfect square using Binary Search | C ++ program to check if a given number is Perfect square using Binary Search ; function to check for perfect square number ; Find the mid value from start and last ; check if we got the number which is square root of the perfect square number N ; if the square ( mid ) is greater than N it means only lower values then mid will be possibly the square root of N ; if the square ( mid ) is less than N it means only higher values then mid will be possibly the square root of N ; Driver code
#include <iostream> NEW_LINE using namespace std ; int checkPerfectSquare ( long int N , long int start , long int last ) { long int mid = ( start + last ) / 2 ; if ( start > last ) { return -1 ; } if ( mid * mid == N ) { return mid ; } else if ( mid * mid > N ) { return checkPerfectSquare ( N , start , mid - 1 ) ; } else { return checkPerfectSquare ( N , mid + 1 , last ) ; } } int main ( ) { long int N = 65 ; cout << checkPerfectSquare ( N , 1 , N ) ; return 0 ; }
Maximum count number of valley elements in a subarray of size K | C ++ implementation to find the maximum number of valley elements in the subarrays of size K ; Function to find the valley elements in the array which contains in the subarrays of the size K ; Increment min_point if element at index i is smaller than element at index i + 1 and i - 1 ; final_point to maintain maximum of min points of subarray ; Iterate over array from kth element ; Leftmost element of subarray ; Rightmost element of subarray ; if new subarray have greater number of min points than previous subarray , then final_point is modified ; Max minimum points in subarray of size k ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minpoint ( int arr [ ] , int n , int k ) { int min_point = 0 ; for ( int i = 1 ; i < k - 1 ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) min_point += 1 ; } int final_point = min_point ; for ( int i = k ; i < n ; i ++ ) { if ( arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) + 1 ] && arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) - 1 ] ) min_point -= 1 ; if ( arr [ i - 1 ] < arr [ i ] && arr [ i - 1 ] < arr [ i - 2 ] ) min_point += 1 ; if ( min_point > final_point ) final_point = min_point ; } cout << ( final_point ) ; } int main ( ) { int arr [ ] = { 2 , 1 , 4 , 2 , 3 , 4 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; minpoint ( arr , n , k ) ; return 0 ; }
For each lowercase English alphabet find the count of strings having these alphabets | C ++ program to find count of strings for each letter [ a - z ] in english alphabet ; Function to find the countStrings for each letter [ a - z ] ; Initialize result as zero ; Mark all letter as not visited ; Loop through each strings ; Increment the global counter for current character of string ; Instead of re - initialising boolean vector every time we just reset all visited letter to false ; Print count for each letter ; Driver program ; Given array of strings ; Call the countStrings function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CountStrings ( vector < string > & str ) { int size = str . size ( ) ; vector < int > count ( 26 , 0 ) ; vector < bool > visited ( 26 , false ) ; for ( int i = 0 ; i < size ; ++ i ) { for ( int j = 0 ; j < str [ i ] . length ( ) ; ++ j ) { if ( visited [ str [ i ] [ j ] ] == false ) count [ str [ i ] [ j ] - ' a ' ] ++ ; visited [ str [ i ] [ j ] ] = true ; } for ( int j = 0 ; j < str [ i ] . length ( ) ; ++ j ) { visited [ str [ i ] [ j ] ] = false ; } } for ( int i = 0 ; i < 26 ; ++ i ) { cout << count [ i ] << " ▁ " ; } } int main ( ) { vector < string > str = { " i " , " will " , " practice " , " everyday " } ; CountStrings ( str ) ; return 0 ; }
Find subarray of Length K with Maximum Peak | C ++ implementation to Find subarray of Length K with Maximum Peak ; Function to find the subarray ; Make prefix array to store the prefix sum of peak count ; Count peak for previous index ; Check if this element is a peak ; Increment the count ; Check if number of peak in the sub array whose l = i is greater or not ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubArray ( int * a , int n , int k ) { int pref [ n ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i < n - 1 ; ++ i ) { pref [ i ] = pref [ i - 1 ] ; if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) pref [ i ] ++ ; } int peak = 0 , left = 0 ; for ( int i = 0 ; i + k - 1 < n ; ++ i ) if ( pref [ i + k - 2 ] - pref [ i ] > peak ) { peak = pref [ i + k - 2 ] - pref [ i ] ; left = i ; } cout << " Left ▁ = ▁ " << left + 1 << endl ; cout << " Right ▁ = ▁ " << left + k << endl ; cout << " Peak ▁ = ▁ " << peak << endl ; } int main ( ) { int arr [ ] = { 3 , 2 , 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; findSubArray ( arr , n , k ) ; return 0 ; }
Sort integers in array according to their distance from the element K | C ++ implementation to Sort the integers in array according to their distance from given element K present in the array ; Function to get sorted array based on their distance from given integer K ; Vector to store respective elements with their distance from integer K ; Find the position of integer K ; Insert the elements with their distance from K in vector ; Element at distance 0 ; Elements at left side of K ; Elements at right side of K ; Print the vector content in sorted order ; Sort elements at same distance ; Print elements at distance i from K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void distanceSort ( int arr [ ] , int K , int n ) { vector < int > vd [ n ] ; int pos ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == K ) { pos = i ; break ; } } int i = pos - 1 , j = pos + 1 ; vd [ 0 ] . push_back ( arr [ pos ] ) ; while ( i >= 0 ) { vd [ pos - i ] . push_back ( arr [ i ] ) ; -- i ; } while ( j < n ) { vd [ j - pos ] . push_back ( arr [ j ] ) ; ++ j ; } for ( int i = 0 ; i <= max ( pos , n - pos - 1 ) ; ++ i ) { sort ( begin ( vd [ i ] ) , end ( vd [ i ] ) ) ; for ( auto element : vd [ i ] ) cout << element << " ▁ " ; } } int main ( ) { int arr [ ] = { 14 , 1101 , 10 , 35 , 0 } , K = 35 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; distanceSort ( arr , K , n ) ; return 0 ; }
Find Quotient and Remainder of two integer without using division operators | C ++ implementation to Find Quotient and Remainder of two integer without using / and % operator using Binary search ; Function to the quotient and remainder ; Check if start is greater than the end ; Calculate mid ; Check if n is greater than divisor then increment the mid by 1 ; Check if n is less than 0 then decrement the mid by 1 ; Check if n equals to divisor ; Return the final answer ; Recursive calls ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > find ( int dividend , int divisor , int start , int end ) { if ( start > end ) return { 0 , dividend } ; int mid = start + ( end - start ) / 2 ; int n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return { mid , n } ; } return find ( dividend , divisor , start , end ) ; } pair < int , int > divide ( int dividend , int divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } int main ( int argc , char * argv [ ] ) { int dividend = 10 , divisor = 3 ; pair < int , int > ans ; ans = divide ( dividend , divisor ) ; cout << ans . first << " , ▁ " ; cout << ans . second << endl ; return 0 ; }
Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X | C ++ implementation to Count the number of unordered triplets such that the numbers are in increasing order and the product of them is less than or equal to integer X ; Function to count the number of triplets ; Iterate through all the triplets ; Rearrange the numbers in ascending order ; Check if the necessary conditions satisfy ; Increment count ; Return the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int a [ ] , int n , int x ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { vector < int > temp ; temp . push_back ( a [ i ] ) ; temp . push_back ( a [ j ] ) ; temp . push_back ( a [ k ] ) ; sort ( temp . begin ( ) , temp . end ( ) ) ; if ( temp [ 0 ] < temp [ 1 ] && temp [ 1 ] < temp [ 2 ] && temp [ 0 ] * temp [ 1 ] * temp [ 2 ] <= x ) answer ++ ; } } } return answer ; } int main ( ) { int A [ ] = { 3 , 2 , 5 , 7 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int X = 42 ; cout << countTriplets ( A , N , X ) ; return 0 ; }
Largest value of x such that axx is N | C ++ implementation of the above approach ; Function to find log_b ( a ) ; Set two pointer for binary search ; Calculating number of digits of a * mid ^ mid in base b ; If number of digits > n we can simply ignore it and decrease our pointer ; if number of digits <= n , we can go higher to reach value exactly equal to n ; return the largest value of x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double log ( int a , int b ) { return log10 ( a ) / log10 ( b ) ; } int get ( int a , int b , int n ) { int lo = 0 , hi = 1e6 ; int ans = 0 ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int dig = ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } int main ( ) { int a = 2 , b = 2 , n = 6 ; cout << get ( a , b , n ) << " STRNEWLINE " ; return 0 ; }
Maximum size square Sub | C ++ program for the above approach ; Function to find the maximum size of matrix with sum <= K ; N size of rows and M size of cols ; To store the prefix sum of matrix ; Create Prefix Sum ; Traverse each rows ; Update the prefix sum till index i x j ; To store the maximum size of matrix with sum <= K ; Traverse the sum matrix ; Index out of bound ; Maximum possible size of matrix ; Binary Search ; Find middle index ; Check whether sum <= K or not If Yes check for other half of the search ; Else check it in first half ; Update the maximum size matrix ; Print the final answer ; Driver Code ; Given target sum ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxMatrixSize ( vector < vector < int > > arr , int K ) { int i , j ; int n = arr . size ( ) ; int m = arr [ 0 ] . size ( ) ; int sum [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) { sum [ i ] [ j ] = 0 ; continue ; } sum [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ; } } int ans = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { if ( i + ans - 1 > n j + ans - 1 > m ) break ; int mid , lo = ans ; int hi = min ( n - i + 1 , m - j + 1 ) ; while ( lo < hi ) { mid = ( hi + lo + 1 ) / 2 ; if ( sum [ i + mid - 1 ] [ j + mid - 1 ] + sum [ i - 1 ] [ j - 1 ] - sum [ i + mid - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j + mid - 1 ] <= K ) { lo = mid ; } else { hi = mid - 1 ; } } ans = max ( ans , lo ) ; } } cout << ans << endl ; } int main ( ) { vector < vector < int > > arr ; arr = { { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } } ; int K = 4 ; findMaxMatrixSize ( arr , K ) ; return 0 ; }
Rank of all elements in a Stream in descending order when they arrive | C ++ program to rank of all elements in a Stream in descending order when they arrive ; FindRank function to find rank ; Rank of first element is always 1 ; Iterate over array ; As element let say its rank is 1 ; Element is compared with previous elements ; If greater than previous than rank is incremented ; print rank ; Driver code ; array named arr ; length of arr
#include <iostream> NEW_LINE using namespace std ; void FindRank ( int arr [ ] , int length ) { cout << "1" << " ▁ " ; for ( int i = 1 ; i < length ; i ++ ) { int rank = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) rank ++ ; } cout << rank << " ▁ " ; } } int main ( ) { int arr [ ] = { 88 , 14 , 69 , 30 , 29 , 89 } ; int len = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; FindRank ( arr , len ) ; return 0 ; }
Longest permutation subsequence in a given array | C ++ Program to find length of Longest Permutation Subsequence in a given array ; Function to find the longest permutation subsequence ; Map data structure to count the frequency of each element ; If frequency of element is 0 , then we can not move forward as every element should be present ; Increasing the length by one ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestPermutation ( int a [ ] , int n ) { map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; } int len = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( freq [ i ] == 0 ) { break ; } len ++ ; } return len ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestPermutation ( arr , n ) << " STRNEWLINE " ; return 0 ; }
Pair of fibonacci numbers with a given sum and minimum absolute difference | C ++ program to find the pair of Fibonacci numbers with a given sum and minimum absolute difference ; Hash to store all the Fibonacci numbers ; Function to generate fibonacci Series and create hash table to check Fibonacci numbers ; Adding the first two Fibonacci numbers into the Hash set ; Computing the remaining Fibonacci numbers based on the previous two numbers ; Function to find the pair of Fibonacci numbers with the given sum and minimum absolute difference ; Start from N / 2 such that the difference between i and N - i will be minimum ; If both ' i ' and ' sum ▁ - ▁ i ' are fibonacci numbers then print them and break the loop ; If there is no Fibonacci pair possible ; Driver code ; Generate the Fibonacci numbers ; Find the Fibonacci pair
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000005 NEW_LINE set < int > fib ; void fibonacci ( ) { int prev = 0 , curr = 1 , len = 2 ; fib . insert ( prev ) ; fib . insert ( curr ) ; while ( len <= MAX ) { int temp = curr + prev ; fib . insert ( temp ) ; prev = curr ; curr = temp ; len ++ ; } } void findFibonacci ( int N ) { for ( int i = N / 2 ; i > 1 ; i -- ) { if ( fib . find ( i ) != fib . end ( ) && fib . find ( N - i ) != fib . end ( ) ) { cout << i << " ▁ " << ( N - i ) << endl ; return ; } } cout << " - 1" << endl ; } int main ( ) { fibonacci ( ) ; int sum = 199 ; findFibonacci ( sum ) ; return 0 ; }
Check if sum of Fibonacci elements in an Array is a Fibonacci number or not | C ++ program to check whether the sum of fibonacci elements of the array is a Fibonacci number or not ; Hash to store the Fibonacci numbers up to Max ; Function to create the hash table to check Fibonacci numbers ; Inserting the first two Fibonacci numbers into the hash ; Add the remaining Fibonacci numbers based on the previous two numbers ; Function to check if the sum of Fibonacci numbers is Fibonacci or not ; Find the sum of all Fibonacci numbers ; Iterating through the array ; If the sum is Fibonacci then return true ; Driver code ; array of elements ; Creating a set containing all fibonacci numbers
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE #define MAX  100005 NEW_LINE using namespace std ; set < int > fibonacci ; void createHash ( ) { int prev = 0 , curr = 1 ; fibonacci . insert ( prev ) ; fibonacci . insert ( curr ) ; while ( curr <= MAX ) { int temp = curr + prev ; fibonacci . insert ( temp ) ; prev = curr ; curr = temp ; } } bool checkArray ( int arr [ ] , int n ) { ll sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( fibonacci . find ( arr [ i ] ) != fibonacci . end ( ) ) sum += arr [ i ] ; if ( fibonacci . find ( sum ) != fibonacci . end ( ) ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 8 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; createHash ( ) ; if ( checkArray ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of numbers whose difference with Fibonacci count upto them is atleast K | C ++ program to find the count of numbers whose difference with Fibonacci count upto them is atleast K ; fibUpto [ i ] denotes the count of fibonacci numbers upto i ; Function to compute all the Fibonacci numbers and update fibUpto array ; Store the first two Fibonacci numbers ; Compute the Fibonacci numbers and store them in isFib array ; Compute fibUpto array ; Function to return the count of valid numbers ; Compute fibUpto array ; Binary search to find the minimum number that follows the condition ; Check if the number is valid , try to reduce it ; Ans is the minimum valid number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000005 ; int fibUpto [ MAX + 1 ] ; void compute ( int sz ) { bool isFib [ sz + 1 ] ; memset ( isFib , false , sizeof ( isFib ) ) ; int prev = 0 , curr = 1 ; isFib [ prev ] = isFib [ curr ] = true ; while ( curr <= sz ) { int temp = curr + prev ; isFib [ temp ] = true ; prev = curr ; curr = temp ; } fibUpto [ 0 ] = 1 ; for ( int i = 1 ; i <= sz ; i ++ ) { fibUpto [ i ] = fibUpto [ i - 1 ] ; if ( isFib [ i ] ) fibUpto [ i ] ++ ; } } int countOfNumbers ( int N , int K ) { compute ( N ) ; int low = 1 , high = N , ans = 0 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( mid - fibUpto [ mid ] >= K ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ( ans ? N - ans + 1 : 0 ) ; } int main ( ) { int N = 10 , K = 3 ; cout << countOfNumbers ( N , K ) ; }
Smallest Palindromic Subsequence of Even Length in Range [ L , R ] | C ++ program to find lexicographically smallest palindromic subsequence of even length ; Frequency array for each character ; Preprocess the frequency array calculation ; Frequency array to track each character in position ' i ' ; Calculating prefix sum over this frequency array to get frequency of a character in a range [ L , R ] . ; Util function for palindromic subsequences ; Find frequency of all characters ; For each character find it 's frequency  in range [L, R] ; If frequency in this range is > 1 , then we must take this character , as it will give lexicographically smallest one ; There is no character in range [ L , R ] such that it 's frequency is > 1. ; Return the character 's value ; Function to find lexicographically smallest palindromic subsequence of even length ; Find in the palindromic subsequences ; No such subsequence exists ; Driver Code ; Function calls
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 100001 ; int f [ 26 ] [ N ] ; void precompute ( string s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { f [ s [ i ] - ' a ' ] [ i ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { f [ i ] [ j ] += f [ i ] [ j - 1 ] ; } } } int palindromicSubsequencesUtil ( int L , int R ) { int c , ok = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int cnt = f [ i ] [ R ] ; if ( L > 0 ) cnt -= f [ i ] [ L - 1 ] ; if ( cnt > 1 ) { ok = 1 ; c = i ; break ; } } if ( ok == 0 ) { return -1 ; } return c ; } void palindromicSubsequences ( int Q [ ] [ 2 ] , int l ) { for ( int i = 0 ; i < l ; i ++ ) { int x = palindromicSubsequencesUtil ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; if ( x == -1 ) { cout << -1 << " STRNEWLINE " ; } else { char c = ' a ' + x ; cout << c << c << " STRNEWLINE " ; } } } int main ( ) { string str = " dbdeke " ; int Q [ ] [ 2 ] = { { 0 , 5 } , { 1 , 5 } , { 1 , 3 } } ; int n = str . size ( ) ; int l = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; precompute ( str , n ) ; palindromicSubsequences ( Q , l ) ; return 0 ; }