codeparrot/xlcost-text-to-code huggingface dataset:
Count of subtrees in a Binary Tree having XOR value K | C ++ program to find the count of subtrees in a Binary Tree having XOR value K ; A binary tree node ; A utility function to allocate a new node ; Base Case : If node is NULL , return 0 ; Calculating the XOR of the current subtree ; Increment res if xr is equal to k ; Return the XOR value of the current subtree ; Function to find the required count ; Initialize result variable ' res ' ; Recursively traverse the tree and compute the count ; return the count ' res ' ; Driver program ; Create the binary tree by adding nodes to it
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return ( newNode ) ; } int rec ( Node * root , int & res , int & k ) { if ( root == NULL ) { return 0 ; } int xr = root -> data ; xr ^= rec ( root -> left , res , k ) ; xr ^= rec ( root -> right , res , k ) ; if ( xr == k ) { res ++ ; } return xr ; } int findCount ( Node * root , int K ) { int res = 0 ; rec ( root , res , K ) ; return res ; } int main ( void ) { struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 1 ) ; root -> right = newNode ( 9 ) ; root -> left -> left = newNode ( 10 ) ; root -> left -> right = newNode ( 5 ) ; int K = 5 ; cout << findCount ( root , K ) ; return 0 ; }
Make array elements equal with minimum cost | C ++ implementation to make array elements equal with minimum cost ; Function to find the minimum cost required to make array elements equal ; Loop to find the count of odd elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makearrayequal ( int arr [ ] , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x += arr [ i ] & 1 ; } cout << min ( x , n - x ) << endl ; } int main ( ) { int arr [ ] = { 4 , 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; makearrayequal ( arr , n ) ; return 0 ; }
Count of Prime digits in a Number | C ++ program to count the number of prime digits in a number ; Function to find the count of prime digits in a number ; Loop to compute all the digits of the number ; Finding every digit of the given number ; Checking if digit is prime or not Only 2 , 3 , 5 and 7 are prime one - digit number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigit ( int n ) { int temp = n , count = 0 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d == 2 d == 3 d == 5 d == 7 ) count ++ ; } return count ; } int main ( ) { int n = 1234567890 ; cout << countDigit ( n ) << endl ; return 0 ; }
Check if an Array is a permutation of numbers from 1 to N : Set 2 | C ++ program to decide if an array represents a permutation or not ; Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string permutation ( int arr [ ] , int N ) { int hash [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " No " ; } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 1 , 5 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << permutation ( arr , n ) << endl ; return 0 ; }
Check if the XOR of an array of integers is Even or Odd | C ++ program to check if the XOR of an array is Even or Odd ; Function to check if the XOR of an array of integers is Even or Odd ; Count the number of odd elements ; If count of odd elements is odd , then XOR will be odd ; Else even ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) count ++ ; } if ( count & 1 ) return " Odd " ; else return " Even " ; } int main ( ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << check ( arr , n ) << endl ; return 0 ; }
Length of Longest Prime Subsequence in an Array | C ++ program to find the length of Longest Prime Subsequence in an Array ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find the longest subsequence which contain all prime numbers ; Precompute N primes ; Find the length of longest prime subsequence ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE void SieveOfEratosthenes ( bool prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } int longestPrimeSubsequence ( int arr [ ] , int n ) { bool prime [ N + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , N ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { answer ++ ; } } return answer ; } int main ( ) { int arr [ ] = { 3 , 4 , 11 , 2 , 9 , 21 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestPrimeSubsequence ( arr , n ) << endl ; return 0 ; }
Check if an Octal number is Even or Odd | C ++ code to check if a Octal number is Even or Odd ; Check if the number is odd or even ; Check if the last digit is either '0' , '2' , '4' , or '6' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string even_or_odd ( string N ) { int len = N . size ( ) ; if ( N [ len - 1 ] == '0' N [ len - 1 ] == '2' N [ len - 1 ] == '4' N [ len - 1 ] == '6' ) return ( " Even " ) ; else return ( " Odd " ) ; } int main ( ) { string N = "735" ; cout << even_or_odd ( N ) ; return 0 ; }
Runge | C ++ program to implement Runge Kutta method ; A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 . ; Count number of iterations using step size or step height h ; Iterate for number of iterations ; Apply Runge Kutta Formulas to find next value of y ; Update next value of y ; Update next value of x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float dydx ( float x , float y ) { return ( x + y - 2 ) ; } float rungeKutta ( float x0 , float y0 , float x , float h ) { int n = ( int ) ( ( x - x0 ) / h ) ; float k1 , k2 ; float y = y0 ; for ( int i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; y = y + ( 1.0 / 6.0 ) * ( k1 + 2 * k2 ) ; x0 = x0 + h ; } return y ; } int main ( ) { float x0 = 0 , y = 1 , x = 2 , h = 0.2 ; cout << fixed << setprecision ( 6 ) << " y ( x ) ▁ = ▁ " << rungeKutta ( x0 , y , x , h ) ; return 0 ; }
Count of Fibonacci pairs which satisfy the given equation | C ++ program to find the count of Fibonacci pairs ( x , y ) which satisfy the equation Ax + By = N ; Array to store the Fibonacci numbers ; Array to store the number of ordered pairs ; Function to find if a number is a perfect square ; Function that returns 1 if N is non - fibonacci number else 0 ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to store the fibonacci numbers and their frequency in form a * x + b * y ; Storing the Fibonacci numbers ; For loop to find all the possible combinations of the Fibonacci numbers ; Finding the number of ordered pairs ; Driver code ; Find the ordered pair for every query
#include <bits/stdc++.h> NEW_LINE #define size  10001 NEW_LINE using namespace std ; long long fib [ 100010 ] ; int freq [ 100010 ] ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } int isFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 1 ; return 0 ; } void compute ( int a , int b ) { for ( int i = 1 ; i < 100010 ; i ++ ) { fib [ i ] = isFibonacci ( i ) ; } for ( int x = 1 ; x < 100010 ; x ++ ) { for ( int y = 1 ; y < size ; y ++ ) { if ( fib [ x ] == 1 && fib [ y ] == 1 && a * x + b * y < 100010 ) { freq [ a * x + b * y ] ++ ; } } } } int main ( ) { int Q = 2 , A = 5 , B = 10 ; compute ( A , B ) ; int arr [ Q ] = { 50 , 150 } ; for ( int i = 0 ; i < Q ; i ++ ) { cout << freq [ arr [ i ] ] << " ▁ " ; } return 0 ; }
Maximise the sum of two Numbers using at most one swap between them | C ++ program to maximise the sum of two Numbers using at most one swap between them ; Function to maximize the sum by swapping only one digit ; Store digits of max ( n1 , n2 ) ; Store digits of min ( n1 , n2 ) ; If length of the two numbers are unequal ; Find the most significant number and the most significant index for swapping ; If both numbers are of equal length ; Fetch the index of current maximum digit present in both the arrays ; Compute the difference ; Find the most significant index and the most significant digit to be swapped ; Restore the new numbers ; Display the maximized sum ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void findMaxSum ( int n1 , int n2 ) { int arr1 [ MAX ] = { 0 } , arr2 [ MAX ] = { 0 } ; int l1 = 0 , l2 = 0 ; int max1 = max ( n1 , n2 ) ; int min1 = min ( n1 , n2 ) ; for ( int i = max1 ; i > 0 ; i /= 10 ) arr1 [ l1 ++ ] = ( i % 10 ) ; for ( int i = min1 ; i > 0 ; i /= 10 ) arr2 [ l2 ++ ] = ( i % 10 ) ; int f = 0 ; if ( l1 != l2 ) { int index = ( max_element ( arr2 , arr2 + l2 ) - arr2 ) ; for ( int i = l1 - 1 ; i > ( l2 - 1 ) ; i -- ) { if ( arr1 [ i ] < arr2 [ index ] ) { swap ( arr1 [ i ] , arr2 [ index ] ) ; f = 1 ; break ; } } } if ( f != 1 ) { int index1 = 0 , index2 = 0 ; int diff1 = 0 , diff2 = 0 ; for ( int i = l2 - 1 ; i >= 0 ; i -- ) { index1 = ( max_element ( arr1 , arr1 + i ) - arr1 ) ; index2 = ( max_element ( arr2 , arr2 + i ) - arr2 ) ; diff1 = ( arr2 [ index2 ] - arr1 [ i ] ) ; diff2 = ( arr1 [ index1 ] - arr2 [ i ] ) ; if ( diff1 > 0 diff2 > 0 ) { if ( diff1 > diff2 ) { swap ( arr1 [ i ] , arr2 [ index2 ] ) ; break ; } else if ( diff2 > diff1 ) { swap ( arr2 [ i ] , arr1 [ index1 ] ) ; break ; } else if ( diff1 == diff2 ) { if ( index1 <= index2 ) { swap ( arr2 [ i ] , arr1 [ index1 ] ) ; break ; } else if ( index2 <= index1 ) { swap ( arr1 [ i ] , arr2 [ index2 ] ) ; break ; } } } } } int f_n1 = 0 , f_n2 = 0 ; for ( int i = l1 - 1 ; i >= 0 ; i -- ) { f_n1 = ( f_n1 * 10 ) + arr1 [ i ] ; f_n2 = ( f_n2 * 10 ) + arr2 [ i ] ; } cout << ( f_n1 + f_n2 ) << " STRNEWLINE " ; } int main ( ) { int N1 = 9987 ; int N2 = 123 ; findMaxSum ( N1 , N2 ) ; return 0 ; }
Check if a number is divisible by 47 or not | C ++ program to check whether a number is divisible by 47 or not ; Function to check if the number is divisible by 47 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting fourteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 47 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdlib.h> NEW_LINE using namespace std ; bool isDivisible ( int n ) { int d ; while ( n / 100 ) { d = n % 10 ; n /= 10 ; n = abs ( n - ( d * 14 ) ) ; } return ( n % 47 == 0 ) ; } int main ( ) { int N = 59173 ; if ( isDivisible ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Check if a HexaDecimal number is Even or Odd | C ++ code to check if a HexaDecimal number is Even or Odd ; Check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' A ' ( = 10 ) , ' C ' ( = 12 ) or ' E ' ( = 14 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string even_or_odd ( string N ) { int len = N . size ( ) ; if ( N [ len - 1 ] == '0' N [ len - 1 ] == '2' N [ len - 1 ] == '4' N [ len - 1 ] == '6' N [ len - 1 ] == '8' N [ len - 1 ] == ' A ' N [ len - 1 ] == ' C ' N [ len - 1 ] == ' E ' ) return ( " Even " ) ; else return ( " Odd " ) ; } int main ( ) { string N = " AB3454D " ; cout << even_or_odd ( N ) ; return 0 ; }
Check if a number is divisible by 31 or not | C ++ program to check whether a number is divisible by 31 or not ; Function to check if the number is divisible by 31 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting three times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 31 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdlib.h> NEW_LINE using namespace std ; bool isDivisible ( int n ) { int d ; while ( n / 100 ) { d = n % 10 ; n /= 10 ; n = abs ( n - ( d * 3 ) ) ; } return ( n % 31 == 0 ) ; } int main ( ) { int N = 1922 ; if ( isDivisible ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Check if the number is divisible 43 or not | C ++ program to check whether a number is divisible by 43 or not ; Function to check if the number is divisible by 43 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; adding thirteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdlib.h> NEW_LINE using namespace std ; bool isDivisible ( int n ) { int d ; while ( n / 100 ) { d = n % 10 ; n /= 10 ; n = abs ( n + ( d * 13 ) ) ; } return ( n % 43 == 0 ) ; } int main ( ) { int N = 2795 ; if ( isDivisible ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Find the next Non | C ++ implementation of the approach ; Function to check if a number is perfect square ; Function to check if a number is Fibinacci Number ; N is Fibinacci if either ( 5 * N * N + 4 ) , ( 5 * N * N - 4 ) or both is a perferct square ; Function to find the next Non - Fibinacci Number ; Case 1 If N <= 3 , then 4 will be next Non - Fibinacci Number ; Case 2 If N + 1 is Fibinacci , then N + 2 will be next Non - Fibinacci Number ; If N + 1 is Non - Fibinacci , then N + 2 will be next Non - Fibinacci Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } bool isFibonacci ( int N ) { return isPerfectSquare ( 5 * N * N + 4 ) || isPerfectSquare ( 5 * N * N - 4 ) ; } int nextNonFibonacci ( int N ) { if ( N <= 3 ) return 4 ; if ( isFibonacci ( N + 1 ) ) return N + 2 ; else return N + 1 ; } int main ( ) { int N = 3 ; cout << nextNonFibonacci ( N ) << endl ; N = 5 ; cout << nextNonFibonacci ( N ) << endl ; N = 7 ; cout << nextNonFibonacci ( N ) << endl ; }
Represent K ^ N as the sum of exactly N numbers | C ++ program to represent K ^ N as the sum of exactly N numbers ; Function to print N numbers whose sum is a power of K ; Printing K ^ 1 ; Loop to print the difference of powers from K ^ 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; void print ( ll n , ll k ) { cout << k << " ▁ " ; for ( int i = 2 ; i <= n ; i ++ ) { ll x = pow ( k , i ) - pow ( k , i - 1 ) ; cout << x << " ▁ " ; } } int main ( ) { ll N = 3 , K = 4 ; print ( N , K ) ; return 0 ; }
Check if the given number is divisible by 71 or not | C ++ program to check whether a number is divisible by 71 or not ; Function to check if the number is divisible by 71 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting seven times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 71 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdlib.h> NEW_LINE using namespace std ; bool isDivisible ( int n ) { int d ; while ( n / 100 ) { d = n % 10 ; n /= 10 ; n = abs ( n - ( d * 7 ) ) ; } return ( n % 71 == 0 ) ; } int main ( ) { int N = 5041 ; if ( isDivisible ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Count of prime digits of a Number which divides the number | C ++ program to count number of digits which is prime and also divides number ; Function to find the number of digits in number which divides the number and is also a prime number ; Only 2 , 3 , 5 and 7 are prime one - digit number ; Loop to compute all the digits of the number untill it is not equal to the zero ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n and is prime too ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigit ( int n ) { bool prime [ 10 ] ; memset ( prime , false , sizeof ( prime ) ) ; prime [ 2 ] = prime [ 3 ] = true ; prime [ 5 ] = prime [ 7 ] = true ; int temp = n , count = 0 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 && prime [ d ] ) count ++ ; } return count ; } int main ( ) { int n = 1032 ; cout << countDigit ( n ) << endl ; return 0 ; }
Bitwise Operations on Digits of a Number | C ++ implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit [ 100000 ] ; int findDigits ( int n ) { int count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } int OR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } int AND_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } int XOR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } void bitwise_operation ( int N ) { int countOfDigit = findDigits ( N ) ; cout << " XOR ▁ = ▁ " << XOR_of_Digits ( N , countOfDigit ) << endl ; cout << " OR ▁ = ▁ " << OR_of_Digits ( N , countOfDigit ) << endl ; cout << " AND ▁ = ▁ " << AND_of_Digits ( N , countOfDigit ) << endl ; } int main ( ) { int N = 123456 ; bitwise_operation ( N ) ; return 0 ; }
Length of longest subarray with product greater than or equal to 0 | C ++ implementation of the above approach ; Function that count the length of longest subarray with product greater than or equals to zero ; If product is greater than zero , return array size ; Traverse the array and if any negative element found then update the length of longest subarray with the length of left and right subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( int arr [ ] , int N ) { int product = 1 , len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } if ( product >= 0 ) { return N ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { len = max ( len , max ( N - i - 1 , i ) ) ; } } return len ; } int main ( ) { int arr [ ] = { -1 , 1 , 1 , -2 , 3 , 2 , -1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxLength ( arr , N ) << endl ; return 0 ; }
Print a pair of numbers with the given Sum and Product | CPP program to find any pair which has sum S and product P . ; Prints roots of quadratic equation ax * 2 + bx + c = 0 ; calculating the sq root value for b * b - 4 * a * c ; Finding the roots ; Check if the roots are valid or not ; Finding the roots ; Check if the roots are valid or not ; when d < 0 ; No such pair exists in this case ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRoots ( int b , int c ) { int a = 1 ; int d = b * b - 4 * a * c ; double sqrt_val = sqrt ( abs ( d ) ) ; if ( d > 0 ) { double x = - b + sqrt_val ; double y = - b - sqrt_val ; int root1 = ( x ) / ( 2 * a ) ; int root2 = ( y ) / ( 2 * a ) ; if ( root1 + root2 == -1 * b && root1 * root2 == c ) cout << root1 << " , ▁ " << root2 ; else cout << -1 ; } else if ( d == 0 ) { int root = - b / ( 2 * a ) ; if ( root + root == -1 * b && root * root == c ) cout << root << " , ▁ " << root ; else cout << -1 ; } else { cout << -1 ; } cout << endl ; } int main ( ) { int S = 5 , P = 6 ; findRoots ( - S , P ) ; S = 5 , P = 9 ; findRoots ( - S , P ) ; return 0 ; }
Print N numbers such that their product is a Perfect Cube | C ++ program to find N numbers such that their product is a perfect cube ; Function to find the N numbers such that their product is a perfect cube ; Loop to traverse each number from 1 to N ; Print the cube of i as the ith term of the output ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int N ) { int i = 1 ; while ( i <= N ) { cout << ( i * i * i ) << " ▁ " ; i ++ ; } } int main ( ) { int N = 4 ; findNumbers ( N ) ; }
Sum of all proper divisors from 1 to N | C ++ implementation to find sum of all proper divisor of number up to N ; Utility function to find sum of all proper divisor of number up to N ; Loop to iterate over all the numbers from 1 to N ; Find all divisors of i and add them ; Subtracting ' i ' so that the number itself is not included ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int properDivisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } sum = sum - i ; } return sum ; } int main ( ) { int n = 4 ; cout << properDivisorSum ( n ) << endl ; n = 5 ; cout << properDivisorSum ( n ) << endl ; return 0 ; }
Sum of all proper divisors from 1 to N | C ++ implementation to find sum of all proper divisor of numbers up to N ; Utility function to find sum of all proper divisor of number up to N ; Loop to find the proper divisor of every number from 1 to N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int properDivisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum - n * ( n + 1 ) / 2 ; } int main ( ) { int n = 4 ; cout << properDivisorSum ( n ) << endl ; n = 5 ; cout << properDivisorSum ( n ) << endl ; return 0 ; }
Minimum change in given value so that it lies in all given Ranges | C ++ implementation find the minimum difference in the number D such that D is inside of every range ; Function to find the minimum difference in the number D such that D is inside of every range ; Loop to find the common range out of the given array of ranges . ; Storing the start and end index ; Sorting the range ; Finding the maximum starting value of common segment ; Finding the minimum ending value of common segment ; If there is no common segment ; If the given number is between the computed common range . ; Finding the minimum distance ; Driver Code ; Given array of ranges
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumOperation ( int n , int d , int arrays [ 3 ] [ 2 ] ) { int cnt = 0 ; int first = INT_MIN , end = INT_MAX ; while ( n -- ) { int arr [ 2 ] = { arrays [ cnt ] [ 0 ] , arrays [ cnt ] [ 1 ] } ; sort ( arr , arr + 2 ) ; first = max ( first , arr [ 0 ] ) ; end = min ( end , arr [ 1 ] ) ; cnt ++ ; } if ( first > end ) cout << " - 1" ; else { if ( d >= first && d <= end ) { cout << "0" ; } else cout << min ( abs ( first - d ) , abs ( d - end ) ) ; } } int main ( ) { int n = 3 , d = 3 ; int arrays [ 3 ] [ 2 ] = { { 0 , 7 } , { 2 , 14 } , { 4 , 6 } } ; findMinimumOperation ( n , d , arrays ) ; }