codeparrot/xlcost-text-to-code huggingface dataset:
Find the Nth digit from right in base B of the given number in Decimal base | C ++ Implementation to find Nth digit from right in base B ; Function to compute Nth digit from right in base B ; Skip N - 1 Digits in Base B ; Nth Digit from right in Base B ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int nthDigit ( int a , int n , int b ) { for ( int i = 1 ; i < n ; i ++ ) a = a / b ; return a % b ; } int main ( ) { int a = 100 ; int n = 3 ; int b = 4 ; cout << nthDigit ( a , n , b ) ; return 0 ; }
Find three prime numbers with given sum | C ++ implementation of the approach ; The vector primes holds the prime numbers ; Function to generate prime numbers ; Initialize the array elements to 0 s ; Set the non - primes to true ; Fill the vector primes with prime numbers which are marked as false in the numbers array ; Function to print three prime numbers which sum up to the number N ; Take the first prime number ; Take the second prime number ; Subtract the two prime numbers from the N to obtain the third number ; If the third number is prime ; Print the three prime numbers if the solution exists ; Driver code ; Function for generating prime numbers using Sieve of Eratosthenes ; Function to print the three prime numbers whose sum is equal to N
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100001 ; vector < int > primes ; void initialize ( ) { bool numbers [ MAX ] = { } ; int n = MAX ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( ! numbers [ i ] ) for ( int j = i * i ; j <= n ; j += i ) numbers [ j ] = true ; for ( int i = 2 ; i <= n ; i ++ ) if ( numbers [ i ] == false ) primes . push_back ( i ) ; } void findNums ( int num ) { bool ans = false ; int first = -1 , second = -1 , third = -1 ; for ( int i = 0 ; i < num ; i ++ ) { first = primes [ i ] ; for ( int j = 0 ; j < num ; j ++ ) { second = primes [ j ] ; third = num - first - second ; if ( binary_search ( primes . begin ( ) , primes . end ( ) , third ) ) { ans = true ; break ; } } if ( ans ) break ; } if ( ans ) cout << first << " ▁ " << second << " ▁ " << third << endl ; else cout << -1 << endl ; } int main ( ) { int n = 101 ; initialize ( ) ; findNums ( n ) ; return 0 ; }
Print all maximal increasing contiguous sub | C ++ Implementation to print all the Maximal Increasing Sub - array of array ; Function to print each of maximal contiguous increasing subarray ; Loop to iterate through the array and print the maximal contiguous increasing subarray . ; Condition to check whether the element at i , is greater than its next neighbouring element or not . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printmaxSubseq ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) cout << arr [ i ] << " ▁ " ; else cout << arr [ i ] << " STRNEWLINE " ; } } int main ( ) { int arr [ ] = { 9 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printmaxSubseq ( arr , n ) ; return 0 ; }
Count of non decreasing arrays of length N formed with values in range L to R | C ++ implementation of the approach ; Function to return the count of different arrays ; No such combination exists ; Arrays formed with single elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSum ( int N , int L , int R ) { if ( L > R ) { return 0 ; } if ( N == 1 ) { return R - L + 1 ; } if ( N > 1 ) { return ( N - 2 ) * ( R - L ) + 1 ; } } int main ( ) { int N = 4 , L = 4 , R = 6 ; cout << countSum ( N , L , R ) ; return 0 ; }
Minimum decrement operations to make Array elements equal by only decreasing K each time | C ++ implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE lli solve ( lli arr [ ] , lli n , lli k ) { lli i , minx = INT_MAX ; for ( i = 0 ; i < n ; i ++ ) { minx = min ( minx , arr [ i ] ) ; } lli decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return -1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } int main ( ) { lli n , k ; n = 3 ; k = 3 ; lli arr [ n ] = { 12 , 9 , 15 } ; cout << solve ( arr , n , k ) ; }
Count non decreasing subarrays of size N from N Natural numbers | C ++ program to count non decreasing subarrays of size N from N Natural numbers ; Returns value of Binomial Coefficient C ( n , k ) ; Since nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Function to find the count of required subarrays ; The required count is the binomial coefficient as explained in the approach above ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ k + 1 ] ; memset ( C , 0 , sizeof ( C ) ) ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } int count_of_subarrays ( int N ) { int count = binomialCoeff ( 2 * N - 1 , N ) ; return count ; } int main ( ) { int N = 3 ; cout << count_of_subarrays ( N ) << " STRNEWLINE " ; }
Length of Smallest subarray in range 1 to N with sum greater than a given value | C ++ implementation of the above approach . ; Function to do a binary search on a given range . ; Total sum is the sum of N numbers . ; Sum until mid ; If remaining sum is < the required value , then the required number is in the right half ; Driver code
#include <iostream> NEW_LINE using namespace std ; int usingBinarySearch ( int start , int end , int N , int S ) { if ( start >= end ) return start ; int mid = start + ( end - start ) / 2 ; int totalSum = ( N * ( N + 1 ) ) / 2 ; int midSum = ( mid * ( mid + 1 ) ) / 2 ; if ( ( totalSum - midSum ) <= S ) { return usingBinarySearch ( start , mid , N , S ) ; } return usingBinarySearch ( mid + 1 , end , N , S ) ; } int main ( ) { int N , S ; N = 5 ; S = 11 ; cout << ( N - usingBinarySearch ( 1 , N , N , S ) + 1 ) << endl ; return 0 ; }
Distribution of a Number in Array within a Range | C ++ implementation of the approach ; Function for the distribution of the number ; Distribute the number among k elements ; If there is some remaining sum to distribute ; If there are elements remaining to distribute i . e . ( n - k ) ; Divide the remaining sum into n - k elements ; Print the distribution ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void distribution ( int n , int k , int l , int r , int S , int Sk ) { int a [ n ] ; int len = k , temp , rem , s ; int diff = S - Sk ; for ( int i = 0 ; i < len ; i ++ ) { temp = Sk / k ; rem = Sk % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { cout << " - 1" ; return ; } Sk = Sk - a [ i ] ; k = k - 1 ; } if ( Sk > 0 ) { cout << " - 1" ; return ; } if ( len ) { k = n - len ; for ( int i = len ; i < n ; i ++ ) { temp = diff / k ; rem = diff % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { cout << " - 1" ; return ; } diff = diff - a [ i ] ; k = k - 1 ; } if ( diff ) { cout << " - 1" ; return ; } } for ( int i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int n = 5 , k = 3 , l = 1 , r = 5 , S = 13 , Sk = 9 ; distribution ( n , k , l , r , S , Sk ) ; return 0 ; }
Number of valid indices in the permutation of first N natural numbers | C ++ implementation of the approach ; Function to return the number of i 's such that Pi <= Pj for all 1 <= j <= i in the permutation of first N natural numbers ; To store the count of such indices ; Store the mini value ; For all the elements ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_index ( int p [ ] , int n ) { int ans = 0 ; int mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] <= mini ) mini = p [ i ] ; if ( mini == p [ i ] ) ans ++ ; } return ans ; } int main ( ) { int P [ ] = { 4 , 2 , 5 , 1 , 3 } ; int n = sizeof ( P ) / sizeof ( int ) ; cout << min_index ( P , n ) ; return 0 ; }
Check if Sum and XOR of all elements of array is equal | C ++ Implementation to Check if Sum and XOR of all Elements of Array is equal ; Function to Check if Sum and XOR of all elements of array is equal ; Sum and XOR of all elements ; Checking Sum and XOR to be equal ; Driver Function ; Check Sum and XOR is equal
#include <iostream> NEW_LINE using namespace std ; int equal_xor_sum ( int arr [ ] , int n ) { int Sum = 0 ; int Xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Sum = Sum + arr [ i ] ; Xor = Xor ^ arr [ i ] ; } if ( Sum == Xor ) cout << " YES " ; else cout << " NO " ; return 0 ; } int main ( ) { int arr [ ] = { 6 , 3 , 7 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; equal_xor_sum ( arr , n ) ; return 0 ; }
Count of odd length contiguous Palindromic sequences in a Matrix | C ++ code to Count the odd length contiguous Palindromic sequences in the matrix ; Function to count the number of contiguous palindromic sequences in the matrix ; Add the total number of elements in the matrix to the count ; Length of possible sequence to be checked for palindrome horizontally and vertically ; Iterate through each element of the matrix and count the number of palindromic sequences in each row and column ; Find the possible length of sequences that can be a palindrome ; From i , check if the sequence formed by elements to its left and right is palindrome or not ; if the sequence [ i , j - k ] to [ i , j + k ] is a palindrome , increment the count by 1 ; From i , check if the sequence formed by elements to its above and below is palindrome or not ; if the sequence [ i - k , j ] to [ i + k , j ] is a palindrome , increment the count by 1 ; Return the total count of the palindromic sequences ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE int countPalindromes ( int n , int m , int matrix [ MAX ] [ MAX ] ) { int count = n * m ; int length_of_sequence_row ; int length_of_sequence_column ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { length_of_sequence_row = min ( j , m - 1 - j ) ; length_of_sequence_column = min ( i , n - i - 1 ) ; for ( int k = 1 ; k <= length_of_sequence_row ; k ++ ) { if ( matrix [ i ] [ j - k ] == matrix [ i ] [ j + k ] ) { count ++ ; } else { break ; } } for ( int k = 1 ; k <= length_of_sequence_column ; k ++ ) { if ( matrix [ i - k ] [ j ] == matrix [ i + k ] [ j ] ) { count ++ ; } else { break ; } } } } return count ; } int main ( void ) { int m = 3 , n = 3 ; int matrix [ MAX ] [ MAX ] = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 2 , 1 , 2 } } ; cout << countPalindromes ( n , m , matrix ) << endl ; return 0 ; }
Reduce a number to 1 by performing given operations | Set 2 | C ++ implementation of the approach ; Function to return the number of set bits in n ; Function to return the minimum steps required to reach 1 ; If n is even then divide it by 2 ; If n is 3 or the number of set bits in ( n - 1 ) is less than the number of set bits in ( n + 1 ) ; Increment the number of steps ; Return the minimum number of steps ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int set_bits ( int n ) { int count = 0 ; while ( n ) { count += n % 2 ; n /= 2 ; } return count ; } int minSteps ( int n ) { int ans = 0 ; while ( n != 1 ) { if ( n % 2 == 0 ) n /= 2 ; else if ( n == 3 or set_bits ( n - 1 ) < set_bits ( n + 1 ) ) n -- ; else n ++ ; ans ++ ; } return ans ; } int main ( ) { int n = 15 ; cout << minSteps ( n ) ; return 0 ; }
Minimum integer that can be obtained by swapping adjacent digits of different parity | C ++ implementation of the above approach . ; Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumNo ( int n ) { int ans = 0 ; stack < int > stack1 ; stack < int > stack2 ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . push ( r ) ; } else { stack2 . push ( r ) ; } n = n / 10 ; } while ( ! stack1 . empty ( ) && ! stack2 . empty ( ) ) { if ( stack1 . top ( ) < stack2 . top ( ) ) { ans = ans * 10 + stack1 . top ( ) ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 . top ( ) ; stack2 . pop ( ) ; } } while ( ! stack1 . empty ( ) ) { ans = ans * 10 + stack1 . top ( ) ; stack1 . pop ( ) ; } while ( ! stack2 . empty ( ) ) { ans = ans * 10 + stack2 . top ( ) ; stack2 . pop ( ) ; } return ans ; } int main ( ) { int n1 = 64432 ; cout << minimumNo ( n1 ) << endl ; int n2 = 3137 ; cout << minimumNo ( n2 ) << endl ; return 0 ; }
Count of possible arrays from prefix | C ++ implementation of the above approach ; Function to find power of a number . ; Function to find factorial of a number . ; Function to print no of arrays ; c variable counts the no of pairs ; Map to store the frequency of each element ; Sum of all elements of the array ; Variable to check if it is possible to make any array ; First element of suffix array and the last element of prefix array ; Check if the element exists in the map ; If elements of any pair are equal and their frequency is not divisible by 2 update the isArrayPossible variable to false and break through the loop ; If elements of any pair are not equal and their frequency is not same update the isArrayPossible variable to false and break through the loop ; Check if frequency is greater than zero ; update the count of pairs ; Multiply the answer by 2 ^ ( frequency of pairs ) since the elements of the pair are not the same in this condition ; Divide the answer by the factorial of no of similar pairs ; Make frequency of both these elements 0 ; Update the count of pairs ; Divide the answer by the factorial of no . of similar pairs ; Make frequency of this element 0 ; Check if it is possible to make the array and there are n - 1 pairs whose sum will be equal to s1 ; Driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int a , int b ) { int result = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) { result = result * a ; } a = a * a ; b = b / 2 ; } return result ; } int factorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { fact = fact * i ; } return fact ; } void findNoOfArrays ( int * a , int n ) { int sum = 0 , s1 , c = 0 ; map < int , int > mp ; for ( int i = 0 ; i < 2 * n ; i ++ ) { mp [ a [ i ] ] ++ ; sum = sum + a [ i ] ; } bool isArrayPossible = true ; int ans = factorial ( n - 1 ) ; s1 = sum / ( n + 1 ) ; if ( mp [ s1 ] >= 2 ) { mp [ s1 ] = mp [ s1 ] - 2 ; } else { isArrayPossible = false ; } if ( isArrayPossible ) { for ( auto i : mp ) { if ( i . first == s1 - i . first ) { if ( mp [ i . first ] % 2 != 0 ) { isArrayPossible = false ; break ; } } if ( i . first != s1 - i . first ) { if ( mp [ i . first ] != mp [ s1 - i . first ] ) { isArrayPossible = false ; break ; } } if ( i . second > 0 ) { if ( i . first != s1 - i . first ) { c = c + i . second ; ans = ans * power ( 2 , i . second ) ; ans = ans / factorial ( i . second ) ; mp [ i . first ] = 0 ; mp [ s1 - i . first ] = 0 ; } if ( i . first == s1 - i . first ) { c = c + i . second / 2 ; ans = ans / factorial ( i . second / 2 ) ; mp [ i . first ] = 0 ; } } } } if ( c < n - 1 isArrayPossible == false ) { cout << "0" << endl ; } else { cout << ans << endl ; } } int main ( ) { int arr1 [ ] = { 5 , 2 , 3 , 5 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; findNoOfArrays ( arr1 , n1 / 2 ) ; int arr2 [ ] = { -1 , -1 , -1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; findNoOfArrays ( arr2 , n2 / 2 ) ; return 0 ; }
Find two numbers with the given LCM and minimum possible difference | C ++ implementation of the approach ; Function to return the LCM of a and b ; Function to find and print the two numbers ; To find the factors ; To check if i is a factor of x and the minimum possible number satisfying the given conditions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lcm ( int a , int b ) { return ( a / __gcd ( a , b ) * b ) ; } void findNums ( int x ) { int ans ; for ( int i = 1 ; i <= sqrt ( x ) ; i ++ ) { if ( x % i == 0 && lcm ( i , x / i ) == x ) { ans = i ; } } cout << ans << " ▁ " << ( x / ans ) ; } int main ( ) { int x = 12 ; findNums ( x ) ; return 0 ; }
Find an integer that is common in the maximum number of given arithmetic progressions | C ++ implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  1000000 NEW_LINE int maxCommonElement ( int A [ ] , int D [ ] , int N ) { int cnt [ MAXN ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } int com = max_element ( cnt , cnt + MAXN ) - cnt ; return com ; } int main ( ) { int A [ ] = { 13 , 1 , 2 , 5 } , D [ ] = { 5 , 10 , 1 , 12 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << maxCommonElement ( A , D , N ) ; return 0 ; }
Number of trailing zeros in N * ( N | C ++ implementation of the approach ; Function to return the count of trailing 0 s in the given function ; If n is odd ; If n is even ; Find the trailing zeros in n / 2 factorial ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findTrailingZeros ( int n ) { if ( n & 1 ) return 0 ; else { int ans = 0 ; n /= 2 ; while ( n ) { ans += n / 5 ; n /= 5 ; } return ans ; } } int main ( ) { int n = 12 ; cout << findTrailingZeros ( n ) ; return 0 ; }
Divide N into K unique parts such that gcd of those parts is maximum | C ++ implementation of the approach ; Function to calculate maximum GCD ; Minimum possible sum for K unique positive integers ; It is not possible to divide N into K unique parts ; All the factors greater than sqrt ( N ) are complementary of the factors less than sqrt ( N ) ; If i is a factor of N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGCD ( int N , int K ) { int minSum = ( K * ( K + 1 ) ) / 2 ; if ( N < minSum ) return -1 ; int i = sqrt ( N ) ; int res = 1 ; while ( i >= 1 ) { if ( N % i == 0 ) { if ( i >= minSum ) res = max ( res , N / i ) ; if ( N / i >= minSum ) res = max ( res , i ) ; } i -- ; } return res ; } int main ( ) { int N = 18 , K = 3 ; cout << maxGCD ( N , K ) ; return 0 ; }
Radius of the inscribed circle within three tangent circles | C ++ implementation of the approach ; Radius of the 3 given circles declared as double . ; Calculation of area of a triangle by Heron 's formula ; Applying binary search to find the radius r4 of the required circle ; Area of main triangle ; Loop runs until l and h becomes approximately equal ; Area of smaller triangles ; If sum of smaller triangles is less than main triangle ; If sum of smaller triangles is greater than or equal to main triangle ; Driver code ; Taking r1 , r2 , r3 as input ; Call to function binary search
#include <bits/stdc++.h> NEW_LINE using namespace std ; double r1 , r2 , r3 ; double area ( double a , double b , double c ) { double p = ( a + b + c ) / 2 ; return sqrt ( p ) * sqrt ( p - a ) * sqrt ( p - b ) * sqrt ( p - c ) ; } double binary_search ( ) { double s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) ; double l = 0 , h = s / ( r1 + r2 + r3 ) ; while ( h - l >= 1.e-7 ) { double mid = ( l + h ) / 2 ; double s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) ; double s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) ; double s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) ; if ( s1 + s2 + s3 < s ) { l = mid ; } else { h = mid ; } } return ( l + h ) / 2 ; } int main ( ) { r1 = 1.0 ; r2 = 2.0 ; r3 = 3.0 ; cout << fixed << setprecision ( 6 ) << binary_search ( ) << endl ; return 0 ; }
Radius of the inscribed circle within three tangent circles | C ++ implementation of the approach ; Driver code ; Radius of the 3 given circles declared as double . ; Taking r1 , r2 , r3 as input ; Calculation of r4 using formula given above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { double r1 , r2 , r3 ; r1 = 1 ; r2 = 2 ; r3 = 3 ; double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; cout << fixed << setprecision ( 6 ) << r4 << ' ' ; return 0 ; }
Make all elements zero by decreasing any two elements by one at a time | C ++ implementation of the approach ; Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkZeroArray ( int * arr , int n ) { int sum = 0 , maximum = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; if ( checkZeroArray ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Subarray permutation that satisfies the given condition | C ++ implementation of the approach ; Function that returns true if the required subarray exists in the given array ; Map to store the positions of each integer in the original permutation ; To store the address of each entry in arr [ n ] but with 1 - based indexing ; To track minimum position sumcur for sum of all positions till this position ; Summing up addresses ; Tracking minimum address encountered till now ; The sum of the addresses if it forms the required subarray ; If current sum of address is equal to val ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; bool subArray ( ll * arr , ll n , ll m ) { ll i ; unordered_map < ll , ll > mp ; for ( i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] = i + 1 ; } ll sumcur = 0 ; ll p = INT_MAX ; vector < ll > ans ; for ( i = 1 ; i <= m ; i ++ ) { sumcur += mp [ i ] ; p = min ( p , mp [ i ] ) ; ll val = p * i - i + ( i * ( i + 1 ) ) / 2 ; if ( i == m ) { if ( val == sumcur ) { return true ; } else return false ; } } } int main ( ) { ll arr [ ] = { 4 , 5 , 1 , 3 , 2 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; ll m = 3 ; if ( subArray ( arr , n , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sum of all N digit palindrome numbers | C ++ program for the above approach ; Function to check palindrome ; Function to calculate the sum of n - digit palindrome ; Run a loop to check all possible palindrome ; If palindrome append sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string & s ) { int left = 0 , right = s . size ( ) - 1 ; while ( left <= right ) { if ( s [ left ] != s [ right ] ) { return false ; } left ++ ; right -- ; } return true ; } long long getSum ( int n ) { int start = pow ( 10 , n - 1 ) ; int end = pow ( 10 , n ) - 1 ; long long sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { string s = to_string ( i ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } int main ( ) { int n = 1 ; long long ans = getSum ( n ) ; cout << ans << ' ' ; return 0 ; }
Sum of all N digit palindrome numbers | C ++ program for the above approach ; Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long double getSum ( int n ) { long double sum = 0 ; if ( n == 1 ) { sum = 45.0 ; } else { sum = ( 99.0 / 2.0 ) * pow ( 10 , n - 1 ) * pow ( 10 , ( n - 1 ) / 2 ) ; } return sum ; } int main ( ) { int n = 3 ; long double ans = getSum ( n ) ; cout << setprecision ( 12 ) << ans << ' ' ; return 0 ; }