codeparrot/xlcost-text-to-code huggingface dataset:
An in | C ++ implementation of above approach ; A utility function to reverse string str [ low . . high ] ; Cycle leader algorithm to move all even positioned elements at the end . ; odd index ; even index ; keep the back - up of element at new position ; The main function to transform a string . This function mainly uses cycleLeader ( ) to transform ; Step 1 : Find the largest prefix subarray of the form 3 ^ k + 1 ; Step 2 : Apply cycle leader algorithm for the largest subarrau ; Step 4.1 : Reverse the second half of first subarray ; Step 4.2 : Reverse the first half of second sub - string . ; Step 4.3 Reverse the second half of first sub - string and first half of second sub - string together ; Increase the length of first subarray ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverse ( char * str , int low , int high ) { while ( low < high ) { swap ( & str [ low ] , & str [ high ] ) ; ++ low ; -- high ; } } void cycleLeader ( char * str , int shift , int len ) { int j ; char item ; for ( int i = 1 ; i < len ; i *= 3 ) { j = i ; item = str [ j + shift ] ; do { if ( j & 1 ) j = len / 2 + j / 2 ; else j /= 2 ; swap ( & str [ j + shift ] , & item ) ; } while ( j != i ) ; } } void moveNumberToSecondHalf ( char * str ) { int k , lenFirst ; int lenRemaining = strlen ( str ) ; int shift = 0 ; while ( lenRemaining ) { k = 0 ; while ( pow ( 3 , k ) + 1 <= lenRemaining ) k ++ ; lenFirst = pow ( 3 , k - 1 ) + 1 ; lenRemaining -= lenFirst ; cycleLeader ( str , shift , lenFirst ) ; reverse ( str , shift / 2 , shift - 1 ) ; reverse ( str , shift , shift + lenFirst / 2 - 1 ) ; reverse ( str , shift / 2 , shift + lenFirst / 2 - 1 ) ; shift += lenFirst ; } } int main ( ) { char str [ ] = " a1b2c3d4e5f6g7" ; moveNumberToSecondHalf ( str ) ; cout << str ; return 0 ; }
Program to find the maximum difference between the index of any two different numbers | C ++ implementation of the approach ; Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaximumDiff ( int a [ ] , int n ) { int ind1 = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( a [ 0 ] != a [ i ] ) { ind1 = i ; break ; } } int ind2 = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ n - 1 ] != a [ i ] ) { ind2 = ( n - 1 - i ) ; break ; } } return max ( ind1 , ind2 ) ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findMaximumDiff ( a , n ) ; return 0 ; }
Find the Kth node in the DFS traversal of a given subtree in a Tree | C ++ program to find the Kth node in the DFS traversal of the subtree of given vertex V in a Tree ; To store nodes ; To store the current index of vertex in DFS ; To store the starting index and ending index of vertex in the DFS traversal array ; To store the DFS of vertex 1 ; Function to add edge between two nodes ; Initialize the vectors ; Function to perform DFS of a vertex 1. stores the DFS of the vertex 1 in vector p , 2. store the start index of DFS of every vertex 3. store the end index of DFS of every vertex ; store staring index of node ch ; store ending index ; Function to find the Kth node in DFS of vertex V ; check if kth number exits or not ; Driver code ; number of nodes ; add edges ; store DFS of 1 st node
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int n ; vector < int > tree [ N ] ; int currentIdx ; vector < int > startIdx , endIdx ; vector < int > p ; void Add_edge ( int u , int v ) { tree [ u ] . push_back ( v ) ; tree [ v ] . push_back ( u ) ; } void intisalise ( ) { startIdx . resize ( n ) ; endIdx . resize ( n ) ; p . resize ( n ) ; } void Dfs ( int ch , int par ) { p [ currentIdx ] = ch ; startIdx [ ch ] = currentIdx ++ ; for ( auto c : tree [ ch ] ) { if ( c != par ) Dfs ( c , ch ) ; } endIdx [ ch ] = currentIdx - 1 ; } int findNode ( int v , int k ) { k += startIdx [ v ] - 1 ; if ( k <= endIdx [ v ] ) return p [ k ] ; return -1 ; } int main ( ) { n = 9 ; Add_edge ( 1 , 2 ) ; Add_edge ( 1 , 3 ) ; Add_edge ( 1 , 4 ) ; Add_edge ( 3 , 5 ) ; Add_edge ( 3 , 7 ) ; Add_edge ( 5 , 6 ) ; Add_edge ( 5 , 8 ) ; Add_edge ( 7 , 9 ) ; intisalise ( ) ; Dfs ( 1 , 0 ) ; int v = 3 , k = 4 ; cout << findNode ( v , k ) ; return 0 ; }
Sum of the series Kn + ( K ( n | Function to return sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int k , int n ) { int sum = pow ( k , n + 1 ) - pow ( k - 1 , n + 1 ) ; return sum ; } int main ( ) { int n = 3 ; int K = 3 ; cout << sum ( K , n ) ; }
Check whether factorial of N is divisible by sum of first N natural numbers | Function to check whether a number is prime or not . ; Count variable to store the number of factors of ' num ' ; Counting the number of factors ; If number is prime return true ; Function to check for divisibility ; if ' n ' equals 1 then divisibility is possible ; Else check whether ' n + 1' is prime or not ; If ' n + 1' is prime then ' n ! ' is not divisible by ' n * ( n + 1 ) / 2' ; else divisibility occurs ; Driver Code ; Test for n = 3 ; Test for n = 4
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_prime ( int num ) { int count = 0 ; for ( int i = 1 ; i * i <= ( num ) ; i ++ ) { if ( ( num ) % i == 0 ) { if ( i * i != ( num ) ) count += 2 ; else count ++ ; } } if ( count == 2 ) return true ; else return false ; } string is_divisible ( int n ) { if ( n == 1 ) { return " YES " ; } else { if ( is_prime ( n + 1 ) ) return " NO " ; else return " YES " ; } } int main ( ) { int n ; n = 3 ; cout << is_divisible ( n ) << endl ; n = 4 ; cout << is_divisible ( n ) << endl ; return 0 ; }
Number of subarrays having sum of the form k ^ m , m >= 0 | C ++ implementation of the above approach ; Function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE #define MAX  100005 NEW_LINE using namespace std ; ll countSubarrays ( int * arr , int n , int k ) { ll prefix_sum [ MAX ] ; prefix_sum [ 0 ] = 0 ; partial_sum ( arr , arr + n , prefix_sum + 1 ) ; ll sum ; if ( k == 1 ) { sum = 0 ; map < ll , int > m ; for ( int i = n ; i >= 0 ; i -- ) { if ( m . find ( prefix_sum [ i ] + 1 ) != m . end ( ) ) sum += m [ prefix_sum [ i ] + 1 ] ; m [ prefix_sum [ i ] ] ++ ; } return sum ; } if ( k == -1 ) { sum = 0 ; map < ll , int > m ; for ( int i = n ; i >= 0 ; i -- ) { if ( m . find ( prefix_sum [ i ] + 1 ) != m . end ( ) ) sum += m [ prefix_sum [ i ] + 1 ] ; if ( m . find ( prefix_sum [ i ] - 1 ) != m . end ( ) ) sum += m [ prefix_sum [ i ] - 1 ] ; m [ prefix_sum [ i ] ] ++ ; } return sum ; } sum = 0 ; ll b ; map < ll , int > m ; for ( int i = n ; i >= 0 ; i -- ) { b = 1 ; while ( true ) { if ( b > 100000000000000 ) break ; if ( m . find ( prefix_sum [ i ] + b ) != m . end ( ) ) sum += m [ prefix_sum [ i ] + b ] ; b *= k ; } m [ prefix_sum [ i ] ] ++ ; } return sum ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << countSubarrays ( arr , n , k ) ; return 0 ; }
Given two binary strings perform operation until B > 0 and print the result | C ++ implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define mod  (int)(1e9 + 7) NEW_LINE ll BitOperations ( string a , int n , string b , int m ) { reverse ( a . begin ( ) , a . end ( ) ) ; reverse ( b . begin ( ) , b . end ( ) ) ; int c = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] == '1' ) c ++ ; ll power [ n ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) power [ i ] = ( power [ i - 1 ] * 2 ) % mod ; ll ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == '1' ) { ans += c * power [ i ] ; if ( ans >= mod ) ans %= mod ; } if ( b [ i ] == '1' ) c -- ; if ( c == 0 ) break ; } return ans ; } int main ( ) { string a = "1001" , b = "10101" ; int n = a . length ( ) , m = b . length ( ) ; cout << BitOperations ( a , n , b , m ) ; return 0 ; }
Sum of sides of largest and smallest child polygons possible from a given polygon | Function to find the sum of largest and smallest secondary polygons if possible ; Count edges of primary polygon ; Calculate edges present in the largest secondary polygon ; Driver Code ; Given Exterior Angle
#include <bits/stdc++.h> NEW_LINE using namespace std ; void secondary_polygon ( int Angle ) { int edges_primary = 360 / Angle ; if ( edges_primary >= 6 ) { int edges_max_secondary = edges_primary / 2 ; cout << edges_max_secondary + 3 ; } else cout << " Not ▁ Possible " ; } int main ( ) { int Angle = 45 ; secondary_polygon ( Angle ) ; return 0 ; }
Print prime numbers with prime sum of digits in an array | C ++ implementation of the above approach ; Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieve ( int maxEle , int prime [ ] ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( int i = 2 ; i * i <= maxEle ; i ++ ) { if ( ! prime [ i ] ) { for ( int j = 2 * i ; j <= maxEle ; j += i ) prime [ j ] = 1 ; } } } int digitSum ( int n ) { int sum = 0 ; while ( n ) { sum += n % 10 ; n = n / 10 ; } return sum ; } void printAdditivePrime ( int arr [ ] , int n ) { int maxEle = * max_element ( arr , arr + n ) ; int prime [ maxEle + 1 ] ; memset ( prime , 0 , sizeof ( prime ) ) ; sieve ( maxEle , prime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 0 ) { int sum = digitSum ( arr [ i ] ) ; if ( prime [ sum ] == 0 ) cout << arr [ i ] << " ▁ " ; } } } int main ( ) { int a [ ] = { 2 , 4 , 6 , 11 , 12 , 18 , 7 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printAdditivePrime ( a , n ) ; return 0 ; }
Program to find nth term of the series 1 4 15 24 45 60 92 | C ++ implementation of the above approach ; function to calculate nth term of the series ; variable nth will store the nth term of series ; if n is even ; if n is odd ; return nth term ; Driver code
#include <stdio.h> NEW_LINE long long int nthTerm ( long long int n ) { long long int nth ; if ( n % 2 == 0 ) nth = 2 * ( ( n * n ) - n ) ; else nth = ( 2 * n * n ) - n ; return nth ; } int main ( ) { long long int n ; n = 5 ; printf ( " % lld STRNEWLINE " , nthTerm ( n ) ) ; n = 25 ; printf ( " % lld STRNEWLINE " , nthTerm ( n ) ) ; n = 25000000 ; printf ( " % lld STRNEWLINE " , nthTerm ( n ) ) ; n = 250000007 ; printf ( " % lld STRNEWLINE " , nthTerm ( n ) ) ; return 0 ; }
Find the Nth term of the series 9 , 45 , 243 , 1377 | C ++ implementation of the approach ; Function to return the nth term of the given series ; nth term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long nthterm ( int n ) { int An = ( pow ( 1 , n ) + pow ( 2 , n ) ) * pow ( 3 , n ) ; return An ; } int main ( ) { int n = 3 ; cout << nthterm ( n ) ; return 0 ; }
Count the numbers < N which have equal number of divisors as K | C ++ implementation of the approach ; Function to return the count of the divisors of a number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n ; This condition is to handle the case when n is a prime number > 2 ; Count the total elements that have divisors exactly equal to as that of k 's ; Exclude k from the result if it is smaller than n . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDivisors ( int n ) { int x = 0 , ans = 1 ; while ( n % 2 == 0 ) { x ++ ; n = n / 2 ; } ans = ans * ( x + 1 ) ; for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { x = 0 ; while ( n % i == 0 ) { x ++ ; n = n / i ; } ans = ans * ( x + 1 ) ; } if ( n > 2 ) ans = ans * 2 ; return ans ; } int getTotalCount ( int n , int k ) { int k_count = countDivisors ( k ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( k_count == countDivisors ( i ) ) count ++ ; if ( k < n ) count = count - 1 ; return count ; } int main ( ) { int n = 500 , k = 6 ; cout << getTotalCount ( n , k ) ; return 0 ; }
Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | C ++ implementation of the approach ; Function to return the nth term of the given series ; Common difference ; First term ; nth term ; nth term of the given series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long term ( int n ) { int d = 2 ; int a1 = 0 ; int An = a1 + ( n - 1 ) * d ; An = pow ( An , 3 ) ; return An ; } int main ( ) { int n = 5 ; cout << term ( n ) ; return 0 ; }
Count the number of intervals in which a given value lies | C ++ program to count the number of intervals in which a given value lies ; Function to count the number of intervals in which a given value lies ; Variables to store overall minimum and maximum of the intervals ; Variables to store start and end of an interval ; Frequency array to keep track of how many of the given intervals an element lies in ; Constructing the frequency array ; Driver code ; length of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_VAL = 200000 ; int countIntervals ( int arr [ ] [ 2 ] , int V , int N ) { int min = INT_MAX ; int max = INT_MIN ; int li , ri ; int freq [ MAX_VAL ] ; for ( int i = 0 ; i < N ; i ++ ) { li = arr [ i ] [ 0 ] ; freq [ li ] = freq [ li ] + 1 ; ri = arr [ i ] [ 1 ] ; freq [ ri + 1 ] = freq [ ri + 1 ] - 1 ; if ( li < min ) min = li ; if ( ri > max ) max = ri ; } for ( int i = min ; i <= max ; i ++ ) freq [ i ] = freq [ i ] + freq [ i - 1 ] ; return freq [ V ] ; } int main ( ) { int arr [ 5 ] [ 2 ] = { { 1 , 10 } , { 5 , 10 } , { 15 , 25 } , { 7 , 12 } , { 20 , 25 } } ; int V = 7 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( countIntervals ( arr , V , N ) ) ; }
Check whether the triangle is valid or not if angles are given | C ++ implementation of the approach ; Function to check if sum of the three angles is 180 or not ; Check condition ; Driver code ; function calling and print output
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Valid ( int a , int b , int c ) { if ( a + b + c == 180 && a != 0 && b != 0 && c != 0 ) return true ; else return false ; } int main ( ) { int a = 60 , b = 40 , c = 80 ; if ( Valid ( a , b , c ) ) cout << " Valid " ; else cout << " Invalid " ; }
Split N ^ 2 numbers into N groups of equal sum | C ++ implementation of the above approach ; Function to print N groups of equal sum ; No . of Groups ; n / 2 pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printGroups ( int n ) { int x = 1 ; int y = n * n ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n / 2 ; j ++ ) { cout << " { ▁ " << x << " , ▁ " << y << " } ▁ " ; x ++ ; y -- ; } cout << endl ; } } int main ( ) { int n = 4 ; printGroups ( n ) ; return 0 ; }
Program to find the Break Even Point | C ++ program to find the break - even point . ; Function to calculate Break Even Point ; Calculating number of articles to be sold ; Main Function
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int breakEvenPoint ( int exp , int S , int M ) { float earn = S - M ; int res = ceil ( exp / earn ) ; return res ; } int main ( ) { int exp = 3550 , S = 90 , M = 65 ; cout << breakEvenPoint ( exp , S , M ) ; return 0 ; }
Minimize the value of N by applying the given operations | C ++ implementation of the above approach ; function to return the product of distinct prime factors of a number ; find distinct prime ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll minimum ( ll n ) { ll product = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { while ( n % i == 0 ) n = n / i ; product = product * i ; } } if ( n >= 2 ) product = product * n ; return product ; } int main ( ) { ll n = 20 ; cout << minimum ( n ) << endl ; return 0 ; }
N digit numbers divisible by 5 formed from the M digits | CPP program to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numbers ( int n , int arr [ ] , int m ) { int isZero = 0 , isFive = 0 ; int result = 0 ; if ( m < n ) { return -1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( arr [ i ] == 0 ) isZero = 1 ; if ( arr [ i ] == 5 ) isFive = 1 ; } if ( isZero && isFive ) { result = 2 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else if ( isZero isFive ) { result = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else result = -1 ; return result ; } int main ( ) { int n = 3 , m = 6 ; int arr [ ] = { 2 , 3 , 5 , 6 , 7 , 9 } ; cout << numbers ( n , arr , m ) ; return 0 ; }
Program to find the smallest element among three elements | C ++ implementation to find the smallest of three elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int a = 5 , b = 7 , c = 10 ; if ( a <= b && a <= c ) cout << a << " ▁ is ▁ the ▁ smallest " ; else if ( b <= a && b <= c ) cout << b << " ▁ is ▁ the ▁ smallest " ; else cout << c << " ▁ is ▁ the ▁ smallest " ; return 0 ; }
Find subsequences with maximum Bitwise AND and Bitwise OR | C ++ implementation of above approach ; function to find the maximum sum ; Maximum And is maximum element ; Maximum OR is bitwise OR of all ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSum ( int a [ ] , int n ) { int maxAnd = 0 ; for ( int i = 0 ; i < n ; i ++ ) maxAnd = max ( maxAnd , a [ i ] ) ; int maxOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxOR = maxOR | a [ i ] ; } cout << maxAnd + maxOR ; } int main ( ) { int a [ ] = { 3 , 5 , 6 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; maxSum ( a , n ) ; }
Count Magic squares in a grid | CPP program to count magic squares ; function to check is subgrid is Magic Square ; Elements of grid must contain all numbers from 1 to 9 , sum of all rows , columns and diagonals must be same , i . e . , 15. ; Function to count total Magic square subgrids ; if condition true skip check ; check for magic square subgrid ; return total magic square ; Driver program ; function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int R = 3 ; const int C = 4 ; int magic ( int a , int b , int c , int d , int e , int f , int g , int h , int i ) { set < int > s1 = { a , b , c , d , e , f , g , h , i } , s2 = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; if ( s1 == s2 && ( a + b + c ) == 15 && ( d + e + f ) == 15 && ( g + h + i ) == 15 && ( a + d + g ) == 15 && ( b + e + h ) == 15 && ( c + f + i ) == 15 && ( a + e + i ) == 15 && ( c + e + g ) == 15 ) return true ; return false ; } int CountMagicSquare ( int Grid [ R ] [ C ] ) { int ans = 0 ; for ( int i = 0 ; i < R - 2 ; i ++ ) for ( int j = 0 ; j < C - 2 ; j ++ ) { if ( Grid [ i + 1 ] [ j + 1 ] != 5 ) continue ; if ( magic ( Grid [ i ] [ j ] , Grid [ i ] [ j + 1 ] , Grid [ i ] [ j + 2 ] , Grid [ i + 1 ] [ j ] , Grid [ i + 1 ] [ j + 1 ] , Grid [ i + 1 ] [ j + 2 ] , Grid [ i + 2 ] [ j ] , Grid [ i + 2 ] [ j + 1 ] , Grid [ i + 2 ] [ j + 2 ] ) ) ans += 1 ; } return ans ; } int main ( ) { int G [ R ] [ C ] = { { 4 , 3 , 8 , 4 } , { 9 , 5 , 1 , 9 } , { 2 , 7 , 6 , 2 } } ; cout << CountMagicSquare ( G ) ; return 0 ; }
Sum of minimum elements of all subarrays | CPP implementation of above approach ; Function to return required minimum sum ; getting number of element strictly larger than A [ i ] on Left . ; get elements from stack until element greater than A [ i ] found ; getting number of element larger than A [ i ] on Right . ; get elements from stack until element greater or equal to A [ i ] found ; calculating required resultult ; Driver program ; function call to get required resultult
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumSubarrayMins ( int A [ ] , int n ) { int left [ n ] , right [ n ] ; stack < pair < int , int > > s1 , s2 ; for ( int i = 0 ; i < n ; ++ i ) { int cnt = 1 ; while ( ! s1 . empty ( ) && ( s1 . top ( ) . first ) > A [ i ] ) { cnt += s1 . top ( ) . second ; s1 . pop ( ) ; } s1 . push ( { A [ i ] , cnt } ) ; left [ i ] = cnt ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { int cnt = 1 ; while ( ! s2 . empty ( ) && ( s2 . top ( ) . first ) >= A [ i ] ) { cnt += s2 . top ( ) . second ; s2 . pop ( ) ; } s2 . push ( { A [ i ] , cnt } ) ; right [ i ] = cnt ; } int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) result = ( result + A [ i ] * left [ i ] * right [ i ] ) ; return result ; } int main ( ) { int A [ ] = { 3 , 1 , 2 , 4 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << sumSubarrayMins ( A , n ) ; return 0 ; }
Minimum and Maximum element of an array which is divisible by a given number k | C ++ implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMin ( int arr [ ] , int n , int k ) { int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) res = min ( res , arr [ i ] ) ; } return res ; } int getMax ( int arr [ ] , int n , int k ) { int res = INT_MIN ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) res = max ( res , arr [ i ] ) ; } return res ; } int main ( ) { int arr [ ] = { 10 , 1230 , 45 , 67 , 1 } ; int k = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " << getMin ( arr , n , k ) << " STRNEWLINE " ; cout << " Maximum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " << getMax ( arr , n , k ) ; return 0 ; }
Print a number containing K digits with digital root D | C ++ implementation of the above approach ; Function to find a number ; If d is 0 k has to be 1 ; Print k - 1 zeroes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNumberWithDR ( int k , int d ) { if ( d == 0 && k != 1 ) cout << " - 1" ; else { cout << d ; k -- ; while ( k -- ) cout << "0" ; } } int main ( ) { int k = 4 , d = 4 ; printNumberWithDR ( k , d ) ; return 0 ; }