codeparrot/xlcost-text-to-code huggingface dataset:
Check if a Sequence is a concatenation of two permutations | C ++ program to check if a given sequence is a concatenation of two permutations or not ; Function to Check if a given sequence is a concatenation of two permutations or not ; Computing the sum of all the elements in the array ; Computing the prefix sum for all the elements in the array ; Iterating through the i from lengths 1 to n - 1 ; Sum of first i + 1 elements ; Sum of remaining n - i - 1 elements ; Lengths of the 2 permutations ; Checking if the sums satisfy the formula or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPermutation ( int arr [ ] , int n ) { long long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; long long prefix [ n + 1 ] = { 0 } ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long long lsum = prefix [ i ] ; long long rsum = sum - prefix [ i ] ; long long l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 3 , 4 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; if ( checkPermutation ( arr , n ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Find a N | CPP program to find N digit number such that it is not divisible by any of its digits ; Function that print the answer ; if n == 1 then it is not possible ; loop to n - 1 times ; print 4 as last digit of the number ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTheNumber ( int n ) { if ( n == 1 ) { cout << " Impossible " << endl ; return ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { cout << "5" ; } cout << "4" ; } int main ( ) { int n = 12 ; findTheNumber ( n ) ; return 0 ; }
Queries to check whether bitwise AND of a subarray is even or odd | C ++ implementation of the approach ; Function to precompute the count of even and odd numbers ; If the current element is odd then put 1 at odd [ i ] ; If the current element is even then put 1 at even [ i ] ; Taking the prefix sums of these two arrays so we can get the count of even and odd numbers in a range [ L , R ] in O ( 1 ) ; Function that returns true if the bitwise AND of the subarray a [ L ... R ] is odd ; cnt will store the count of odd numbers in the range [ L , R ] ; Check if all the numbers in the range are odd or not ; Function to perform the queries ; Perform queries ; Driver code ; Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAXN = 1000005 ; int even [ MAXN ] , odd [ MAXN ] ; void precompute ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd [ i ] = 1 ; if ( arr [ i ] % 2 == 0 ) even [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { even [ i ] = even [ i ] + even [ i - 1 ] ; odd [ i ] = odd [ i ] + odd [ i - 1 ] ; } } bool isOdd ( int L , int R ) { int cnt = odd [ R ] ; if ( L > 0 ) cnt -= odd [ L - 1 ] ; if ( cnt == R - L + 1 ) return true ; return false ; } void performQueries ( int a [ ] , int n , int q [ ] [ 2 ] , int m ) { precompute ( a , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; if ( isOdd ( L , R ) ) cout << " Odd STRNEWLINE " ; else cout << " Even STRNEWLINE " ; } } int main ( ) { int a [ ] = { 2 , 1 , 5 , 7 , 6 , 8 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int q [ ] [ 2 ] = { { 0 , 2 } , { 1 , 2 } , { 2 , 3 } , { 3 , 6 } } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; performQueries ( a , n , q , m ) ; return 0 ; }
Find distinct integers for a triplet with given product | C ++ implementation of the approach ; Function to find the required triplets ; To store the factors ; Find factors in sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Print the valid triplet ; Triplet found ; Triplet not found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTriplets ( int x ) { vector < int > fact ; unordered_set < int > factors ; for ( int i = 2 ; i <= sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . push_back ( i ) ; if ( x / i != i ) fact . push_back ( x / i ) ; factors . insert ( i ) ; factors . insert ( x / i ) ; } } bool found = false ; int k = fact . size ( ) ; for ( int i = 0 ; i < k ; i ++ ) { int a = fact [ i ] ; for ( int j = 0 ; j < k ; j ++ ) { int b = fact [ j ] ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { cout << a << " ▁ " << b << " ▁ " << ( x / ( a * b ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) cout << " - 1" ; } int main ( ) { int x = 105 ; findTriplets ( x ) ; return 0 ; }
Count of pairs satisfying the given condition | C ++ implementation of the approach ; Function to return the number of pairs satisfying the equation ; Converting integer b to string by using to_string function ; Loop to check if all the digits of b are 9 or not ; If '9' doesn 't appear  then break the loop ; If all the digits of b contain 9 then multiply a with string length else multiply a with string length - 1 ; Return the number of pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPair ( int a , int b ) { string s = to_string ( b ) ; int i ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] != '9' ) break ; } int result ; if ( i == s . length ( ) ) result = a * s . length ( ) ; else result = a * ( s . length ( ) - 1 ) ; return result ; } int main ( ) { int a = 5 , b = 101 ; cout << countPair ( a , b ) ; return 0 ; }
Partition the digits of an integer such that it satisfies a given condition | C ++ implementation of the approach ; Function to generate sequence from the given string ; Initialize vector to store sequence ; First add all the digits of group A from left to right ; Then add all the digits of group B from left to right ; Return the sequence ; Function that returns true if the sequence is non - decreasing ; Initialize result ; Function to partition the digits of an integer such that it satisfies the given conditions ; Convert the integer to string ; Length of the string ; Array to store the digits ; Storing the digits of X in array ; Initialize the result ; Loop through the digits ; Put into group A if digit less than D ; Put into group B if digit greater than D ; Put into group C if digit equal to D ; Loop through the digits to decide for group C digits ; Set flag equal to true if group B digit present ; If flag is true put in group A or else put in B ; Generate the sequence from partition ; Check if the sequence is non decreasing ; Return - 1 if no such partition is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > makeSeq ( string s , int a [ ] ) { vector < int > seq ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) if ( s [ i ] == ' A ' ) seq . push_back ( a [ i ] ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) if ( s [ i ] == ' B ' ) seq . push_back ( a [ i ] ) ; return seq ; } bool checkSeq ( vector < int > v ) { bool check = true ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) if ( v [ i ] < v [ i - 1 ] ) check = false ; return check ; } string digitPartition ( int X ) { string num = to_string ( X ) ; int l = num . size ( ) ; int a [ l ] ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = ( num [ i ] - '0' ) ; for ( int D = 0 ; D < 10 ; D ++ ) { string res = " " ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] < D ) res += ' A ' ; else if ( a [ i ] > D ) res += ' B ' ; else res += ' C ' ; } bool flag = false ; for ( int i = 0 ; i < l ; i ++ ) { if ( res [ i ] == ' B ' ) flag = true ; if ( res [ i ] == ' C ' ) res [ i ] = flag ? ' A ' : ' B ' ; } vector < int > seq = makeSeq ( res , a ) ; if ( checkSeq ( seq ) ) return res ; } return " - 1" ; } int main ( ) { int X = 777147777 ; cout << digitPartition ( X ) ; return 0 ; }
Area of the circle that has a square and a circle inscribed in it | C ++ implementation of the approach ; Function to return the required area ; Calculate the area ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float getArea ( int a ) { float area = ( M_PI * a * a ) / 4.0 ; return area ; } int main ( ) { int a = 3 ; cout << getArea ( a ) ; return 0 ; }
Check if all the elements can be made of same parity by inverting adjacent elements | C ++ implementation of the approach ; Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool flipsPossible ( int a [ ] , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] & 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 && count_even % 2 ) return false ; else return true ; } int main ( ) { int a [ ] = { 1 , 0 , 1 , 1 , 0 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( flipsPossible ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Queries On Array with disappearing and reappearing elements | C ++ implementation of the approach ; Function to perform the queries ; Size of the array with 1 - based indexing ; Number of queries ; Iterating through the queries ; If m is more than the size of the array ; Count of turns ; Find the remainder ; If the remainder is 0 and turn is odd then the array is empty ; If the remainder is 0 and turn is even then array is full and is in its initial state ; If the remainder is not 0 and the turn is even ; Current size of the array ; Current size of the array ; Print the result ; Driver code ; The initial array , - 1 is for 1 base indexing ; Queries in the form of the pairs of ( t , M )
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PerformQueries ( vector < int > & a , vector < pair < long long , int > > & vec ) { vector < int > ans ; int n = ( int ) a . size ( ) - 1 ; int q = ( int ) vec . size ( ) ; for ( int i = 0 ; i < q ; ++ i ) { long long t = vec [ i ] . first ; int m = vec [ i ] . second ; if ( m > n ) { ans . push_back ( -1 ) ; continue ; } int turn = t / n ; int rem = t % n ; if ( rem == 0 and turn % 2 == 1 ) { ans . push_back ( -1 ) ; continue ; } if ( rem == 0 and turn % 2 == 0 ) { ans . push_back ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { int cursize = n - rem ; if ( cursize < m ) { ans . push_back ( -1 ) ; continue ; } ans . push_back ( a [ m + rem ] ) ; } else { int cursize = rem ; if ( cursize < m ) { ans . push_back ( -1 ) ; continue ; } ans . push_back ( a [ m ] ) ; } } for ( int i : ans ) cout << i << " STRNEWLINE " ; } int main ( ) { vector < int > a = { -1 , 1 , 2 , 3 , 4 , 5 } ; vector < pair < long long , int > > vec = { { 1 , 4 } , { 6 , 1 } , { 3 , 5 } } ; PerformQueries ( a , vec ) ; return 0 ; }
Number of pairs in an array having sum equal to product | C ++ implementation of the approach ; Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } int main ( ) { int a [ ] = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << sumEqualProduct ( a , n ) ; return 0 ; }
Minimize the sum of digits of A and B such that A + B = N | C ++ implementation of the approach ; Function to return the minimum possible sum of digits of A and B such that A + B = n ; Find the sum of digits of n ; If num is a power of 10 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } if ( sum == 1 ) return 10 ; return sum ; } int main ( ) { int n = 1884 ; cout << minSum ( n ) ; return 0 ; }
Multiplication on Array : Range update query in O ( 1 ) | C ++ program for the above approach ; Creates a mul [ ] array for A [ ] and returns it after filling initial values . ; Does range update ; Prints updated Array ; Driver code ; ; Array to be updated ; Create and fill mul and div Array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void initialize ( int mul [ ] , int div [ ] , int size ) { for ( int i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } void update ( int l , int r , int x , int mul [ ] , int div [ ] ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } void printArray ( int ar [ ] , int mul [ ] , int div [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; cout << ar [ i ] << " ▁ " ; } } int main ( ) { int ar [ ] = { 10 , 5 , 20 , 40 } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; int mul [ n + 1 ] , div [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ; return 0 ; }
Optimal Strategy for a Game | Special Gold Coin | C ++ implementation of the approach ; Function to return the winner of the game ; To store the count of silver coins ; Update the position of the gold coin ; First player will win the game ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getWinner ( string str , int len ) { int total = 0 ; if ( str [ 0 ] == ' G ' str [ len - 1 ] == ' G ' ) return " First " ; else { for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' S ' ) { total ++ ; } } if ( ( total % 2 ) == 1 ) return " First " ; return " Second " ; } } int main ( ) { string str = " GSSS " ; int len = str . length ( ) ; cout << getWinner ( str , len ) ; return 0 ; }
Find the coordinates of a triangle whose Area = ( S / 2 ) | C ++ implementation of the approach ; Function to find the triangle with area = ( S / 2 ) ; Fix the two pairs of coordinates ; Find ( X3 , Y3 ) with integer coordinates ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const long MAX = 1000000000 ; void findTriangle ( long S ) { long X1 = 0 , Y1 = 0 ; long X2 = MAX , Y2 = 1 ; long X3 = ( MAX - S % MAX ) % MAX ; long Y3 = ( S + X3 ) / MAX ; cout << " ( " << X1 << " , ▁ " << Y1 << " ) STRNEWLINE " ; cout << " ( " << X2 << " , ▁ " << Y2 << " ) STRNEWLINE " ; cout << " ( " << X3 << " , ▁ " << Y3 << " ) " ; } int main ( ) { long S = 4 ; findTriangle ( S ) ; return 0 ; }
Rearrange array elements such that Bitwise AND of first N | C ++ implementation of the approach ; Utility function to print the elements of an array ; Function to find the required arrangement ; There has to be atleast 2 elements ; Minimum element from the array ; Swap any occurrence of the minimum element with the last element ; Find the bitwise AND of the first ( n - 1 ) elements ; If the bitwise AND is equal to the last element then print the arrangement ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void findArrangement ( int arr [ ] , int n ) { if ( n < 2 ) { cout << " - 1" ; return ; } int minVal = * min_element ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minVal ) { swap ( arr [ i ] , arr [ n - 1 ] ) ; break ; } } int andVal = arr [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { andVal &= arr [ i ] ; } if ( andVal == arr [ n - 1 ] ) printArr ( arr , n ) ; else cout << " - 1" ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; findArrangement ( arr , n ) ; return 0 ; }
Maximum prime moves to convert X to Y | C ++ implementation of the approach ; Function to return the maximum operations required to convert X to Y ; X cannot be converted to Y ; If the difference is 1 ; If the difference is even ; Add 3 to X and the new difference will be even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOperations ( int X , int Y ) { if ( X > Y ) return -1 ; int diff = Y - X ; if ( diff == 1 ) return -1 ; if ( diff % 2 == 0 ) return ( diff / 2 ) ; return ( 1 + ( ( diff - 3 ) / 2 ) ) ; } int main ( ) { int X = 5 , Y = 16 ; cout << maxOperations ( X , Y ) ; return 0 ; }
Sum of Digits of the Good Strings | C ++ implementation of the approach ; To store the states of the dp ; Function to fill the dp table ; Sum of digits of the string of length 1 is i as i is only number in that string and count of good strings of length 1 that end with i is also 1 ; Adjacent digits are different ; Increment the count as digit at ( i - 1 ) 'th index is k and count  of good strings is equal to this  because at the end of the strings of  length (i - 1) we are just  putting digit j as the last digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define DIGITS  10 NEW_LINE #define MAX  10000 NEW_LINE #define MOD  1000000007 NEW_LINE long dp [ MAX ] [ DIGITS ] , cnt [ MAX ] [ DIGITS ] ; void precompute ( ) { for ( int i = 0 ; i < DIGITS ; i ++ ) dp [ 1 ] [ i ] = i , cnt [ 1 ] [ i ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < DIGITS ; j ++ ) { for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( j != k ) { dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ; dp [ i ] [ j ] %= MOD ; cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ; cnt [ i ] [ j ] %= MOD ; } } } } } int main ( ) { long long int x = 6 , y = 4 ; precompute ( ) ; cout << dp [ x ] [ y ] ; return 0 ; }
Count non | C ++ implementation of the approach ; Function to pre - compute the sequence ; For N = 1 the answer will be 2 ; Starting two terms of the sequence ; Compute the rest of the sequence with the relation F [ i ] = F [ i - 1 ] + F [ i - 2 ] ; Driver code ; Pre - compute the sequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE const ll N = 10000 ; const ll MOD = 1000000007 ; ll F [ N ] ; void precompute ( ) { F [ 1 ] = 2 ; F [ 2 ] = 3 ; F [ 3 ] = 4 ; for ( int i = 4 ; i < N ; i ++ ) F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ; } int main ( ) { int n = 8 ; precompute ( ) ; cout << F [ n ] ; return 0 ; }
Minimize sum by dividing all elements of a subarray by K | C ++ implementation of the approach ; Function to return the maximum subarray sum ; Function to return the minimized sum of the array elements after performing the given operation ; Find maximum subarray sum ; Find total sum of the array ; Maximum subarray sum is already negative ; Choose the subarray whose sum is maximum and divide all elements by K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } double minimizedSum ( int a [ ] , int n , int K ) { int sum = maxSubArraySum ( a , n ) ; double totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) totalSum += a [ i ] ; if ( sum < 0 ) return totalSum ; totalSum = totalSum - sum + ( double ) sum / ( double ) K ; return totalSum ; } int main ( ) { int a [ ] = { 1 , -2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int K = 2 ; cout << minimizedSum ( a , n , K ) ; return 0 ; }
Minimum numbers with one 's place as 9 to be added to get N | C ++ implementation of the approach ; Function to find minimum count of numbers ( with one 's digit 9) that sum up to N ; Fetch one 's digit ; Apply Cases mentioned in approach ; If no possible answer exists ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMin ( int N ) { int digit = N % 10 ; switch ( digit ) { case 0 : if ( N >= 90 ) return 10 ; break ; case 1 : if ( N >= 81 ) return 9 ; break ; case 2 : if ( N >= 72 ) return 8 ; break ; case 3 : if ( N >= 63 ) return 7 ; break ; case 4 : if ( N >= 54 ) return 6 ; break ; case 5 : if ( N >= 45 ) return 5 ; break ; case 6 : if ( N >= 36 ) return 4 ; break ; case 7 : if ( N >= 27 ) return 3 ; break ; case 8 : if ( N >= 18 ) return 2 ; break ; case 9 : if ( N >= 9 ) return 1 ; break ; } return -1 ; } int main ( ) { int N = 27 ; cout << findMin ( N ) ; }
Random list of M non | C ++ implementation of the approach ; Utility function to print the elements of an array ; Function to generate a list of m random non - negative integers whose sum is n ; Create an array of size m where every element is initialized to 0 ; To make the sum of the final list as n ; Increment any random element from the array by 1 ; Print the generated list ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void randomList ( int m , int n ) { int arr [ m ] = { 0 } ; srand ( time ( 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ rand ( ) % m ] ++ ; } printArr ( arr , m ) ; } int main ( ) { int m = 4 , n = 8 ; randomList ( m , n ) ; return 0 ; }
Maximum String Partition | C ++ implementation of the above approach ; Return the count of string ; P will store the answer ; Current will store current string Previous will store the previous string that has been taken already ; Add a character to current string ; Here we will create a partition and update the previous string with current string ; Now we will clear the current string ; Increment the count of partition . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPartition ( string s ) { int n = s . length ( ) , P = 0 ; string current = " " , previous = " " ; for ( int i = 0 ; i < n ; i ++ ) { current += s [ i ] ; if ( current != previous ) { previous = current ; current . clear ( ) ; P ++ ; } } return P ; } int main ( ) { string s = " geeksforgeeks " ; int ans = maxPartition ( s ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
How to learn Pattern printing easily ? |
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int N = 4 , i , j , min ; cout << " Value ▁ of ▁ N : ▁ " << N << endl ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; cout << N - min + 1 ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; cout << N - min + 1 ; } cout << endl ; } return 0 ; }
Find the smallest positive number missing from an unsorted array | Set 3 | CPP program to find the smallest positive missing number ; Function to find the smallest positive missing number ; Default smallest Positive Integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMissingPositive ( int arr [ ] , int n ) { int m = 1 ; set < int > x ; for ( int i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . insert ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . count ( m ) ) { x . erase ( m ) ; m = m + 1 ; } } } return m ; } int main ( ) { int arr [ ] = { 2 , 3 , -7 , 6 , 8 , 1 , -10 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMissingPositive ( arr , n ) ; return 0 ; }