codeparrot/xlcost-text-to-code huggingface dataset:
Min Cost Path | DP | C ++ program for the above approach ; for 1 st column ; for 1 st row ; for rest of the 2d matrix ; returning the value in last cell ; Driver code
#include <bits/stdc++.h> NEW_LINE #define endl  " NEW_LINE " using namespace std ; const int row = 3 ; const int col = 3 ; int minCost ( int cost [ row ] [ col ] ) { for ( int i = 1 ; i < row ; i ++ ) { cost [ i ] [ 0 ] += cost [ i - 1 ] [ 0 ] ; } for ( int j = 1 ; j < col ; j ++ ) { cost [ 0 ] [ j ] += cost [ 0 ] [ j - 1 ] ; } for ( int i = 1 ; i < row ; i ++ ) { for ( int j = 1 ; j < col ; j ++ ) { cost [ i ] [ j ] += min ( cost [ i - 1 ] [ j - 1 ] , min ( cost [ i - 1 ] [ j ] , cost [ i ] [ j - 1 ] ) ) ; } } return cost [ row - 1 ] [ col - 1 ] ; } int main ( int argc , char const * argv [ ] ) { int cost [ row ] [ col ] = { { 1 , 2 , 3 } , { 4 , 8 , 2 } , { 1 , 5 , 3 } } ; cout << minCost ( cost ) << endl ; return 0 ; }
Longest Increasing Subsequence | DP | A Naive C ++ recursive implementation of LIS problem ; stores the LIS ; To make use of recursive calls , thisfunction must return two things : 1 ) Length of LIS ending with element arr [ n - 1 ] . We use max_ending_here for this purpose2 ) Overall maximum as the LIS may end with an element before arr [ n - 1 ] max_ref is used this purpose . The value of LIS of full array of size n is stored in * max_ref which is our final result ; Base case ; ' max _ ending _ here ' is length of LIS ending with arr [ n - 1 ] ; Recursively get all LIS ending with arr [ 0 ] , arr [ 1 ] ... arr [ n - 2 ] . If arr [ i - 1 ] is smaller than arr [ n - 1 ] , and max ending with arr [ n - 1 ] needs to be updated , then update it ; Compare max_ending_here with the overall max . And update the overall max if needed ; Return length of LIS ending with arr [ n - 1 ] ; The wrapper function for _lis ( ) ; The max variable holds the result ; The function _lis ( ) stores its result in max ; returns max ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; static int max_ref ; int _lis ( int arr [ ] , int n , int * max_ref ) { if ( n == 1 ) return 1 ; int res , max_ending_here = 1 ; for ( int i = 1 ; i < n ; i ++ ) { res = _lis ( arr , i , max_ref ) ; if ( arr [ i - 1 ] < arr [ n - 1 ] && res + 1 > max_ending_here ) max_ending_here = res + 1 ; } if ( * max_ref < max_ending_here ) * max_ref = max_ending_here ; return max_ending_here ; } int lis ( int arr [ ] , int n ) { int max = 1 ; _lis ( arr , n , & max ) ; return max ; } int main ( ) { int arr [ ] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Length ▁ of ▁ lis ▁ is ▁ " << lis ( arr , n ) ; return 0 ; }
Total number of possible Binary Search Trees and Binary Trees with n keys | See https : www . geeksforgeeks . org / program - nth - catalan - number / for reference of below code . ; A function to find factorial of a given number ; Calculate value of [ 1 * ( 2 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; A function to count number of BST with n nodes using catalan ; find nth catalan number ; return nth catalan number ; A function to count number of binary trees with n nodes ; find count of BST with n numbers ; return count * n ! ; Driver Program to test above functions ; find count of BST and binary trees with n nodes ; print count of BST and binary trees with n nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long int factorial ( unsigned int n ) { unsigned long int res = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { res *= i ; } return res ; } unsigned long int binomialCoeff ( unsigned int n , unsigned int k ) { unsigned long int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } unsigned long int catalan ( unsigned int n ) { unsigned long int c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } unsigned long int countBST ( unsigned int n ) { unsigned long int count = catalan ( n ) ; return count ; } unsigned long int countBT ( unsigned int n ) { unsigned long int count = catalan ( n ) ; return count * factorial ( n ) ; } int main ( ) { int count1 , count2 , n = 5 ; count1 = countBST ( n ) ; count2 = countBT ( n ) ; cout << " Count ▁ of ▁ BST ▁ with ▁ " << n << " ▁ nodes ▁ is ▁ " << count1 << endl ; cout << " Count ▁ of ▁ binary ▁ trees ▁ with ▁ " << n << " ▁ nodes ▁ is ▁ " << count2 ; return 0 ; }
Find all numbers in range [ 1 , N ] that are not present in given Array | C ++ program for above approach ; Function to find the missing numbers ; traverse the array arr [ ] ; Update ; Traverse the array arr [ ] ; If Num is not present ; Driver Code ; Given Input ; Function Call
#include <iostream> NEW_LINE using namespace std ; void getMissingNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ abs ( arr [ i ] ) - 1 ] = - ( abs ( arr [ abs ( arr [ i ] ) - 1 ] ) ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) cout << i + 1 << " ▁ " ; } } int main ( ) { int N = 5 ; int arr [ ] = { 5 , 5 , 4 , 4 , 2 } ; getMissingNumbers ( arr , N ) ; return 0 ; }
Check if elements of a Binary Matrix can be made alternating | C ++ program for the above approach ; Function to check for the grid in one of the alternating ways ; Iterate over the range ; Iterate over the range ; ( i + j ) % 2 == 1 cells should be with the character check and the rest should be with the other character ; Function to fill the grid in a possible way ; Function to find if the grid can be made alternating ; If grid contains atleast one 1 or 0 ; Fill the grid in any possible way ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool PosCheck ( char a [ ] [ 1001 ] , int n , int m , char check ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] == ' * ' ) { continue ; } else { if ( ( ( i + j ) & 1 ) && a [ i ] [ j ] != check ) { return false ; } if ( ! ( ( i + j ) & 1 ) && a [ i ] [ j ] == check ) { return false ; } } } } return true ; } void fill ( char a [ ] [ 1001 ] , int n , int m , char odd , char even ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( i + j ) & 1 ) { a [ i ] [ j ] = odd ; } else { a [ i ] [ j ] = even ; } } } } void findPossibleGrid ( int n , int m , char a [ ] [ 1001 ] ) { bool flag = true ; int k = -1 , o = -1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] != ' * ' ) { k = i ; o = j ; break ; } } if ( k != -1 ) { break ; } } if ( k != -1 ) { flag = PosCheck ( a , n , m , '1' ) ; if ( flag ) { fill ( a , n , m , '1' , '0' ) ; } else { flag = PosCheck ( a , n , m , '0' ) ; if ( flag ) { fill ( a , n , m , '0' , '1' ) ; } } } else { char h = '1' ; for ( int i = 0 ; i < m ; i ++ ) { a [ 0 ] [ i ] = h ; if ( h == '1' ) { h = '0' ; } else { h = '1' ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( i - 1 < 0 ) { continue ; } if ( a [ i - 1 ] [ j ] == '1' ) { a [ i ] [ j ] = '0' ; } else { a [ i ] [ j ] = '1' ; } } } flag = true ; } if ( ! flag ) { cout << " NO STRNEWLINE " ; } else { cout << " YES STRNEWLINE " ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { cout << a [ i ] [ j ] ; } cout << endl ; } } } int main ( ) { int n = 4 , m = 4 ; char grid [ ] [ 1001 ] = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( n , m , grid ) ; return 0 ; }
Find the Kth smallest odd length palindrome number | C ++ program for the above approach ; Function to find the Kth smallest odd length palindrome ; Store the original number K ; Removing the last digit of K ; Generate the palindrome by appending the reverse of K except last digit to itself ; Find the remainder ; Add the digit to palin ; Divide K by 10 ; Return the resultant palindromic number formed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oddLengthPalindrome ( int k ) { int palin = k ; k = k / 10 ; while ( k > 0 ) { int rev = k % 10 ; palin = ( palin * 10 ) + rev ; k = k / 10 ; } return palin ; } int main ( ) { int k = 504 ; cout << oddLengthPalindrome ( k ) ; }
Length of the longest substring consisting only of vowels in non | C ++ program for the above approach ; Function to find length of the longest substring consisting only of vowels in non - increasing order ; Stores all vowels in decreasing order ; Stores current index of array ch [ ] ; Stores the result ; Stores the count of current substring ; Declare a HashSet to store the vowels ; Traverse the string , S ; If S [ i ] is equal to ch [ j ] ; Increment count by 1 ; Add S [ i ] in the mp ; If length of mp is 5 , update res ; Else if j + 1 is less than 5 and S [ i ] is equal to ch [ j + 1 ] ; Add the S [ i ] in the mp ; Increment count by 1 ; Increment j by 1 ; If length of mp is 5 , update res ; Clear the mp ; If S [ i ] is ' u ' ; Add S [ i ] in the mp ; Update j and assign 1 to count ; Else assign 0 to j and count ; Return the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string S , int N ) { char ch [ ] = { ' u ' , ' o ' , ' i ' , ' e ' , ' a ' } ; int j = 0 ; int res = 0 ; int count = 0 ; unordered_set < char > mp ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ch [ j ] ) { ++ count ; mp . insert ( S [ i ] ) ; if ( mp . size ( ) == 5 ) { res = max ( res , count ) ; } } else if ( j + 1 < 5 && S [ i ] == ch [ j + 1 ] ) { mp . insert ( S [ i ] ) ; ++ count ; j ++ ; if ( mp . size ( ) == 5 ) { res = max ( res , count ) ; } } else { mp . clear ( ) ; if ( S [ i ] == ' u ' ) { mp . insert ( ' u ' ) ; j = 0 ; count = 1 ; } else { j = 0 ; count = 0 ; } } } return res ; } int main ( ) { string S = " ueiaoaeiouuoiea " ; int N = S . size ( ) ; cout << count ( S , N ) ; return 0 ; }
Maximize length of subsequence consisting of single distinct character possible by K increments in a string | C ++ program for the above approach ; Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of S ; Sort the given string ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSubsequenceLen ( string S , int K ) { int N = S . length ( ) ; int start = 0 , end = 0 ; sort ( S . begin ( ) , S . end ( ) ) ; int ans = INT_MIN , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] - ' a ' ) ; while ( sum + K < ( S [ end ] - ' a ' ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] - ' a ' ) ; start ++ ; } ans = max ( ans , end - start + 1 ) ; } cout << ans ; } int main ( ) { string S = " acscbcca " ; int K = 1 ; maxSubsequenceLen ( S , K ) ; return 0 ; }
Check if a given integer is the product of K consecutive integers | C ++ program for the above approach ; Function to check if N can be expressed as the product of K consecutive integers ; Stores the K - th root of N ; Stores the product of K consecutive integers ; Traverse over the range [ 1 , K ] ; Update the product ; If product is N , then return " Yes " ; Otherwise , traverse over the range [ 2 , Kthroot ] ; Update the value of product ; If product is equal to N ; Otherwise , return " No " ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkPro ( int n , int k ) { double exp = 1.0 / k ; int KthRoot = ( int ) pow ( n , exp ) ; int product = 1 ; for ( int i = 1 ; i < k + 1 ; i ++ ) { product = product * i ; } if ( product == n ) return " Yes " ; else { for ( int j = 2 ; j < KthRoot + 1 ; j ++ ) { product = product * ( j + k - 1 ) ; product = product / ( j - 1 ) ; if ( product == n ) return " Yes " ; } } return " No " ; } int main ( ) { int N = 210 ; int K = 3 ; cout << checkPro ( N , K ) ; return 0 ; }
Convert a Binary String to another by flipping prefixes minimum number of times | C ++ program for the above approach ; Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperations ( string a , string b , int n ) { vector < int > ops ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] != b [ i ] ) { if ( a [ 0 ] == b [ i ] ) { ops . push_back ( 1 ) ; a [ 0 ] = '0' + ! ( a [ 0 ] - '0' ) ; } reverse ( a . begin ( ) , a . begin ( ) + i + 1 ) ; for ( int j = 0 ; j <= i ; j ++ ) { a [ j ] = '0' + ! ( a [ j ] - '0' ) ; } ops . push_back ( i + 1 ) ; } } cout << ops . size ( ) << " STRNEWLINE " ; for ( int x : ops ) { cout << x << ' ▁ ' ; } } int main ( ) { string a = "10" , b = "01" ; int N = a . size ( ) ; minOperations ( a , b , N ) ; return 0 ; }
Count possible removals to make absolute difference between the sum of odd and even indexed elements equal to K | C ++ program for the above approach ; Function to check if difference between the sum of odd and even indexed elements after removing the first element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to check if difference between the sum of odd and even indexed elements after removing the second element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to count number of elements to be removed to make sum of differences between odd and even indexed elements equal to K ; Size of given array ; Base Conditions ; Stores prefix and suffix sums ; Base assignments ; Store prefix sums of even indexed elements ; Store prefix sums of odd indexed elements ; Similarly , store suffix sums of elements at even and odd indices ; Stores the count of possible removals ; Traverse and remove the ith element ; If the current element is excluded , then previous index ( i - 1 ) points to ( i + 2 ) and ( i - 2 ) points to ( i + 1 ) ; Find count when 0 th element is removed ; Find count when 1 st element is removed ; Count gives the required answer ; Driver Code ; Function call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int findCount0th ( vector < int > & arr , int N , int K ) { int oddsum = 0 , evensum = 0 ; for ( int i = 1 ; i < N ; i += 2 ) { oddsum += arr [ i ] ; } for ( int i = 2 ; i < N ; i += 2 ) { evensum += arr [ i ] ; } if ( abs ( oddsum - evensum ) == K ) return 1 ; else return 0 ; } int findCount1st ( vector < int > & arr , int N , int K ) { int evensum = arr [ 0 ] , oddsum = 0 ; for ( int i = 3 ; i < N ; i += 2 ) { evensum += arr [ i ] ; } for ( int i = 2 ; i < N ; i += 2 ) { oddsum += arr [ i ] ; } if ( abs ( oddsum - evensum ) == K ) return 1 ; else return 0 ; } int countTimes ( vector < int > & arr , int K ) { int N = ( int ) arr . size ( ) ; if ( N == 1 ) return 1 ; if ( N < 3 ) return 0 ; if ( N == 3 ) { int cnt = 0 ; cnt += ( abs ( arr [ 0 ] - arr [ 1 ] ) == K ? 1 : 0 ) + ( abs ( arr [ 2 ] - arr [ 1 ] ) == K ? 1 : 0 ) + ( abs ( arr [ 0 ] - arr [ 2 ] ) == K ? 1 : 0 ) ; return cnt ; } vector < int > prefix ( N + 2 , 0 ) ; vector < int > suffix ( N + 2 , 0 ) ; prefix [ 0 ] = arr [ 0 ] ; prefix [ 1 ] = arr [ 1 ] ; suffix [ N - 1 ] = arr [ N - 1 ] ; suffix [ N - 2 ] = arr [ N - 2 ] ; for ( int i = 2 ; i < N ; i += 2 ) { prefix [ i ] = arr [ i ] + prefix [ i - 2 ] ; } for ( int i = 3 ; i < N ; i += 2 ) { prefix [ i ] = arr [ i ] + prefix [ i - 2 ] ; } for ( int i = N - 3 ; i >= 0 ; i -= 2 ) { suffix [ i ] = arr [ i ] + suffix [ i + 2 ] ; } for ( int i = N - 4 ; i >= 0 ; i -= 2 ) { suffix [ i ] = arr [ i ] + suffix [ i + 2 ] ; } int count = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( abs ( prefix [ i - 1 ] + suffix [ i + 2 ] - prefix [ i - 2 ] - suffix [ i + 1 ] ) == K ) { count ++ ; } } count += findCount0th ( arr , N , K ) ; count += findCount1st ( arr , N , K ) ; return count ; } int main ( ) { vector < int > arr = { 1 , 2 , 4 , 5 , 6 } ; int K = 2 ; cout << countTimes ( arr , K ) ; return 0 ; }
Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups | C ++ program for the above approach ; Function to fill the matrix with the given conditions ; Count of parts with size sizeOfPart ; Assigning the cell with no of groups ; Update row ; Update col ; Increment count ; For new group increment start ; Function to return the reference of the matrix to be filled ; Create matrix of size N * M ; Starting index of the matrix ; Size of one group ; Element to assigned to matrix ; Fill the matrix that have rem no of parts with size size + 1 ; Fill the remaining number of parts with each part size is ' size ' ; Return the matrix ; Function to print the matrix ; Traverse the rows ; Traverse the columns ; Driver Code ; Given N , M , K ; Function Call ; Function Call to print matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fillMatrix ( int * * mat , int & row , int & col , int sizeOfpart , int noOfPart , int & start , int m , int & flag ) { for ( int i = 0 ; i < noOfPart ; ++ i ) { int count = 0 ; while ( count < sizeOfpart ) { mat [ row ] [ col ] = start ; if ( col == m - 1 && flag == 1 ) { row ++ ; col = m ; flag = 0 ; } else if ( col == 0 && flag == 0 ) { row ++ ; col = -1 ; flag = 1 ; } if ( flag == 1 ) { col ++ ; } else { col -- ; } count ++ ; } start ++ ; } } int * * findMatrix ( int N , int M , int k ) { int * * mat = ( int * * ) malloc ( N * sizeof ( int * ) ) ; for ( int i = 0 ; i < N ; ++ i ) { mat [ i ] = ( int * ) malloc ( M * sizeof ( int ) ) ; } int row = 0 , col = 0 ; int size = ( N * M ) / k ; int rem = ( N * M ) % k ; int start = 1 , flag = 1 ; fillMatrix ( mat , row , col , size + 1 , rem , start , M , flag ) ; fillMatrix ( mat , row , col , size , k - rem , start , M , flag ) ; return mat ; } void printMatrix ( int * * mat , int N , int M ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int N = 5 , M = 5 , K = 6 ; int * * mat = findMatrix ( N , M , K ) ; printMatrix ( mat , N , M ) ; return 0 ; }
Replace ' ? ' in a string such that no two adjacent characters are same | C ++ program for the above approach ; Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given string ; If the first character is ' ? ' ; Traverse the string [ 1 , N - 1 ] ; If the current character is ' ? ' ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is ' ? ' ; Change character ; Check with previous character ; Return the resultant string ; Driver Code ; Given string S ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; string changeString ( string S ) { string s = S ; int N = ( int ) s . length ( ) ; if ( s [ 0 ] == ' ? ' ) { s [ 0 ] = ' a ' ; if ( s [ 0 ] == s [ 1 ] ) { s [ 0 ] ++ ; } } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ' ? ' ) { s [ i ] = ' a ' ; if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i + 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } } } if ( s [ N - 1 ] == ' ? ' ) { s [ N - 1 ] = ' a ' ; if ( s [ N - 1 ] == s [ N - 2 ] ) { s [ N - 1 ] ++ ; } } return s ; } int main ( ) { string S = " ? a ? a " ; cout << changeString ( S ) ; return 0 ; }
Check if a given string is a Reverse Bitonic String or not | C ++ program to implement the above approach ; Function to check if the given string is reverse bitonic ; Check for decreasing sequence ; If end of string has been reached ; Check for increasing sequence ; If the end of string hasn 't been reached ; If the string is reverse bitonic ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkReverseBitonic ( string s ) { int i , j ; for ( i = 1 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] < s [ i - 1 ] ) continue ; if ( s [ i ] >= s [ i - 1 ] ) break ; } if ( i == s . size ( ) - 1 ) return 1 ; for ( j = i + 1 ; j < s . size ( ) ; j ++ ) { if ( s [ j ] > s [ j - 1 ] ) continue ; if ( s [ j ] <= s [ j - 1 ] ) break ; } i = j ; if ( i != s . size ( ) ) return 0 ; return 1 ; } int main ( ) { string s = " abcdwef " ; ( checkReverseBitonic ( s ) == 1 ) ? cout << " YES " : cout << " NO " ; return 0 ; }
Smallest number whose square has N digits | C ++ Program to find the smallest number whose square has N digits ; Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestNum ( int N ) { float x = pow ( 10.0 , ( N - 1 ) / 2.0 ) ; return ceil ( x ) ; } int main ( ) { int N = 4 ; cout << smallestNum ( N ) ; return 0 ; }
Count of K | C ++ code for the above program . ; Function to to count the number of K - countdowns for multiple queries ; flag which stores the current value of value in the countdown ; count of K - countdowns ; Loop to iterate over the elements of the array ; condition check if the elements of the array is equal to K ; condition check if the elements of the array is in continuous order ; condition check if the elements of the array are not in continuous order ; condition check to increment the counter if the there is a K - countdown present in the array ; returning the count of K - countdowns ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countKCountdown ( int arr [ ] , int N , int K ) { int flag = -1 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) flag = K ; if ( arr [ i ] == flag ) flag -- ; else flag = -1 ; if ( flag == 0 ) count ++ ; } return count ; } int main ( ) { int N = 8 ; int K = 3 ; int arr [ N ] = { 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 } ; cout << countKCountdown ( arr , N , K ) ; }