codeparrot/xlcost-text-to-code huggingface dataset:
Partition the array into three equal sum segments | C ++ implementation of the approach ; First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static int pos1 = -1 ; static int pos2 = -1 ; bool equiSumUtil ( int arr [ ] , int n ) { int pre [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; pre [ i ] = sum ; } int suf [ n ] ; sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; suf [ i ] = sum ; } int total_sum = sum ; int i = 0 , j = n - 1 ; while ( i < j - 1 ) { if ( pre [ i ] == total_sum / 3 ) { pos1 = i ; } if ( suf [ j ] == total_sum / 3 ) { pos2 = j ; } if ( pos1 != -1 && pos2 != -1 ) { if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } void equiSum ( int arr [ ] , int n ) { bool ans = equiSumUtil ( arr , n ) ; if ( ans ) { cout << " First ▁ Segment ▁ : ▁ " ; for ( int i = 0 ; i <= pos1 ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; cout << " Second ▁ Segment ▁ : ▁ " ; for ( int i = pos1 + 1 ; i < pos2 ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; cout << " Third ▁ Segment ▁ : ▁ " ; for ( int i = pos2 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; } else { cout << " Array ▁ cannot ▁ be ▁ divided ▁ into ▁ three ▁ equal ▁ sum ▁ segments " ; } } int main ( ) { int arr [ ] = { 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; equiSum ( arr , n ) ; return 0 ; }
Print cousins of a given node in Binary Tree | C ++ program to print cousins of a node ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; It returns level of the node if it is present in tree , otherwise returns 0. ; base cases ; If node is present in left subtree ; If node is not present in left subtree ; Print nodes at a given level such that sibling of node is not printed if it exists ; Base cases ; If current node is parent of a node with given level ; Recur for left and right subtrees ; This function prints cousins of a given node ; Get level of given node ; Print nodes of given level . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int item ) { Node * temp = new Node ; temp -> data = item ; temp -> left = temp -> right = NULL ; return temp ; } int getLevel ( Node * root , Node * node , int level ) { if ( root == NULL ) return 0 ; if ( root == node ) return level ; int downlevel = getLevel ( root -> left , node , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; return getLevel ( root -> right , node , level + 1 ) ; } void printGivenLevel ( Node * root , Node * node , int level ) { if ( root == NULL level < 2 ) return ; if ( level == 2 ) { if ( root -> left == node root -> right == node ) return ; if ( root -> left ) cout << root -> left -> data << " ▁ " ; if ( root -> right ) cout << root -> right -> data ; } else if ( level > 2 ) { printGivenLevel ( root -> left , node , level - 1 ) ; printGivenLevel ( root -> right , node , level - 1 ) ; } } void printCousins ( Node * root , Node * node ) { int level = getLevel ( root , node , 1 ) ; printGivenLevel ( root , node , level ) ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> left -> right -> right = newNode ( 15 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> left -> right = newNode ( 8 ) ; printCousins ( root , root -> left -> right ) ; return 0 ; }
Leftmost and rightmost indices of the maximum and the minimum element of an array | C ++ implementation of above idea ; Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; Driver code ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getRightMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; int rightMin = 0 ; int i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } int getLeftMax ( int arr [ ] , int n ) { int max = arr [ n - 1 ] ; int leftMax = n - 1 ; int i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i /= 2 ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } int main ( ) { int arr [ ] = { 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ left ▁ : ▁ " << 0 << " STRNEWLINE " ; cout << " Minimum ▁ right ▁ : ▁ " << getRightMin ( arr , n ) << " STRNEWLINE " ; cout << " Maximum ▁ left ▁ : ▁ " << getLeftMax ( arr , n ) << " STRNEWLINE " ; cout << " Maximum ▁ right ▁ : ▁ " << ( n - 1 ) ; }
Elements to be added so that all elements of a range are present in array | C ++ program for above implementation ; Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNum ( int arr [ ] , int n ) { int count = 0 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } int main ( ) { int arr [ ] = { 3 , 5 , 8 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countNum ( arr , n ) << endl ; return 0 ; }
Find the k smallest numbers after deleting given elements |  ; Find k minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it in the min heap ; Print top k elements in the min heap ; Pop the top element ; Driver code
#include " iostream " NEW_LINE #include " queue " NEW_LINE #include " unordered _ map " NEW_LINE #include " vector " NEW_LINE using namespace std ; void findElementsAfterDel ( int arr [ ] , int m , int del [ ] , int n , int k ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; ++ i ) { mp [ del [ i ] ] ++ ; } priority_queue < int , vector < int > , greater < int > > heap ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . find ( arr [ i ] ) != mp . end ( ) ) { mp [ arr [ i ] ] -- ; if ( mp [ arr [ i ] ] == 0 ) mp . erase ( arr [ i ] ) ; } else heap . push ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; ++ i ) { cout << heap . top ( ) << " ▁ " ; heap . pop ( ) ; } } int main ( ) { int array [ ] = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = sizeof ( array ) / sizeof ( array [ 0 ] ) ; int del [ ] = { 12 , 56 , 5 } ; int n = sizeof ( del ) / sizeof ( del [ 0 ] ) ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; return 0 ; }
Elements to be added so that all elements of a range are present in array | C ++ program for above implementation ; Function to count numbers to be added ; Make a hash of elements and store minimum and maximum element ; Traverse all elements from minimum to maximum and count if it is not in the hash ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNum ( int arr [ ] , int n ) { unordered_set < int > s ; int count = 0 , maxm = INT_MIN , minm = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { s . insert ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( int i = minm ; i <= maxm ; i ++ ) if ( s . find ( arr [ i ] ) == s . end ( ) ) count ++ ; return count ; } int main ( ) { int arr [ ] = { 3 , 5 , 8 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countNum ( arr , n ) << endl ; return 0 ; }
Find minimum speed to finish all Jobs | CPP program to find minimum speed to finish all jobs ; Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver program ; Print required maxLenwer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int A [ ] , int n , int H , int K ) { int time = 0 ; for ( int i = 0 ; i < n ; ++ i ) time += ( A [ i ] - 1 ) / K + 1 ; return time <= H ; } int minJobSpeed ( int A [ ] , int n , int H ) { if ( H < n ) return -1 ; int * max = max_element ( A , A + n ) ; int lo = 1 , hi = * max ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } int main ( ) { int A [ ] = { 3 , 6 , 7 , 11 } , H = 8 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minJobSpeed ( A , n , H ) ; return 0 ; }
Number of anomalies in an array |  ; Sort the array so that we can apply binary search . ; One by one check every element if it is anomaly or not using binary search . ; If arr [ i ] is not largest element and element just greater than it is within k , then return false . ; If there are more than one occurrences of arr [ i ] , return false . ; If arr [ i ] is not smallest element and just smaller element is not k distance away ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countAnomalies ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int * u = upper_bound ( a , a + n , a [ i ] ) ; if ( u != a + n && ( ( * u ) - a [ i ] ) <= k ) continue ; int * s = lower_bound ( a , a + n , a [ i ] ) ; if ( u - s > 1 ) continue ; if ( s != a && ( * ( s - 1 ) - a [ i ] ) <= k ) continue ; res ++ ; } return res ; } int main ( ) { int arr [ ] = { 7 , 1 , 8 } , k = 5 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countAnomalies ( arr , n , k ) ; return 0 ; }
Subarrays with distinct elements | C ++ program to calculate sum of lengths of subarrays of distinct elements . ; Returns sum of lengths of all subarrays with distinct elements . ; For maintaining distinct elements . ; Initialize ending point and result ; Fix starting point ; Calculating and adding all possible length subarrays in arr [ i . . j ] ; Remove arr [ i ] as we pick new stating point from next ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumoflength ( int arr [ ] , int n ) { unordered_set < int > s ; int j = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n && s . find ( arr [ j ] ) == s . end ( ) ) { s . insert ( arr [ j ] ) ; j ++ ; } ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ; s . erase ( arr [ i ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumoflength ( arr , n ) << endl ; return 0 ; }
Count subarrays having total distinct elements same as original array | C ++ program Count total number of sub - arrays having total distinct elements same as that original array . ; Function to calculate distinct sub - array ; Count distinct elements in whole array ; Reset the container by removing all elements ; Use sliding window concept to find count of subarrays having k distinct elements . ; If window size equals to array distinct element size , then update answer ; Decrease the frequency of previous element for next sliding window ; If frequency is zero then decrease the window size ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDistictSubarray ( int arr [ ] , int n ) { unordered_map < int , int > vis ; for ( int i = 0 ; i < n ; ++ i ) vis [ arr [ i ] ] = 1 ; int k = vis . size ( ) ; vis . clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { ++ vis [ arr [ right ] ] ; if ( vis [ arr [ right ] ] == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; -- vis [ arr [ left ] ] ; if ( vis [ arr [ left ] ] == 0 ) -- window ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 1 , 3 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countDistictSubarray ( arr , n ) << " n " ; return 0 ; }
Count subarrays with same even and odd elements | C ++ program to find total number of even - odd subarrays present in given array ; function that returns the count of subarrays that contain equal number of odd as well as even numbers ; initialize difference and answer with 0 ; create two auxiliary hash arrays to count frequency of difference , one array for non - negative difference and other array for negative difference . Size of these two auxiliary arrays is ' n + 1' because difference can reach maximum value ' n ' as well as minimum value ' - n ' ; initialize these auxiliary arrays with 0 ; since the difference is initially 0 , we have to initialize hash_positive [ 0 ] with 1 ; for loop to iterate through whole array ( zero - based indexing is used ) ; incrementing or decrementing difference based on arr [ i ] being even or odd , check if arr [ i ] is odd ; adding hash value of ' difference ' to our answer as all the previous occurrences of the same difference value will make even - odd subarray ending at index ' i ' . After that , we will increment hash array for that ' difference ' value for its occurrence at index ' i ' . if difference is negative then use hash_negative ; else use hash_positive ; return total number of even - odd subarrays ; Driver code ; Printing total number of even - odd subarrays
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubarrays ( int arr [ ] , int n ) { int difference = 0 ; int ans = 0 ; int hash_positive [ n + 1 ] , hash_negative [ n + 1 ] ; fill_n ( hash_positive , n + 1 , 0 ) ; fill_n ( hash_negative , n + 1 , 0 ) ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 == 1 ) difference ++ ; else difference -- ; if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Total ▁ Number ▁ of ▁ Even - Odd ▁ subarrays " " ▁ are ▁ " << countSubarrays ( arr , n ) ; return 0 ; }
Given a binary tree , print out all of its root | C ++ program to print all of its root - to - leaf paths for a tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function prototypes ; Given a binary tree , print out all of its root - to - leaf paths , one per line . Uses a recursive helper to do the work . ; Recursive helper function -- given a node , and an array containing the path from the root node up to but not including this node , print out all the root - leaf paths . ; append this node to the path array ; it 's a leaf, so print the path that led to here  ; otherwise try both subtrees ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Utility that prints out an array on a line ; Driver code ; Print all root - to - leaf paths of the input tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; void printArray ( int [ ] , int ) ; void printPathsRecur ( node * , int [ ] , int ) ; node * newNode ( int ) ; void printPaths ( node * ) ; void printPaths ( node * node ) { int path [ 1000 ] ; printPathsRecur ( node , path , 0 ) ; } void printPathsRecur ( node * node , int path [ ] , int pathLen ) { if ( node == NULL ) return ; path [ pathLen ] = node -> data ; pathLen ++ ; if ( node -> left == NULL && node -> right == NULL ) { printArray ( path , pathLen ) ; } else { printPathsRecur ( node -> left , path , pathLen ) ; printPathsRecur ( node -> right , path , pathLen ) ; } } node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } void printArray ( int ints [ ] , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) { cout << ints [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; printPaths ( root ) ; return 0 ; }
Evaluation of Expression Tree | C ++ program to evaluate an expression tree ; Class to represent the nodes of syntax tree ; Utility function to return the integer value of a given string ; Check if the integral value is negative or not If it is not negative , generate the number normally ; If it is negative , calculate the + ve number first ignoring the sign and invert the sign at the end ; This function receives a node of the syntax tree and recursively evaluates it ; empty tree ; leaf node i . e , an integer ; Evaluate left subtree ; Evaluate right subtree ; Check which operator to apply ; create a syntax tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : string info ; node * left = NULL , * right = NULL ; node ( string x ) { info = x ; } } ; int toInt ( string s ) { int num = 0 ; if ( s [ 0 ] != ' - ' ) for ( int i = 0 ; i < s . length ( ) ; i ++ ) num = num * 10 + ( int ( s [ i ] ) - 48 ) ; else for ( int i = 1 ; i < s . length ( ) ; i ++ ) { num = num * 10 + ( int ( s [ i ] ) - 48 ) ; num = num * -1 ; } return num ; } int eval ( node * root ) { if ( ! root ) return 0 ; if ( ! root -> left && ! root -> right ) return toInt ( root -> info ) ; int l_val = eval ( root -> left ) ; int r_val = eval ( root -> right ) ; if ( root -> info == " + " ) return l_val + r_val ; if ( root -> info == " - " ) return l_val - r_val ; if ( root -> info == " * " ) return l_val * r_val ; return l_val / r_val ; } int main ( ) { node * root = new node ( " + " ) ; root -> left = new node ( " * " ) ; root -> left -> left = new node ( "5" ) ; root -> left -> right = new node ( " - 4" ) ; root -> right = new node ( " - " ) ; root -> right -> left = new node ( "100" ) ; root -> right -> right = new node ( "20" ) ; cout << eval ( root ) << endl ; delete ( root ) ; root = new node ( " + " ) ; root -> left = new node ( " * " ) ; root -> left -> left = new node ( "5" ) ; root -> left -> right = new node ( "4" ) ; root -> right = new node ( " - " ) ; root -> right -> left = new node ( "100" ) ; root -> right -> right = new node ( " / " ) ; root -> right -> right -> left = new node ( "20" ) ; root -> right -> right -> right = new node ( "2" ) ; cout << eval ( root ) ; return 0 ; }
Saddleback Search Algorithm in a 2D array | C ++ program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE bool search ( int mat [ ] [ MAX ] , int m , int n , int x ) { int i = m - 1 , j = 0 ; while ( i >= 0 && j < n ) { if ( mat [ i ] [ j ] == x ) return true ; if ( mat [ i ] [ j ] > x ) i -- ; else j ++ ; } return false ; } int main ( ) { int mat [ ] [ MAX ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } , { 50 , 60 , 70 , 80 } , } ; if ( search ( mat , 5 , 4 , 29 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum number of distinct elements after removing m items | C ++ program for above implementation ; Function to find distintc id 's ; Store the occurrence of ids ; Store into the vector second as first and vice - versa ; Sort the vector ; Start removing elements from the beginning ; Remove if current value is less than or equal to mi ; Return the remaining size ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctIds ( int arr [ ] , int n , int mi ) { unordered_map < int , int > m ; vector < pair < int , int > > v ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) v . push_back ( make_pair ( it -> second , it -> first ) ) ; sort ( v . begin ( ) , v . end ( ) ) ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { if ( v [ i ] . first <= mi ) { mi -= v [ i ] . first ; count ++ ; } else return size - count ; } return size - count ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 2 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = 3 ; cout << distinctIds ( arr , n , m ) ; return 0 ; }
Number of Larger Elements on right side in a string | CPP program to find counts of right greater characters for every character . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printGreaterCount ( string str ) { int len = str . length ( ) , right [ len ] = { 0 } ; for ( int i = 0 ; i < len ; i ++ ) for ( int j = i + 1 ; j < len ; j ++ ) if ( str [ i ] < str [ j ] ) right [ i ] ++ ; for ( int i = 0 ; i < len ; i ++ ) cout << right [ i ] << " ▁ " ; } int main ( ) { string str = " abcd " ; printGreaterCount ( str ) ; return 0 ; }
Maximum consecutive numbers present in an array | CPP program to find largest consecutive numbers present in arr [ ] . ; We insert all the array elements into unordered set . ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; increment the value of array element and repeat search in the set ; Update optimal length if this length is more . To get the length as it is incremented one by one ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestConseqSubseq ( int arr [ ] , int n ) { unordered_set < int > S ; for ( int i = 0 ; i < n ; i ++ ) S . insert ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) { int j = arr [ i ] ; while ( S . find ( j ) != S . end ( ) ) j ++ ; ans = max ( ans , j - arr [ i ] ) ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 94 , 93 , 1000 , 5 , 92 , 78 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findLongestConseqSubseq ( arr , n ) << endl ; return 0 ; }