codeparrot/xlcost-text-to-code huggingface dataset:
Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour | C ++ implementation of the approach ; Node for a Linked List ; Function to find the last 2 survivors ; Total is the count of alive people ; Initiating the list of n people ; Total != 2 is terminating condition because at last only two - person will remain alive ; Del represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int val ; struct Node * next ; Node ( int _val ) { val = _val ; next = NULL ; } } ; void getLastTwoPerson ( int n ) { int total = n ; struct Node * head = new Node ( 1 ) ; struct Node * temp = head ; for ( int i = 2 ; i <= n ; i ++ ) { temp -> next = new Node ( i ) ; temp = temp -> next ; } temp -> next = head ; temp = head ; struct Node * del ; while ( total != 2 ) { del = temp -> next -> next ; temp -> next -> next = temp -> next -> next -> next ; temp = temp -> next ; free ( del ) ; total -= 1 ; } cout << temp -> val << " ▁ " << temp -> next -> val ; } int main ( ) { int n = 2 ; getLastTwoPerson ( n ) ; return 0 ; }
Philaland Coin | TCS Mockvita 2020 | C ++ implementation to find the minimum number of denominations required for any number ; Function to find the minimum number of denomminations required ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDenomin ( int n ) { return log2 ( n ) + 1 ; } int main ( ) { int n = 10 ; cout << findMinDenomin ( n ) ; return 0 ; }
Content of a Polynomial | C ++ implementation to find the content of the polynomial ; Function to find the content of the polynomial ; Loop to iterate over the elements of the array ; __gcd ( a , b ) is a inbuilt function for Greatest Common Divisor ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define newl  " NEW_LINE " #define ll  long long NEW_LINE #define pb  push_back NEW_LINE int findContent ( int arr [ ] , int n ) { int content = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { content = __gcd ( content , arr [ i ] ) ; } return content ; } int main ( ) { int n = 3 ; int arr [ ] = { 9 , 6 , 12 } ; cout << findContent ( arr , n ) ; return 0 ; }
Check if the given array is same as its inverse permutation | C ++ Program to implement the above approach ; Function to check if the inverse permutation of the given array is same as the original array ; Stores the inverse permutation ; Generate the inverse permutation ; Check if the inverse permutation is same as the given array ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void inverseEqual ( int arr [ ] , int n ) { int brr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int present_index = arr [ i ] - 1 ; brr [ present_index ] = i + 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != brr [ i ] ) { cout << " No " << endl ; return ; } } cout << " Yes " << endl ; } int main ( ) { int n = 4 ; int arr [ n ] = { 1 , 4 , 3 , 2 } ; inverseEqual ( arr , n ) ; return 0 ; }
Check if the given array is same as its inverse permutation | C ++ Program to implement the above approach ; Function to check if the inverse permutation of the given array is same as the original array ; Check the if inverse permutation is not same ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool inverseEqual ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ arr [ i ] - 1 ] != i + 1 ) return false ; return true ; } int main ( ) { int n = 4 ; int arr [ n ] = { 1 , 4 , 3 , 2 } ; cout << ( inverseEqual ( arr , n ) ? " Yes " : " No " ) ; return 0 ; }
Linear Congruence method for generating Pseudo Random Numbers | C ++ implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the linear congruential method ; Driver Code ; Seed value ; Modulus parameter ; Multiplier term ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; void linearCongruentialMethod ( int Xo , int m , int a , int c , vector < int > & randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( ( randomNums [ i - 1 ] * a ) + c ) % m ; } } int main ( ) { int Xo = 5 ; int m = 7 ; int a = 3 ; int c = 3 ; int noOfRandomNums = 10 ; vector < int > randomNums ( noOfRandomNums ) ; linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { cout << randomNums [ i ] << " ▁ " ; } return 0 ; }
Multiplicative Congruence method for generating Pseudo Random Numbers | C ++ implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the multiplicative congruential method ; Driver Code ; seed value ; modulus parameter ; multiplier term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; void multiplicativeCongruentialMethod ( int Xo , int m , int a , vector < int > & randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] * a ) % m ; } } int main ( ) { int Xo = 3 ; int m = 15 ; int a = 7 ; int noOfRandomNums = 10 ; vector < int > randomNums ( noOfRandomNums ) ; multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { cout << randomNums [ i ] << " ▁ " ; } return 0 ; }
Product of divisors of a number from a given list of its prime factors | C ++ Program to implement the above approach ; Function to calculate ( a ^ b ) % m ; Function to calculate and return the product of divisors ; Stores the frequencies of prime divisors ; Iterate over the prime divisors ; Update the product ; Update the count of divisors ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MOD = 1000000007 ; int power ( int a , int b , int m ) { a %= m ; int res = 1 ; while ( b > 0 ) { if ( b & 1 ) res = ( ( res % m ) * ( a % m ) ) % m ; a = ( ( a % m ) * ( a % m ) ) % m ; b >>= 1 ; } return res % m ; } int productOfDivisors ( int p [ ] , int n ) { map < int , int > prime ; for ( int i = 0 ; i < n ; i ++ ) { prime [ p [ i ] ] ++ ; } int product = 1 , d = 1 ; for ( auto itr : prime ) { int val = power ( itr . first , ( itr . second ) * ( itr . second + 1 ) / 2 , MOD ) ; product = ( power ( product , itr . second + 1 , MOD ) * power ( val , d , MOD ) ) % MOD ; d = ( d * ( itr . second + 1 ) ) % ( MOD - 1 ) ; } return product ; } int main ( ) { int arr [ ] = { 11 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << productOfDivisors ( arr , n ) ; }
Print all proper fractions with denominators less than equal to N | C ++ program to implement the above approach ; Function to print all proper fractions ; If the numerator and the denominator are coprime ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFractions ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { if ( __gcd ( i , j ) == 1 ) { string a = to_string ( i ) ; string b = to_string ( j ) ; cout << a + " / " + b << " , ▁ " ; } } } } int main ( ) { int n = 3 ; printFractions ( n ) ; return 0 ; }
Maximum number of objects that can be created as per given conditions | C ++ program for the above problem ; Function for finding the maximum number of objects from N type - 1 and M type - 2 items ; storing minimum of N and M ; storing maximum number of objects from given items ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfObjects ( int N , int M ) { int initial = min ( N , M ) ; int final = ( N + M ) / 3 ; return min ( initial , final ) ; } int main ( ) { int N = 8 ; int M = 7 ; cout << numberOfObjects ( N , M ) << endl ; return 0 ; }
Square root of a number by Repeated Subtraction method | C ++ implementation of the above approach ; Function to return the square root of the given number ; Subtract n - th odd number ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SquareRoot ( int num ) { int count = 0 ; for ( int n = 1 ; n <= num ; n += 2 ) { num = num - n ; count += 1 ; if ( num == 0 ) break ; } return count ; } int main ( ) { int N = 81 ; cout << SquareRoot ( N ) ; }
Maximize count of distinct elements possible in an Array from the given operation | C ++ program to find the maximum possible distinct elements that can be obtained from the array by performing the given operations ; Function to find the gcd of two numbers ; Function to calculate and return the count of maximum possible distinct elements in the array ; Find the maximum element ; Base Case ; Finding the gcd of first two element ; Calculate Gcd of the array ; Return the total count of distinct elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int x , int y ) { if ( x == 0 ) return y ; return gcd ( y % x , x ) ; } int findDistinct ( int arr [ ] , int n ) { int maximum = * max_element ( arr , arr + n ) ; if ( n == 1 ) return 1 ; if ( n == 2 ) { return ( maximum / gcd ( arr [ 0 ] , arr [ 1 ] ) ) ; } int k = gcd ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { k = gcd ( k , arr [ i ] ) ; } return ( maximum / k ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findDistinct ( arr , n ) ; return 0 ; }
Find indices of all local maxima and local minima in an Array | C ++ program for the above approach ; Function to find all the local maxima and minima in the given array arr [ ] ; Empty vector to store points of local maxima and minima ; Checking whether the first point is local maxima or minima or none ; Iterating over all points to check local maxima and local minima ; Condition for local minima ; Condition for local maxima ; Checking whether the last point is local maxima or minima or none ; Print all the local maxima and local minima indexes stored ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLocalMaximaMinima ( int n , int arr [ ] ) { vector < int > mx , mn ; if ( arr [ 0 ] > arr [ 1 ] ) mx . push_back ( 0 ) ; else if ( arr [ 0 ] < arr [ 1 ] ) mn . push_back ( 0 ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] > arr [ i ] ) and ( arr [ i ] < arr [ i + 1 ] ) ) mn . push_back ( i ) ; else if ( ( arr [ i - 1 ] < arr [ i ] ) and ( arr [ i ] > arr [ i + 1 ] ) ) mx . push_back ( i ) ; } if ( arr [ n - 1 ] > arr [ n - 2 ] ) mx . push_back ( n - 1 ) ; else if ( arr [ n - 1 ] < arr [ n - 2 ] ) mn . push_back ( n - 1 ) ; if ( mx . size ( ) > 0 ) { cout << " Points ▁ of ▁ Local ▁ maxima ▁ are ▁ : ▁ " ; for ( int a : mx ) cout << a << " ▁ " ; cout << endl ; } else cout << " There ▁ are ▁ no ▁ points ▁ of ▁ " << " Local ▁ Maxima ▁ STRNEWLINE " ; if ( mn . size ( ) > 0 ) { cout << " Points ▁ of ▁ Local ▁ minima ▁ are ▁ : ▁ " ; for ( int a : mn ) cout << a << " ▁ " ; cout << endl ; } else cout << " There ▁ are ▁ no ▁ points ▁ of ▁ " << " Local ▁ Minima ▁ STRNEWLINE " ; } int main ( ) { int N = 9 ; int arr [ ] = { 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 } ; findLocalMaximaMinima ( N , arr ) ; return 0 ; }
Smallest N digit number with none of its digits as its divisor | C ++ Program for the given problem ; Function to calculate power ; Function to check if the N - digit number satisfies the given condition or not ; Getting the last digit ; Every number is divisible by 1 and dividing a number by 0 isn 't possible. Thus  numbers with 0 as a digit  must be discarded. ; If any digit divides the number , return false ; If no digit divides the number , return true ; ; Function to find the smallest number not divisible by any of its digits . ; Get the lower range of n digit number ; Get the high range of n digit number ; check all the N - digit numbers ; If the first number to satify the constraints is found print it , set the flag value and break out of the loop . ; If no such digit found , return - 1 as per problem statement ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int a , int b ) { if ( b == 0 ) return 1 ; if ( b == 1 ) return a ; int tmp = power ( a , b / 2 ) ; int result = tmp * tmp ; if ( b % 2 == 1 ) result *= a ; return result ; } bool check ( int n ) { int temp = n ; while ( temp > 0 ) { int last_digit = temp % 10 ; if ( last_digit == 0 last_digit == 1 ) return false ; if ( n % last_digit == 0 ) return false ; temp = temp / 10 ; } return true ; } void solve ( int n ) { int L = power ( 10 , n - 1 ) ; int R = power ( 10 , n ) - 1 ; int flag = 0 ; for ( int i = L ; i <= R ; i ++ ) { bool answer = check ( i ) ; if ( answer == true ) { cout << i << endl ; flag ++ ; break ; } } if ( flag == 0 ) cout << -1 << endl ; } int main ( ) { int N = 4 ; solve ( N ) ; }
C / C ++ program for Armstrong Numbers | C ++ program to find Armstrong number ; Function to calculate N raised to the power D ; Function to calculate the order of the number ; For each digit ; Function to check whether the given number is Armstrong number or not ; To find order of N ; Traverse each digit ; If satisfies Armstrong condition ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int N , unsigned int D ) { if ( D == 0 ) return 1 ; if ( D % 2 == 0 ) return power ( N , D / 2 ) * power ( N , D / 2 ) ; return N * power ( N , D / 2 ) * power ( N , D / 2 ) ; } int order ( int N ) { int r = 0 ; while ( N ) { r ++ ; N = N / 10 ; } return r ; } int isArmstrong ( int N ) { int D = order ( N ) ; int temp = N , sum = 0 ; while ( temp ) { int Ni = temp % 10 ; sum += power ( Ni , D ) ; temp = temp / 10 ; } if ( sum == N ) return 1 ; else return 0 ; } int main ( ) { int N = 153 ; if ( isArmstrong ( N ) == 1 ) cout << " True " ; else cout << " False " ; return 0 ; }
Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; initializing cost = 0 ; to store the removed element ; this will store the sum of the subsequence ; checking all the element of the vector ; storing the value of arr [ i ] in temp variable ; if the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; insert the element in the set removedElements ; storing the value of arr [ i ] in temp ; taking the element not in removedElements ; adding the value of elements of subsequence ; if we have to remove the element then we need to add the cost associated with the element ; printing the sum of the subsequence with minimum length possible ; printing the cost incurred in creating subsequence ; Driver code ; calling the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void costOfSubsequence ( int N , int arr [ ] , int costArray [ ] ) { int i , temp ; int cost = 0 ; set < int > removedElements ; int ans = 0 ; for ( i = 1 ; i < ( N - 1 ) ; i ++ ) { temp = arr [ i ] ; if ( ( ( arr [ i - 1 ] < temp ) && ( temp < arr [ i + 1 ] ) ) || ( ( arr [ i - 1 ] > temp ) && ( temp > arr [ i + 1 ] ) ) ) { removedElements . insert ( temp ) ; } } for ( i = 0 ; i < ( N ) ; i ++ ) { temp = arr [ i ] ; if ( ! ( removedElements . count ( temp ) > 0 ) ) { ans += arr [ i ] ; } else { cost += costArray [ i ] ; } } cout << ans << " , ▁ " ; cout << cost << endl ; } int main ( ) { int N ; N = 4 ; int arr [ N ] = { 1 , 3 , 4 , 2 } ; int costArray [ N ] = { 0 , 1 , 0 , 0 } ; costOfSubsequence ( N , arr , costArray ) ; return 0 ; }
Count of natural numbers in range [ L , R ] which are relatively prime with N | C ++ code to count of natural numbers in range [ L , R ] which are relatively prime with N ; container of all the primes up to sqrt ( n ) ; Function to calculate prime factors of n ; run the sieve of Eratosthenes ; 0 ( false ) means prime , 1 ( true ) means not prime ; no even number is prime except for 2 ; all the multiples of each each prime numbers are non - prime ; get all the primes in prime vector ; Count the number of numbers up to m which are divisible by given prime numbers ; Run from i = 000. . 0 to i = 111. . 1 or check all possible subsets of the array ; if the number of set bits is odd , then add to the number of multiples ; Function calculates all number not greater than ' m ' which are relatively prime with n . ; if square of the prime number is greater than ' n ' , it can ' t ▁ ▁ be ▁ a ▁ factor ▁ of ▁ ' n ' ; if prime is a factor of n then increment count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  (long long)1000000000000 NEW_LINE vector < int > prime ; void sieve ( long long n ) { bool check [ 1000007 ] = { 0 } ; long long i , j ; check [ 0 ] = 1 , check [ 1 ] = 1 , check [ 2 ] = 0 ; for ( i = 4 ; i <= n ; i += 2 ) check [ i ] = true ; for ( i = 3 ; i * i <= n ; i += 2 ) if ( ! check [ i ] ) { for ( j = i * i ; j <= n ; j += 2 * i ) check [ j ] = true ; } prime . push_back ( 2 ) ; for ( int i = 3 ; i <= n ; i += 2 ) if ( ! check [ i ] ) prime . push_back ( i ) ; return ; } long long count ( long long a [ ] , int n , long long m ) { long long parity [ 3 ] = { 0 } ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { long long mult = 1 ; for ( int j = 0 ; j < n ; j ++ ) if ( i & ( 1 << j ) ) mult *= a [ j ] ; parity [ __builtin_popcount ( i ) & 1 ] += ( m / mult ) ; } return parity [ 1 ] - parity [ 0 ] ; } long long countRelPrime ( long long n , long long m ) { long long a [ 20 ] ; int i = 0 , j = 0 ; long long pz = prime . size ( ) ; while ( n != 1 && i < pz ) { if ( ( long long ) prime [ i ] * ( long long ) prime [ i ] > n ) break ; if ( n % prime [ i ] == 0 ) a [ j ] = ( long long ) prime [ i ] , j ++ ; while ( n % prime [ i ] == 0 ) n /= prime [ i ] ; i ++ ; } if ( n != 1 ) a [ j ] = n , j ++ ; return m - count ( a , j , m ) ; } void countRelPrimeInRange ( long long n , long long l , long long r ) { sieve ( sqrt ( maxN ) ) ; long long result = countRelPrime ( n , r ) - countRelPrime ( n , l - 1 ) ; cout << result << " STRNEWLINE " ; } int main ( ) { long long N = 7 , L = 3 , R = 9 ; countRelPrimeInRange ( N , L , R ) ; return 0 ; }
C / C ++ Program to find Prime Numbers between given range | C ++ program to find the prime numbers between a given interval ; Function for print prime number in given range ; Traverse each number in the interval with the help of for loop ; Skip 0 and 1 as they are neither prime nor composite ; flag variable to tell if i is prime or not ; Iterate to check if i is prime or not ; flag = 1 means i is prime and flag = 0 means i is not prime ; Driver Code ; Given Range ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void primeInRange ( int L , int R ) { int flag ; for ( int i = L ; i <= R ; i ++ ) { if ( i == 1 i == 0 ) continue ; flag = 1 ; for ( int j = 2 ; j <= i / 2 ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) cout << i << " ▁ " ; } } int main ( ) { int L = 1 ; int R = 10 ; primeInRange ( L , R ) ; return 0 ; }
XOR of a subarray ( range of elements ) | Set 2 | C ++ program to find XOR in a range from L to R ; Function to find XOR in a range from L to R ; Compute xor from arr [ 0 ] to arr [ i ] ; process every query in constant time ; if L == 0 ; Driver Code ; query [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_Xor ( int arr [ ] , pair < int , int > query [ ] , int N , int Q ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i - 1 ] ; } int ans = 0 ; for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] . first == 0 ) ans = arr [ query [ i ] . second ] ; else ans = arr [ query [ i ] . first - 1 ] ^ arr [ query [ i ] . second ] ; cout << ans << endl ; } } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 } ; int N = 8 ; int Q = 2 ; pair < int , int > query [ Q ] = { { 1 , 4 } , { 3 , 7 } } ; find_Xor ( arr , query , N , Q ) ; return 0 ; }
Generate an alternate increasing and decreasing Array | C ++ program to Generate an increasing and decreasing array ; Function that returns generated array ; Dynamically allocate array ; START = 0 , END = N ; iterate over array ; if Str [ i ] == ' I ' assign arr [ i ] as START and increment START ; if str [ i ] == ' D ' assign arr [ i ] as END and decrement END ; assign A [ N ] as START ; return starting address of array A ; Driver Program
#include <iostream> NEW_LINE using namespace std ; int * DiStirngMatch ( string Str ) { int N = Str . length ( ) ; int * arr = new int [ N ] ; int START = 0 , END = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( Str [ i ] == ' I ' ) arr [ i ] = START ++ ; if ( Str [ i ] == ' D ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } int main ( ) { string Str = " IDID " ; int N = Str . length ( ) ; int * ptr = DiStirngMatch ( Str ) ; for ( int i = 0 ; i <= N ; i ++ ) cout << ptr [ i ] << " ▁ " ; return 0 ; }
Count of square free divisors of a given number | C ++ Program to find the square free divisors of a given number ; The function to check if a number is prime or not ; If the number is even then its not prime ; Driver Code ; Stores the count of distinct prime factors ; Print the number of square - free divisors
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool IsPrime ( int i ) { if ( i % 2 == 0 && i != 2 ) return false ; else { for ( int j = 3 ; j <= sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) return false ; } return true ; } } int main ( ) { int c = 0 ; int N = 72 ; for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) { if ( IsPrime ( i ) ) { if ( N % i == 0 ) { c ++ ; if ( IsPrime ( N / i ) && i != ( N / i ) ) { c ++ ; } } } } cout << pow ( 2 , c ) - 1 << endl ; return 0 ; }
Generate Array whose difference of each element with its left yields the given Array | C ++ program for the above approach ; Function to find the sequence ; initializing 1 st element ; Creating sequence in terms of x ; Finding min element ; Finding value of x ; Creating original sequence ; Output original sequence ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_seq ( int arr [ ] , int m , int n ) { int b [ n ] ; int x = 0 ; b [ 0 ] = x ; for ( int i = 0 ; i < n - 1 ; i ++ ) { b [ i + 1 ] = x + arr [ i ] + b [ i ] ; } int mn = n ; for ( int i = 0 ; i < n ; i ++ ) { mn = min ( mn , b [ i ] ) ; } x = 1 - mn ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] += x ; } for ( int i = 0 ; i < n ; i ++ ) { cout << b [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { int N = 3 ; int arr [ ] = { -2 , 1 } ; int M = sizeof ( arr ) / sizeof ( int ) ; find_seq ( arr , M , N ) ; return 0 ; }
Check whether count of odd and even factors of a number are equal | C ++ solution for the above approach ; Function to check condition ; Initialize even_count and od_count ; loop runs till square root ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE void isEqualFactors ( lli N ) { lli ev_count = 0 , od_count = 0 ; for ( lli i = 1 ; i <= sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count == od_count ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { lli N = 10 ; isEqualFactors ( N ) ; return 0 ; }
Check whether count of odd and even factors of a number are equal | C ++ code for the above approach ; Function to check condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE void isEqualFactors ( lli N ) { if ( ( N % 2 == 0 ) and ( N % 4 != 0 ) ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { lli N = 10 ; isEqualFactors ( N ) ; N = 125 ; isEqualFactors ( N ) ; return 0 ; }