codeparrot/xlcost-text-to-code huggingface dataset:
Leftmost Column with atleast one 1 in a row | C ++ program to calculate leftmost column having at least a 1 ; Function return leftmost column having at least a 1 ; If current element is 1 decrement column by 1 ; If current element is 0 increment row by 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  4 NEW_LINE int FindColumn ( int mat [ N ] [ M ] ) { int row = 0 , col = M - 1 ; int flag = 0 ; while ( row < N && col >= 0 ) { if ( mat [ row ] [ col ] == 1 ) { col -- ; flag = 1 ; } else { row ++ ; } } col ++ ; if ( flag ) return col + 1 ; else return -1 ; } int main ( ) { int mat [ N ] [ M ] = { { 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 1 } } ; cout << FindColumn ( mat ) ; return 0 ; }
Find two numbers made up of a given digit such that their difference is divisible by N | C ++ implementation of the above approach ; Function to implement the above approach ; Hashmap to store remainder - length of the number as key - value pairs ; Iterate till N + 1 length ; Search remainder in the map ; If remainder is not already present insert the length for the corresponding remainder ; Keep increasing M ; To keep M in range of integer ; Length of one number is the current Length ; Length of the other number is the length paired with current remainder in map ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int N , int M ) { int m = M ; map < int , int > remLen ; int len , remainder ; for ( len = 1 ; len <= N + 1 ; ++ len ) { remainder = M % N ; if ( remLen . find ( remainder ) == remLen . end ( ) ) remLen [ remainder ] = len ; else break ; M = M * 10 + m ; M = M % N ; } int LenA = len ; int LenB = remLen [ remainder ] ; for ( int i = 0 ; i < LenB ; ++ i ) cout << m ; cout << " ▁ " ; for ( int i = 0 ; i < LenA ; ++ i ) cout << m ; return ; } int main ( ) { int N = 8 , M = 2 ; findNumbers ( N , M ) ; return 0 ; }
Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements | C ++ implementation to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; 2D prefix sum for submatrix sum query for matrix ; Function to find the prefix sum of the matrix from i and j ; Function to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; Combining the two arrays ; Calculating the 2D prefix sum ; iterating through all the elements of matrix and considering them to be the bottom right ; applying binary search over side length ; if sum of this submatrix >= k then new search space will be [ low , mid ] ; else new search space will be [ mid + 1 , high ] ; Adding the total submatrices ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prefix_2D [ 2005 ] [ 2005 ] ; int subMatrixSum ( int i , int j , int len ) { return prefix_2D [ i ] [ j ] - prefix_2D [ i ] [ j - len ] - prefix_2D [ i - len ] [ j ] + prefix_2D [ i - len ] [ j - len ] ; } int numberOfWays ( int a [ ] , int b [ ] , int n , int m , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( a [ i - 1 ] == b [ j - 1 ] ) prefix_2D [ i ] [ j ] = 1 ; else prefix_2D [ i ] [ j ] = 0 ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { prefix_2D [ i ] [ j ] += prefix_2D [ i ] [ j - 1 ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { prefix_2D [ i ] [ j ] += prefix_2D [ i - 1 ] [ j ] ; } } int answer = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int low = 1 ; int high = min ( i , j ) ; while ( low < high ) { int mid = ( low + high ) >> 1 ; if ( subMatrixSum ( i , j , mid ) >= k ) { high = mid ; } else { low = mid + 1 ; } } if ( subMatrixSum ( i , j , low ) >= k ) { answer += ( min ( i , j ) - low + 1 ) ; } } } return answer ; } int main ( ) { int N = 2 , M = 3 ; int A [ N ] = { 1 , 2 } ; int B [ M ] = { 1 , 2 , 3 } ; int K = 1 ; cout << numberOfWays ( A , B , N , M , K ) ; return 0 ; }
Find lexicographically smallest string in at most one swaps | C ++ implementation of the above approach ; Function to return the lexicographically smallest string that can be formed by swapping at most one character . The characters might not necessarily be adjacent . ; Store last occurrence of every character ; Set - 1 as default for every character . ; Character index to fill in the last occurrence array ; If this is true then this character is being visited for the first time from the last Thus last occurrence of this character is stored in this index ; Character to replace ; Find the last occurrence of this character . ; Swap this with the last occurrence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findSmallest ( string s ) { int len = s . size ( ) ; int loccur [ 26 ] ; memset ( loccur , -1 , sizeof ( loccur ) ) ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == -1 ) { loccur [ chI ] = i ; } } string sorted_s = s ; sort ( sorted_s . begin ( ) , sorted_s . end ( ) ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; swap ( s [ i ] , s [ last_occ ] ) ; break ; } } return s ; } int main ( ) { string s = " geeks " ; cout << findSmallest ( s ) ; return 0 ; }
Maximum size of square such that all submatrices of that size have sum less than K | C ++ implementation to find the maximum size square submatrix such that their sum is less than K ; Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to check whether square sub matrices of size mid satisfy the condition or not ; Iterating throught all possible submatrices of given size ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Search space ; Binary search for size ; Check if the mid satisfies the given condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE #define M  5 NEW_LINE void preProcess ( int mat [ N ] [ M ] , int aux [ N ] [ M ] ) { for ( int i = 0 ; i < M ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 1 ; j < M ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } int sumQuery ( int aux [ N ] [ M ] , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } bool check ( int mid , int aux [ N ] [ M ] , int K ) { bool satisfies = true ; for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < M ; y ++ ) { if ( x + mid - 1 <= N - 1 && y + mid - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + mid - 1 , y + mid - 1 ) > K ) satisfies = false ; } } } return ( satisfies == true ) ; } int maximumSquareSize ( int mat [ N ] [ M ] , int K ) { int aux [ N ] [ M ] ; preProcess ( mat , aux ) ; int low = 1 , high = min ( N , M ) ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , aux , K ) ) { low = mid ; } else high = mid ; } if ( check ( high , aux , K ) ) return high ; return low ; } int main ( ) { int K = 30 ; int mat [ N ] [ M ] = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; cout << maximumSquareSize ( mat , K ) ; return 0 ; }
Count of numbers from range [ L , R ] that end with any of the given digits | C ++ implementation of the approach ; Function to return the count of the required numbers ; Last digit of the current number ; If the last digit is equal to any of the given digits ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countNums ( int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { int lastDigit = ( i % 10 ) ; if ( ( lastDigit % 10 ) == 2 || ( lastDigit % 10 ) == 3 || ( lastDigit % 10 ) == 9 ) { cnt ++ ; } } return cnt ; } int main ( ) { int l = 11 , r = 33 ; cout << countNums ( l , r ) ; }
Minimum K such that sum of array elements after division by K does not exceed S | C ++ implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; Find the maximum element ; Lowest answer can be 1 and the highest answer can be ( maximum + 1 ) ; Binary search ; Get the mid element ; Calculate the sum after dividing the array by new K which is mid ; Search in the second half ; First half ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimumK ( int a [ ] , int n , int s ) { int maximum = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { maximum = max ( maximum , a [ i ] ) ; } int low = 1 , high = maximum + 1 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( a [ i ] / mid ) ; } if ( sum > s ) low = mid + 1 ; else { ans = min ( ans , mid ) ; high = mid - 1 ; } } return ans ; } int main ( ) { int a [ ] = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int s = 27 ; cout << findMinimumK ( a , n , s ) ; return 0 ; }
Calculate the Sum of GCD over all subarrays | C ++ program to find Sum of GCD over all subarrays ; int a [ 100001 ] ; ; Build Sparse Table ; Building the Sparse Table for GCD [ L , R ] Queries ; Utility Function to calculate GCD in range [ L , R ] ; Calculating where the answer is stored in our Sparse Table ; Utility Function to find next - farther position where gcd is same ; BinarySearch for Next Position for EndPointer ; Utility function to calculate sum of gcd ; Initializing all the values ; Finding the next position for endPointer ; Adding the suitable sum to our answer ; Changing prevEndPointer ; Recalculating tempGCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SparseTable [ 100001 ] [ 51 ] ; void buildSparseTable ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { SparseTable [ i ] [ 0 ] = a [ i ] ; } for ( int j = 1 ; j <= 19 ; j ++ ) { for ( int i = 0 ; i <= n - ( 1 << j ) ; i ++ ) { SparseTable [ i ] [ j ] = __gcd ( SparseTable [ i ] [ j - 1 ] , SparseTable [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ) ; } } } int queryForGCD ( int L , int R ) { int returnValue ; int j = int ( log2 ( R - L + 1 ) ) ; returnValue = __gcd ( SparseTable [ L ] [ j ] , SparseTable [ R - ( 1 << j ) + 1 ] [ j ] ) ; return returnValue ; } int nextPosition ( int tempGCD , int startPointer , int prevEndPointer , int n ) { int high = n - 1 ; int low = prevEndPointer ; int mid = prevEndPointer ; int nextPos = prevEndPointer ; while ( high >= low ) { mid = ( ( high + low ) >> 1 ) ; if ( queryForGCD ( startPointer , mid ) == tempGCD ) { nextPos = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return nextPos + 1 ; } int calculateSum ( int a [ ] , int n ) { buildSparseTable ( a , n ) ; int endPointer , startPointer , prevEndPointer , tempGCD ; int tempAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { endPointer = i ; startPointer = i ; prevEndPointer = i ; tempGCD = a [ i ] ; while ( endPointer < n ) { endPointer = nextPosition ( tempGCD , startPointer , prevEndPointer , n ) ; tempAns += ( ( endPointer - prevEndPointer ) * tempGCD ) ; prevEndPointer = endPointer ; if ( endPointer < n ) { tempGCD = __gcd ( tempGCD , a [ endPointer ] ) ; } } } return tempAns ; } int main ( ) { int n = 6 ; int a [ ] = { 2 , 2 , 2 , 3 , 5 , 5 } ; cout << calculateSum ( a , n ) << " STRNEWLINE " ; return 0 ; }
QuickSelect ( A Simple Iterative Implementation ) | CPP program for iterative implementation of QuickSelect ; Standard Lomuto partition function ; Implementation of QuickSelect ; Partition a [ left . . right ] around a pivot and find the position of the pivot ; If pivot itself is the k - th smallest element ; If there are more than k - 1 elements on left of pivot , then k - th smallest must be on left side . ; Else k - th smallest is on right side . ; Driver program to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int partition ( int arr [ ] , int low , int high ) { int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; swap ( arr [ i ] , arr [ j ] ) ; } } swap ( arr [ i + 1 ] , arr [ high ] ) ; return ( i + 1 ) ; } int kthSmallest ( int a [ ] , int left , int right , int k ) { while ( left <= right ) { int pivotIndex = partition ( a , left , right ) ; if ( pivotIndex == k - 1 ) return a [ pivotIndex ] ; else if ( pivotIndex > k - 1 ) right = pivotIndex - 1 ; else left = pivotIndex + 1 ; } return -1 ; } int main ( ) { int arr [ ] = { 10 , 4 , 5 , 8 , 11 , 6 , 26 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 5 ; cout << " K - th ▁ smallest ▁ element ▁ is ▁ " << kthSmallest ( arr , 0 , n - 1 , k ) ; return 0 ; }
Pair with largest sum which is less than K in the array | CPP program to find pair with largest sum which is less than K in the array ; Function to find pair with largest sum which is less than K in the array ; To store the break point ; Sort the given array ; Find the break point ; No need to look beyond i 'th index ; Find the required pair ; Print the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Max_Sum ( int arr [ ] , int n , int k ) { int p = n ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= k ) { p = i ; break ; } } int maxsum = 0 , a , b ; for ( int i = 0 ; i < p ; i ++ ) { for ( int j = i + 1 ; j < p ; j ++ ) { if ( arr [ i ] + arr [ j ] < k and arr [ i ] + arr [ j ] > maxsum ) { maxsum = arr [ i ] + arr [ j ] ; a = arr [ i ] ; b = arr [ j ] ; } } } cout << a << " ▁ " << b ; } int main ( ) { int arr [ ] = { 5 , 20 , 110 , 100 , 10 } , k = 85 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Max_Sum ( arr , n , k ) ; return 0 ; }
Maximum length palindromic substring such that it starts and ends with given char | C ++ implementation of the approach ; Function that returns true if str [ i ... j ] is a palindrome ; Function to return the length of the longest palindromic sub - string such that it starts and ends with the character ch ; If current character is a valid starting index ; Instead of finding the ending index from the beginning , find the index from the end This is because if the current sub - string is a palindrome then there is no need to check the sub - strings of smaller length and we can skip to the next iteration of the outer loop ; If current character is a valid ending index ; If str [ i ... j ] is a palindrome then update the length of the maximum palindrome so far ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str , int i , int j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } int maxLenPalindrome ( string str , int n , char ch ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ch ) { for ( int j = n - 1 ; j >= i ; j -- ) { if ( str [ j ] == ch ) { if ( isPalindrome ( str , i , j ) ) { maxLen = max ( maxLen , j - i + 1 ) ; break ; } } } } } return maxLen ; } int main ( ) { string str = " lapqooqpqpl " ; int n = str . length ( ) ; char ch = ' p ' ; cout << maxLenPalindrome ( str , n , ch ) ; return 0 ; }
Longest sub | C ++ implementation of the approach ; Function to find the starting and the ending index of the sub - array with equal number of alphabets and numeric digits ; If its an alphabet ; Else its a number ; Pick a starting point as i ; Consider all sub - arrays starting from i ; If this is a 0 sum sub - array then compare it with maximum size sub - array calculated so far ; If no valid sub - array found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubArray ( int arr [ ] , int n ) { int sum = 0 ; int maxsize = -1 , startindex ; for ( int i = 0 ; i < n ; i ++ ) { if ( isalpha ( arr [ i ] ) ) { arr [ i ] = 0 ; } else { arr [ i ] = 1 ; } } for ( int i = 0 ; i < n - 1 ; i ++ ) { sum = ( arr [ i ] == 0 ) ? -1 : 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { ( arr [ j ] == 0 ) ? ( sum += -1 ) : ( sum += 1 ) ; if ( sum == 0 && maxsize < j - i + 1 ) { maxsize = j - i + 1 ; startindex = i ; } } } if ( maxsize == -1 ) cout << maxsize ; else cout << startindex << " ▁ " << ( startindex + maxsize - 1 ) ; } int main ( ) { int arr [ ] = { ' A ' , ' B ' , ' X ' , 4 , 6 , ' X ' , ' a ' } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSubArray ( arr , size ) ; return 0 ; }
Check if given string can be formed by two other strings or their permutations | C ++ implementation of the approach ; Function to sort the given string using counting sort ; Array to store the count of each character ; Insert characters in the string in increasing order ; Function that returns true if str can be generated from any permutation of the two strings selected from the given vector ; Sort the given string ; Select two strings at a time from given vector ; Get the concatenated string ; Sort the resultant string ; If the resultant string is equal to the given string str ; No valid pair found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE void countingsort ( string & s ) { int count [ MAX ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = i + ' a ' ; j ++ ; } } } bool isPossible ( vector < string > v , string str ) { countingsort ( str ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { string temp = v [ i ] + v [ j ] ; countingsort ( temp ) ; if ( temp . compare ( str ) == 0 ) { return true ; } } } return false ; } int main ( ) { string str = " amazon " ; vector < string > v { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; if ( isPossible ( v , str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Queries to find the first non | C ++ implementation of the approach ; Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string str [ l ... r ] ; Function to return the first non - repeating character in range [ l . . r ] ; Starting from the first character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  256 NEW_LINE int freq [ MAX ] [ MAX ] ; void preCalculate ( string str , int n ) { freq [ ( int ) str [ 0 ] ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str [ i ] ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = j ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch ] [ r ] ; else return ( freq [ ( int ) ch ] [ r ] - freq [ ( int ) ch ] [ l - 1 ] ) ; } string firstNonRepeating ( string str , int n , int l , int r ) { char t [ 2 ] = " " ; for ( int i = l ; i < r ; i ++ ) { char ch = str [ i ] ; if ( getFrequency ( ch , l , r ) == 1 ) { t [ 0 ] = ch ; return t ; } } return " - 1" ; } int main ( ) { string str = " GeeksForGeeks " ; int n = str . length ( ) ; int queries [ ] [ 2 ] = { { 0 , 3 } , { 2 , 3 } , { 5 , 12 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; freq [ MAX ] [ n ] = { 0 } ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) cout << firstNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << endl ; return 0 ; }
Length of the largest substring which have character with frequency greater than or equal to half of the substring | C ++ implementation of the above approach ; Function to return the length of the longest sub string having frequency of a character greater than half of the length of the sub string ; for each of the character ' a ' to ' z ' ; finding frequency prefix array of the character ; Finding the r [ ] and l [ ] arrays . ; for each j from 0 to n ; Finding the lower bound of i . ; storing the maximum value of i - j + 1 ; clearing all the vector so that it clearing be use for other character . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( string s , int n ) { int ans = INT_MIN ; vector < int > A , L , R ; int freq [ n + 5 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { int count = 0 ; memset ( freq , 0 , sizeof freq ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] - ' a ' == i ) count ++ ; freq [ j ] = count ; } for ( int j = 0 ; j < n ; j ++ ) { L . push_back ( ( 2 * freq [ j - 1 ] ) - j ) ; R . push_back ( ( 2 * freq [ j ] ) - j ) ; } int max_len = INT_MIN ; int min_val = INT_MAX ; for ( int j = 0 ; j < n ; j ++ ) { min_val = min ( min_val , L [ j ] ) ; A . push_back ( min_val ) ; int l = 0 , r = j ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( A [ mid ] <= R [ j ] ) { max_len = max ( max_len , j - mid + 1 ) ; r = mid - 1 ; } else { l = mid + 1 ; } } } ans = max ( ans , max_len ) ; A . clear ( ) ; R . clear ( ) ; L . clear ( ) ; } return ans ; } int main ( ) { string s = " ababbbacbcbcca " ; int n = s . length ( ) ; cout << maxLength ( s , n ) << ' ' ; return 0 ; }
Repeated Character Whose First Appearance is Leftmost | CPP program to find first repeating character ; The function returns index of the first repeating character in a string . If all characters are repeating then returns - 1 ; Mark all characters as not visited ; Traverse from right and update res as soon as we see a visited character . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE int firstRepeating ( string & str ) { bool visited [ NO_OF_CHARS ] ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) visited [ i ] = false ; int res = -1 ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( visited [ str [ i ] ] == false ) visited [ str [ i ] ] = true ; else res = i ; } return res ; } int main ( ) { string str = " geeksforgeeks " ; int index = firstRepeating ( str ) ; if ( index == -1 ) printf ( " Either ▁ all ▁ characters ▁ are ▁ " " distinct ▁ or ▁ string ▁ is ▁ empty " ) ; else printf ( " First ▁ Repeating ▁ character " " ▁ is ▁ % c " , str [ index ] ) ; return 0 ; }
Find maximum sum taking every Kth element in the array | C ++ implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Initialize the sum array with zero ; Iterate from the right ; Update the sum starting at the current element ; Update the maximum so far ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int n , int K ) { int maximum = INT_MIN ; int sum [ n ] = { 0 } ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i + K < n ) sum [ i ] = sum [ i + K ] + arr [ i ] ; else sum [ i ] = arr [ i ] ; maximum = max ( maximum , sum [ i ] ) ; } return maximum ; } int main ( ) { int arr [ ] = { 3 , 6 , 4 , 7 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << maxSum ( arr , n , K ) ; return ( 0 ) ; }