codeparrot/xlcost-text-to-code huggingface dataset:
Find whether a given number is a power of 4 or not | C ++ program to check if given number is power of 4 or not ; Function to check if x is power of 4 ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is even then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfFour ( unsigned int n ) { int count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } int main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) cout << test_no << " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else cout << test_no << " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ; }
Find whether a given number is a power of 4 or not | C ++ program to check if given number is power of 4 or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfFour ( unsigned int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ! ( n & 0xAAAAAAAA ) ; } int main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) cout << test_no << " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else cout << test_no << " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ; }
Find whether a given number is a power of 4 or not | C ++ program to check if given number is power of 4 or not ; 0 is not considered as a power of 4 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float logn ( int n , int r ) { return log ( n ) / log ( r ) ; } bool isPowerOfFour ( int n ) { if ( n == 0 ) return false ; return floor ( logn ( n , 4 ) ) == ceil ( logn ( n , 4 ) ) ; } int main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) cout << test_no << " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else cout << test_no << " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ; return 0 ; }
Find whether a given number is a power of 4 or not | C ++ program to check if given number is power of 4 or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPowerOfFour ( int n ) { return ( n > 0 and pow ( 4 , int ( log2 ( n ) / log2 ( 4 ) ) == n ) ) ; } int main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) cout << test_no << " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else cout << test_no << " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ; return 0 ; }
Compute the minimum or maximum of two integers without branching | C ++ program to Compute the minimum or maximum of two integers without branching ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code
#include <iostream> NEW_LINE using namespace std ; class gfg { public : int min ( int x , int y ) { return y ^ ( ( x ^ y ) & - ( x < y ) ) ; } int max ( int x , int y ) { return x ^ ( ( x ^ y ) & - ( x < y ) ) ; } } ; int main ( ) { gfg g ; int x = 15 ; int y = 6 ; cout << " Minimum ▁ of ▁ " << x << " ▁ and ▁ " << y << " ▁ is ▁ " ; cout << g . min ( x , y ) ; cout << " Maximum of " ▁ < < ▁ x ▁ < < STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " and " ▁ < < ▁ y ▁ < < ▁ " is " cout << g . max ( x , y ) ; getchar ( ) ; }
Compute the minimum or maximum of two integers without branching |  ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define CHARBIT  8 NEW_LINE int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHARBIT - 1 ) ) ) ; } int max ( int x , int y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHARBIT - 1 ) ) ) ; } int main ( ) { int x = 15 ; int y = 6 ; cout << " Minimum ▁ of ▁ " << x << " ▁ and ▁ " << y << " ▁ is ▁ " ; cout << min ( x , y ) ; cout << " Maximum of " < < x < < " and " < < y < < " is " cout << max ( x , y ) ; }
Compute the minimum or maximum of two integers without branching | C ++ program for the above approach ; absbit32 function ; max function ; min function ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int absbit32 ( int x , int y ) { int sub = x - y ; int mask = ( sub >> 31 ) ; return ( sub ^ mask ) - mask ; } int max ( int x , int y ) { int abs = absbit32 ( x , y ) ; return ( x + y + abs ) / 2 ; } int min ( int x , int y ) { int abs = absbit32 ( x , y ) ; return ( x + y - abs ) / 2 ; } int main ( ) { cout << max ( 2 , 3 ) << endl ; cout << max ( 2 , -3 ) << endl ; cout << max ( -2 , -3 ) << endl ; cout << min ( 2 , 3 ) << endl ; cout << min ( 2 , -3 ) << endl ; cout << min ( -2 , -3 ) << endl ; return 0 ; }
Find the Number Occurring Odd Number of Times | C ++ program to find the element occurring odd number of times ; Function to find the element occurring odd number of times ; driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getOddOccurrence ( int arr [ ] , int arr_size ) { for ( int i = 0 ; i < arr_size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return -1 ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getOddOccurrence ( arr , n ) ; return 0 ; }
Find the Number Occurring Odd Number of Times | C ++ program to find the element occurring odd number of times ; function to find the element occurring odd number of times ; Putting all elements into the HashMap ; Iterate through HashMap to check an element occurring odd number of times and return it ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getOddOccurrence ( int arr [ ] , int size ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < size ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( auto i : hash ) { if ( i . second % 2 != 0 ) { return i . first ; } } return -1 ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getOddOccurrence ( arr , n ) ; return 0 ; }
Check for Integer Overflow |  ; Takes pointer to result and two numbers as arguments . If there is no overflow , the function places the resultant = sum a + b in result and returns 0 , otherwise it returns - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int addOvf ( int * result , int a , int b ) { * result = a + b ; if ( a > 0 && b > 0 && * result < 0 ) return -1 ; if ( a < 0 && b < 0 && * result > 0 ) return -1 ; return 0 ; } int main ( ) { int * res = new int [ ( sizeof ( int ) ) ] ; int x = 2147483640 ; int y = 10 ; cout << addOvf ( res , x , y ) ; cout << " STRNEWLINE " << * res ; return 0 ; }
Check for Integer Overflow |
#include <bits/stdc++.h> NEW_LINE using namespace std ; int addOvf ( int * result , int a , int b ) { if ( a > INT_MAX - b ) return -1 ; else { * result = a + b ; return 0 ; } } int main ( ) { int * res = new int [ ( sizeof ( int ) ) ] ; int x = 2147483640 ; int y = 10 ; cout << addOvf ( res , x , y ) << endl ; cout << * res ; return 0 ; }
Little and Big Endian Mystery |
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { unsigned int i = 1 ; char * c = ( char * ) & i ; if ( * c ) cout << " Little ▁ endian " ; else cout << " Big ▁ endian " ; return 0 ; }
Count number of bits to be flipped to convert A to B | Count number of bits to be flipped to convert A into B ; Function that count set bits ; Function that return count of flipped number ; Return count of set bits in a XOR b ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } int FlippedCount ( int a , int b ) { return countSetBits ( a ^ b ) ; } int main ( ) { int a = 10 ; int b = 20 ; cout << FlippedCount ( a , b ) << endl ; return 0 ; }
Position of rightmost set bit | C ++ program for Position of rightmost set bit ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class gfg { public : unsigned int getFirstSetBitPos ( int n ) { return log2 ( n & - n ) + 1 ; } } ; int main ( ) { gfg g ; int n = 12 ; cout << g . getFirstSetBitPos ( n ) ; return 0 ; }
Position of rightmost set bit | C ++ program to find the first rightmost set bit using XOR operator ; function to find the rightmost set bit ; Position variable initialize with 1 m variable is used to check the set bit ; left shift ; Driver Code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int PositionRightmostSetbit ( int n ) { int position = 1 ; int m = 1 ; while ( ! ( n & m ) ) { m = m << 1 ; position ++ ; } return position ; } int main ( ) { int n = 16 ; cout << PositionRightmostSetbit ( n ) ; return 0 ; }
Position of rightmost set bit | C ++ implementation of above approach ; for num == 0 there is zero set bit ; counting the position of first set bit ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define INT_SIZE  32 NEW_LINE int Right_most_setbit ( int num ) { if ( num == 0 ) { return 0 ; } else { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ! ( num & ( 1 << i ) ) ) pos ++ ; else break ; } return pos ; } } int main ( ) { int num = 0 ; int pos = Right_most_setbit ( num ) ; cout << pos << endl ; return 0 ; }
Position of rightmost set bit | C ++ program for above approach ; Program to find position of rightmost set bit ; Iterate till number > 0 ; Checking if last bit is set ; Increment position and right shift number ; set bit not found . ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int PositionRightmostSetbit ( int n ) { int p = 1 ; while ( n > 0 ) { if ( n & 1 ) { return p ; } p ++ ; n = n >> 1 ; } return -1 ; } int main ( ) { int n = 18 ; int pos = PositionRightmostSetbit ( n ) ; if ( pos != -1 ) cout << pos ; else cout << 0 ; return 0 ; }
Binary representation of a given number | C ++ Program for the binary representation of a given number ; bin function ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bin ( long n ) { long i ; cout << "0" ; for ( i = 1 << 30 ; i > 0 ; i = i / 2 ) { if ( ( n & i ) != 0 ) { cout << "1" ; } else { cout << "0" ; } } } int main ( void ) { bin ( 7 ) ; cout << endl ; bin ( 4 ) ; }
Binary representation of a given number | C ++ implementation of the approach ; Function to convert decimal to binary number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bin ( unsigned n ) { if ( n > 1 ) bin ( n >> 1 ) ; printf ( " % d " , n & 1 ) ; } int main ( void ) { bin ( 131 ) ; printf ( " STRNEWLINE " ) ; bin ( 3 ) ; return 0 ; }
Swap all odd and even bits | C ++ program to swap even and odd bits of a given number ; Function to swap even and odd bits ; Get all even bits of x ; Get all odd bits of x ; Right shift even bits ; Left shift odd bits ; Combine even and odd bits ; Driver code ; 00010111 ; Output is 43 ( 00101011 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int swapBits ( unsigned int x ) { unsigned int even_bits = x & 0xAAAAAAAA ; unsigned int odd_bits = x & 0x55555555 ; even_bits >>= 1 ; odd_bits <<= 1 ; return ( even_bits odd_bits ) ; } int main ( ) { unsigned int x = 23 ; cout << swapBits ( x ) ; return 0 ; }
Find position of the only set bit | C ++ program to find position of only set bit in a given number ; A utility function to check whether n is a power of 2 or not . goo . gl / 17 Arj See http : ; Returns position of the only set bit in ' n ' ; Iterate through bits of n till we find a set bit i & n will be non - zero only when ' i ' and ' n ' have a set bit at same position ; Unset current bit and set the next bit in ' i ' ; increment position ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPowerOfTwo ( unsigned n ) { return n && ( ! ( n & ( n - 1 ) ) ) ; } int findPosition ( unsigned n ) { if ( ! isPowerOfTwo ( n ) ) return -1 ; unsigned i = 1 , pos = 1 ; while ( ! ( i & n ) ) { i = i << 1 ; ++ pos ; } return pos ; } int main ( void ) { int n = 16 ; int pos = findPosition ( n ) ; ( pos == -1 ) ? cout << " n ▁ = ▁ " << n << " , ▁ Invalid ▁ number " << endl : cout << " n ▁ = ▁ " << n << " , ▁ Position ▁ " << pos << endl ; n = 12 ; pos = findPosition ( n ) ; ( pos == -1 ) ? cout << " n ▁ = ▁ " << n << " , ▁ Invalid ▁ number " << endl : cout << " n ▁ = ▁ " << n << " , ▁ Position ▁ " << pos << endl ; n = 128 ; pos = findPosition ( n ) ; ( pos == -1 ) ? cout << " n ▁ = ▁ " << n << " , ▁ Invalid ▁ number " << endl : cout << " n ▁ = ▁ " << n << " , ▁ Position ▁ " << pos << endl ; return 0 ; }
Find position of the only set bit | C ++ program to find position of only set bit in a given number ; A utility function to check whether n is power of 2 or not ; Returns position of the only set bit in ' n ' ; One by one move the only set bit to right till it reaches end ; increment count of shifts ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPowerOfTwo ( unsigned n ) { return n && ( ! ( n & ( n - 1 ) ) ) ; } int findPosition ( unsigned n ) { if ( ! isPowerOfTwo ( n ) ) return -1 ; unsigned count = 0 ; while ( n ) { n = n >> 1 ; ++ count ; } return count ; } int main ( void ) { int n = 0 ; int pos = findPosition ( n ) ; ( pos == -1 ) ? cout << " n ▁ = ▁ " << n << " , ▁ Invalid ▁ number STRNEWLINE " : cout << " n ▁ = ▁ " << n << " , ▁ Position ▁ " << pos << endl ; n = 12 ; pos = findPosition ( n ) ; ( pos == -1 ) ? cout << " n ▁ = ▁ " << n << " , ▁ Invalid ▁ number STRNEWLINE " : cout << " n ▁ = ▁ " << n << " , ▁ Position ▁ " << pos << endl ; n = 128 ; pos = findPosition ( n ) ; ( pos == -1 ) ? cout << " n ▁ = ▁ " << n << " , ▁ Invalid ▁ number STRNEWLINE " : cout << " n ▁ = ▁ " << n << " , ▁ Position ▁ " << pos << endl ; return 0 ; }
How to swap two numbers without using a temporary variable ? | C ++ Program to swap two numbers without using temporary variable ; Code to swap ' x ' and ' y ' x now becomes 15 ; y becomes 10 ; x becomes 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int x = 10 , y = 5 ; x = x * y ; y = x / y ; x = x / y ; cout << " After ▁ Swapping : ▁ x ▁ = " << x << " , ▁ y = " << y ; }
How to swap two numbers without using a temporary variable ? | C ++ code to swap using XOR ; Code to swap ' x ' ( 1010 ) and ' y ' ( 0101 ) x now becomes 15 ( 1111 ) ; y becomes 10 ( 1010 ) ; x becomes 5 ( 0101 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int x = 10 , y = 5 ; x = x ^ y ; y = x ^ y ; x = x ^ y ; cout << " After ▁ Swapping : ▁ x ▁ = " << x << " , ▁ y = " << y ; return 0 ; }
How to swap two numbers without using a temporary variable ? | C ++ program to implement the above approach ; Swap function ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( int * xp , int * yp ) { * xp = * xp ^ * yp ; * yp = * xp ^ * yp ; * xp = * xp ^ * yp ; } int main ( ) { int x = 10 ; swap ( & x , & x ) ; cout << " After ▁ swap ( & x , ▁ & x ) : ▁ x ▁ = ▁ " << x ; return 0 ; }
How to swap two numbers without using a temporary variable ? | C ++ program to swap two numbers . Including header file . ; Function to swap the numbers . ; same as a = a + b ; same as b = a - b ; same as a = a - b ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( int & a , int & b ) { a = ( a & b ) + ( a b ) ; b = a + ( ~ b ) + 1 ; a = a + ( ~ b ) + 1 ; cout << " After ▁ swapping : ▁ a ▁ = ▁ " << a << " , ▁ b ▁ = ▁ " << b ; } int main ( ) { int a = 5 , b = 10 ; swap ( a , b ) ; return 0 ; }
Check a given sentence for a given set of simple grammer rules | C program to validate a given sentence for a set of rules ; Method to check a given sentence for given rules ; Calculate the length of the string . ; Check that the first character lies in [ A - Z ] . Otherwise return false . ; If the last character is not a full stop ( . ) no need to check further . ; Maintain 2 states . Previous and current state based on which vertex state you are . Initialise both with 0 = start state . ; Keep the index to the next character in the string . ; Loop to go over the string . ; Set states according to the input characters in the string and the rule defined in the description . If current character is [ A - Z ] . Set current state as 0. ; If current character is a space . Set current state as 1. ; If current character is [ a - z ] . Set current state as 2. ; If current state is a dot ( . ) . Set current state as 3. ; Validates all current state with previous state for the rules in the description of the problem . ; If we have reached last state and previous state is not 1 , then check next character . If next character is ' \0' , then return true , else false ; Set previous state as current state before going over to the next character . ; Driver program
#include <stdio.h> NEW_LINE #include <string.h> NEW_LINE #include <stdbool.h> NEW_LINE bool checkSentence ( char str [ ] ) { int len = strlen ( str ) ; if ( str [ 0 ] < ' A ' str [ 0 ] > ' Z ' ) return false ; if ( str [ len - 1 ] != ' . ' ) return false ; int prev_state = 0 , curr_state = 0 ; int index = 1 ; while ( str [ index ] ) { if ( str [ index ] >= ' A ' && str [ index ] <= ' Z ' ) curr_state = 0 ; else if ( str [ index ] == ' ▁ ' ) curr_state = 1 ; else if ( str [ index ] >= ' a ' && str [ index ] <= ' z ' ) curr_state = 2 ; else if ( str [ index ] == ' . ' ) curr_state = 3 ; if ( prev_state == curr_state && curr_state != 2 ) return false ; if ( prev_state == 2 && curr_state == 0 ) return false ; if ( curr_state == 3 && prev_state != 1 ) return ( str [ index + 1 ] == ' \0' ) ; index ++ ; prev_state = curr_state ; } return false ; } int main ( ) { char * str [ ] = { " I ▁ love ▁ cinema . " , " The ▁ vertex ▁ is ▁ S . " , " I ▁ am ▁ single . " , " My ▁ name ▁ is ▁ KG . " , " I ▁ lovE ▁ cinema . " , " GeeksQuiz . ▁ is ▁ a ▁ quiz ▁ site . " , " I ▁ love ▁ Geeksquiz ▁ and ▁ Geeksforgeeks . " , " ▁ You ▁ are ▁ my ▁ friend . " , " I ▁ love ▁ cinema " } ; int str_size = sizeof ( str ) / sizeof ( str [ 0 ] ) ; int i = 0 ; for ( i = 0 ; i < str_size ; i ++ ) checkSentence ( str [ i ] ) ? printf ( " \" % s \" ▁ is ▁ correct ▁ STRNEWLINE " , str [ i ] ) : printf ( " \" % s \" ▁ is ▁ incorrect ▁ STRNEWLINE " , str [ i ] ) ; return 0 ; }
Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array | C ++ program to find Index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array ; Returns index of 0 to be replaced with 1 to get longest continuous sequence of 1 s . If there is no 0 in array , then it returns - 1. ; for maximum number of 1 around a zero ; for storing result ; index of previous zero ; index of previous to previous zero ; Traverse the input array ; If current element is 0 , then calculate the difference between curr and prev_prev_zero ; Update result if count of 1 s around prev_zero is more ; Update for next iteration ; Check for the last encountered zero ; Driver program
#include <iostream> NEW_LINE using namespace std ; int maxOnesIndex ( bool arr [ ] , int n ) { int max_count = 0 ; int max_index ; int prev_zero = -1 ; int prev_prev_zero = -1 ; for ( int curr = 0 ; curr < n ; ++ curr ) { if ( arr [ curr ] == 0 ) { if ( curr - prev_prev_zero > max_count ) { max_count = curr - prev_prev_zero ; max_index = prev_zero ; } prev_prev_zero = prev_zero ; prev_zero = curr ; } } if ( n - prev_prev_zero > max_count ) max_index = prev_zero ; return max_index ; } int main ( ) { bool arr [ ] = { 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " << maxOnesIndex ( arr , n ) ; return 0 ; }
Length of the largest subarray with contiguous elements | Set 1 |  ; Utility functions to find minimum and maximum of two elements ; Returns length of the longest contiguous subarray ; Initialize result ; Initialize min and max for all subarrays starting with i ; Consider all subarrays starting with i and ending with j ; Update min and max in this subarray if needed ; If current subarray has all contiguous elements ; Return result ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int min ( int x , int y ) { return ( x < y ) ? x : y ; } int max ( int x , int y ) { return ( x > y ) ? x : y ; } int findLength ( int arr [ ] , int n ) { int max_len = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int mn = arr [ i ] , mx = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { mn = min ( mn , arr [ j ] ) ; mx = max ( mx , arr [ j ] ) ; if ( ( mx - mn ) == j - i ) max_len = max ( max_len , mx - mn + 1 ) ; } } return max_len ; } int main ( ) { int arr [ ] = { 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Length ▁ of ▁ the ▁ longest ▁ contiguous ▁ subarray ▁ is ▁ " << findLength ( arr , n ) ; return 0 ; }
Print all increasing sequences of length k from first n natural numbers | C ++ program to print all increasing sequences of length ' k ' such that the elements in every sequence are from first ' n ' natural numbers . ; A utility function to print contents of arr [ 0. . k - 1 ] ; A recursive function to print all increasing sequences of first n natural numbers . Every sequence should be length k . The array arr [ ] is used to store current sequence . ; If length of current increasing sequence becomes k , print it ; Decide the starting number to put at current position : If length is 0 , then there are no previous elements in arr [ ] . So start putting new numbers with 1. If length is not 0 , then start from value of previous element plus 1. ; Increase length ; Put all numbers ( which are greater than the previous element ) at new position . ; This is important . The variable ' len ' is shared among all function calls in recursion tree . Its value must be brought back before next iteration of while loop ; This function prints all increasing sequences of first n natural numbers . The length of every sequence must be k . This function mainly uses printSeqUtil ( ) ; An array to store individual sequences ; Initial length of current sequence ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int k ) { for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } void printSeqUtil ( int n , int k , int & len , int arr [ ] ) { if ( len == k ) { printArr ( arr , k ) ; return ; } int i = ( len == 0 ) ? 1 : arr [ len - 1 ] + 1 ; len ++ ; while ( i <= n ) { arr [ len - 1 ] = i ; printSeqUtil ( n , k , len , arr ) ; i ++ ; } len -- ; } void printSeq ( int n , int k ) { int arr [ k ] ; int len = 0 ; printSeqUtil ( n , k , len , arr ) ; } int main ( ) { int k = 3 , n = 7 ; printSeq ( n , k ) ; return 0 ; }
Given two strings , find if first string is a subsequence of second | Recursive C ++ program to check if a string is subsequence of another string ; Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Base Cases ; If last characters of two strings are matching ; If last characters are not matching ; Driver program to test methods of graph class
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; bool isSubSequence ( char str1 [ ] , char str2 [ ] , int m , int n ) { if ( m == 0 ) return true ; if ( n == 0 ) return false ; if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ; return isSubSequence ( str1 , str2 , m , n - 1 ) ; } int main ( ) { char str1 [ ] = " gksrek " ; char str2 [ ] = " geeksforgeeks " ; int m = strlen ( str1 ) ; int n = strlen ( str2 ) ; isSubSequence ( str1 , str2 , m , n ) ? cout << " Yes ▁ " : cout << " No " ; return 0 ; }