codeparrot/xlcost-text-to-code huggingface dataset:
Find element position in given monotonic sequence | C ++ implementation of the approach ; Function to return the value of f ( n ) for given values of a , b , c , n ; if c is 0 , then value of n can be in order of 10 ^ 15. if c != 0 , then n ^ 3 value has to be in order of 10 ^ 18 so maximum value of n can be 10 ^ 6. ; for efficient searching , use binary search . ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE #define SMALL_N  1000000 NEW_LINE #define LARGE_N  1000000000000000 NEW_LINE using namespace std ; long long func ( long long a , long long b , long long c , long long n ) { long long res = a * n ; long long logVlaue = floor ( log2 ( n ) ) ; res += b * n * logVlaue ; res += c * ( n * n * n ) ; return res ; } long long getPositionInSeries ( long long a , long long b , long long c , long long k ) { long long start = 1 , end = SMALL_N ; if ( c == 0 ) { end = LARGE_N ; } long long ans = 0 ; while ( start <= end ) { long long mid = ( start + end ) / 2 ; long long val = func ( a , b , c , mid ) ; if ( val == k ) { ans = mid ; break ; } else if ( val > k ) { end = mid - 1 ; } else { start = mid + 1 ; } } return ans ; } int main ( ) { long long a = 2 , b = 1 , c = 1 ; long long k = 12168587437017 ; cout << getPositionInSeries ( a , b , c , k ) ; return 0 ; }
Check if array can be sorted with one swap | CPP program to check if an array can be sorted with at - most one swap ; Create a sorted copy of original array ; Check if 0 or 1 swap required to get the sorted array ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkSorted ( int n , int arr [ ] ) { int b [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = arr [ i ] ; sort ( b , b + n ) ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != b [ i ] ) ct ++ ; if ( ct == 0 ct == 2 ) return true ; else return false ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkSorted ( n , arr ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether ( i , j ) exists such that arr [ i ] != arr [ j ] and arr [ arr [ i ] ] is equal to arr [ arr [ j ] ] | C ++ implementation of the above approach ; Function that will tell whether such Indices present or Not . ; Checking 1 st condition i . e whether Arr [ i ] equal to Arr [ j ] or not ; Checking 2 nd condition i . e whether Arr [ Arr [ i ] ] equal to Arr [ Arr [ j ] ] or not . ; Driver Code ; Calling function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIndices ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] != Arr [ j ] ) { if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) return true ; } } } return false ; } int main ( ) { int Arr [ ] = { 3 , 2 , 1 , 1 , 4 } ; int N = sizeof ( Arr ) / sizeof ( Arr [ 0 ] ) ; checkIndices ( Arr , N ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Find two non | C ++ programs to find two non - overlapping pairs having equal sum in an Array ; Function to find two non - overlapping with same sum in an array ; first create an empty map key -> which is sum of a pair of elements in the array value -> vector storing index of every pair having that sum ; consider every pair ( arr [ i ] , arr [ j ] ) and where ( j > i ) ; calculate sum of current pair ; if sum is already present in the map ; check every pair having equal sum ; if pairs don 't overlap,  print them and return ; Insert current pair into the map ; If no such pair found ; Driver Code
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; typedef pair < int , int > Pair ; void findPairs ( int arr [ ] , int n ) { unordered_map < int , vector < Pair > > map ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( map . find ( sum ) != map . end ( ) ) { for ( auto pair : map . find ( sum ) -> second ) { int m = pair . first , n = pair . second ; if ( ( m != i && m != j ) && ( n != i && n != j ) ) { cout << " Pair ▁ First ( " << arr [ i ] << " , ▁ " << arr [ j ] << " Pair Second ( " ▁ < < ▁ arr [ m ] ▁ < < ▁ " , " ▁ < < ▁ arr [ n ] ▁ < < ▁ " ) " return ; } } } map [ sum ] . push_back ( { i , j } ) ; } } cout << " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ; } int main ( ) { int arr [ ] = { 8 , 4 , 7 , 8 , 4 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPairs ( arr , size ) ; return 0 ; }
Print all pairs with given sum | C ++ implementation of simple method to find print pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Consider all possible pairs and check their sums ; Driver function to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printPairs ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) cout << " ( " << arr [ i ] << " , ▁ " << arr [ j ] << " ) " << endl ; } int main ( ) { int arr [ ] = { 1 , 5 , 7 , -1 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int sum = 6 ; printPairs ( arr , n , sum ) ; return 0 ; }
Ways to choose three points with distance between the most distant points <= L | C ++ program to count ways to choose triplets such that the distance between the farthest points <= L ; Returns the number of triplets with the distance between farthest points <= L ; sort the array ; find index of element greater than arr [ i ] + L ; find Number of elements between the ith index and indexGreater since the Numbers are sorted and the elements are distinct from the points btw these indices represent points within range ( a [ i ] + 1 and a [ i ] + L ) both inclusive ; if there are at least two elements in between i and indexGreater find the Number of ways to select two points out of these ; Driver Code ; set of n points on the X axis
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTripletsLessThanL ( int n , int L , int * arr ) { sort ( arr , arr + n ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int indexGreater = upper_bound ( arr , arr + n , arr [ i ] + L ) - arr ; int numberOfElements = indexGreater - ( i + 1 ) ; if ( numberOfElements >= 2 ) { ways += ( numberOfElements * ( numberOfElements - 1 ) / 2 ) ; } } return ways ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int L = 4 ; int ans = countTripletsLessThanL ( n , L , arr ) ; cout << " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " << ans << " STRNEWLINE " ; return 0 ; }
Making elements distinct in a sorted array by minimum increments | CPP program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int minSum ( int arr [ ] , int n ) { int sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { int j = i ; while ( j < n && arr [ j ] <= arr [ j - 1 ] ) { arr [ j ] = arr [ j ] + 1 ; j ++ ; } } sum = sum + arr [ i ] ; } return sum ; } int main ( ) { int arr [ ] = { 2 , 2 , 3 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minSum ( arr , n ) << endl ; return 0 ; }
Making elements distinct in a sorted array by minimum increments | Efficient CPP program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Drivers code
#include <iostream> NEW_LINE using namespace std ; int minSum ( int arr [ ] , int n ) { int sum = arr [ 0 ] , prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= prev ) { prev = prev + 1 ; sum = sum + prev ; } else { sum = sum + arr [ i ] ; prev = arr [ i ] ; } } return sum ; } int main ( ) { int arr [ ] = { 2 , 2 , 3 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minSum ( arr , n ) << endl ; return 0 ; }
Randomized Binary Search Algorithm | C ++ program to implement recursive randomized algorithm . ; To generate random number between x and y ie . . [ x , y ] ; A recursive randomized binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code
#include <iostream> NEW_LINE #include <ctime> NEW_LINE using namespace std ; int getRandom ( int x , int y ) { srand ( time ( NULL ) ) ; return ( x + rand ( ) % ( y - x + 1 ) ) ; } int randomizedBinarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = getRandom ( l , r ) ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return randomizedBinarySearch ( arr , l , mid - 1 , x ) ; return randomizedBinarySearch ( arr , mid + 1 , r , x ) ; } return -1 ; } int main ( void ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) ; ( result == -1 ) ? printf ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) : printf ( " Element ▁ is ▁ present ▁ at ▁ index ▁ % d " , result ) ; return 0 ; }
Program to remove vowels from a String | C ++ program to remove vowels from a String ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string remVowel ( string str ) { vector < char > vowels = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' , ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( find ( vowels . begin ( ) , vowels . end ( ) , str [ i ] ) != vowels . end ( ) ) { str = str . replace ( i , 1 , " " ) ; i -= 1 ; } } return str ; } int main ( ) { string str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer " " ▁ Science ▁ Portal ▁ for ▁ Geeks " ; cout << remVowel ( str ) << endl ; return 0 ; }
Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first . ; If there are odd elements , we choose middle element ; If there are even elements , then we choose the average of middle two . ; After deciding the final value , find the result . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCostToMakeElementEqual ( int a [ ] , int n ) { int y ; if ( n % 2 == 1 ) y = a [ n / 2 ] ; else y = ( a [ n / 2 ] + a [ ( n - 2 ) / 2 ] ) / 2 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) s += abs ( a [ i ] - y ) ; return s ; } int main ( ) { int a [ ] = { 1 , 100 , 101 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << ( minCostToMakeElementEqual ( a , n ) ) ; }
Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Driver Code ; Given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSorted ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } bool sortPoss ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int idx = -1 ; int minVar = arr [ i ] ; int j = i ; while ( j < n ) { if ( arr [ j ] < minVar ) { minVar = arr [ j ] ; idx = j ; } j = j + 2 ; } if ( idx != -1 ) { swap ( arr [ i ] , arr [ idx ] ) ; } } if ( isSorted ( arr , n ) ) return true ; else return false ; } int main ( ) { int arr [ ] = { 3 , 5 , 1 , 2 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( sortPoss ( arr , n ) ) cout << " True " ; else cout << " False " ; return 0 ; }
Print all array elements appearing more than N / K times | C ++ program to implement the above approach ; Function to + find the upper_bound of an array element ; Stores minimum index in which K lies ; Stores maximum index in which K lies ; Calculate the upper bound of K ; Stores mid element of l and r ; If arr [ mid ] is less than or equal to K ; Right subarray ; Left subarray ; Function to print all array elements whose frequency is greater than N / K ; Sort the array arr [ ] ; Stores index of an array element ; Traverse the array ; Stores upper bound of arr [ i ] ; If frequency of arr [ i ] is greater than N / 4 ; Update i ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int upperBound ( int arr [ ] , int N , int K ) { int l = 0 ; int r = N ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( arr [ mid ] <= K ) { l = mid + 1 ; } else { r = mid ; } } return l ; } void NDivKWithFreq ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int i = 0 ; while ( i < N ) { int X = upperBound ( arr , N , arr [ i ] ) ; if ( ( X - i ) > N / 4 ) { cout << arr [ i ] << " ▁ " ; } i = X ; } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 4 ; NDivKWithFreq ( arr , N , K ) ; return 0 ; }
Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 | C ++ program for the above approach ; Function to find maximum possible sum after changing the array elements as per the given constraints ; Stores the frequency of elements in given array ; Update frequency ; Stores the previously selected integer ; Stores the maximum possible sum ; Traverse over array count [ ] ; Run loop for each k ; Update ans ; Return maximum possible sum ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <iostream> NEW_LINE using namespace std ; long maxSum ( int a [ ] , int n ) { int count [ n + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ min ( a [ i ] , n ) ] ++ ; int size = 0 ; long ans = 0 ; for ( int k = 1 ; k <= n ; k ++ ) { while ( count [ k ] > 0 && size < k ) { size ++ ; ans += size ; count [ k ] -- ; } ans += k * count [ k ] ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 5 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( maxSum ( arr , n ) ) ; return 0 ; }
Find sum of product of every number and its frequency in given range | C ++ implementation to find sum of product of every number and square of its frequency in the given range ; Function to solve queries ; Calculating answer for every query ; The end points of the ith query ; map for storing frequency ; Incrementing the frequency ; Iterating over map to find answer ; adding the contribution of ith number ; print answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void answerQueries ( int arr [ ] , int n , vector < pair < int , int > > & queries ) { for ( int i = 0 ; i < queries . size ( ) ; i ++ ) { int ans = 0 ; int l = queries [ i ] . first - 1 ; int r = queries [ i ] . second - 1 ; map < int , int > freq ; for ( int j = l ; j <= r ; j ++ ) { freq [ arr [ j ] ] ++ ; } for ( auto & i : freq ) { ans += ( i . first * i . second ) ; } cout << ans << endl ; } } int main ( ) { int arr [ ] = { 1 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < pair < int , int > > queries = { { 1 , 2 } , { 1 , 3 } } ; answerQueries ( arr , n , queries ) ; }
Longest subsequence with first and last element greater than all other elements | C ++ implementation of the above approach ; Comparison functions ; Function to update the value in Fenwick tree ; Function to return the query result ; Function to return the length of subsequence ; Store the value in struct Array ; If less than 2 elements are present return that element . ; Set the left and right pointers to extreme ; Calculate left and right pointer index . ; Store the queries from [ L + 1 , R - 1 ] . ; Sort array and queries for fenwick updates ; For each query calculate maxx for the answer and store it in ans array . ; Mx will be mx + 2 as while calculating mx , we excluded element at index left and right ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Query { int l , r , x , index ; } ; struct Arrays { int val , index ; } ; bool cmp1 ( Query q1 , Query q2 ) { return q1 . x < q2 . x ; } bool cmp2 ( Arrays x , Arrays y ) { return x . val < y . val ; } void update ( int * Fenwick , int index , int val , int n ) { while ( index <= n ) { Fenwick [ index ] += val ; index += index & ( - index ) ; } } int query ( int * Fenwick , int index , int n ) { int sum = 0 ; while ( index > 0 ) { sum = sum + Fenwick [ index ] ; index -= index & ( - index ) ; } return sum ; } int maxLength ( int n , vector < int > & v ) { int where [ n + 2 ] ; memset ( where , 0 , sizeof where ) ; Arrays arr [ n ] ; for ( int i = 1 ; i <= n ; ++ i ) { v [ i - 1 ] = v [ i - 1 ] - 1 ; int x = v [ i - 1 ] ; where [ x ] = i - 1 ; arr [ i - 1 ] . val = x ; arr [ i - 1 ] . index = i - 1 ; } if ( n <= 2 ) { cout << n << endl ; return 0 ; } int left = n , right = 0 , mx = 0 ; Query queries [ 4 * n ] ; int j = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { left = min ( left , where [ i ] ) ; right = max ( right , where [ i ] ) ; int diff = right - left ; if ( diff == 0 diff == 1 ) { continue ; } int val1 = v [ left ] ; int val2 = v [ right ] ; int minn = min ( val1 , val2 ) ; queries [ j ] . l = left + 1 ; queries [ j ] . r = right - 1 ; queries [ j ] . x = minn ; queries [ j ] . index = j ; ++ j ; } int Fenwick [ n + 1 ] ; memset ( Fenwick , 0 , sizeof Fenwick ) ; int q = j - 1 ; sort ( arr , arr + n + 1 , cmp2 ) ; sort ( queries , queries + q + 1 , cmp1 ) ; int curr = 0 ; int ans [ q ] ; memset ( ans , 0 , sizeof ans ) ; for ( int i = 0 ; i <= q ; ++ i ) { while ( arr [ curr ] . val <= queries [ i ] . x and curr < n ) { update ( Fenwick , arr [ curr ] . index + 1 , 1 , n ) ; curr ++ ; } ans [ queries [ i ] . index ] = query ( Fenwick , queries [ i ] . r + 1 , n ) - query ( Fenwick , queries [ i ] . l , n ) ; } for ( int i = 0 ; i <= q ; ++ i ) { mx = max ( mx , ans [ i ] ) ; } mx = mx + 2 ; return mx ; } int main ( ) { int n = 6 ; vector < int > v = { 4 , 2 , 6 , 5 , 3 , 1 } ; cout << maxLength ( n , v ) << endl ; return 0 ; }
Pandigital Product | C ++ code to check the number is Pandigital Product or not ; To check the string formed from multiplicand , multiplier and product is pandigital ; calculate the multiplicand , multiplier , and product eligible for pandigital ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPandigital ( string str ) { if ( str . length ( ) != 9 ) return false ; char ch [ str . length ( ) ] ; strcpy ( ch , str . c_str ( ) ) ; sort ( ch , ch + str . length ( ) ) ; string s = ch ; if ( s . compare ( "123456789" ) == 0 ) return true ; else return true ; } bool PandigitalProduct_1_9 ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 && isPandigital ( to_string ( n ) + to_string ( i ) + to_string ( n / i ) ) ) return true ; return false ; } int main ( ) { int n = 6952 ; if ( PandigitalProduct_1_9 ( n ) == true ) cout << " yes " ; else cout << " no " ; return 0 ; }
Median and Mode using Counting Sort | C ++ Program for Mode and Median using Counting Sort technique ; function that sort input array a [ ] and calculate mode and median using counting sort . ; The output array b [ ] will have sorted array ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Update count [ ] array with sum ; Sorted output array b [ ] to calculate median ; Median according to odd and even array size respectively . ; Output the result ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printModeMedian ( int a [ ] , int n ) { int b [ n ] ; int max = * max_element ( a , a + n ) ; int t = max + 1 ; int count [ t ] ; for ( int i = 0 ; i < t ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } for ( int i = 1 ; i < t ; i ++ ) count [ i ] = count [ i ] + count [ i - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { b [ count [ a [ i ] ] - 1 ] = a [ i ] ; count [ a [ i ] ] -- ; } float median ; if ( n % 2 != 0 ) median = b [ n / 2 ] ; else median = ( b [ ( n - 1 ) / 2 ] + b [ ( n / 2 ) ] ) / 2.0 ; cout << " median ▁ = ▁ " << median << endl ; cout << " mode ▁ = ▁ " << mode ; } int main ( ) { int a [ ] = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printModeMedian ( a , n ) ; return 0 ; }
Check if both halves of the string have at least one different character | C ++ implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function
#include <cstring> NEW_LINE #include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; # define MAX  26 NEW_LINE bool function ( string str ) { int l = str . length ( ) ; int counter1 [ MAX ] ; int counter2 [ MAX ] ; memset ( counter1 , 0 , sizeof ( counter1 ) ) ; memset ( counter2 , 0 , sizeof ( counter2 ) ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) counter1 [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter2 [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) if ( counter2 [ i ] != counter1 [ i ] ) return true ; return false ; } int main ( ) { string str = " abcasdsabcae " ; if ( function ( str ) ) cout << " Yes , ▁ both ▁ halves ▁ differ " << " ▁ by ▁ at ▁ least ▁ one ▁ character " ; else cout << " No , ▁ both ▁ halves ▁ do ▁ " << " not ▁ differ ▁ at ▁ all " ; return 0 ; }
Check if both halves of the string have at least one different character | C ++ implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
#include <cstring> NEW_LINE #include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; # define MAX  26 NEW_LINE bool function ( string str ) { int l = str . length ( ) ; int counter [ MAX ] ; memset ( counter , 0 , sizeof ( counter ) ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) counter [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter [ str [ i ] - ' a ' ] -- ; for ( int i = 0 ; i < MAX ; i ++ ) if ( counter [ i ] != 0 ) return true ; return false ; } int main ( ) { string str = " abcasdsabcae " ; if ( function ( str ) ) cout << " Yes , ▁ both ▁ halves ▁ differ " << " ▁ by ▁ at ▁ least ▁ one ▁ character " ; else cout << " No , ▁ both ▁ halves ▁ do " << " ▁ not ▁ differ ▁ at ▁ all " ; return 0 ; }