codeparrot/xlcost-text-to-code huggingface dataset:
Find the sum of power of bit count raised to the power B | C ++ program for the above approach ; Function to calculate a ^ b mod m using fast - exponentiation method ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fastmod ( int base , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( base , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; } int findPowerSum ( int n , int ar [ ] ) { const int mod = 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int base = __builtin_popcount ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( base , exp , mod ) ; sum %= mod ; } return sum ; } int main ( ) { int n = 3 ; int ar [ ] = { 1 , 2 , 3 } ; cout << findPowerSum ( n , ar ) ; return 0 ; }
Count the total number of triangles after Nth operation |  ; function to return the total no . of Triangles ; For every subtriangle formed there are possibilities of generating ( curr * 3 ) + 2 ; Changing the curr value to Tri_count ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountTriangles ( int n ) { int curr = 1 ; int Tri_count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { Tri_count = ( curr * 3 ) + 2 ; curr = Tri_count ; } return Tri_count ; } int main ( ) { int n = 10 ; cout << CountTriangles ( n ) ; return 0 ; }
In how many ways the ball will come back to the first boy after N turns | Function to return the number of sequences that get back to Bob ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numSeq ( int n ) { return ( pow ( 3 , n ) + 3 * pow ( -1 , n ) ) / 4 ; } int main ( ) { int N = 10 ; printf ( " % d " , numSeq ( N ) ) ; return 0 ; }
Count subarrays such that remainder after dividing sum of elements by K gives count of elements | C ++ implementation of the approach ; Function to return the number of subarrays of the given array such that the remainder when dividing the sum of its elements by K is equal to the number of its elements ; To store prefix sum ; We are dealing with zero indexed array ; Taking modulus value ; Prefix sum ; To store the required answer , the left index and the right index ; To store si - i value ; Include sum ; Increment the right index ; If subarray has at least k elements ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sub_arrays ( int a [ ] , int n , int k ) { int sum [ n + 2 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] -- ; a [ i ] %= k ; sum [ i + 1 ] += sum [ i ] + a [ i ] ; sum [ i + 1 ] %= k ; } int ans = 0 , l = 0 , r = 0 ; map < int , int > mp ; for ( int i = 0 ; i < n + 1 ; i ++ ) { ans += mp [ sum [ i ] ] ; mp [ sum [ i ] ] ++ ; r ++ ; if ( r - l >= k ) { mp [ sum [ l ] ] -- ; l ++ ; } } return ans ; } int main ( ) { int a [ ] = { 1 , 4 , 2 , 3 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; cout << sub_arrays ( a , n , k ) ; return 0 ; }
Sum of all the prime numbers with the maximum position of set bit â ‰¤ D | C ++ ; Function for Sieve of Eratosthenes ; Function to return the sum of the required prime numbers ; Maximum number of the required range ; Sieve of Eratosthenes ; To store the required sum ; If current element is prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieve ( bool prime [ ] , int n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } int sumPrime ( int d ) { int maxVal = pow ( 2 , d ) - 1 ; bool prime [ maxVal + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; sieve ( prime , maxVal ) ; int sum = 0 ; for ( int i = 2 ; i <= maxVal ; i ++ ) { if ( prime [ i ] ) { sum += i ; } } return sum ; } int main ( ) { int d = 8 ; cout << sumPrime ( d ) ; return 0 ; }
Check whether the exchange is possible or not | C ++ implementation of the approach ; Function that returns true if the exchange is possible ; Find the GCD of the array elements ; If the exchange is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int n , int p ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; if ( p % gcd == 0 ) return true ; return false ; } int main ( ) { int arr [ ] = { 6 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int p = 3 ; if ( isPossible ( arr , n , p ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Choose two elements from the given array such that their sum is not present in any of the arrays | C ++ implementation of the approach ; Function to find the numbers from the given arrays such that their sum is not present in any of the given array ; Find the maximum element from both the arrays ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNum ( int a [ ] , int n , int b [ ] , int m ) { int x = * max_element ( a , a + n ) ; int y = * max_element ( b , b + m ) ; cout << x << " ▁ " << y ; } int main ( ) { int a [ ] = { 3 , 2 , 2 } ; int n = sizeof ( a ) / sizeof ( int ) ; int b [ ] = { 1 , 5 , 7 , 7 , 9 } ; int m = sizeof ( b ) / sizeof ( int ) ; findNum ( a , n , b , m ) ; return 0 ; }
Generate an array B [ ] from the given array A [ ] which satisfies the given conditions | C ++ implementation of the approach ; Utility function to print the array elements ; Function to generate and print the required array ; To switch the ceil and floor function alternatively ; For every element of the array ; If the number is odd then print the ceil or floor value after division by 2 ; Use the ceil and floor alternatively ; If arr [ i ] is even then it will be completely divisible by 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void generateArr ( int arr [ ] , int n ) { bool flip = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) { if ( flip ^= true ) cout << ceil ( ( float ) ( arr [ i ] ) / 2.0 ) << " ▁ " ; else cout << floor ( ( float ) ( arr [ i ] ) / 2.0 ) << " ▁ " ; } else { cout << arr [ i ] / 2 << " ▁ " ; } } } int main ( ) { int arr [ ] = { 3 , -5 , -7 , 9 , 2 , -2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; generateArr ( arr , n ) ; return 0 ; }
Number of K length subsequences with minimum sum | C ++ implementation of the approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the count of valid subsequences ; Sort the array ; Maximum among the minimum K elements ; Y will store the frequency of num in the minimum K elements ; cntX will store the frequency of num in the complete array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int cntSubSeq ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int num = arr [ k - 1 ] ; int Y = 0 ; for ( int i = k - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == num ) Y ++ ; } int cntX = Y ; for ( int i = k ; i < n ; i ++ ) { if ( arr [ i ] == num ) cntX ++ ; } return binomialCoeff ( cntX , Y ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 2 ; cout << cntSubSeq ( arr , n , k ) ; return 0 ; }
Find all even length binary sequences with same sum of first and second half bits | Iterative | C ++ implementation ; Function to convert the number into binary and store the number into an array ; Function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; Calculating the sum from 0 till mid and store in sum1 ; Calculating the sum from mid till n and store in sum2 ; If sum1 is same as sum2 print the binary ; Function to print sequence ; Creating the array ; Looping over powers of 2 ; Converting the number into binary first ; Checking if the sum of the first half of the array is same as the sum of the next half ; Driver Code
#include <iostream> NEW_LINE #include <malloc.h> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void convertToBinary ( int num , int a [ ] , int n ) { int pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = num / 2 ; pointer -- ; } } void checkforsum ( int a [ ] , int n ) { int sum1 = 0 ; int sum2 = 0 ; int mid = n / 2 ; for ( int i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( int j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] ; cout << " STRNEWLINE " ; } } void print_seq ( int m ) { int n = ( 2 * m ) ; int a [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( int i = 0 ; i < ( int ) pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } int main ( ) { int m = 2 ; print_seq ( m ) ; return 0 ; }
Find out the prime numbers in the form of A + nB or B + nA | C ++ implementation of the above approach ; Utility function to check whether two numbers is co - prime or not ; Utility function to check whether a number is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; finding the Prime numbers ; Checking whether given numbers are co - prime or not ; To store the N primes ; If ' possible ' is true ; Printing n numbers of prime ; checking the form of a + nb ; Checking the form of b + na ; If ' possible ' is false return - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int coprime ( int a , int b ) { if ( __gcd ( a , b ) == 1 ) return true ; else return false ; } bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n == 2 or n == 3 ) return true ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void findNumbers ( int a , int b , int n ) { bool possible = true ; if ( ! coprime ( a , b ) ) possible = false ; int c1 = 1 ; int c2 = 1 ; int num1 , num2 ; set < int > st ; if ( possible ) { while ( ( int ) st . size ( ) != n ) { num1 = a + ( c1 * b ) ; if ( isPrime ( num1 ) ) { st . insert ( num1 ) ; } c1 ++ ; num2 = b + ( c2 * a ) ; if ( isPrime ( num2 ) ) { st . insert ( num2 ) ; } c2 ++ ; } for ( int i : st ) cout << i << " ▁ " ; } else cout << " - 1" ; } int main ( ) { int a = 3 ; int b = 5 ; int n = 4 ; findNumbers ( a , b , n ) ; return 0 ; }
Find the integers that doesnot ends with T1 or T2 when squared and added X | C ++ program to find the integers that ends with either T1 or T2 when squared and added X ; Function to print the elements Not ending with T1 or T2 ; Flag to check if none of the elements Do not end with t1 or t2 ; Traverse through all the elements ; Temporary variable to store the value ; If the last digit is neither t1 nor t2 then ; Print the number ; Set the flag as False ; If none of the elements meets the specification ; Driver Code ; Test case 1 ; Call the function ; Test case 2 ; Call the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findIntegers ( int n , int a [ ] , int x , int t1 , int t2 ) { bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { int temp = pow ( a [ i ] , 2 ) + x ; if ( temp % 10 != t1 && temp % 10 != t2 ) { cout << temp << " ▁ " ; flag = false ; } } if ( flag ) cout << " - 1" ; } int main ( ) { int N = 4 , X = 10 , T1 = 5 , T2 = 6 ; int a [ N ] = { 3 , 1 , 4 , 7 } ; findIntegers ( N , a , X , T1 , T2 ) ; cout << endl ; N = 4 , X = 2 , T1 = 5 , T2 = 6 ; int b [ N ] = { 2 , 18 , 22 , 8 } ; findIntegers ( N , b , X , T1 , T2 ) ; return 0 ; }
First N terms whose sum of digits is a multiple of 10 |  ; Function to return the sum of digits of n ; Add last digit to the sum ; Remove last digit ; Function to return the nth term of the required series ; If sum of digit is already a multiple of 10 then append 0 ; To store the minimum digit that must be appended ; Return n after appending the required digit ; Function to print the first n terms of the required series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int TEN = 10 ; int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % TEN ; n /= TEN ; } return sum ; } int getNthTerm ( int n ) { int sum = digitSum ( n ) ; if ( sum % TEN == 0 ) return ( n * TEN ) ; int extra = TEN - ( sum % TEN ) ; return ( ( n * TEN ) + extra ) ; } void firstNTerms ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << getNthTerm ( i ) << " ▁ " ; } int main ( ) { int n = 10 ; firstNTerms ( n ) ; return 0 ; }
Nearest greater number by interchanging the digits | C ++ program to find nearest greater value ; Find all the possible permutation of Value A . ; Convert into Integer ; Find the minimum value of A by interchanging the digit of A and store min1 . ; Driver code ; Convert integer value into string to find all the permutation of the number ; count = 1 means number greater than B exists
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min1 = INT_MAX ; int _count = 0 ; int permutation ( string str1 , int i , int n , int p ) { if ( i == n ) { int q = stoi ( str1 ) ; if ( q - p > 0 && q < min1 ) { min1 = q ; _count = 1 ; } } else { for ( int j = i ; j <= n ; j ++ ) { swap ( str1 [ i ] , str1 [ j ] ) ; permutation ( str1 , i + 1 , n , p ) ; swap ( str1 [ i ] , str1 [ j ] ) ; } } return min1 ; } int main ( ) { int A = 213 ; int B = 111 ; string str1 = to_string ( A ) ; int len = str1 . length ( ) ; int h = permutation ( str1 , 0 , len - 1 , B ) ; _count ? cout << h << endl : cout << -1 << endl ; return 0 ; }
Alcuin 's Sequence | CPP program for Alcuin 's Sequence ; find the nth term of Alcuin 's sequence ; return the ans ; print first n terms of Alcuin number ; display the number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Alcuin ( int n ) { double _n = n , ans ; ans = round ( ( _n * _n ) / 12 ) - floor ( _n / 4 ) * floor ( ( _n + 2 ) / 4 ) ; return ans ; } void solve ( int n ) { int i = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { cout << Alcuin ( i ) << " , ▁ " ; } } int main ( ) { int n = 15 ; solve ( n ) ; return 0 ; }
Find the final sequence of the array after performing given operations | C ++ implementation of the approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * solve ( int arr [ ] , int n ) { static int b [ 4 ] ; int p = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { b [ p ] = arr [ i -- ] ; if ( i >= 0 ) b [ n - 1 - p ] = arr [ i ] ; p ++ ; } return b ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int * b ; b = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << b [ i ] << " ▁ " ; }
Check if the product of every contiguous subsequence is different or not in a number | C ++ implementation of the approach ; Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool productsDistinct ( int N ) { string s = " " ; while ( N ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } reverse ( s . begin ( ) , s . end ( ) ) ; int sz = s . size ( ) ; set < int > se ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s [ j ] - '0' ) ; if ( se . find ( product ) != se . end ( ) ) return false ; else se . insert ( product ) ; } } return true ; } int main ( ) { int N = 2345 ; if ( productsDistinct ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides |  ; Function to find factorial of a number ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( n - r ) * factorial ( r ) ) ; } int NumberOfWays ( int n , int x , int y ) { return nCr ( 2 * n - x - y , n - x ) * factorial ( n ) * factorial ( n ) ; } int main ( ) { int n = 5 , x = 4 , y = 2 ; cout << NumberOfWays ( n , x , y ) ; return 0 ; }
Find smallest positive number Y such that Bitwise AND of X and Y is Zero | C ++ program to find smallest number Y for a given value of X such that X AND Y is zero ; Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; int findSmallestNonZeroY ( int A_num ) { string A_binary = bitset < 8 > ( A_num ) . to_string ( ) ; int B = 1 ; int length = A_binary . size ( ) ; int no_ones = __builtin_popcount ( A_num ) ; if ( length == no_ones ) return A_num + 1 ; for ( int i = 0 ; i < length ; i ++ ) { char ch = A_binary [ length - i - 1 ] ; if ( ch == '0' ) { B = pow ( 2.0 , i ) ; break ; } } return B ; } int main ( ) { int X = findSmallestNonZeroY ( 10 ) ; cout << X ; }
Count number of set bits in a range using bitset | C ++ implementation of above approach ; function to count number of 1 's using bitset ; Converting the string into bitset ; Bitwise operations Left shift ; Right shifts ; return count of one in [ L , R ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  32 NEW_LINE int GetOne ( string s , int L , int R ) { int len = s . length ( ) ; bitset < N > bit ( s ) ; bit <<= ( N - len + L - 1 ) ; bit >>= ( N - len + L - 1 ) ; bit >>= ( len - R ) ; return bit . count ( ) ; } int main ( ) { string s = "01010001011" ; int L = 2 , R = 4 ; cout << GetOne ( s , L , R ) ; return 0 ; }
Maximum element in an array such that its previous and next element product is maximum |  ; Function to return the largest element such that its previous and next element product is maximum ; Calculate the product of the previous and the next element for the current element ; Update the maximum product ; If current product is equal to the current maximum product then choose the maximum element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxElement ( int a [ ] , int n ) { if ( n < 3 ) return -1 ; int maxElement = a [ 0 ] ; int maxProd = a [ n - 1 ] * a [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { int currProd = a [ i - 1 ] * a [ ( i + 1 ) % n ] ; if ( currProd > maxProd ) { maxProd = currProd ; maxElement = a [ i ] ; } else if ( currProd == maxProd ) { maxElement = max ( maxElement , a [ i ] ) ; } } return maxElement ; } int main ( ) { int a [ ] = { 5 , 6 , 4 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxElement ( a , n ) ; return 0 ; }
Total ways of choosing X men and Y women from a total of M men and W women | C ++ implementation of the approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } int totalWays ( int X , int Y , int M , int W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } int main ( ) { int X = 4 , Y = 3 , M = 6 , W = 5 ; cout << totalWays ( X , Y , M , W ) ; return 0 ; }
Generate elements of the array following given conditions | C ++ implementation of the approach ; Function to generate the required array ; Initialize cnt variable for assigning unique value to prime and its multiples ; When we get a prime for the first time then assign a unique smallest value to it and all of its multiples ; Print the generated array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void specialSieve ( int n ) { int cnt = 0 ; int prime [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) { cnt ++ ; for ( int j = i ; j <= n ; j += i ) prime [ j ] = cnt ; } } for ( int i = 2 ; i <= n ; i ++ ) cout << prime [ i ] << " ▁ " ; } int main ( ) { int n = 6 ; specialSieve ( n ) ; return 0 ; }
Find the node whose sum with X has maximum set bits | C ++ implementation of the approach ; Function to perform dfs to find the maximum set bits value ; If current set bits value is greater than the current maximum ; If count is equal to the maximum then choose the node with minimum value ; Driver code ; Weights of the node ; Edges of the tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximum = INT_MIN , x , ans = INT_MAX ; vector < int > graph [ 100 ] ; vector < int > weight ( 100 ) ; void dfs ( int node , int parent ) { int a = __builtin_popcount ( weight [ node ] + x ) ; if ( maximum < a ) { maximum = a ; ans = node ; } else if ( maximum == a ) ans = min ( ans , node ) ; for ( int to : graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; } } int main ( ) { x = 15 ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . push_back ( 2 ) ; graph [ 2 ] . push_back ( 3 ) ; graph [ 2 ] . push_back ( 4 ) ; graph [ 1 ] . push_back ( 5 ) ; dfs ( 1 , 1 ) ; cout << ans ; return 0 ; }
Count Pairs from two arrays with even sum | C ++ implementation of the approach ; Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_pairs ( int a [ ] , int b [ ] , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } int pairs = min ( odd1 , odd2 ) + min ( even1 , even2 ) ; return pairs ; } int main ( ) { int a [ ] = { 9 , 14 , 6 , 2 , 11 } ; int b [ ] = { 8 , 4 , 7 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; cout << count_pairs ( a , b , n , m ) ; return 0 ; }
Find an index such that difference between product of elements before and after it is minimum |  ; Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; void solve ( int Array [ ] , int N ) { double Arraynew [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = log ( Array [ i ] ) ; } double prefixsum [ N ] ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } int answer = 0 ; double minabs = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double ans1 = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } cout << " Index ▁ is : ▁ " << answer << endl ; } int main ( ) { int Array [ 5 ] = { 1 , 4 , 12 , 2 , 6 } ; int N = 5 ; solve ( Array , N ) ; }