codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest subarray having only K distinct Prime Numbers | C ++ program for the above approach ; Function to precalculate all the prime up to 10 ^ 6 ; Initialize prime to true ; Iterate [ 2 , sqrt ( N ) ] ; If p is prime ; Mark all multiple of p as true ; Function that finds the length of longest subarray K distinct primes ; Precompute all prime up to 2 * 10 ^ 6 ; Keep track occurrence of prime ; Initialize result to - 1 ; If number is prime then increment its count and decrease k ; Decrement K ; Remove required elements till k become non - negative ; Decrease count so that it may appear in another subarray appearing after this present subarray ; Increment K ; Take the max value as length of subarray ; Return the final length ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isprime [ 2000010 ] ; void SieveOfEratosthenes ( int n ) { memset ( isprime , true , sizeof ( isprime ) ) ; isprime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isprime [ i ] = false ; } } } int KDistinctPrime ( int arr [ ] , int n , int k ) { SieveOfEratosthenes ( 2000000 ) ; map < int , int > cnt ; int result = -1 ; for ( int i = 0 , j = -1 ; i < n ; ++ i ) { int x = arr [ i ] ; if ( isprime [ x ] ) { if ( ++ cnt [ x ] == 1 ) { -- k ; } } while ( k < 0 ) { x = arr [ ++ j ] ; if ( isprime [ x ] ) { if ( -- cnt [ x ] == 0 ) { ++ k ; } } } if ( k == 0 ) result = max ( result , i - j ) ; } return result ; } int main ( void ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << KDistinctPrime ( arr , N , K ) ; return 0 ; }
Smallest composite number not divisible by first N prime numbers | C ++ Program to implement the above approach ; Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_SIZE  1000005 NEW_LINE void SieveOfEratosthenes ( vector < int > & StorePrimes ) { bool IsPrime [ MAX_SIZE ] ; memset ( IsPrime , true , sizeof ( IsPrime ) ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX_SIZE ; p ++ ) if ( IsPrime [ p ] ) StorePrimes . push_back ( p ) ; } int Smallest_non_Prime ( vector < int > StorePrimes , int N ) { int x = StorePrimes [ N ] ; return x * x ; } int main ( ) { int N = 3 ; vector < int > StorePrimes ; SieveOfEratosthenes ( StorePrimes ) ; cout << Smallest_non_Prime ( StorePrimes , N ) ; return 0 ; }
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C ++ program for the above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; update n ; Print the ans in sorted order ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdio.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE void printSubset ( lli n , int k ) { vector < lli > answer ; while ( n > 0 ) { lli p = log2 ( n ) ; answer . push_back ( pow ( k , p ) ) ; n %= ( int ) pow ( 2 , p ) ; } reverse ( answer . begin ( ) , answer . end ( ) ) ; for ( auto x : answer ) { cout << x << " ▁ " ; } } int main ( ) { lli n = 5 ; int k = 4 ; printSubset ( n , k ) ; }
Minimum time taken by each job to be completed given by a Directed Acyclic Graph | C ++ program for the above approach ; Adjacency List to store the graph ; Array to store the in - degree of node ; Array to store the time in which the job i can be done ; Function to add directed edge between two vertices ; Insert edge from u to v ; Increasing the indegree of vertex v ; Function to find the minimum time needed by each node to get the task ; Queue to store the nodes while processing ; Update the time of the jobs who don 't require any job to  be completed before this job ; Iterate until queue is empty ; Get front element of queue ; Pop the front element ; Decrease in - degree of the current node ; Push its adjacent elements ; Print the time to complete the job ; Driver Code ; Given Nodes N and edges M ; Given Directed Edges of graph ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  100000 NEW_LINE vector < int > graph [ maxN ] ; int indegree [ maxN ] ; int job [ maxN ] ; void addEdge ( int u , int v ) { graph [ u ] . push_back ( v ) ; indegree [ v ] ++ ; } void printOrder ( int n , int m ) { queue < int > q ; for ( int i = 1 ; i <= n ; i ++ ) { if ( indegree [ i ] == 0 ) { q . push ( i ) ; job [ i ] = 1 ; } } while ( ! q . empty ( ) ) { int cur = q . front ( ) ; q . pop ( ) ; for ( int adj : graph [ cur ] ) { indegree [ adj ] -- ; if ( indegree [ adj ] == 0 ) { job [ adj ] = job [ cur ] + 1 ; q . push ( adj ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) cout << job [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { int n , m ; n = 10 ; m = 13 ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 1 , 5 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 8 ) ; addEdge ( 2 , 9 ) ; addEdge ( 3 , 6 ) ; addEdge ( 4 , 6 ) ; addEdge ( 4 , 8 ) ; addEdge ( 5 , 8 ) ; addEdge ( 6 , 7 ) ; addEdge ( 7 , 8 ) ; addEdge ( 8 , 10 ) ; printOrder ( n , m ) ; return 0 ; }
Permutation of Array such that products of all adjacent elements are even | C ++ program to Permutation of Array such that product of all adjacent elements is even ; Function to print the required permutation ; push odd elements in ' odd ' and even elements in ' even ' ; Check if it possible to arrange the elements ; else print the permutation ; Print remaining odds are even . and even elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int arr [ ] , int n ) { vector < int > odd , even ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . push_back ( arr [ i ] ) ; else odd . push_back ( arr [ i ] ) ; } int size_odd = odd . size ( ) ; int size_even = even . size ( ) ; if ( size_odd > size_even + 1 ) cout << -1 << endl ; else { int i = 0 ; int j = 0 ; while ( i < size_odd && j < size_even ) { cout << odd [ i ] << " ▁ " ; ++ i ; cout << even [ j ] << " ▁ " ; ++ j ; } while ( i < size_odd ) { cout << odd [ i ] << " ▁ " ; ++ i ; } while ( j < size_even ) { cout << even [ j ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 6 , 7 , 9 , 8 , 10 , 11 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPermutation ( arr , N ) ; return 0 ; }
Maximize GCD of all possible pairs from 1 to N | C ++ Program to implement the approach ; Function to obtain the maximum gcd of all pairs from 1 to n ; Print the answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( int n ) { cout << n / 2 << endl ; } int main ( ) { int n = 5 ; find ( n ) ; return 0 ; }
Count of pairs of Array elements which are divisible by K when concatenated | C ++ Program to count pairs of array elements which are divisible by K when concatenated ; Function to calculate and return the count of pairs ; Compute power of 10 modulo k ; Calculate length of a [ i ] ; Increase count of remainder ; Calculate ( a [ i ] * 10 ^ lenj ) % k ; Calculate ( k - ( a [ i ] * 10 ^ lenj ) % k ) % k ; Increase answer by count ; If a pair ( a [ i ] , a [ i ] ) is counted ; Return the count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , int > rem [ 11 ] ; int countPairs ( vector < int > a , int n , int k ) { vector < int > len ( n ) ; vector < int > p ( 11 ) ; p [ 0 ] = 1 ; for ( int i = 1 ; i <= 10 ; i ++ ) { p [ i ] = ( p [ i - 1 ] * 10 ) % k ; } for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; while ( x > 0 ) { len [ i ] ++ ; x /= 10 ; } rem [ len [ i ] ] [ a [ i ] % k ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= 10 ; j ++ ) { int r = ( a [ i ] * p [ j ] ) % k ; int xr = ( k - r ) % k ; ans += rem [ j ] [ xr ] ; if ( len [ i ] == j && ( r + a [ i ] % k ) % k == 0 ) ans -- ; } } return ans ; } int main ( ) { vector < int > a = { 4 , 5 , 2 } ; int n = a . size ( ) , k = 2 ; cout << countPairs ( a , n , k ) ; }
Count of N digit Numbers whose sum of every K consecutive digits is equal | C ++ program for the above approach ; Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits using sliding window ; Driver Code ; Given integer N and K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigitSum ( int N , int K ) { int l = ( int ) pow ( 10 , N - 1 ) , r = ( int ) pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int digits [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = K ; j < N ; j ++ ) { if ( sum - digits [ j - K ] + digits [ j ] != sum ) { flag = 1 ; break ; } } if ( flag == 0 ) count ++ ; } return count ; } int main ( ) { int N = 2 , K = 1 ; cout << countDigitSum ( N , K ) << endl ; return 0 ; }
Array formed using sum of absolute differences of that element with all other elements | C ++ program for the above approach ; Function to return the new array ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize int sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > calculate ( int * arr , int n ) { vector < int > ans ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { sum += abs ( arr [ i ] - arr [ j ] ) ; } ans . push_back ( sum ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < int > ans = calculate ( arr , n ) ; cout << " [ " ; for ( auto itr : ans ) cout << itr << " , ▁ " ; cout << " ] " ; return 0 ; }
Number of pair of positions in matrix which are not accessible | C ++ program to count number of pair of positions in matrix which are not accessible ; Counts number of vertices connected in a component containing x . Stores the count in k . ; Incrementing the number of node in a connected component . ; Return the number of count of non - accessible cells . ; Initialize count of connected vertices found by DFS starting from i . ; Update result ; Inserting the edge between edge . ; Mapping the cell coordinate into node number . ; Inserting the edge . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( vector < int > graph [ ] , bool visited [ ] , int x , int * k ) { for ( int i = 0 ; i < graph [ x ] . size ( ) ; i ++ ) { if ( ! visited [ graph [ x ] [ i ] ] ) { ( * k ) ++ ; visited [ graph [ x ] [ i ] ] = true ; dfs ( graph , visited , graph [ x ] [ i ] , k ) ; } } } int countNonAccessible ( vector < int > graph [ ] , int N ) { bool visited [ N * N + N ] ; memset ( visited , false , sizeof ( visited ) ) ; int ans = 0 ; for ( int i = 1 ; i <= N * N ; i ++ ) { if ( ! visited [ i ] ) { visited [ i ] = true ; int k = 1 ; dfs ( graph , visited , i , & k ) ; ans += k * ( N * N - k ) ; } } return ans ; } void insertpath ( vector < int > graph [ ] , int N , int x1 , int y1 , int x2 , int y2 ) { int a = ( x1 - 1 ) * N + y1 ; int b = ( x2 - 1 ) * N + y2 ; graph [ a ] . push_back ( b ) ; graph [ b ] . push_back ( a ) ; } int main ( ) { int N = 2 ; vector < int > graph [ N * N + 1 ] ; insertpath ( graph , N , 1 , 1 , 1 , 2 ) ; insertpath ( graph , N , 1 , 2 , 2 , 2 ) ; cout << countNonAccessible ( graph , N ) << endl ; return 0 ; }
Minimum number of distinct powers of 2 required to express a given binary number | C ++ Program to implement the above approach ; Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimum ( string s ) { int n = s . size ( ) ; int x [ n + 1 ] = { 0 } ; reverse ( s . begin ( ) , s . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { if ( x [ i ] == 1 ) { x [ i + 1 ] = 1 ; x [ i ] = 0 ; } else if ( i and x [ i - 1 ] == 1 ) { x [ i + 1 ] = 1 ; x [ i - 1 ] = -1 ; } else x [ i ] = 1 ; } } int c = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( x [ i ] != 0 ) c ++ ; } cout << c << endl ; } int main ( ) { string str = "111" ; findMinimum ( str ) ; return 0 ; }
Split a Numeric String into Fibonacci Sequence | C ++ program of the above approach ; Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Driver Code ; Given String ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define LL  long long NEW_LINE bool splitIntoFibonacciHelper ( int pos , string S , vector < int > & seq ) { if ( pos == S . length ( ) and ( seq . size ( ) >= 3 ) ) { return true ; } LL num = 0 ; for ( int i = pos ; i < S . length ( ) ; i ++ ) { num = num * 10 + ( S [ i ] - '0' ) ; if ( num > INT_MAX ) break ; if ( S [ pos ] == '0' and i > pos ) break ; if ( seq . size ( ) > 2 and ( num > ( ( LL ) seq . back ( ) + ( LL ) seq [ seq . size ( ) - 2 ] ) ) ) break ; if ( seq . size ( ) < 2 or ( num == ( ( LL ) seq . back ( ) + ( LL ) seq [ seq . size ( ) - 2 ] ) ) ) { seq . push_back ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . pop_back ( ) ; } } return false ; } void splitIntoFibonacci ( string S ) { vector < int > seq ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . size ( ) >= 3 ) { for ( int i : seq ) cout << i << " ▁ " ; } else { cout << -1 ; } } int main ( ) { string S = "11235813" ; splitIntoFibonacci ( S ) ; return 0 ; }
Count pair of strings whose concatenation has every vowel | C ++ program for the above approach ; Function to return the count of all concatenated string with each vowel at least once ; Creating a hash array with initial value as 0 ; Traversing through each string and getting hash value for each of them ; Initializing the weight of each string ; Find the hash value for each string ; Increasing the count of the hash value ; Getting all possible pairs of indexes in hash array ; Check if the pair which has hash value 31 and multiplying the count of string and add it strCount ; Corner case , for strings which independently has all the vowels ; Return thre final count ; Driver Code ; Given array of strings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int good_pairs ( string str [ ] , int N ) { int arr [ 32 ] = { 0 } , strCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int Weight = 0 ; for ( int j = 0 ; j < str [ i ] . size ( ) ; j ++ ) { switch ( str [ i ] [ j ] ) { case ' a ' : Weight = Weight | 1 ; break ; case ' e ' : Weight = Weight | 2 ; break ; case ' i ' : Weight = Weight | 4 ; break ; case ' o ' : Weight = Weight | 8 ; break ; case ' u ' : Weight = Weight | 16 ; break ; } } arr [ Weight ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = i + 1 ; j < 32 ; j ++ ) { if ( ( i j ) == 31 ) strCount += arr [ i ] * arr [ j ] ; } } strCount += ( arr [ 31 ] * ( arr [ 31 ] - 1 ) ) / 2 ; return strCount ; } int main ( ) { string str [ ] = { " aaweiolkju " , " oxdfgujkmi " } ; int N = sizeof ( str ) / sizeof ( str [ 0 ] ) ; cout << good_pairs ( str , N ) ; return 0 ; }
Color a grid such that all same color cells are connected either horizontally or vertically | C ++ Program to Color a grid such that all same color cells are connected either horizontally or vertically ; Current color ; final grid ; if even row ; traverse from left to right ; if color has been exhausted , move to the next color ; color the grid at this position ; reduce the color count ; traverse from right to left for odd rows ; print the grid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( vector < int > & arr , int r , int c ) { int idx = 1 ; int dp [ r ] ; for ( int i = 0 ; i < r ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < c ; j ++ ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i ] [ j ] = idx ; arr [ idx - 1 ] -- ; } } else { for ( int j = c - 1 ; j >= 0 ; j -- ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i ] [ j ] = idx ; arr [ idx - 1 ] -- ; } } } for ( int i = 0 ; i < r ; ++ i ) { for ( int j = 0 ; j < c ; ++ j ) { cout << dp [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int r = 3 , c = 5 ; int n = 5 ; vector < int > arr = { 1 , 2 , 3 , 4 , 5 } ; solve ( arr , r , c ) ; return 0 ; }
Count of 0 s to be flipped to make any two adjacent 1 s at least K 0 s apart | C ++ program for the above problem ; Function to find the count of 0 s to be flipped ; Loop traversal to mark K adjacent positions to the right of already existing 1 s . ; Loop traversal to mark K adjacent positions to the left of already existing 1 s . ; Loop to count the maximum number of 0 s that will be replaced by 1 s ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int k , string s ) { int ar [ s . length ( ) ] ; int end = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) { for ( int j = i ; j < s . length ( ) && j <= i + k ; j ++ ) { ar [ j ] = -1 ; end = j ; } i = end ; } } end = 0 ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { for ( int j = i ; j >= 0 && j >= i - k ; j -- ) { ar [ j ] = -1 ; end = j ; } i = end ; } } int ans = 0 ; end = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { if ( ar [ j ] == 0 ) { ans ++ ; for ( int g = j ; g <= j + k && g < s . length ( ) ; g ++ ) { ar [ g ] = -1 ; end = g ; } j = end - 1 ; } } return ans ; } int main ( ) { int K = 2 ; string s = "000000" ; cout << count ( K , s ) << endl ; return 0 ; }
Length of longest connected 1 â €™ s in a Binary Grid | C ++ program for the above approach ; Keeps a track of directions that is up , down , left , right ; Function to perform the dfs traversal ; Mark the current node as visited ; Increment length from this node ; Update the diameter length ; Move to next cell in x - direction ; Move to next cell in y - direction ; Check if cell is invalid then continue ; Perform DFS on new cell ; Decrement the length ; Function to find the maximum length of connected 1 s in the given grid ; Increment the id ; Traverse the grid [ ] ; Find start point of start dfs call ; DFS Traversal from cell ( x , y ) ; Print the maximum length ; Driver Code ; Given grid [ ] [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE #define row  6 NEW_LINE #define col  7 NEW_LINE using namespace std ; int vis [ row + 1 ] [ col + 1 ] , id ; int diameter = 0 , length = 0 ; int dx [ ] = { -1 , 1 , 0 , 0 } ; int dy [ ] = { 0 , 0 , -1 , 1 } ; void dfs ( int a , int b , int lis [ ] [ col ] , int & x , int & y ) { vis [ a ] [ b ] = id ; length ++ ; if ( length > diameter ) { x = a ; y = b ; diameter = length ; } for ( int j = 0 ; j < 4 ; j ++ ) { int cx = a + dx [ j ] ; int cy = b + dy [ j ] ; if ( cx < 0 cy < 0 cx > = row cy > = col lis [ cx ] [ cy ] == 0 vis [ cx ] [ cy ] ) { continue ; } dfs ( cx , cy , lis , x , y ) ; } vis [ a ] [ b ] = 0 ; length -- ; } void findMaximumLength ( int lis [ ] [ col ] ) { int x , y ; id ++ ; length = 0 ; diameter = 0 ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { if ( lis [ i ] [ j ] != 0 ) { dfs ( i , j , lis , x , y ) ; i = row ; break ; } } } id ++ ; length = 0 ; diameter = 0 ; dfs ( x , y , lis , x , y ) ; cout << diameter ; } int main ( ) { int grid [ ] [ col ] = { { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 } } ; findMaximumLength ( grid ) ; return 0 ; }
Last two digits of powers of 7 | C ++ program for the above approach ; Function to find the last two digits of 7 ^ N ; Case 4 ; Case 3 ; Case 2 ; Case 1 ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string get_last_two_digit ( int N ) { if ( N % 4 == 0 ) return "01" ; else if ( N % 4 == 1 ) return "07" ; else if ( N % 4 == 2 ) return "49" ; return "43" ; } int main ( ) { int N = 12 ; cout << get_last_two_digit ( N ) ; return 0 ; }
Subsequence with maximum pairwise absolute difference and minimum size | C ++ program for the above approach ; Function to find the subsequence with maximum absolute difference ; To store the resultant subsequence ; First element should be included in the subsequence ; Traverse the given array arr [ ] ; If current element is greater than the previous element ; If the current element is not the local maxima then continue ; Else push it in subsequence ; If the current element is less then the previous element ; If the current element is not the local minima then continue ; Else push it in subsequence ; Last element should also be included in subsequence ; Print the element ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getSubsequence ( vector < int > ar ) { int N = ar . size ( ) ; vector < int > ans ; ans . push_back ( ar [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( ar [ i ] > ar [ i - 1 ] ) { if ( i < N - 1 && ar [ i ] <= ar [ i + 1 ] ) { continue ; } else { ans . push_back ( ar [ i ] ) ; } } else { if ( i < N - 1 && ar [ i + 1 ] < ar [ i ] ) { continue ; } else { ans . push_back ( ar [ i ] ) ; } } } ans . push_back ( ar [ N - 1 ] ) ; for ( auto & it : ans ) cout << it << ' ▁ ' ; } int main ( ) { vector < int > arr = { 1 , 2 , 4 , 3 , 5 } ; getSubsequence ( arr ) ; }