codeparrot/xlcost-text-to-code huggingface dataset:
Arithmetic Mean | C ++ program to find n arithmetic means between A and B ; Prints N arithmetic means between A and B . ; calculate common difference ( d ) ; for finding N the arithmetic mean between A and B ; Driver code to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAMeans ( int A , int B , int N ) { float d = ( float ) ( B - A ) / ( N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) cout << ( A + i * d ) << " ▁ " ; } int main ( ) { int A = 20 , B = 32 , N = 5 ; printAMeans ( A , B , N ) ; return 0 ; }
Prime Factor | Program to print all prime factors ; A function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver program to test above function
# include <stdio.h> NEW_LINE # include <math.h> NEW_LINE void primeFactors ( int n ) { while ( n % 2 == 0 ) { printf ( " % d ▁ " , 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { printf ( " % d ▁ " , i ) ; n = n / i ; } } if ( n > 2 ) printf ( " % d ▁ " , n ) ; } int main ( ) { int n = 315 ; primeFactors ( n ) ; return 0 ; }
Time taken by two persons to meet on a circular track | C ++ implementation of above approach ; Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int startingPoint ( int Length , int Speed1 , int Speed2 ) { int result1 = 0 , result2 = 0 ; int time1 = Length / Speed1 ; int time2 = Length / Speed2 ; result1 = __gcd ( time1 , time2 ) ; result2 = time1 * time2 / ( result1 ) ; return result2 ; } float firstTime ( int Length , int Speed1 , int Speed2 ) { float result = 0 ; int relativeSpeed = abs ( Speed1 - Speed2 ) ; result = ( ( float ) Length / relativeSpeed ) ; return result ; } int main ( ) { int L = 30 , S1 = 5 , S2 = 2 ; float first_Time = firstTime ( L , S1 , S2 ) ; int starting_Point = startingPoint ( L , S1 , S2 ) ; cout << " Met ▁ first ▁ time ▁ after ▁ " << first_Time << " ▁ hrs " << endl ; cout << " Met ▁ at ▁ starting ▁ point ▁ after ▁ " << starting_Point << " ▁ hrs " << endl ; return 0 ; }
Check if the array has an element which is equal to product of remaining elements | C ++ implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckArray ( int arr [ ] , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) prod *= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] == prod / arr [ i ] ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( CheckArray ( arr , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Sum of common divisors of two numbers A and B | C ++ implementation of above approach ; print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int a , int b ) { int sum = 0 ; for ( int i = 1 ; i <= min ( a , b ) ; i ++ ) if ( a % i == 0 && b % i == 0 ) sum += i ; return sum ; } int main ( ) { int A = 10 , B = 15 ; cout << " Sum ▁ = ▁ " << sum ( A , B ) << endl ; return 0 ; }
Minimum number of cuts required to make circle segments equal sized | CPP program to find the minimum number of additional cuts required to make circle segments equal sized ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumCuts ( int a [ ] , int n ) { sort ( a , a + n ) ; int gcd = a [ 1 ] - a [ 0 ] ; int s = gcd ; for ( int i = 2 ; i < n ; i ++ ) { gcd = __gcd ( gcd , a [ i ] - a [ i - 1 ] ) ; s += a [ i ] - a [ i - 1 ] ; } if ( 360 - s > 0 ) gcd = __gcd ( gcd , 360 - s ) ; return ( 360 / gcd ) - n ; } int main ( ) { int arr [ ] = { 30 , 60 , 180 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumCuts ( arr , n ) ; return 0 ; }
Find a number that divides maximum array elements | CPP program to find a number that divides maximum array elements ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Function to find a number that divides maximum array elements ; precalculating Smallest Prime Factor ; Hash to store frequency of each divisors ; Traverse the array and get spf of each element ; calling getFactorization function ; Returns Set view ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  100001 NEW_LINE int spf [ MAXN ] ; void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } vector < int > getFactorization ( int x ) { vector < int > ret ; while ( x != 1 ) { int temp = spf [ x ] ; ret . push_back ( temp ) ; while ( x % temp == 0 ) x = x / temp ; } return ret ; } int maxElement ( int A [ ] , int n ) { sieve ( ) ; map < int , int > m ; for ( int i = 0 ; i < n ; ++ i ) { vector < int > p = getFactorization ( A [ i ] ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) m [ p [ i ] ] ++ ; } int cnt = 0 , ans = 1e+7 ; for ( auto i : m ) { if ( i . second >= cnt ) { cnt = i . second ; ans > i . first ? ans = i . first : ans = ans ; } } return ans ; } int main ( ) { int A [ ] = { 2 , 5 , 10 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << maxElement ( A , n ) ; return 0 ; }
Find Selling Price from given Profit Percentage and Cost | C ++ implementation of above approach ; Function to calculate the Selling Price ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Driver code ; Get the CP and Profit % ; Printing the returned value
#include <iostream> NEW_LINE using namespace std ; float SellingPrice ( float CP , float PP ) { float P_decimal = 1 + ( PP / 100 ) ; float res = P_decimal * CP ; return res ; } int main ( ) { float C = 720 , P = 13 ; cout << SellingPrice ( C , P ) ; return 0 ; }
Sum and product of k smallest and k largest composite numbers in the array | C ++ program to find the sum and product of k smallest and k largest composite numbers in an array ; Create a boolean vector " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function that calculates the sum and product of k smallest and k largest composite numbers in an array ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val ; Set 0 and 1 as primes so that they don 't get counted as   composite numbers ; Max Heap to store all the composite numbers ; Min Heap to store all the composite numbers ; Push all the composite numbers from the array to the heaps ; Calculate the products ; Calculate the sum ; Pop the current minimum element ; Pop the current maximum element ; Print results ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < bool > SieveOfEratosthenes ( int max_val ) { vector < bool > prime ( max_val + 1 , true ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } return prime ; } void compositeSumAndProduct ( int arr [ ] , int n , int k ) { int max_val = * max_element ( arr , arr + n ) ; vector < bool > prime = SieveOfEratosthenes ( max_val ) ; prime [ 0 ] = true ; prime [ 1 ] = true ; priority_queue < int > maxHeap ; priority_queue < int , vector < int > , greater < int > > minHeap ; for ( int i = 0 ; i < n ; i ++ ) if ( ! prime [ arr [ i ] ] ) { minHeap . push ( arr [ i ] ) ; maxHeap . push ( arr [ i ] ) ; } long long int minProduct = 1 , maxProduct = 1 , minSum = 0 , maxSum = 0 ; while ( k -- ) { minProduct *= minHeap . top ( ) ; maxProduct *= maxHeap . top ( ) ; minSum += minHeap . top ( ) ; maxSum += maxHeap . top ( ) ; minHeap . pop ( ) ; maxHeap . pop ( ) ; } cout << " Sum ▁ of ▁ k - minimum ▁ composite ▁ numbers ▁ is ▁ " << minSum << " STRNEWLINE " ; cout << " Sum ▁ of ▁ k - maximum ▁ composite ▁ numbers ▁ is ▁ " << maxSum << " STRNEWLINE " ; cout << " Product ▁ of ▁ k - minimum ▁ composite ▁ numbers ▁ is ▁ " << minProduct << " STRNEWLINE " ; cout << " Product ▁ of ▁ k - maximum ▁ composite ▁ numbers ▁ is ▁ " << maxProduct ; } int main ( ) { int arr [ ] = { 4 , 2 , 12 , 13 , 5 , 19 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; compositeSumAndProduct ( arr , n , k ) ; return 0 ; }
Product of all the Composite Numbers in an array | C ++ program to find the product of all the composite numbers in an array ; Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int compositeProduct ( int arr [ ] , int n ) { int max_val = * max_element ( arr , arr + n ) ; vector < bool > prime ( max_val + 1 , true ) ; prime [ 0 ] = true ; prime [ 1 ] = true ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( ! prime [ arr [ i ] ] ) { product *= arr [ i ] ; } return product ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << compositeProduct ( arr , n ) ; return 0 ; }
Primality test for the sum of digits at odd places of a number | C ++ program to do Primality test for the sum of digits at odd places of a number ; Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_odd ( int n ) { int sum = 0 , pos = 1 ; while ( n ) { if ( pos % 2 == 1 ) sum += n % 10 ; n = n / 10 ; pos ++ ; } return sum ; } bool check_prime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int main ( ) { int n = 223 ; int sum = sum_odd ( n ) ; if ( check_prime ( sum ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Check if a number is a Mystery Number | C ++ implementation of above approach ; Finds reverse of given num x . ; if found print the pair , return
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverseNum ( int x ) { string s = to_string ( x ) ; reverse ( s . begin ( ) , s . end ( ) ) ; stringstream ss ( s ) ; int rev = 0 ; ss >> rev ; return rev ; } bool isMysteryNumber ( int n ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) { int j = reverseNum ( i ) ; if ( i + j == n ) { cout << i << " ▁ " << j ; return true ; } } cout << " Not ▁ a ▁ Mystery ▁ Number " ; return false ; } int main ( ) { int n = 121 ; isMysteryNumber ( n ) ; return 0 ; }
Replace every element of the array by product of all other elements | C ++ program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver code ; Print the modified array .
#include " iostream " NEW_LINE using namespace std ; void ReplaceElements ( int arr [ ] , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) { prod *= arr [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { arr [ i ] = prod / arr [ i ] ; } } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 5 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { cout << arr [ i ] << " ▁ " ; } return 0 ; }
Check if there is any pair in a given range with GCD is divisible by k |  ; function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Check_is_possible ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( i % k == 0 ) count ++ ; } return ( count > 1 ) ; } int main ( ) { int l = 4 , r = 12 ; int k = 5 ; if ( Check_is_possible ( l , r , k ) ) cout << " YES STRNEWLINE " ; else cout << " NO STRNEWLINE " ; return 0 ; }
Check if any permutation of N equals any power of K | CPP implementation of above approach ; function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; generate all power of K under 10 ^ 18 ; check if any power of K is valid ; Driver program ; function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( long long int N , long long int K ) { multiset < int > m1 , m2 ; while ( N > 0 ) { m1 . insert ( N % 10 ) ; N /= 10 ; } while ( K > 0 ) { m2 . insert ( K % 10 ) ; K /= 10 ; } if ( m1 == m2 ) return true ; return false ; } string anyPermutation ( long long int N , long long int K ) { long long int powK [ 100 ] , Limit = pow ( 10 , 18 ) ; powK [ 0 ] = K ; int i = 1 ; while ( powK [ i - 1 ] * K < Limit ) { powK [ i ] = powK [ i - 1 ] * K ; i ++ ; } for ( int j = 0 ; j < i ; j ++ ) if ( isValid ( N , powK [ j ] ) ) { return " True " ; } return " False " ; } int main ( ) { long long int N = 96889010407 , K = 7 ; cout << anyPermutation ( N , K ) ; return 0 ; }
Sum of first N natural numbers which are divisible by 2 and 7 | C ++ program to find sum of numbers from 1 to N which are divisible by 2 or 7 ; Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int N ) { int S1 , S2 , S3 ; S1 = ( ( N / 2 ) ) * ( 2 * 2 + ( N / 2 - 1 ) * 2 ) / 2 ; S2 = ( ( N / 7 ) ) * ( 2 * 7 + ( N / 7 - 1 ) * 7 ) / 2 ; S3 = ( ( N / 14 ) ) * ( 2 * 14 + ( N / 14 - 1 ) * 14 ) / 2 ; return S1 + S2 - S3 ; } int main ( ) { int N = 20 ; cout << sum ( N ) ; return 0 ; }
Ways to color a skewed tree such that parent and child have different colors | C ++ program to count number of ways to color a N node skewed tree with k colors such that parent and children have different colors . ; fast_way is recursive method to calculate power ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fastPow ( int N , int K ) { if ( K == 0 ) return 1 ; int temp = fastPow ( N , K / 2 ) ; if ( K % 2 == 0 ) return temp * temp ; else return N * temp * temp ; } int countWays ( int N , int K ) { return K * fastPow ( K - 1 , N - 1 ) ; } int main ( ) { int N = 3 , K = 3 ; cout << countWays ( N , K ) ; return 0 ; }
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | CPP program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; Driver code ; m is the size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumNth ( int A [ ] , int B [ ] , int m , int n ) { int res = 0 ; if ( n == 1 ) { for ( int i = 0 ; i < m ; i ++ ) res = res + A [ i ] ; } else if ( n == 2 ) { for ( int i = 0 ; i < m ; i ++ ) res = res + B [ i ] * m ; } else { int f [ n ] ; f [ 0 ] = 0 , f [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; for ( int i = 0 ; i < m ; i ++ ) { res = res + ( m * ( B [ i ] * f [ n - 1 ] ) ) + ( m * ( A [ i ] * f [ n - 2 ] ) ) ; } } return res ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 4 , 5 , 6 } ; int n = 3 ; int m = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << sumNth ( A , B , m , n ) ; return 0 ; }
Puzzle | Minimum distance for Lizard | CPP program to find minimum distance to be travlled by lizard . ; side of cube ; understand from diagram ; understand from diagram ; minimum distance
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; int main ( ) { ll a = 5 ; ll AC = a ; ll CE = 2 * a ; double shortestDistace = sqrt ( AC * AC + CE * CE ) ; cout << shortestDistace << endl ; return 0 ; }
Find Sum of Series 1 ^ 2 | C ++ Program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... ; Function to find sum of series ; If n is even ; If n is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_of_series ( int n ) { int result = 0 ; if ( n % 2 == 0 ) { result = - ( n * ( n + 1 ) ) / 2 ; } else { result = ( n * ( n + 1 ) ) / 2 ; } return result ; } int main ( void ) { int n = 3 ; cout << sum_of_series ( n ) << endl ; n = 10 ; cout << sum_of_series ( n ) << endl ; }
Check whether the given numbers are Cousin prime or not | CPP program to check Cousin prime ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are Cousin primes ; Check if they differ by 4 or not ; Check if both are prime number or not ; Driver code ; Get the 2 numbers ; Check the numbers for cousin prime
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } bool isCousinPrime ( int n1 , int n2 ) { if ( abs ( n1 - n2 ) != 4 ) return false ; else return ( isPrime ( n1 ) && isPrime ( n2 ) ) ; } int main ( ) { int n1 = 7 , n2 = 11 ; if ( isCousinPrime ( n1 , n2 ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Chen Prime Number | CPP program to check Chen prime number ; Utility function to check whether number is semiprime or not ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Utility function to check whether the given number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check Chen prime number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) num /= I ; ++ cnt ; if ( num > 1 ) ++ cnt ; return cnt == 2 ; } bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } bool isChenPrime ( int n ) { if ( isPrime ( n ) && ( isSemiprime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } int main ( ) { int n = 7 ; if ( isChenPrime ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Thabit number | CPP program to check if a given number is Thabit number or not . ; Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Program ; Check if number is thabit number
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int n ) { return ( n && ! ( n & ( n - 1 ) ) ) ; } bool isThabitNumber ( int n ) { n = n + 1 ; if ( n % 3 == 0 ) n = n / 3 ; else return false ; if ( isPowerOfTwo ( n ) ) return true ; else return false ; } int main ( ) { int n = 47 ; if ( isThabitNumber ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Sum of all the prime numbers in a given range | C ++ Program to compute sum of prime number in a given range ; Suppose the constraint is N <= 1000 ; Declare an array for dynamic approach ; Method to compute the array ; Declare an extra array as arr ; Iterate the loop till sqrt ( n ) Time Complexity is O ( log ( n ) X sqrt ( n ) ) ; if ith element of arr is 0 i . e . marked as prime ; mark all of it 's multiples till N as non-prime  by setting the locations to 1 ; Update the array ' dp ' with the running sum of prime numbers within the range [ 1 , N ] Time Complexity is O ( n ) ; Here , dp [ i ] is the sum of all the prime numbers within the range [ 1 , i ] ; Driver code ; Compute dp
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1000 ; int dp [ N + 1 ] ; void sieve ( ) { int arr [ N + 1 ] ; arr [ 0 ] = 1 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) if ( arr [ i ] == 0 ) for ( int j = i * i ; j <= N ; j += i ) arr [ j ] = 1 ; long runningPrimeSum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] == 0 ) runningPrimeSum += i ; dp [ i ] = runningPrimeSum ; } } int main ( ) { int l = 4 , r = 13 ; sieve ( ) ; cout << dp [ r ] - dp [ l - 1 ] ; return 0 ; }
Smallest Integer to be inserted to have equal sums | C ++ program to find the smallest number to be added to make the sum of left and right subarrays equal ; Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinEqualSums ( int a [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } int sum1 = 0 , sum2 = 0 ; int min = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += a [ i ] ; sum2 = sum - sum1 ; if ( abs ( sum1 - sum2 ) < min ) { min = abs ( sum1 - sum2 ) ; } if ( min == 0 ) { break ; } } return min ; } int main ( ) { int a [ ] = { 3 , 2 , 1 , 5 , 7 , 8 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << ( findMinEqualSums ( a , N ) ) ; }