codeparrot/xlcost-text-to-code huggingface dataset:
Smallest positive integer that does not divide any elements of the given array | C ++ program for the above approach ; Function to find the smallest number which doesn 't divides any integer in the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; void smallestNumber ( int arr [ ] , int len ) { int maxi = 0 ; for ( int i = 0 ; i < len ; i ++ ) { maxi = std :: max ( maxi , arr [ i ] ) ; } int ans = -1 ; for ( int i = 2 ; i < maxi + 2 ; i ++ ) { bool flag = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } cout << ans ; } int main ( ) { int arr [ ] = { 3 , 2 , 6 , 9 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smallestNumber ( arr , N ) ; return 0 ; }
Find the duplicate characters in a string in O ( 1 ) space | C ++ program to implement the above approach ; Function to find duplicate characters in string without using any additional data structure ; Check if ( i + ' a ' ) is present in str at least once or not . ; Check if ( i + ' a ' ) is present in str at least twice or not . ; Iterate over the characters of the string str ; If str [ i ] has already occurred in str ; Set ( str [ i ] - ' a ' ) - th bit of second ; Set ( str [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDuplicate ( string str , int N ) { int first = 0 ; int second = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( first & ( 1 << ( str [ i ] - ' a ' ) ) ) { second = second | ( 1 << ( str [ i ] - ' a ' ) ) ; } else { first = first | ( 1 << ( str [ i ] - ' a ' ) ) ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( ( first & ( 1 << i ) ) && ( second & ( 1 << i ) ) ) { cout << char ( i + ' a ' ) << " ▁ " ; } } } int main ( ) { string str = " geeksforgeeks " ; int N = str . length ( ) ; findDuplicate ( str , N ) ; }
Minimize insertions required to make all characters of a given string equal | C ++ program for the above approach ; Function to calculate the minimum number of operations required to make all characters of the string same ; Stores count of operations ; Traverse the string ; Check if adjacent characters are same or not ; Increment count ; Print the count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string & S ) { int count = 0 ; for ( int i = 1 ; i < S . length ( ) ; i ++ ) { if ( S [ i ] != S [ i - 1 ] ) { count += 1 ; } } cout << count ; } int main ( ) { string S = "0101010101" ; minOperations ( S ) ; return 0 ; }
Count pairs made up of an element divisible by the other from an array consisting of powers of 2 | C ++ program for the above approach ; Function to count the number of pairs as per the given conditions ; Initialize array set_bits as 0 ; Store the total number of required pairs ; Traverse the array arr [ ] ; Store arr [ i ] in x ; Store the position of the leftmost set bit in arr [ i ] ; Increase bit position ; Divide by 2 to shift bits in right at each step ; Count of pairs for index i till its set bit position ; Increasing count of set bit position of current elelement ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numberOfPairs ( int arr [ ] , int N ) { int set_bits [ 31 ] = { 0 } ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = arr [ i ] ; int bitpos = -1 ; while ( x > 0 ) { bitpos ++ ; x /= 2 ; } for ( int j = 0 ; j <= bitpos ; j ++ ) { count += set_bits [ j ] ; } set_bits [ bitpos ] ++ ; } cout << count ; } int main ( ) { int arr [ ] = { 4 , 16 , 8 , 64 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; numberOfPairs ( arr , N ) ; return 0 ; }
Queries to find minimum sum of array elements from either end of an array | C ++ implementation of the above approach ; Function to calculate the minimum sum from either end of the arrays for the given queries ; Traverse the query [ ] array ; Stores sum from start and end of the array ; Calculate distance from start ; Calculate distance from end ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void calculateQuery ( int arr [ ] , int N , int query [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int X = query [ i ] ; int sum_start = 0 , sum_end = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum_start += arr [ j ] ; if ( arr [ j ] == X ) break ; } for ( int j = N - 1 ; j >= 0 ; j -- ) { sum_end += arr [ j ] ; if ( arr [ j ] == X ) break ; } cout << min ( sum_end , sum_start ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 30 } ; int queries [ ] = { 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; calculateQuery ( arr , N , queries , M ) ; return 0 ; }
Queries to find minimum sum of array elements from either end of an array | C ++ implementation of the above approach ; Function to find the minimum sum for the given queries ; Stores prefix and suffix sums ; Stores pairs of prefix and suffix sums ; Traverse the array ; Add element to prefix ; Store prefix for each element ; Traverse the array in reverse ; Add element to suffix ; Storing suffix for each element ; Traverse the array queries [ ] ; Minimum of suffix and prefix sums ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void calculateQuery ( int arr [ ] , int N , int query [ ] , int M ) { int prefix = 0 , suffix = 0 ; unordered_map < int , pair < int , int > > mp ; for ( int i = 0 ; i < N ; i ++ ) { prefix += arr [ i ] ; mp [ arr [ i ] ] . first = prefix ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suffix += arr [ i ] ; mp [ arr [ i ] ] . second = suffix ; } for ( int i = 0 ; i < M ; i ++ ) { int X = query [ i ] ; cout << min ( mp [ X ] . first , mp [ X ] . second ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 30 } ; int queries [ ] = { 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; calculateQuery ( arr , N , queries , M ) ; return 0 ; }
Queries to find the maximum array element after removing elements from a given range | C ++ program for the above approach ; Function to find the maximum element after removing elements in range [ l , r ] ; Store prefix maximum element ; Store suffix maximum element ; Prefix max till first index is first index itself ; Traverse the array to fill prefix max array ; Store maximum till index i ; Suffix max till last index is last index itself ; Traverse the array to fill suffix max array ; Store maximum till index i ; Traverse all queries ; Store the starting and the ending index of the query ; Edge Cases ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaximum ( int arr [ ] , int N , int Q , int queries [ ] [ 2 ] ) { int prefix_max [ N + 1 ] = { 0 } ; int suffix_max [ N + 1 ] = { 0 } ; prefix_max [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefix_max [ i ] = max ( prefix_max [ i - 1 ] , arr [ i ] ) ; } suffix_max [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { suffix_max [ i ] = max ( suffix_max [ i + 1 ] , arr [ i ] ) ; } for ( int i = 0 ; i < Q ; i ++ ) { int l = queries [ i ] [ 0 ] ; int r = queries [ i ] [ 1 ] ; if ( l == 0 && r == ( N - 1 ) ) cout << "0 STRNEWLINE " ; else if ( l == 0 ) cout << suffix_max [ r + 1 ] << " STRNEWLINE " ; else if ( r == ( N - 1 ) ) cout << prefix_max [ l - 1 ] << " STRNEWLINE " ; else cout << max ( prefix_max [ l - 1 ] , suffix_max [ r + 1 ] ) << " STRNEWLINE " ; } } int main ( ) { int arr [ ] = { 5 , 6 , 8 , 10 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int queries [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 4 } } ; int Q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; findMaximum ( arr , N , Q , queries ) ; return 0 ; }
Queries to find first occurrence of a character in a given range | C ++ implementation for the above approach ; Function to find the first occurence for a given range ; N = length of string ; M = length of queries ; Stores the indices of a character ; Traverse the string ; Push the index i into the vector [ s [ i ] ] ; Traverse the query ; Stores the value L ; Stores the value R ; Stores the character C ; Find index >= L in the vector v ; If there is no index of C >= L ; Stores the value at idx ; If idx > R ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstOccurence ( string s , vector < pair < pair < int , int > , char > > Q ) { int N = s . size ( ) ; int M = Q . size ( ) ; map < char , vector < int > > v ; for ( int i = 0 ; i < N ; i ++ ) { v [ s [ i ] ] . push_back ( i ) ; } for ( int i = 0 ; i < M ; i ++ ) { int left = Q [ i ] . first . first ; int right = Q [ i ] . first . second ; char c = Q [ i ] . second ; if ( v . size ( ) == 0 ) { cout << " - 1 ▁ " ; continue ; } int idx = lower_bound ( v . begin ( ) , v . end ( ) , left ) - v . begin ( ) ; if ( idx == ( int ) v . size ( ) ) { cout << " - 1 ▁ " ; continue ; } idx = v [ idx ] ; if ( idx > right ) { cout << " - 1 ▁ " ; } else { cout << idx << " ▁ " ; } } } int main ( ) { string S = " abcabcabc " ; vector < pair < pair < int , int > , char > > Q = { { { 0 , 3 } , ' a ' } , { { 0 , 2 } , ' b ' } , { { 2 , 4 } , ' z ' } } ; firstOccurence ( S , Q ) ; return 0 ; }
Find index of the element differing in parity with all other array elements | C ++ program for the above approach ; Function to print the element which differs in parity ; Stores the count of odd and even array elements encountered ; Stores the indices of the last odd and even array elements encountered ; Traverse the array ; If array element is even ; Otherwise ; If only one odd element is present in the array ; If only one even element is present in the array ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oddOneOut ( int arr [ ] , int N ) { int odd = 0 , even = 0 ; int lastOdd = 0 , lastEven = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { cout << lastOdd << endl ; } else { cout << lastEven << endl ; } } int main ( ) { int arr [ ] = { 2 , 4 , 7 , 8 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; oddOneOut ( arr , N ) ; return 0 ; }
Reduce a string to a valid email address of minimum length by replacing specified substrings | C ++ program for the above approach ; Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' # such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; Driver code ; To display the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; string minEmail ( string email ) { string ans = " " ; int len = email . length ( ) ; ans += email [ 0 ] ; int i = 1 ; bool notAt = true ; while ( i < len ) { if ( i < len - 3 && notAt && email [ i ] == ' a ' && email [ i + 1 ] == ' t ' ) { ans += ' @ ' ; i += 1 ; notAt = false ; } else if ( i < len - 4 && email [ i ] == ' d ' && email [ i + 1 ] == ' o ' && email [ i + 2 ] == ' t ' ) { ans += ' . ' ; i += 2 ; } else { ans += email [ i ] ; } i += 1 ; } return ans ; } int main ( ) { string email = " geeksforgeeksatgmaildotcom " ; cout << ( minEmail ( email ) ) ; }
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | C ++ Program to implement the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Check if sum of first i natural number >= X ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isGreaterEqual ( int N , int X ) { return ( N * 1LL * ( N + 1 ) / 2 ) >= X ; } int minimumPossible ( int X ) { for ( int i = 1 ; i <= X ; i ++ ) { if ( isGreaterEqual ( i , X ) ) return i ; } } int main ( ) { int X = 14 ; cout << minimumPossible ( X ) ; return 0 ; }
Queries to count numbers from given range which are divisible by all its digits | C ++ program to implement the above approach ; Function to check if a number is divisible by all of its non - zero digits or not ; Stores the number ; Iterate over the digits of the numbers ; If digit of number is non - zero ; If number is not divisible by its current digit ; Update n ; Function to count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Stores count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Iterate over the range [ 1 , Max ] ; Update ; Traverse the array , arr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define Max  1000005 NEW_LINE bool CheckDivByAllDigits ( int number ) { int n = number ; while ( n > 0 ) { if ( n % 10 ) if ( number % ( n % 10 ) ) { return false ; } n /= 10 ; } return true ; } void cntNumInRang ( int arr [ ] [ 2 ] , int N ) { int prefCntDiv [ Max ] = { 0 } ; for ( int i = 1 ; i <= Max ; i ++ ) { prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ( CheckDivByAllDigits ( i ) ) ; } for ( int i = 0 ; i < N ; i ++ ) cout << ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] ) << " ▁ " ; } int main ( ) { int arr [ ] [ 2 ] = { { 1 , 5 } , { 12 , 14 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cntNumInRang ( arr , N ) ; return 0 ; }
Longest subset of nested elements from a given array | C ++ program to implement the above approach ; Function to find length of longest subset such that subset { arr [ i ] , arr [ arr [ i ] ] , . . } ; Stores length of the longest subset that satisfy the condition ; Traverse in the array , arr [ ] ; If arr [ i ] equals to i ; Update res ; Count of elements in a subset ; Stores index of elements in the current subset ; Calculate length of a subset that satisfy the condition ; Make visited the current index ; Update curr_index ; Update count ; Update res ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int arrayNesting ( vector < int > arr ) { int res = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] == i ) { res = max ( res , 1 ) ; } else { int count = 0 ; int curr_index = i ; while ( arr [ curr_index ] != curr_index ) { int next_index = arr [ curr_index ] ; arr [ curr_index ] = curr_index ; curr_index = next_index ; count ++ ; } res = max ( res , count ) ; } } return res ; } int main ( ) { vector < int > arr = { 5 , 4 , 0 , 3 , 1 , 6 , 2 } ; int res = arrayNesting ( arr ) ; cout << res ; }
Count numbers from a given range having exactly 5 distinct factors | C ++ Program to implement the above approach ; Stores all prime numbers up to 2 * 10 ^ 5 ; Function to generate all prime numbers up to 2 * 10 ^ 5 using Sieve of Eratosthenes ; Mark 0 and 1 as non - prime ; If i is prime ; Mark all its factors as non - prime ; If current number is prime ; Store the prime ; Function to count numbers in the range [ L , R ] having exactly 5 factors ; Stores the required count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 2e5 ; vector < long long > prime ; void Sieve ( ) { prime . clear ( ) ; vector < bool > p ( N + 1 , true ) ; p [ 0 ] = p [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( p [ i ] == true ) { for ( int j = i * i ; j <= N ; j += i ) { p [ j ] = false ; } } } for ( int i = 1 ; i < N ; i ++ ) { if ( p [ i ] ) { prime . push_back ( 1LL * pow ( i , 4 ) ) ; } } } void countNumbers ( long long int L , long long int R ) { int Count = 0 ; for ( int p : prime ) { if ( p >= L && p <= R ) { Count ++ ; } } cout << Count << endl ; } int main ( ) { long long L = 16 , R = 85000 ; Sieve ( ) ; countNumbers ( L , R ) ; return 0 ; }
Minimize remaining array sizes by removing equal pairs of first array elements | C ++ program for the above approach ; Function to count the remaining elements in the arrays ; Stores the count of 1 s in L1 [ ] ; Store the count of 0 s in L2 [ ] ; Traverse the array L1 [ ] ; If condition is true ; Increment one by 1 ; Increment zero by 1 ; Stores the index after which no further removals are possible ; Traverse the array L2 [ ] ; If condition is true ; Decrement one by 1 ; If one < 0 , then break out of the loop ; Decrement zero by 1 ; If zero < 0 , then break out of loop ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countRemainingElements ( int L1 [ ] , int L2 [ ] , int n ) { int one = 0 ; int zero = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( L1 [ i ] == 1 ) one ++ ; else zero ++ ; } int ans = n ; for ( int i = 0 ; i < n ; i ++ ) { if ( L2 [ i ] == 1 ) { one -- ; if ( one < 0 ) { ans = i ; break ; } } else { zero -- ; if ( zero < 0 ) { ans = i ; break ; } } } cout << n - ans ; } int main ( ) { int L1 [ ] = { 1 , 1 , 0 , 0 } ; int L2 [ ] = { 0 , 0 , 0 , 1 } ; int N = sizeof ( L1 ) / sizeof ( L1 [ 0 ] ) ; countRemainingElements ( L1 , L2 , N ) ; return 0 ; }
Count pairs of similar rectangles possible from a given array | C ++ Program for the above approach ; Function to calculate the count of similar rectangles ; Driver Code ; Input
#include <iostream> NEW_LINE using namespace std ; int getCount ( int rows , int columns , int A [ ] [ 2 ] ) { int res = 0 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = i + 1 ; j < rows ; j ++ ) { if ( A [ i ] [ 0 ] * 1LL * A [ j ] [ 1 ] == A [ i ] [ 1 ] * 1LL * A [ j ] [ 0 ] ) { res ++ ; } } } return res ; } int main ( ) { int A [ ] [ 2 ] = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << getCount ( rows , columns , A ) ; return 0 ; }
Find the index with minimum score from a given array | C ++ program for the above approach ; Function to find the index with minimum score ; Stores the score of current index ; Traverse the array in reverse ; Update minimum score ; Print the index with minimum score ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Min_Score_Index ( int N , vector < int > A ) { vector < int > Score ( N , 0 ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] + i < N ) Score [ i ] = A [ i ] * Score [ A [ i ] + i ] ; else Score [ i ] = A [ i ] ; } int min_value = INT_MAX ; for ( int i : Score ) min_value = min ( i , min_value ) ; int ind = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Score [ i ] == min_value ) ind = i ; } cout << ( ind ) ; } int main ( ) { int N = 5 ; vector < int > A = { 1 , 2 , 3 , 4 , 5 } ; Min_Score_Index ( N , A ) ; }
Minimize length of a string by removing occurrences of another string from it as a substring | C ++ program for the above approach ; Function to minimize length of string S after removing all occurrences of string T as substring ; Count of characters required to be removed ; Iterate over the string ; Insert the current character to temp ; Check if the last M characters forms t or not ; Getting the last M characters . If they are equal to t then remove the last M characters from the temp string ; Incrementing subtract by M ; Removing last M characters from the string ; Print the final answer ; Driver Code ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minLength ( string & S , string & T , int N , int M ) { string temp ; int subtract = 0 ; for ( int i = 0 ; i < N ; ++ i ) { temp . push_back ( S [ i ] ) ; if ( temp . size ( ) >= M ) { if ( temp . substr ( temp . size ( ) - M , M ) == T ) { subtract += M ; int cnt = 0 ; while ( cnt != M ) { temp . pop_back ( ) ; ++ cnt ; } } } } cout << ( N - subtract ) << " STRNEWLINE " ; } int main ( ) { string S = " aabcbcbd " , T = " abc " ; int N = S . size ( ) ; int M = T . size ( ) ; minLength ( S , T , N , M ) ; }
Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array | C ++ program for the above approach ; Function to find the minimum count of swaps required between the two arrays to make the sum of arr1 [ ] greater than that of arr2 [ ] ; Stores the sum of the two arrays ; Calculate sum of arr1 [ ] ; Calculate sum of arr2 [ ] ; Sort the arrays arr1 [ ] and arr2 [ ] ; Traverse the array arr [ ] ; If the sum1 is less than or equal to sum2 ; Swapping the elements ; Update the sum1 and sum2 ; Increment the count ; Return the final count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumCount ( int arr1 [ ] , int arr2 [ ] , int s1 , int s2 ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < s1 ; i ++ ) { sum1 += arr1 [ i ] ; } for ( int j = 0 ; j < s2 ; j ++ ) { sum2 += arr2 [ j ] ; } int len = 0 ; if ( s1 >= s2 ) { len = s2 ; } else { len = s1 ; } sort ( arr1 , arr1 + s1 ) ; sort ( arr2 , arr2 + s2 ) ; int j = 0 , k = s2 - 1 , count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( sum1 <= sum2 ) { if ( arr2 [ k ] >= arr1 [ i ] ) { int dif1 = arr1 [ j ] , dif2 = arr2 [ k ] ; sum1 -= dif1 ; sum1 += dif2 ; sum2 -= dif2 ; sum2 += dif1 ; j ++ ; k -- ; count ++ ; } else { break ; } } else { break ; } } return count ; } int main ( ) { int arr1 [ ] = { 1 , 3 , 2 , 4 } ; int arr2 [ ] = { 6 , 7 , 8 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int M = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << maximumCount ( arr1 , arr2 , N , M ) ; return 0 ; }
Count array elements whose all distinct digits appear in K | C ++ program for the above approach ; Function to check a digit occurs in the digit of K or not ; Iterate over all possible digits of K ; If current digit equal to digit ; Update K ; Function to find the count of array elements whose distinct digits are a subset of digits of K ; Stores count of array elements whose distinct digits are subset of digits of K ; Traverse the array , [ ] arr ; Stores the current element ; Check if all the digits arr [ i ] is a subset of the digits of K or not ; Iterate over all possible digits of arr [ i ] ; Stores current digit ; If current digit does not appear in K ; Update flag ; Update no ; If all the digits arr [ i ] appear in K ; Update count ; Finally print count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static bool isValidDigit ( int digit , int K ) { while ( K != 0 ) { if ( K % 10 == digit ) { return true ; } K = K / 10 ; } return false ; } int noOfValidNumbers ( int K , int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int no = arr [ i ] ; bool flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( ! isValidDigit ( digit , K ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } int main ( ) { int K = 12 ; int arr [ ] = { 1 , 12 , 1222 , 13 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << noOfValidNumbers ( K , arr , n ) ; return 0 ; }