codeparrot/xlcost-text-to-code huggingface dataset:
Find position of an element in a sorted array of infinite numbers | C ++ program to demonstrate working of an algorithm that finds an element in an array of infinite size ; Simple binary search algorithm ; function takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can assume size to be infinite in this function. NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , Thus use binary search between them ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return -1 ; } int findPos ( int arr [ ] , int key ) { int l = 0 , h = 1 ; int val = arr [ 0 ] ; while ( val < key ) { l = h ; h = 2 * h ; val = arr [ h ] ; } return binarySearch ( arr , l , h , key ) ; } int main ( ) { int arr [ ] = { 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 } ; int ans = findPos ( arr , 10 ) ; if ( ans == -1 ) cout << " Element ▁ not ▁ found " ; else cout << " Element ▁ found ▁ at ▁ index ▁ " << ans ; return 0 ; }
Count of possible hexagonal walks | C ++ implementation of counting number of possible hexagonal walks ; We initialize our origin with 1 ; For each N = 1 to 14 , we traverse in all possible direction . Using this 3D array we calculate the number of ways at each step and the total ways for a given step shall be found at ways [ step number ] [ 8 ] [ 8 ] because all the steps after that will be used to trace back to the original point index 0 : 0 according to the image . ; This array stores the number of ways possible for a given step ; Driver function ; Preprocessing all possible ways
#include <iostream> NEW_LINE using namespace std ; int depth = 16 ; int ways [ 16 ] [ 16 ] [ 16 ] ; int stepNum ; void preprocess ( int list [ ] ) { ways [ 0 ] [ 8 ] [ 8 ] = 1 ; for ( int N = 1 ; N <= 14 ; N ++ ) { for ( int i = 1 ; i <= depth ; i ++ ) { for ( int j = 1 ; j <= depth ; j ++ ) { ways [ N ] [ i ] [ j ] = ways [ N - 1 ] [ i ] [ j + 1 ] + ways [ N - 1 ] [ i ] [ j - 1 ] + ways [ N - 1 ] [ i + 1 ] [ j ] + ways [ N - 1 ] [ i - 1 ] [ j ] + ways [ N - 1 ] [ i + 1 ] [ j - 1 ] + ways [ N - 1 ] [ i - 1 ] [ j + 1 ] ; } } list [ N ] = ways [ N ] [ 8 ] [ 8 ] ; } } int main ( ) { int list [ 15 ] ; preprocess ( list ) ; int steps = 4 ; cout << " Number ▁ of ▁ walks ▁ possible ▁ is / are ▁ " << list [ steps ] << endl ; return 0 ; }
Check if possible to cross the matrix with given power | CPP program to find if it is possible to cross the matrix with given power ; Initializing array dp with false value . ; For each value of dp [ i ] [ j ] [ k ] ; For first cell and for each value of k ; For first cell of each row ; For first cell of each column ; For rest of the cell ; Down movement . ; Right movement . ; Diagonal movement . ; Finding maximum k . ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define N  105 NEW_LINE #define R  3 NEW_LINE #define C  4 NEW_LINE using namespace std ; int maximumValue ( int n , int m , int p , int grid [ R ] [ C ] ) { bool dp [ N ] [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) dp [ i ] [ j ] [ k ] = false ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { for ( int k = grid [ i ] [ j ] ; k <= p ; k ++ ) { if ( i == 0 && j == 0 ) { if ( k == grid [ i ] [ j ] ) dp [ i ] [ j ] [ k ] = true ; } else if ( i == 0 ) { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; } else if ( j == 0 ) { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) ; } else { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) ; dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; } } } } int ans = 0 ; for ( ans = k ; ans >= 0 ; ans -- ) if ( dp [ n - 1 ] [ m - 1 ] [ ans ] ) break ; return ans ; } int main ( ) { int n = 3 , m = 4 , p = 9 ; int grid [ R ] [ C ] = { { 2 , 3 , 4 , 1 } , { 6 , 5 , 5 , 3 } , { 5 , 2 , 3 , 4 } } ; cout << maximumValue ( n , m , p , grid ) << endl ; return 0 ; }
Number of n digit stepping numbers | CPP program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long answer ( int n ) { int dp [ n + 1 ] [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; else if ( j == 9 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ; } } long long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ n ] [ j ] ; return sum ; } int main ( ) { int n = 2 ; cout << answer ( n ) ; return 0 ; }
Print Longest Palindromic Subsequence | CPP program to print longest palindromic subsequence ; Returns LCS X and Y ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a string length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lcs ( string & X , string & Y ) { int m = X . length ( ) ; int n = Y . length ( ) ; int L [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } int index = L [ m ] [ n ] ; string lcs ( index + 1 , ' \0' ) ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { lcs [ index - 1 ] = X [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ) i -- ; else j -- ; } return lcs ; } string longestPalSubseq ( string & str ) { string rev = str ; reverse ( rev . begin ( ) , rev . end ( ) ) ; return lcs ( str , rev ) ; } int main ( ) { string str = " GEEKSFORGEEKS " ; cout << longestPalSubseq ( str ) ; return 0 ; }
Count all subsequences having product less than K | CPP program to find number of subarrays having product less than k . ; Function to count numbers of such subsequences having product less than k . ; number of subsequence using j - 1 terms ; if arr [ j - 1 ] > i it will surely make product greater thus it won 't contribute then ; number of subsequence using 1 to j - 1 terms and j - th term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int productSubSeqCount ( vector < int > & arr , int k ) { int n = arr . size ( ) ; int dp [ k + 1 ] [ n + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; if ( arr [ j - 1 ] <= i ) dp [ i ] [ j ] += dp [ i / arr [ j - 1 ] ] [ j - 1 ] + 1 ; } } return dp [ k ] [ n ] ; } int main ( ) { vector < int > A ; A . push_back ( 1 ) ; A . push_back ( 2 ) ; A . push_back ( 3 ) ; A . push_back ( 4 ) ; int k = 10 ; cout << productSubSeqCount ( A , k ) << endl ; }
Find the element that appears once in an array where every other element appears twice | C ++ program to find the array element that appears only once ; Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSingle ( int ar [ ] , int ar_size ) { int res = ar [ 0 ] ; for ( int i = 1 ; i < ar_size ; i ++ ) res = res ^ ar [ i ] ; return res ; } int main ( ) { int ar [ ] = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; cout << " Element ▁ occurring ▁ once ▁ is ▁ " << findSingle ( ar , n ) ; return 0 ; }
Count all triplets whose sum is equal to a perfect cube | C ++ program to calculate all triplets whose sum is perfect cube . ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1001 ] [ 15001 ] ; void computeDpArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j <= 15000 ; ++ j ) { if ( i == 0 ) dp [ i ] [ j ] = ( j == arr [ i ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + ( arr [ i ] == j ) ; } } } int countTripletSum ( int arr [ ] , int n ) { computeDpArray ( arr , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { for ( int j = i + 1 ; j < n - 1 ; ++ j ) { for ( int k = 1 ; k <= 24 ; ++ k ) { int cube = k * k * k ; int rem = cube - ( arr [ i ] + arr [ j ] ) ; if ( rem > 0 ) ans += dp [ n - 1 ] [ rem ] - dp [ j ] [ rem ] ; } } } return ans ; } int main ( ) { int arr [ ] = { 2 , 5 , 1 , 20 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countTripletSum ( arr , n ) ; return 0 ; }
Find the element that appears once in an array where every other element appears twice | C ++ program to find element that appears once ; function which find number ; applying the formula . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int singleNumber ( int nums [ ] , int n ) { map < int , int > m ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ nums [ i ] ] == 0 ) { sum1 += nums [ i ] ; m [ nums [ i ] ] ++ ; } sum2 += nums [ i ] ; } return 2 * ( sum1 ) - sum2 ; } int main ( ) { int a [ ] = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; cout << singleNumber ( a , n ) << " STRNEWLINE " ; int b [ ] = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; cout << singleNumber ( b , n ) ; return 0 ; }
Maximum Subarray Sum Excluding Certain Elements | C ++ Program to find max subarray sum excluding some elements ; Function to check the element present in array B ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPresent ( int B [ ] , int m , int x ) { for ( int i = 0 ; i < m ; i ++ ) if ( B [ i ] == x ) return true ; return false ; } int findMaxSubarraySumUtil ( int A [ ] , int B [ ] , int n , int m ) { int max_so_far = INT_MIN , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPresent ( B , m , A [ i ] ) ) { curr_max = 0 ; continue ; } curr_max = max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = max ( max_so_far , curr_max ) ; } return max_so_far ; } void findMaxSubarraySum ( int A [ ] , int B [ ] , int n , int m ) { int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == INT_MIN ) { cout << " Maximum ▁ Subarray ▁ Sum ▁ cant ▁ be ▁ found " << endl ; } else { cout << " The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ▁ " << maxSubarraySum << endl ; } } int main ( ) { int A [ ] = { 3 , 4 , 5 , -4 , 6 } ; int B [ ] = { 1 , 8 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int m = sizeof ( B ) / sizeof ( B [ 0 ] ) ; findMaxSubarraySum ( A , B , n , m ) ; return 0 ; }
Maximum Subarray Sum Excluding Certain Elements | C ++ Program to find max subarray sum excluding some elements ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; sort array B to apply Binary Search ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSubarraySumUtil ( int A [ ] , int B [ ] , int n , int m ) { int max_so_far = INT_MIN , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( binary_search ( B , B + m , A [ i ] ) ) { curr_max = 0 ; continue ; } curr_max = max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = max ( max_so_far , curr_max ) ; } return max_so_far ; } void findMaxSubarraySum ( int A [ ] , int B [ ] , int n , int m ) { sort ( B , B + m ) ; int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == INT_MIN ) { cout << " Maximum ▁ subarray ▁ sum ▁ cant ▁ be ▁ found " << endl ; } else { cout << " The ▁ Maximum ▁ subarray ▁ sum ▁ = ▁ " << maxSubarraySum << endl ; } } int main ( ) { int A [ ] = { 3 , 4 , 5 , -4 , 6 } ; int B [ ] = { 1 , 8 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int m = sizeof ( B ) / sizeof ( B [ 0 ] ) ; findMaxSubarraySum ( A , B , n , m ) ; return 0 ; }
Number of n | C ++ program for counting n digit numbers with non decreasing digits ; Returns count of non - decreasing numbers with n digits . ; a [ i ] [ j ] = count of all possible number with i digits having leading digit as j ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nonDecNums ( int n ) { int a [ n + 1 ] [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ; return a [ n ] [ 0 ] ; } int main ( ) { int n = 2 ; cout << " Non - decreasing ▁ digits ▁ = ▁ " << nonDecNums ( n ) << endl ; return 0 ; }
Count Balanced Binary Trees of Height h | C ++ program to count number of balanced binary trees of height h . ; base cases ; Driver program
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; long long int countBT ( int h ) { long long int dp [ h + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % mod + dp [ i - 1 ] ) % mod ) % mod ; } return dp [ h ] ; } int main ( ) { int h = 3 ; cout << " No . ▁ of ▁ balanced ▁ binary ▁ trees " " ▁ of ▁ height ▁ h ▁ is : ▁ " << countBT ( h ) << endl ; }
Maximum Subarray Sum Excluding Certain Elements | C ++ Program implementation of the above idea ; Function to calculate the max sum of contigous subarray of B whose elements are not present in A ; mark all the elements present in B ; initialize max_so_far with INT_MIN ; traverse the array A ; if current max is greater than max so far then update max so far ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSubarraySum ( vector < int > A , vector < int > B ) { unordered_map < int , int > m ; for ( int i = 0 ; i < B . size ( ) ; i ++ ) { m [ B [ i ] ] = 1 ; } int max_so_far = INT_MIN ; int currmax = 0 ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { if ( currmax < 0 m [ A [ i ] ] == 1 ) { currmax = 0 ; continue ; } currmax = max ( A [ i ] , A [ i ] + currmax ) ; if ( max_so_far < currmax ) { max_so_far = currmax ; } } return max_so_far ; } int main ( ) { vector < int > a = { 3 , 4 , 5 , -4 , 6 } ; vector < int > b = { 1 , 8 , 5 } ; cout << findMaxSubarraySum ( a , b ) ; return 0 ; }
Print all k | C ++ program to print all paths with sum k . ; utility function to print contents of a vector from index i to it 's end ; binary tree node ; This function prints all paths that have sum k ; empty node ; add current node to the path ; check if there 's any k sum path  in the left sub-tree. ; check if there 's any k sum path  in the right sub-tree. ; check if there 's any k sum path that  terminates at this node  Traverse the entire path as  there can be negative elements too ; If path sum is k , print the path ; Remove the current element from the path ; A wrapper over printKPathUtil ( ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printVector ( const vector < int > & v , int i ) { for ( int j = i ; j < v . size ( ) ; j ++ ) cout << v [ j ] << " ▁ " ; cout << endl ; } struct Node { int data ; Node * left , * right ; Node ( int x ) { data = x ; left = right = NULL ; } } ; void printKPathUtil ( Node * root , vector < int > & path , int k ) { if ( ! root ) return ; path . push_back ( root -> data ) ; printKPathUtil ( root -> left , path , k ) ; printKPathUtil ( root -> right , path , k ) ; int f = 0 ; for ( int j = path . size ( ) - 1 ; j >= 0 ; j -- ) { f += path [ j ] ; if ( f == k ) printVector ( path , j ) ; } path . pop_back ( ) ; } void printKPath ( Node * root , int k ) { vector < int > path ; printKPathUtil ( root , path , k ) ; } int main ( ) { Node * root = new Node ( 1 ) ; root -> left = new Node ( 3 ) ; root -> left -> left = new Node ( 2 ) ; root -> left -> right = new Node ( 1 ) ; root -> left -> right -> left = new Node ( 1 ) ; root -> right = new Node ( -1 ) ; root -> right -> left = new Node ( 4 ) ; root -> right -> left -> left = new Node ( 1 ) ; root -> right -> left -> right = new Node ( 2 ) ; root -> right -> right = new Node ( 5 ) ; root -> right -> right -> right = new Node ( 2 ) ; int k = 5 ; printKPath ( root , k ) ; return 0 ; }
Number of substrings divisible by 8 but not by 3 | CPP Program to count substrings which are divisible by 8 but not by 3 ; Returns count of substrings divisible by 8 but not by 3. ; Iterating the string . ; Prefix sum of number of substrings whose sum of digits mudolo 3 is 0 , 1 , 2. ; Iterating the string . ; Since single digit 8 is divisible by 8 and not by 3. ; Taking two digit number . ; 10 th position ; Complete 2 digit ; Taking 3 digit number . ; 100 th position ; 10 th position ; Complete 3 digit number . ; If number formed is divisible by 8 then last 3 digits are also divisible by 8. Then all the substring ending at this index is divisible . ; But those substring also contain number which are not divisible by 3 so remove them . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE int count ( char s [ ] , int len ) { int cur = 0 , dig = 0 ; int sum [ MAX ] , dp [ MAX ] [ 3 ] ; memset ( sum , 0 , sizeof ( sum ) ) ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = int ( s [ i - 1 ] ) - 48 ; cur += dig ; cur %= 3 ; sum [ i ] = cur ; dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 2 ] ; dp [ i ] [ sum [ i ] ] ++ ; } int ans = 0 , dprev = 0 , value = 0 , dprev2 = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = int ( s [ i - 1 ] ) - 48 ; if ( dig == 8 ) ans ++ ; if ( i - 2 >= 0 ) { dprev = int ( s [ i - 2 ] ) - 48 ; value = dprev * 10 + dig ; if ( ( value % 8 == 0 ) && ( value % 3 != 0 ) ) ans ++ ; } if ( i - 3 >= 0 ) { dprev2 = int ( s [ i - 3 ] ) - 48 ; dprev = int ( s [ i - 2 ] ) - 48 ; value = dprev2 * 100 + dprev * 10 + dig ; if ( value % 8 != 0 ) continue ; ans += ( i - 2 ) ; ans -= ( dp [ i - 3 ] [ sum [ i ] ] ) ; } } return ans ; } int main ( ) { char str [ ] = "6564525600" ; int len = strlen ( str ) ; cout << count ( str , len ) << endl ; return 0 ; }
Print all distinct characters of a string in order ( 3 Methods ) | C ++ program to find all distinct characters in a string ; Function to print distinct characters in given string str [ ] ; count [ x ] is going to store count of character ' x ' in str . If x is not present , then it is going to store 0. ; index [ x ] is going to store index of character ' x ' in str . If x is not present or x is more than once , then it is going to store a value ( for example , length of string ) that cannot be a valid index in str [ ] ; Initialize counts of all characters and indexes of distinct characters . ; index [ i ] = n ; A value more than any index in str [ ] ; Traverse the input string ; Find current character and increment its count ; If this is first occurrence , then set value in index as index of it . ; If character repeats , then remove it from index [ ] ; Since size of index is constant , below operations take constant time . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; void printDistinct ( string str ) { int n = str . length ( ) ; int count [ MAX_CHAR ] ; int index [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { count [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { char x = str [ i ] ; ++ count [ x ] ; if ( count [ x ] == 1 && x != ' ▁ ' ) index [ x ] = i ; if ( count [ x ] == 2 ) index [ x ] = n ; } sort ( index , index + MAX_CHAR ) ; for ( int i = 0 ; i < MAX_CHAR && index [ i ] != n ; i ++ ) cout << str [ index [ i ] ] ; } int main ( ) { string str = " GeeksforGeeks " ; printDistinct ( str ) ; return 0 ; }