codeparrot/xlcost-text-to-code huggingface dataset:
Find if there exists multiple ways to draw line through ( x , y ) to cut rectangle in equal halfs | C ++ implementation of the approach ; Function that returns true if multiple lines are possible passing through ( x , y ) that divide the given rectangle into two equal parts ; If the point ( x , y ) is the centre of the rectangle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPossible ( int w , int h , int x , int y ) { if ( x * 2 == w && y * 2 == h ) return true ; return false ; } int main ( ) { int w = 1 , h = 2 , x = 1 , y = 2 ; if ( isPossible ( w , h , x , y ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimize the number by changing at most K digits | C ++ implementation of the approach ; Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code
#include <iostream> NEW_LINE using namespace std ; string minNum ( string num , int k ) { int len = num . length ( ) ; if ( len == 0 k == 0 ) return num ; if ( len == 1 ) return "0" ; if ( num [ 0 ] != '1' ) { num [ 0 ] = '1' ; k -- ; } int i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != '0' ) { num [ i ] = '0' ; k -- ; } i ++ ; } return num ; } int main ( ) { string num = "91945" ; int k = 3 ; cout << minNum ( num , k ) ; return 0 ; }
Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M | C ++ program to find minimum number of steps to cover distance x ; Function to calculate the minimum number of steps required total steps taken is divisible by m and only 1 or 2 steps can be taken at a time ; If m > n ans is - 1 ; else discussed above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minsteps ( int n , int m ) { if ( m > n ) { return -1 ; } else { return ( ( n + 1 ) / 2 + m - 1 ) / m * m ; } } int main ( ) { int n = 17 , m = 4 ; int ans = minsteps ( n , m ) ; cout << ans << ' ' ; return 0 ; }
Compare numbers represented by Linked Lists | C ++ implementation of the approach ; Structure for a linked list node ; A helper function to remove zeros from the start of the linked list ; A helper function to find the length of linked list ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Allocate node ; Set the data ; Link the old list after the new node ; Set the head to point to the new node ; Function to compare the numbers represented as linked lists ; Remover leading zeroes from the linked lists ; Since the number represented by a has a greater length , it will be greater ; If the lengths of two numbers are equal we have to check their magnitudes ; If we reach here , then a and b are not NULL and their data is same , so move to next nodes in both lists ; If linked lists are identical , then we need to return zero ; Driver code ; The constructed linked lists are : a : 5 -> 6 -> 7 b : 2 -> 3 -> 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; Node * removeLeadingZeros ( struct Node * a ) { if ( a != NULL && a -> data == 0 ) return removeLeadingZeros ( a -> next ) ; else return a ; } int getSize ( struct Node * a ) { int sz = 0 ; while ( a != NULL ) { a = a -> next ; sz ++ ; } return sz ; } void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int compare ( struct Node * a , struct Node * b ) { a = removeLeadingZeros ( a ) ; b = removeLeadingZeros ( b ) ; int lenA = getSize ( a ) ; int lenB = getSize ( b ) ; if ( lenA > lenB ) { return 1 ; } else if ( lenB > lenA ) { return -1 ; } while ( a != NULL && b != NULL ) { if ( a -> data > b -> data ) return 1 ; else if ( a -> data < b -> data ) return -1 ; a = a -> next ; b = b -> next ; } return 0 ; } int main ( ) { struct Node * a = NULL ; push ( & a , 7 ) ; push ( & a , 6 ) ; push ( & a , 5 ) ; struct Node * b = NULL ; push ( & b , 3 ) ; push ( & b , 3 ) ; push ( & b , 2 ) ; cout << compare ( a , b ) ; return 0 ; }
Sum of the updated array after performing the given operation | C ++ implementation of the approach ; Utility function to return the sum of the array ; Function to return the sum of the modified array ; Find the sum of the subarray arr [ i + 1. . . n - 1 ] ; Subtract the subarray sum ; Return the sum of the modified array ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } int sumModArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int subSum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { subSum += arr [ j ] ; } arr [ i ] -= subSum ; } return sumArr ( arr , n ) ; } int main ( ) { int arr [ ] = { 40 , 25 , 12 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumModArr ( arr , n ) ; return 0 ; }
Find the minimum possible health of the winning player | C ++ implementation of the approach ; Function to return the minimum possible health of the last player ; Find the GCD of the array elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minHealth ( int health [ ] , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { gcd = __gcd ( gcd , health [ i ] ) ; } return gcd ; } int main ( ) { int health [ ] = { 5 , 6 , 1 , 2 , 3 , 4 } ; int n = sizeof ( health ) / sizeof ( int ) ; cout << minHealth ( health , n ) ; return 0 ; }
Construct an array from its pair | C ++ implementation of the approach ; Utility function to print the array ; Function to generate the original array from the pair - product array ; First element of the resulting array ; Find all the other elements ; Print the elements of the generated array ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void constructArr ( int pair [ ] , int n ) { int size = ( 1 + ( int ) sqrt ( 1 + 8 * n ) ) / 2 ; int arr [ size ] ; arr [ 0 ] = sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } int main ( ) { int pair [ ] = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = sizeof ( pair ) / sizeof ( int ) ; constructArr ( pair , n ) ; return 0 ; }
Count of odd and even sum pairs in an array | C ++ implementation of the approach ; Function to find the count of pairs with odd sum and the count of pairs with even sum ; To store the count of even and odd number from the array ; If the current element is even ; If it is odd ; To store the count of pairs with even sum ; All the even elements will make pairs with each other and the sum of the pair will be even ; All the odd elements will make pairs with each other and the sum of the pair will be even ; To store the count of pairs with odd sum ; All the even elements will make pairs with all the odd element and the sum of the pair will be odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int arr [ ] , int n ) { int cntEven = 0 , cntOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) cntEven ++ ; else cntOdd ++ ; } int evenPairs = 0 ; evenPairs += ( ( cntEven * ( cntEven - 1 ) ) / 2 ) ; evenPairs += ( ( cntOdd * ( cntOdd - 1 ) ) / 2 ) ; int oddPairs = 0 ; oddPairs += ( cntEven * cntOdd ) ; cout << " Odd ▁ pairs ▁ = ▁ " << oddPairs << endl ; cout << " Even ▁ pairs ▁ = ▁ " << evenPairs ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; findPairs ( arr , n ) ; return 0 ; }
Number of ways to distribute N Paper Set among M students | C ++ implementation of the approach ; Function to return n ! % 1000000007 ; To store the factorial ; Find the factorial ; Function to return the count of possible ways ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1000000007 ; int factMod ( int n ) { long fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact *= ( i % MOD ) ; fact %= MOD ; } return fact ; } int countWays ( int n , int m ) { return factMod ( m ) ; } int main ( ) { int n = 2 , m = 2 ; cout << countWays ( n , m ) ; return 0 ; }
Program for nth Fuss – Catalan Number | C ++ program for nth FussCatalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; 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 FussCatalan number in O ( n ) time ; Calculate value of 3 nCn ; return 3 nCn / ( 2 n + 1 ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; 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 Fuss_catalan ( unsigned int n ) { unsigned long int c = binomialCoeff ( 3 * n , n ) ; return c / ( 2 * n + 1 ) ; } int main ( ) { for ( int i = 0 ; i < 10 ; i ++ ) cout << Fuss_catalan ( i ) << " ▁ " ; return 0 ; }
Check if a number is Euler Pseudoprime | C ++ implementation of the approach ; Function that returns true if n is composite ; Check if there is any divisor of n . we only need check divisor till sqrt ( n ) because if there is divisor which is greater than sqrt ( n ) then there must be a divisor which is less than sqrt ( n ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that returns true if N is Euler Pseudoprime to the base A ; Invalid base ; N is not a composite odd number ; If A and N are not coprime ; All the conditions for Euler Pseudoprime are satisfied ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isComposite ( int n ) { for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return true ; } return false ; } int Power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } bool isEulerPseudoprime ( int N , int A ) { if ( A <= 0 ) return false ; if ( N % 2 == 0 || ! isComposite ( N ) ) return false ; if ( __gcd ( A , N ) != 1 ) return false ; int mod = Power ( A , ( N - 1 ) / 2 , N ) ; if ( mod != 1 && mod != N - 1 ) return false ; return true ; } int main ( ) { int N = 121 , A = 3 ; if ( isEulerPseudoprime ( N , A ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum difference between any two primes from the given range | C ++ implementation of the approach ; Function for Sieve of Eratosthenes ; Function to return the minimum difference between any two prime numbers from the given range [ L , R ] ; Find the first prime from the range ; Find the second prime from the range ; If the number of primes in the given range is < 2 ; To store the minimum difference between two consecutive primes from the range ; Range left to check for primes ; For every integer in the range ; If the current integer is prime ; If the difference between i and snd is minimum so far ; Driver code ; Generate primes
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e5 ; bool isPrime [ sz + 1 ] ; void sieve ( ) { memset ( isPrime , true , sizeof ( isPrime ) ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } int minDifference ( int L , int R ) { int fst = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { fst = i ; break ; } } int snd = 0 ; for ( int i = fst + 1 ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { snd = i ; break ; } } if ( snd == 0 ) return -1 ; int diff = snd - fst ; int left = snd + 1 ; int right = R ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { if ( i - snd <= diff ) { fst = snd ; snd = i ; diff = snd - fst ; } } } return diff ; } int main ( ) { sieve ( ) ; int L = 21 , R = 50 ; cout << minDifference ( L , R ) ; return 0 ; }
Number of non | C ++ implementation of the approach ; Function to return the required count ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Update ret ; Update i ; Return ret ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCnt ( int * arr , int n , int k ) { int ret = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n and arr [ j ] > = arr [ j - 1 ] ) j ++ ; int x = max ( 0 , j - i - k + 1 ) ; ret += ( x * ( x + 1 ) ) / 2 ; i = j ; } return ret ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 2 ; cout << findCnt ( arr , n , k ) ; return 0 ; }
Count of elements which are second smallest among three consecutive elements | C ++ implementation of the approach ; Function to return the count of elements P [ i ] such that P [ i ] is the second smallest among P [ i 1 ] , P [ i ] and P [ i + 1 ] ; To store the required answer ; Traverse from the second element to the second last element ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElements ( int p [ ] , int n ) { int ans = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( p [ i - 1 ] > p [ i ] and p [ i ] > p [ i + 1 ] ) ans ++ ; else if ( p [ i - 1 ] < p [ i ] and p [ i ] < p [ i + 1 ] ) ans ++ ; } return ans ; } int main ( ) { int p [ ] = { 2 , 5 , 1 , 3 , 4 } ; int n = sizeof ( p ) / sizeof ( p [ 0 ] ) ; cout << countElements ( p , n ) ; return 0 ; }
Count integers in the range [ A , B ] that are not divisible by C and D | C ++ implementation of the approach ; Function to return the count of integers from the range [ a , b ] that are not divisible by c and d ; Numbers which are divisible by c ; Numbers which are divisible by d ; Find lowest common factor of c and d ; Numbers which are divisible by both c and d ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNums ( int a , int b , int c , int d ) { int x = b / c - ( a - 1 ) / c ; int y = b / d - ( a - 1 ) / d ; int k = ( c * d ) / __gcd ( c , d ) ; int z = b / k - ( a - 1 ) / k ; return b - a + 1 - x - y + z ; } int main ( ) { int a = 10 , b = 50 , c = 4 , d = 6 ; cout << countNums ( a , b , c , d ) ; return 0 ; }
Number of non | C ++ implementation of the approach ; Function to return the count of increasing subarrays of length k ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Updating the required count ; Updating i ; Returning res ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntSubArrays ( int * arr , int n , int k ) { int res = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n and arr [ j ] > = arr [ j - 1 ] ) j ++ ; res += max ( j - i - k + 1 , 0 ) ; i = j ; } return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 2 ; cout << cntSubArrays ( arr , n , k ) ; return 0 ; }
Longest sub | C ++ implementation of the approach ; Function to return the length of the largest subsequence with minimum possible LCM ; Minimum value from the array ; To store the frequency of the minimum element in the array ; If current element is equal to the minimum element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLen ( int * arr , int n ) { int min_val = * min_element ( arr , arr + n ) ; int freq = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == min_val ) freq ++ ; } return freq ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << maxLen ( arr , n ) ; return 0 ; }
Program to find the last digit of X in base Y | C ++ Program to find the last digit of X in base Y ; Function to find the last digit of X in base Y ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void last_digit ( int X , int Y ) { cout << X % Y ; } int main ( ) { int X = 55 , Y = 3 ; last_digit ( X , Y ) ; return 0 ; }
Count of squares that can be drawn without lifting the pencil | C ++ implementation of the approach ; Function to return the count of squares that can be formed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSquares ( int n ) { return ( pow ( n , 2 ) - ( 2 * n ) + 2 ) ; } int main ( ) { int n = 2 ; cout << countSquares ( n ) ; return 0 ; }
Integer part of the geometric mean of the divisors of N | C ++ implementation of the approach ; Function to return the integer part of the geometric mean of the divisors of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int geometricMean ( int n ) { return sqrt ( n ) ; } int main ( ) { int n = 16 ; cout << geometricMean ( n ) ; return 0 ; }
Number of K 's such that the given array can be divided into two sets satisfying the given conditions | C ++ implementation of the approach ; Function to return the count of K 's such that the array can be divided into two sets containing equal number of elements when all the elements less than K are in one set and the rest of the elements are in the other set ; Sort the given array ; Return number of such Ks ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int two_sets ( int a [ ] , int n ) { sort ( a , a + n ) ; return a [ n / 2 ] - a [ ( n / 2 ) - 1 ] ; } int main ( ) { int a [ ] = { 1 , 4 , 4 , 6 , 7 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << two_sets ( a , n ) ; return 0 ; }
Count of pairs in an array such that the highest power of 2 that divides their product is 1 | C ++ implementation of the approach ; Function to return the count of valid pairs ; To store the count of odd numbers and the count of even numbers such that 2 is the only even factor of that number ; If current number is odd ; If current number is even and 2 is the only even factor of it ; Calculate total number of valid pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntPairs ( int a [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd ++ ; else if ( ( a [ i ] / 2 ) % 2 == 1 ) even ++ ; } int ans = odd * even + ( odd * ( odd - 1 ) ) / 2 ; return ans ; } int main ( ) { int a [ ] = { 4 , 2 , 7 , 11 , 14 , 15 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << cntPairs ( a , n ) ; return 0 ; }
Make the array non | C ++ implementation of the approach ; Function to make array non - decreasing ; Take the first element ; Perform the operation ; Traverse the array ; Next element ; If next element is greater than the current element then decrease it to increase the possibilities ; It is not possible to make the array non - decreasing with the given operation ; Next element is now the current ; The array can be made non - decreasing with the given operation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int a [ ] , int n ) { int cur = a [ 0 ] ; cur -- ; for ( int i = 1 ; i < n ; i ++ ) { int nxt = a [ i ] ; if ( nxt > cur ) nxt -- ; else if ( nxt < cur ) return false ; cur = nxt ; } return true ; } int main ( ) { int a [ ] = { 1 , 2 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isPossible ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the maximum element in the array other than Ai | C ++ implementation of the approach ; Function to find maximum element among ( N - 1 ) elements other than a [ i ] for each i from 1 to N ; To store prefix max element ; To store suffix max element ; Find the maximum element in the array other than a [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_element ( int a [ ] , int n ) { int pre [ n ] ; pre [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = max ( pre [ i - 1 ] , a [ i ] ) ; int suf [ n ] ; suf [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suf [ i ] = max ( suf [ i + 1 ] , a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) cout << suf [ i + 1 ] << " ▁ " ; else if ( i == n - 1 ) cout << pre [ i - 1 ] << " ▁ " ; else cout << max ( pre [ i - 1 ] , suf [ i + 1 ] ) << " ▁ " ; } } int main ( ) { int a [ ] = { 2 , 5 , 6 , 1 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; max_element ( a , n ) ; return 0 ; }
Find the Kth position element of the given sequence | C ++ implementation of the approach ; Function to return the kth number from the required sequence ; Count of odd integers in the sequence ; kth number is even ; It is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthNum ( int n , int k ) { int a = ( n + 1 ) / 2 ; if ( k > a ) return ( 2 * ( k - a ) ) ; return ( 2 * k - 1 ) ; } int main ( ) { int n = 7 , k = 7 ; cout << kthNum ( n , k ) ; return 0 ; }
Find K such that | A | C ++ implementation of the approach ; Function to find k such that | a - k | = | b - k | ; If ( a + b ) is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_k ( int a , int b ) { if ( ( a + b ) % 2 == 0 ) return ( ( a + b ) / 2 ) ; return -1 ; } int main ( ) { int a = 2 , b = 16 ; cout << find_k ( a , b ) ; return 0 ; }
Increasing permutation of first N natural numbers | C ++ implementation of the approach ; Function that returns true if it is possible to make the permutation increasing by swapping any two numbers ; To count misplaced elements ; Count all misplaced elements ; If possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int a [ ] , int n ) { int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != i + 1 ) k ++ ; } if ( k <= 2 ) return true ; return false ; } int main ( ) { int a [ ] = { 5 , 2 , 3 , 4 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isPossible ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the number of positive integers less than or equal to N that have an odd number of digits | C ++ implementation of the approach ; Function to return the number of positive integers less than or equal to N that have odd number of digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int odd_digits ( int n ) { if ( n < 10 ) return n ; else if ( n / 10 < 10 ) return 9 ; else if ( n / 100 < 10 ) return 9 + n - 99 ; else if ( n / 1000 < 10 ) return 9 + 900 ; else if ( n / 10000 < 10 ) return 909 + n - 9999 ; else return 90909 ; } int main ( ) { int n = 893 ; cout << odd_digits ( n ) ; return 0 ; }
Count of N | C ++ implementation of the approach ; Function to return the count of N - digit palindrome numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nDigitPalindromes ( int n ) { return ( 9 * pow ( 10 , ( n - 1 ) / 2 ) ) ; } int main ( ) { int n = 2 ; cout << nDigitPalindromes ( n ) ; return 0 ; }
Maximum LCM among all pairs ( i , j ) of first N natural numbers | C ++ implementation of the approach ; Function to return the maximum LCM among all the pairs ( i , j ) of first n natural numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLCM ( int n ) { return ( n * ( n - 1 ) ) ; } int main ( ) { int n = 3 ; cout << maxLCM ( n ) ; return 0 ; }
Sum of all the numbers in the Nth row of the given triangle | C ++ implementation of the approach ; Function to return the sum of the nth row elements of the given triangle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { return ( ( n - 1 ) + pow ( n , 2 ) ) ; } int main ( ) { int n = 3 ; cout << getSum ( n ) ; return 0 ; }