codeparrot/xlcost-text-to-code huggingface dataset:
Longest subarray with GCD greater than 1 | C ++ program of the above approach ; Function to build the Segment Tree from the given array to process range queries in log ( N ) time ; Termination Condition ; Find the mid value ; Left and Right Recursive Call ; Update the Segment Tree Node ; Function to return the GCD of the elements of the Array from index l to index r ; Base Case ; Find the middle range ; Find the GCD and return ; Function to print maximum length of the subarray having GCD > one ; Stores the Segment Tree ; Function call to build the Segment tree from array arr [ ] ; Store maximum length of subarray ; Starting and ending pointer of the current window ; Case where the GCD of the current window is 1 ; Update the maximum length ; Print answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void build_tree ( int * b , vector < int > & seg_tree , int l , int r , int vertex ) { if ( l == r ) { seg_tree [ vertex ] = b [ l ] ; return ; } int mid = ( l + r ) / 2 ; build_tree ( b , seg_tree , l , mid , 2 * vertex ) ; build_tree ( b , seg_tree , mid + 1 , r , 2 * vertex + 1 ) ; seg_tree [ vertex ] = __gcd ( seg_tree [ 2 * vertex ] , seg_tree [ 2 * vertex + 1 ] ) ; } int range_gcd ( vector < int > & seg_tree , int v , int tl , int tr , int l , int r ) { if ( l > r ) return 0 ; if ( l == tl && r == tr ) return seg_tree [ v ] ; int tm = ( tl + tr ) / 2 ; return __gcd ( range_gcd ( seg_tree , 2 * v , tl , tm , l , min ( tm , r ) ) , range_gcd ( seg_tree , 2 * v + 1 , tm + 1 , tr , max ( tm + 1 , l ) , r ) ) ; } void maxSubarrayLen ( int arr [ ] , int n ) { vector < int > seg_tree ( 4 * ( n ) + 1 , 0 ) ; build_tree ( arr , seg_tree , 0 , n - 1 , 1 ) ; int maxLen = 0 ; int l = 0 , r = 0 ; while ( r < n && l < n ) { if ( range_gcd ( seg_tree , 1 , 0 , n - 1 , l , r ) == 1 ) { l ++ ; } maxLen = max ( maxLen , r - l + 1 ) ; r ++ ; } cout << maxLen ; } int main ( ) { int arr [ ] = { 410 , 52 , 51 , 180 , 222 , 33 , 33 } ; int N = sizeof ( arr ) / sizeof ( int ) ; maxSubarrayLen ( arr , N ) ; return 0 ; }
Smallest pair of integers with minimum difference whose Bitwise XOR is N | C ++ program for the above approach ; Function to find the numbers A and B whose Bitwise XOR is N and the difference between them is minimum ; Find the MSB of the N ; Find the value of B ; Find the value of A ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAandB ( int N ) { int K = log2 ( N ) ; int B = ( 1 << K ) ; int A = B ^ N ; cout << A << ' ▁ ' << B ; } int main ( ) { int N = 26 ; findAandB ( N ) ; return 0 ; }
Find all possible values of K such that the sum of first N numbers starting from K is G | C ++ program for the above approach ; Function to find the count the value of K such that sum of the first N numbers from K is G ; Stores the total count of K ; Iterate till square root of g ; If the number is factor of g ; If the second factor is not equal to first factor ; Check if two factors are odd or not ; If second factor is the same as the first factor then check if the first factor is odd or not ; Print the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findValuesOfK ( int g ) { int count = 0 ; for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { if ( i != g / i ) { if ( i & 1 ) { count ++ ; } if ( ( g / i ) & 1 ) { count ++ ; } } else if ( i & 1 ) { count ++ ; } } } cout << count ; } int main ( ) { int G = 125 ; findValuesOfK ( G ) ; return 0 ; }
Difference between maximum and minimum average of all K | C ++ program for the above approach ; Function to find the difference between the maximum and minimum subarrays of length K ; Stores the sum of subarray over the range [ 0 , K ] ; Iterate over the range [ 0 , K ] ; Store min and max sum ; Iterate over the range [ K , N - K ] ; Increment sum by arr [ i ] - arr [ i - K ] ; Update max and min moving sum ; Return difference between max and min average ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double Avgdifference ( double arr [ ] , int N , int K ) { double sum = 0 ; for ( int i = 0 ; i < K ; i ++ ) sum += arr [ i ] ; double min = sum ; double max = sum ; for ( int i = K ; i <= N - K + 1 ; i ++ ) { sum += arr [ i ] - arr [ i - K ] ; if ( min > sum ) min = sum ; if ( max < sum ) max = sum ; } return ( max - min ) / K ; } int main ( ) { double arr [ ] = { 3 , 8 , 9 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << Avgdifference ( arr , N , K ) ; return 0 ; }
Count of distinct integers in range [ 1 , N ] that do not have any subset sum as K | C ++ program for the above approach ; Function to find maximum number of distinct integers in [ 1 , N ] having no subset with sum equal to K ; Declare a vector to store the required numbers ; Store all the numbers in [ 1 , N ] except K ; Store the maximum number of distinct numbers ; Reverse the array ; Print the required numbers ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSet ( int N , int K ) { vector < int > a ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i != K ) a . push_back ( i ) ; } int MaxDistinct = ( N - K ) + ( K / 2 ) ; reverse ( a . begin ( ) , a . end ( ) ) ; for ( int i = 0 ; i < MaxDistinct ; i ++ ) cout << a [ i ] << " ▁ " ; } int main ( ) { int N = 5 , K = 3 ; findSet ( N , K ) ; return 0 ; }
Solve Linear Congruences Ax = B ( mod N ) for values of x in range [ 0 , N | C ++ program for the above approach ; Function to stores the values of x and y and find the value of gcd ( a , b ) ; Base Case ; Store the result of recursive call ; Update x and y using results of recursive call ; Function to give the distinct solutions of ax = b ( mod n ) ; Function Call to find the value of d and u ; No solution exists ; Else , initialize the value of x0 ; Print all the answers ; Driver Code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long ExtendedEuclidAlgo ( long long a , long long b , long long & x , long long & y ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } else { long long x1 , y1 ; long long gcd = ExtendedEuclidAlgo ( b , a % b , x1 , y1 ) ; x = y1 ; y = x1 - floor ( a / b ) * y1 ; return gcd ; } } void linearCongruence ( long long A , long long B , long long N ) { A = A % N ; B = B % N ; long long u = 0 , v = 0 ; long long d = ExtendedEuclidAlgo ( A , N , u , v ) ; if ( B % d != 0 ) { cout << -1 << endl ; return ; } long long x0 = ( u * ( B / d ) ) % N ; if ( x0 < 0 ) x0 += N ; for ( long long i = 0 ; i <= d - 1 ; i ++ ) cout << ( x0 + i * ( N / d ) ) % N << " ▁ " ; } int main ( ) { long long A = 15 ; long long B = 9 ; long long N = 18 ; linearCongruence ( A , B , N ) ; return 0 ; }
Factorial of a number without using multiplication | C ++ program for the above approach ; Function to calculate factorial of the number without using multiplication operator ; variable to store the final factorial ; Outer loop ; Inner loop ; Driver code ; Input ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorialWithoutMul ( int N ) { int ans = N ; for ( int i = N - 1 ; i > 0 ; i -- ) { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ans ; ans = sum ; } return ans ; } int main ( ) { int N = 5 ; cout << factorialWithoutMul ( N ) << endl ; return 0 ; }
Sum of Bitwise AND of all unordered triplets of an array | C ++ program for the above approach ; Function to calculate sum of Bitwise AND of all unordered triplets from a given array such that ( i < j < k ) ; Stores the resultant sum of Bitwise AND of all triplets ; Generate all triplets of ( arr [ i ] , arr [ j ] , arr [ k ] ) ; Add Bitwise AND to ans ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void tripletAndSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { ans += arr [ i ] & arr [ j ] & arr [ k ] ; } } } cout << ans ; } int main ( ) { int arr [ ] = { 3 , 5 , 4 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; tripletAndSum ( arr , N ) ; return 0 ; }
Sum of Bitwise AND of all unordered triplets of an array | C ++ program for the above approach ; Function to calculate sum of Bitwise AND of all unordered triplets from a given array such that ( i < j < k ) ; Stores the resultant sum of Bitwise AND of all triplets ; Traverse over all the bits ; Count number of elements with the current bit set ; There are ( cnt ) C ( 3 ) numbers with the current bit set and each triplet contributes 2 ^ bit to the result ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int tripletAndSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int bit = 0 ; bit < 32 ; bit ++ ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & ( 1 << bit ) ) cnt ++ ; } ans += ( 1 << bit ) * cnt * ( cnt - 1 ) * ( cnt - 2 ) / 6 ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 5 , 4 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << tripletAndSum ( arr , N ) ; return 0 ; }
Lexicographically smallest permutation of length 2 N that can be obtained from an N | C ++ program for the above approach ; Function to find the lexicographically smallest permutation of length 2 * N satisfying the given conditions ; Stores if i - th element is placed at odd position or not ; Traverse the array ; Mark arr [ i ] true ; Stores all the elements not placed at odd positions ; Iterate in the range [ 1 , 2 * N ] ; If w [ i ] is not marked ; Stores whether it is possible to obtain the required permutation or not ; Stores the permutation ; Traverse the array arr [ ] ; Finds the iterator of the smallest number greater than the arr [ i ] ; If it is S . end ( ) ; Mark found false ; Push arr [ i ] and * it into the array ; Erase the current element from the Set ; If found is not marked ; Otherwise , ; Print the permutation ; Driver Code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallestPermutation ( int arr [ ] , int N ) { vector < bool > w ( 2 * N + 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { w [ arr [ i ] ] = true ; } set < int > S ; for ( int i = 1 ; i <= 2 * N ; i ++ ) { if ( ! w [ i ] ) S . insert ( i ) ; } bool found = true ; vector < int > P ; for ( int i = 0 ; i < N ; i ++ ) { auto it = S . lower_bound ( arr [ i ] ) ; if ( it == S . end ( ) ) { found = false ; break ; } P . push_back ( arr [ i ] ) ; P . push_back ( * it ) ; S . erase ( it ) ; } if ( ! found ) { cout << " - 1 STRNEWLINE " ; } else { for ( int i = 0 ; i < 2 * N ; i ++ ) cout << P [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 4 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smallestPermutation ( arr , N ) ; return 0 ; }
Maximum sum of a subsequence having difference between their indices equal to the difference between their values | C ++ program for the above approach ; Function to find the maximum sum of a subsequence having difference between indices equal to difference in their values ; Stores the maximum sum ; Stores the value for each A [ i ] - i ; Traverse the array ; Update the value in map ; Update the answer ; Finally , print the answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSubsequenceSum ( int A [ ] , int N ) { int ans = 0 ; map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ A [ i ] - i ] += A [ i ] ; ans = max ( ans , mp [ A [ i ] - i ] ) ; } cout << ans << endl ; } int main ( ) { int A [ ] = { 10 , 7 , 1 , 9 , 10 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; maximumSubsequenceSum ( A , N ) ; return 0 ; }
Find the nearest perfect square for each element of the array | C ++ program for the above approach ; Function to find the nearest perfect square for every element in the given array ; Traverse the array ; Calculate square root of current element ; Calculate perfect square ; Find the nearest ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestPerfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) cout << a << " ▁ " ; else cout << b << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 2 , 7 , 13 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestPerfectSquare ( arr , N ) ; return 0 ; }
Count of sets possible using integers from a range [ 2 , N ] using given operations that are in Equivalence Relation | C ++ program for the above approach ; Sieve of Eratosthenes to find primes less than or equal to N ; Function to find number of Sets ; Handle Base Case ; Set which contains less than or equal to N / 2 ; Number greater than N / 2 and are prime increment it by 1 ; If the number is prime Increment answer by 1 ; Driver Code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime [ 100001 ] ; void SieveOfEratosthenes ( int n ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } void NumberofSets ( int N ) { SieveOfEratosthenes ( N ) ; if ( N == 2 ) { cout << 1 << endl ; } else if ( N == 3 ) { cout << 2 << endl ; } else { int ans = 1 ; for ( int i = N / 2 + 1 ; i <= N ; i ++ ) { if ( prime [ i ] ) { ans += 1 ; } } cout << ans << endl ; } } int main ( ) { int N = 9 ; NumberofSets ( N ) ; return 0 ; }
Absolute difference between floor of Array sum divided by X and floor sum of every Array element when divided by X | C ++ program for the above approach ; Function to find absolute difference between the two sum values ; Variable to store total sum ; Variable to store sum of A [ i ] / X ; Traverse the array ; Update totalSum ; Update perElementSum ; Floor of total sum divided by X ; Return the absolute difference ; Driver Code ; Input ; Size of Array ; Function call to find absolute difference between the two sum values
#include <bits/stdc++.h> NEW_LINE using namespace std ; int floorDifference ( int A [ ] , int N , int X ) { int totalSum = 0 ; int perElementSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += A [ i ] ; perElementSum += A [ i ] / X ; } int totalFloorSum = totalSum / X ; return abs ( totalFloorSum - perElementSum ) ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int X = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << floorDifference ( A , N , X ) ; return 0 ; }
Convert a number from base A to base B | C ++ program for the above approach ; Function to return ASCII value of a character ; Function to convert a number from given base to decimal number ; Stores the length of the string ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Update num ; Update power ; Function to return equivalent character of a given value ; Function to convert a given decimal number to a given base ; Store the result ; Repeatedly divide inputNum by base and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a base to another base ; Convert the number from base A to decimal ; Convert the number from decimal to base B ; Print the result ; Driver Code ; Given input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } int toDeci ( string str , int base ) { int len = str . size ( ) ; int power = 1 ; int num = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { printf ( " Invalid ▁ Number " ) ; return -1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } char reVal ( int num ) { if ( num >= 0 && num <= 9 ) return ( char ) ( num + '0' ) ; else return ( char ) ( num - 10 + ' A ' ) ; } string fromDeci ( int base , int inputNum ) { string res = " " ; while ( inputNum > 0 ) { res += reVal ( inputNum % base ) ; inputNum /= base ; } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } void convertBase ( string s , int a , int b ) { int num = toDeci ( s , a ) ; string ans = fromDeci ( b , num ) ; cout << ans ; } int main ( ) { string s = "10B " ; int a = 16 , b = 10 ; convertBase ( s , a , b ) ; return 0 ; }
Prefix Factorials of a Prefix Sum Array | C ++ program for the above approach ; Function to find the factorial of a number N ; Base Case ; Find the factorial recursively ; Function to find the prefix factorial array ; Find the prefix sum array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int N ) { if ( N == 1 N == 0 ) return 1 ; return N * fact ( N - 1 ) ; } void prefixFactorialArray ( int arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = fact ( arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; prefixFactorialArray ( arr , N ) ; return 0 ; }
Mean of fourth powers of first N natural numbers | C ++ program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Store the resultant average calculated using formula ; Return the average ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findAverage ( int N ) { double avg = ( ( 6 * N * N * N * N ) + ( 15 * N * N * N ) + ( 10 * N * N ) - 1 ) / 30.0 ; return avg ; } int main ( ) { int N = 3 ; cout << findAverage ( N ) ; return 0 ; }
Modify array by removing ( arr [ i ] + arr [ i + 1 ] ) th element exactly K times | C ++ program for the above approach ; Function to modify array by removing every K - th element from the array ; Check if current element is the k - th element ; Stores the elements after removing every kth element ; Append the current element if it is not k - th element ; Return the new array after removing every k - th element ; Function to print the array ; Traverse the array l [ ] ; Function to print the array after performing the given operations exactly k times ; Store first N natural numbers ; Iterate over the range [ 0 , k - 1 ] ; Store sums of the two consecutive terms ; Remove every p - th element from the array ; Increment x by 1 for the next iteration ; Print the resultant array ; Driver Code ; Given arrays ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > removeEveryKth ( vector < int > l , int k ) { for ( int i = 0 ; i < l . size ( ) ; i ++ ) { if ( i % k == 0 ) l [ i ] = 0 ; } vector < int > arr ; arr . push_back ( 0 ) ; for ( int i = 1 ; i < l . size ( ) ; i ++ ) { if ( l [ i ] != 0 ) arr . push_back ( l [ i ] ) ; } return arr ; } void printArray ( vector < int > l ) { for ( int i = 1 ; i < l . size ( ) ; i ++ ) cout << l [ i ] << " ▁ " ; cout << endl ; } void printSequence ( int n , int k ) { vector < int > l ( n + 1 ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) l [ i ] = i ; int x = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int p = l [ x ] + l [ x + 1 ] ; l = removeEveryKth ( l , p ) ; x += 1 ; } printArray ( l ) ; } int main ( ) { int N = 8 ; int K = 2 ; printSequence ( N , K ) ; }
Minimum number of bits of array elements required to be flipped to make all array elements equal | C ++ program for the above approach ; Function to count minimum number of bits required to be flipped to make all array elements equal ; Stores the count of unset bits ; Stores the count of set bits ; Traverse the array ; Traverse the bit of arr [ i ] ; If current bit is set ; Increment fre1 [ j ] ; Otherwise ; Increment fre0 [ j ] ; Right shift x by 1 ; Stores the count of total moves ; Traverse the range [ 0 , 32 ] ; Update the value of ans ; Return the minimum number of flips required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int makeEqual ( int * arr , int n ) { int fre0 [ 33 ] = { 0 } ; int fre1 [ 33 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; for ( int j = 0 ; j < 33 ; j ++ ) { if ( x & 1 ) { fre1 [ j ] += 1 ; } else { fre0 [ j ] += 1 ; } x = x >> 1 ; } } int ans = 0 ; for ( int i = 0 ; i < 33 ; i ++ ) { ans += min ( fre0 [ i ] , fre1 [ i ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << makeEqual ( arr , N ) ; return 0 ; }
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | C ++ program for the above approach ; Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Traverse the array arr [ ] ; Traverse the vector ; If v [ i ] is divisible by K ; Iterate over the range [ 0 , K ] ; Update v ; Otherwise ; Traverse the vector v ; Return the sum of the updated array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int arr [ ] , int N , int K ) { int sum = 0 ; vector < long long > v ; for ( int i = 0 ; i < N ; i ++ ) { v . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] % K == 0 ) { long long x = v [ i ] / K ; for ( int j = 0 ; j < K ; j ++ ) { v . push_back ( x ) ; } } else break ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) sum = sum + v [ i ] ; return sum ; } int main ( ) { int arr [ ] = { 4 , 6 , 8 , 2 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sum ( arr , N , K ) ; return 0 ; }
Check if sum of arr [ i ] / j for all possible pairs ( i , j ) in an array is 0 or not | C ++ program for the above approach ; Function to check if sum of all values of ( arr [ i ] / j ) for all 0 < i <= j < ( N - 1 ) is 0 or not ; Stores the required sum ; Traverse the array ; If the sum is equal to 0 ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int arr [ ] = { 1 , -1 , 3 , -2 , -1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; check ( arr , N ) ; return 0 ; }
Program to calculate expected increase in price P after N consecutive days | C ++ program for the above approach ; Function to find the increased value of P after N days ; Expected value of the number P after N days ; Print the expected value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void expectedValue ( int P , int a , int b , int N ) { double expValue = P + ( N * 0.5 * ( a + b ) ) ; cout << expValue ; } int main ( ) { int P = 3000 , a = 20 , b = 10 , N = 30 ; expectedValue ( P , a , b , N ) ; return 0 ; }
Find the index in a circular array from which prefix sum is always non | C ++ program for the above approach ; Function to find the starting index of the given circular array s . t . prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array arr [ ] ; Update the value of sum ; If sum is less than min ; Update the min as the value of prefix sum ; Update in ; Otherwise , no such index is possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int startingPoint ( int A [ ] , int N ) { int sum = 0 ; int in = 0 ; int min = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( sum < min ) { min = sum ; in = i + 1 ; } } if ( sum < 0 ) { return -1 ; } return in % N ; } int main ( ) { int arr [ ] = { 3 , -6 , 7 , -4 , -4 , 6 , -1 } ; int N = ( sizeof ( arr ) / sizeof ( arr [ 0 ] ) ) ; cout << startingPoint ( arr , N ) ; return 0 ; }