codeparrot/xlcost-text-to-code huggingface dataset:
Count of triplets of numbers 1 to N such that middle element is always largest | C ++ program to implement the above approach ; Function to find Number of triplets for given Number N such that middle element is always greater than left and right side element . ; check if arrangement is possible or Not ; else return total ways ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findArrangement ( int N ) { if ( N < 3 ) return 0 ; return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) / 3 ; } int main ( ) { int N = 10 ; cout << findArrangement ( N ) ; return 0 ; }
Lexicographically smallest array formed by at most one swap for every pair of adjacent indices | C ++ implementation to find the lexicographically smallest array by at most single swap for every pair of adjacent indices ; Function to find the lexicographically smallest array ; maximum swaps possible ; hash to store swaps performed ; let current element be the minimum possible ; Find actual position of the minimum element ; Update minimum element and its position ; Perform swaps if possible ; Insert current swap in hash ; print the required array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSmallestArray ( int A [ ] , int n ) { int count = n - 1 ; map < pair < int , int > , int > mp ; for ( int i = 0 ; i < n && count > 0 ; ++ i ) { int mn = A [ i ] , pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( A [ j ] < mn ) { mn = A [ j ] ; pos = j ; } } while ( pos > i && count > 0 && ! mp [ { pos - 1 , pos } ] ) { mp [ { pos - 1 , pos } ] = 1 ; swap ( A [ pos ] , A [ pos - 1 ] ) ; -- pos ; -- count ; } } for ( int i = 0 ; i < n ; ++ i ) cout << A [ i ] << " ▁ " ; } int main ( ) { int A [ ] = { 2 , 1 , 4 , 3 , 6 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findSmallestArray ( A , n ) ; return 0 ; }
Sum of all perfect square divisors of numbers from 1 to N | C ++ Program to find the sum of all perfect square divisors of numbers from 1 to N ; Function for finding inverse of a number iteratively Here we will find the inverse of 6 , since it appears as denominator in the formula of sum of squares from 1 to N ; Store the value of the inverse of 6 once as we don 't need to call the function again and again ; Formula for finding the sum of first n squares ; No perfect square exists which is less than 4 ; Starting from 2 , present value of start is denoted here as curStart ; Finding end of the segment for which the contribution will be same ; Using the above mentioned formula to find ans % MOD ; Now for mthe next iteration start will become end + 1 ; Finally returning the answer ; Driver Code ; Here we are adding x because we have not counted 1 as perfect squares so if u want to add it you can just add that number to the ans
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD  1000000007 NEW_LINE #define int  unsigned long long NEW_LINE int inv ( int a ) { int o = 1 ; for ( int p = MOD - 2 ; p > 0 ; p >>= 1 ) { if ( ( p & 1 ) == 1 ) o = ( o * a ) % MOD ; a = ( a * a ) % MOD ; } return o ; } int inv6 = inv ( 6 ) ; int sumOfSquares ( int n ) { n %= MOD ; return ( ( ( n * ( n + 1 ) ) % MOD * ( 2 * n + 1 ) ) % MOD * inv6 ) % MOD ; } int sums ( int n ) { if ( n < 4 ) return 0 ; int curStart = 2 , ans = 0 ; int sqrtN = sqrt ( n ) ; while ( curStart <= n / curStart ) { int V = n / ( curStart * curStart ) ; int end = sqrt ( n / V ) ; ans += ( n / ( curStart * curStart ) % MOD * ( sumOfSquares ( end ) + MOD - sumOfSquares ( curStart - 1 ) ) ) % MOD ; if ( ans >= MOD ) ans -= MOD ; curStart = end + 1 ; } return ans ; } int32_t main ( ) { int input [ ] = { 5 } ; for ( auto x : input ) { cout << " sum ▁ of ▁ all ▁ perfect " << " ▁ square ▁ divisors ▁ from " << " ▁ 1 ▁ to ▁ " << x << " ▁ is : ▁ " ; cout << x + sums ( x ) << endl ; } return 0 ; }
Check whether the binary equivalent of a number ends with "001" or not | C ++ implementation of the above approach ; Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "001" or not ; To store the binary number ; Count used to store exponent value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSuffix ( string s1 , string s2 ) { int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } bool CheckBinaryEquivalent ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } string bin = to_string ( B_Number ) ; return isSuffix ( "001" , bin ) ; } int main ( ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether the binary equivalent of a number ends with "001" or not | C ++ implementation of the above approach ; Function to check if binary equivalent of a number ends in "001" or not ; To check if binary equivalent of a number ends in "001" or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckBinaryEquivalent ( int N ) { return ( N - 1 ) % 8 == 0 ; } int main ( ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Panarithmic numbers within a given range | C ++ program to print Practical Numbers in given range ; function to compute divisors of a number ; vector to store divisors ; 1 will always be a divisor ; check if i is squareroot of A then only one time insert it in ans ; function to check that a number can be represented as sum of distinct divisor or not ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; return the possibility of given sum ; function to check a number is Practical or not ; vector to store divisors ; if all numbers can be represented as sum of unique divisors ; function to print Practical Numbers in a range ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > get_divisors ( int A ) { vector < int > ans ; ans . push_back ( 1 ) ; for ( int i = 2 ; i <= sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . push_back ( i ) ; if ( ( i * i ) != A ) ans . push_back ( A / i ) ; } } return ans ; } bool Sum_Possible ( vector < int > set , int sum ) { int n = set . size ( ) ; bool subset [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } bool Is_Practical ( int A ) { vector < int > divisors ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { cout << i << " ▁ " ; } } } int main ( ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; return 0 ; }
Maximize the division result of Array using given operations | C ++ implementation to maximize the result of division of the given array elements ; Function to find the max result ; Sort the array in descending order ; loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float maxDivision ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; float mxdiv = arr [ 1 ] ; for ( int i = 2 ; i < n ; ++ i ) mxdiv = mxdiv / arr [ i ] ; return arr [ 0 ] / mxdiv ; } int main ( ) { int arr [ ] = { 100 , 1000 , 10 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxDivision ( arr , n ) ; return 0 ; }
Print all distinct Coprime sets possible from 1 to N | C ++ implementation to print all distinct co - prime sets possible for numbers from 1 to N ; Function to print all coprime sets ; Check if n is less than 4 then simply print all values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; if n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; Last element for odd case ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void coPrimeSet ( int n ) { int firstadj , secadj ; if ( n < 4 ) { cout << " ( ▁ " ; for ( int i = 1 ; i <= n ; i ++ ) cout << i << " , ▁ " ; cout << " ) STRNEWLINE " ; } else { if ( n % 2 == 0 ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; cout << " ( " << firstadj << " , ▁ " << secadj << " ) STRNEWLINE " ; } } else { for ( int i = 0 ; i < n / 2 - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; cout << " ( " << firstadj << " , ▁ " << secadj << " ) STRNEWLINE " ; } cout << " ( " << n - 2 << " , ▁ " << n - 1 << " , ▁ " << n << " ) STRNEWLINE " ; } } } int main ( ) { int n = 5 ; coPrimeSet ( n ) ; return 0 ; }
Find the sequence number of a triangular number | C ++ code to print sequence number of a triangular number ; If N is not tringular number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int N = 21 ; int A = sqrt ( 2 * N + 0.25 ) - 0.5 ; int B = A ; if ( B != A ) cout << " - 1" ; else cout << B ; }
Count subarrays with sum equal to its XOR value | C ++ program to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll operation ( int arr [ ] , int N ) { ll right = 0 , ans = 0 , num = 0 ; for ( ll left = 0 ; left < N ; left ++ ) { while ( right < N && num + arr [ right ] == ( num ^ arr [ right ] ) ) { num += arr [ right ] ; right ++ ; } ans += right - left ; if ( left == right ) right ++ ; else num -= arr [ left ] ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << operation ( arr , N ) ; }
Count of pairs in an Array whose sum is Prime | C ++ code to find number of pairs of elements in an array whose sum is prime ; Function for Sieve Of Eratosthenes ; Function to count total number of pairs of elements whose sum is prime ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool * sieveOfEratosthenes ( int N ) { bool * isPrime = new bool [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( isPrime [ i ] == true ) { int j = 2 ; while ( i * j <= N ) { isPrime [ i * j ] = false ; j ++ ; } } } return isPrime ; } int numPairsWithPrimeSum ( int * arr , int n ) { int N = 2 * 1000000 ; bool * isPrime = sieveOfEratosthenes ( N ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( isPrime [ sum ] ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numPairsWithPrimeSum ( arr , n ) ; return 0 ; }
Count of numbers in Array ending with digits of number N | C ++ program to find the count of numbers in Array ending with digits of number N ; Array to keep the track of digits occurred Initially all are 0 ( false ) ; Function to initialize true if the digit is present ; Variable to store the last digit ; Loop to iterate through every digit of the number N ; Updating the array according to the presence of the digit in n at the array index ; Function to check if the numbers in the array end with the digits of the number N ; Variable to store the count ; Variable to store the last digit ; Checking the presence of the last digit in N ; Function to find the required count ; Driver code ; Preprocessing
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit [ 10 ] = { 0 } ; void digitsPresent ( int n ) { int lastDigit ; while ( n != 0 ) { lastDigit = n % 10 ; digit [ lastDigit ] = true ; n /= 10 ; } } int checkLastDigit ( int num ) { int count = 0 ; int lastDigit ; lastDigit = num % 10 ; if ( digit [ lastDigit ] == true ) count ++ ; return count ; } void findCount ( int N , int K , int arr [ ] ) { int count = 0 ; for ( int i = 0 ; i < K ; i ++ ) { count = checkLastDigit ( arr [ i ] ) == 1 ? count + 1 : count ; } cout << count << endl ; } int main ( ) { int N = 1731 ; digitsPresent ( N ) ; int K = 5 ; int arr [ ] = { 57 , 6786 , 1111 , 3 , 9812 } ; findCount ( N , K , arr ) ; return 0 ; }
Check if count of even divisors of N is equal to count of odd divisors | C ++ program for the above approach ; Function to check if count of even and odd divisors are equal ; If ( n - 2 ) % 4 is an integer , then return true else return false ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool divisorsSame ( int n ) { return ( n - 2 ) % 4 == 0 ; } int main ( ) { int N = 6 ; if ( divisorsSame ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit | C ++ program for the above approach . ; Function to find integer ; because 1 st integer is K itself ; Initialize min_d and max_d ; updating min_d and max_d ; break if min digit is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( int K , int N ) { N -- ; while ( N -- ) { int curr_term = K ; int min_d = 9 ; int max_d = 0 ; while ( curr_term > 0 ) { int r = curr_term % 10 ; min_d = min ( min_d , r ) ; max_d = max ( max_d , r ) ; curr_term = curr_term / 10 ; } if ( min_d == 0 ) { break ; } K = K + min_d * max_d ; } return K ; } int main ( ) { int K = 487 ; int N = 2 ; cout << find ( K , N ) << endl ; return 0 ; }
Count of subarrays whose sum is a perfect square | C ++ code for the above approach . ; Function to find count of subarrays whose sum is a perfect square . ; to search for index with ( current prefix sum - j * j ) ; storing the prefix sum ; used to track the minimum value in prefixSum ; Calculating the prefixSum and tracking the prefixMin ; below statement is used if array contains negative numbers ; counts the no of subarrays with perfect square sum ; as 0 is a perfect square , so we initialize 0 th index - key with value 1 ; Here we count the perfect square subarray sum by searching if there is a prefix with sum = ( current prefixSum - ( sq * sq ) ) ; increasing our subarray count ; increasing the current prefixSum index value in map by 1 to count the other perfect squares while traversing further ; Driver code ; printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE lli countSubarrays ( int arr [ ] , int n ) { unordered_map < int , int > mp ; int prefixSum [ n ] ; int prefixMin = 0 ; prefixSum [ 0 ] = arr [ 0 ] ; prefixMin = min ( prefixMin , prefixSum [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; prefixMin = min ( prefixMin , prefixSum [ i ] ) ; } lli countSubs = 0 ; mp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; prefixSum [ i ] - j * j >= prefixMin ; j ++ ) { if ( mp . find ( prefixSum [ i ] - j * j ) != mp . end ( ) ) countSubs += mp [ prefixSum [ i ] - j * j ] ; } mp [ prefixSum [ i ] ] ++ ; } return countSubs ; } int main ( ) { int arr [ ] = { 2 , 3 , -5 , 6 , -7 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; lli ans = countSubarrays ( arr , n ) ; cout << ans ; return 0 ; }
Find if two given Quadratic equations have common roots or not | C ++ Program to Find if two given Quadratic equations have common roots or not ; function to check if 2 quadratic equations have common roots or not . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkSolution ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { return ( a1 / a2 ) == ( b1 / b2 ) && ( b1 / b2 ) == ( c1 / c2 ) ; } int main ( ) { float a1 = 1 , b1 = -5 , c1 = 6 ; float a2 = 2 , b2 = -10 , c2 = 12 ; if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not | C ++ program to check if roots of a Quadratic Equation are numerically equal but opposite in sign or not ; Function to find the required answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; void checkSolution ( int a , int b , int c ) { if ( b == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ; return 0 ; }
Count of index pairs in array whose range product is a positive integer | C ++ Program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition if number of negative elements is even then add even_count in answer ; Otherwise add odd_count in answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void positiveProduct ( int arr [ ] , int n ) { int even_count = 0 ; int odd_count = 0 ; int total_count = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( total_count % 2 == 0 ) even_count ++ ; else odd_count ++ ; if ( arr [ i ] < 0 ) total_count ++ ; if ( total_count % 2 == 0 ) ans += even_count ; else ans += odd_count ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int A [ ] = { 5 , -3 , 3 , -1 , 1 } ; int size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; positiveProduct ( A , size ) ; return 0 ; }
Pair of integers having difference of their fifth power as X | C ++ implementation to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Function to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Loop to choose every possible pair with in the range ; Check if equation holds ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( int x ) { int lim = 120 ; for ( int i = - lim ; i <= lim ; i ++ ) { for ( int j = - lim ; j <= lim ; j ++ ) { if ( pow ( i , 5 ) - pow ( j , 5 ) == x ) { cout << i << ' ▁ ' << j << endl ; return ; } } } cout << " - 1" ; } signed main ( ) { int X = 33 ; findPair ( X ) ; return 0 ; }
Sum of GCD of all numbers upto N with N itself | C ++ Program to find the Sum of GCD of all integers up to N with N itself ; Function to Find Sum of GCD of each numbers ; Consider all prime factors of no . and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update no and result ; If no has a prime factor greater than sqrt ( n ) then at - most one such prime factor exists ; Return the result ; Finding GCD of pairs ; Calculate the divisors ; Return count of numbers from 1 to N with GCD d with N ; Check if d1 and d2 are equal then skip this ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int d , int n ) { int no = n / d ; int result = no ; for ( int p = 2 ; p * p <= no ; ++ p ) { if ( no % p == 0 ) { while ( no % p == 0 ) no /= p ; result -= result / p ; } } if ( no > 1 ) result -= result / no ; return result ; } int sumOfGCDofPairs ( int n ) { int res = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int d1 = i ; int d2 = n / i ; res += d1 * getCount ( d1 , n ) ; if ( d1 != d2 ) res += d2 * getCount ( d2 , n ) ; } } return res ; } int main ( ) { int n = 12 ; cout << sumOfGCDofPairs ( n ) ; return 0 ; }
Check whether Array represents a Fibonacci Series or not | C ++ program to check if the elements of a given array can form a Fibonacci Series ; Returns true if a permutation of arr [ 0. . n - 1 ] can form a Fibonacci Series ; Sort array ; After sorting , check if every element is equal to the sum of previous 2 elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIsFibonacci ( int arr [ ] , int n ) { if ( n == 1 n == 2 ) return true ; sort ( arr , arr + n ) ; for ( int i = 2 ; i < n ; i ++ ) if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) return false ; return true ; } int main ( ) { int arr [ ] = { 8 , 3 , 5 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkIsFibonacci ( arr , n ) ) cout << " Yes " << endl ; else cout << " No " ; return 0 ; }
Last digit in a power of 2 | C ++ program to find last digit in a power of 2. ; Corner case ; Find the shift in current cycle and return value accordingly ; return 6 ; When n % 4 == 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastDigit2PowerN ( int n ) { if ( n == 0 ) return 1 ; else if ( n % 4 == 1 ) return 2 ; else if ( n % 4 == 2 ) return 4 ; else if ( n % 4 == 3 ) return 8 ; else } int main ( ) { for ( int n = 0 ; n < 20 ; n ++ ) cout << lastDigit2PowerN ( n ) << " ▁ " ; return 0 ; }
Sum of all subsequences of length K | C ++ implementation to find sum of all subsequences of length K ; Function to find nCr ; Function that returns factorial of n ; Function for finding sum of all K length subsequences ; Calculate the sum of array ; Calculate nCk ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) ; int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int sumSubsequences ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int kLengthSubSequence ; kLengthSubSequence = nCr ( n , k ) ; int ans = sum * ( ( k * kLengthSubSequence ) / n ) ; return ans ; } int main ( ) { int arr [ ] = { 7 , 8 , 9 , 2 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumSubsequences ( arr , n , K ) ; return 0 ; }
Sum of first K numbers which are not divisible by N | C ++ Program to calculate the sum of first K numbers not divisible by N ; Function to find the sum ; Find the last multiple of N ; Find the K - th non - multiple of N ; Calculate the sum of all elements from 1 to val ; Calculate the sum of all multiples of N between 1 to val ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n , int k ) { int val = ( k / ( n - 1 ) ) * n ; int rem = k % ( n - 1 ) ; if ( rem == 0 ) { val = val - 1 ; } else { val = val + rem ; } int sum = ( val * ( val + 1 ) ) / 2 ; int x = k / ( n - 1 ) ; int sum_of_multiples = ( x * ( x + 1 ) * n ) / 2 ; sum -= sum_of_multiples ; return sum ; } int main ( ) { int n = 7 , k = 13 ; cout << findSum ( n , k ) << endl ; }