codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of digits of palindrome numbers in an array | C ++ implementation of the approach ; Function to return the reverse of n ; Function that returns true if n is a palindrome ; Function to return the count of digits of n ; Function to return the count of digits in all the palindromic numbers of arr [ ] ; If arr [ i ] is a one digit number or it is a palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverse ( int n ) { int rev = 0 ; while ( n > 0 ) { int d = n % 10 ; rev = rev * 10 + d ; n = n / 10 ; } return rev ; } bool isPalin ( int n ) { return ( n == reverse ( n ) ) ; } int countDigits ( int n ) { int c = 0 ; while ( n > 0 ) { n = n / 10 ; c ++ ; } return c ; } int countPalinDigits ( int arr [ ] , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 10 || isPalin ( arr [ i ] ) ) { s += countDigits ( arr [ i ] ) ; } } return s ; } int main ( ) { int arr [ ] = { 121 , 56 , 434 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( countPalinDigits ( arr , n ) ) ; return 0 ; }
Sum of all palindrome numbers present in an Array | C ++ program to calculate the sum of all palindromic numbers in array ; Function to reverse a number n ; Function to check if a number n is palindrome ; If n is equal to the reverse of n it is a palindrome ; Function to calculate sum of all array elements which are palindrome ; summation of all palindrome numbers present in array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverse ( int n ) { int d = 0 , s = 0 ; while ( n > 0 ) { d = n % 10 ; s = s * 10 + d ; n = n / 10 ; } return s ; } bool isPalin ( int n ) { return n == reverse ( n ) ; } int sumOfArray ( int arr [ ] , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] > 10 ) && isPalin ( arr [ i ] ) ) { s += arr [ i ] ; } } return s ; } int main ( ) { int n = 6 ; int arr [ ] = { 12 , 313 , 11 , 44 , 9 , 1 } ; cout << sumOfArray ( arr , n ) ; return 0 ; }
Queries for the difference between the count of composite and prime numbers in a given range | C ++ implementation of the approach ; Function to update prime [ ] such prime [ i ] stores the count of prime numbers <= i ; Initialization ; 0 and 1 are not primes ; Mark composite numbers as false and prime numbers as true ; Update prime [ ] such that prime [ i ] will store the count of all the prime numbers <= i ; Function to return the absolute difference between the number of primes and the number of composite numbers in the range [ l , r ] ; Total elements in the range ; Count of primes in the range [ l , r ] ; Count of composite numbers in the range [ l , r ] ; Return the sbsolute difference ; Driver code ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE int prime [ MAX + 1 ] ; void updatePrimes ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { prime [ i ] = 1 ; } prime [ 0 ] = prime [ 1 ] = 0 ; for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j <= MAX ; j += i ) { prime [ j ] = 0 ; } } } for ( int i = 1 ; i <= MAX ; i ++ ) { prime [ i ] += prime [ i - 1 ] ; } } int getDifference ( int l , int r ) { int total = r - l + 1 ; int primes = prime [ r ] - prime [ l - 1 ] ; int composites = total - primes ; return ( abs ( primes - composites ) ) ; } int main ( ) { int queries [ ] [ 2 ] = { { 1 , 10 } , { 5 , 30 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; updatePrimes ( ) ; for ( int i = 0 ; i < q ; i ++ ) cout << getDifference ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << endl ; return 0 ; }
Check if the given Prufer sequence is valid or not | C ++ implementation of the approach ; Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidSeq ( int a [ ] , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } int main ( ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isValidSeq ( a , n ) ) cout << " Valid " ; else cout << " Invalid " ; return 0 ; }
Program to Calculate e ^ x by Recursion ( using Taylor Series ) | C ++ implementation of the approach ; Recursive Function with static variables p and f ; Termination condition ; Recursive call ; Update the power of x ; Factorial ; Driver code
#include <iostream> NEW_LINE using namespace std ; double e ( int x , int n ) { static double p = 1 , f = 1 ; double r ; if ( n == 0 ) return 1 ; r = e ( x , n - 1 ) ; p = p * x ; f = f * n ; return ( r + p / f ) ; } int main ( ) { int x = 4 , n = 15 ; cout << " STRNEWLINE " << e ( x , n ) ; return 0 ; }
Check if each element of the given array is the product of exactly K prime numbers | C ++ program to check if each element of the given array is a product of exactly K prime factors ; initialise the global sieve array ; Function to generate Sieve ; NOTE : k value is necessarily more than 1 hence , 0 , 1 and any prime number cannot be represented as product of two or more prime numbers ; Function to check if each number of array satisfies the given condition ; Driver Code ; first construct the sieve
#include <iostream> NEW_LINE #define MAX  1000000 NEW_LINE using namespace std ; int Sieve [ MAX ] = { 0 } ; void constructSieve ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( int j = 2 * i ; j <= MAX ; j += i ) { int temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = temp / i ; } } } } } void checkElements ( int A [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { cout << " YES STRNEWLINE " ; } else { cout << " NO STRNEWLINE " ; } } } int main ( ) { constructSieve ( ) ; int k = 3 ; int A [ ] = { 12 , 36 , 42 , 72 } ; int n = sizeof ( A ) / sizeof ( int ) ; checkElements ( A , n , k ) ; return 0 ; }
Fibonacci Power |  ; Iterative function to compute modular power ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Helper function that multiplies 2 matrices F and M of size 2 * 2 , and puts the multiplication result back to F [ ] [ ] ; Helper function that calculates F [ ] [ ] raise to the power n and puts the result in F [ ] [ ] Note that this function is designed only for fib ( ) and won 't work as general power function ; Function that returns nth Fibonacci number ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; long long modularexpo ( long long x , long long y , long long p ) { long long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } void multiply ( long long F [ 2 ] [ 2 ] , long long M [ 2 ] [ 2 ] , long long m ) { long long x = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; long long y = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; long long z = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; long long w = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } void power ( long long F [ 2 ] [ 2 ] , long long n , long long m ) { if ( n == 0 n == 1 ) return ; long long M [ 2 ] [ 2 ] = { { 1 , 1 } , { 1 , 0 } } ; power ( F , n / 2 , m ) ; multiply ( F , F , m ) ; if ( n % 2 != 0 ) multiply ( F , M , m ) ; } long long fib ( long long n , long long m ) { long long F [ 2 ] [ 2 ] = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) return 0 ; power ( F , n - 1 , m ) ; return F [ 0 ] [ 0 ] ; } int main ( ) { long long n = 4 ; long long base = fib ( n , mod ) % mod ; long long expo = fib ( n , mod - 1 ) % ( mod - 1 ) ; long long result = modularexpo ( base , expo , mod ) % mod ; cout << result << endl ; }
Count number of ways to divide an array into two halves with same sum | C ++ program to count the number of ways to divide an array into two halves with the same sum ; Function to count the number of ways to divide an array into two halves with same sum ; if length of array is 1 answer will be 0 as we have to split it into two non - empty halves ; variables to store total sum , current sum and count ; finding total sum ; checking if sum equals total_sum / 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntWays ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; int tot_sum = 0 , sum = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) tot_sum += arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] ; if ( sum == tot_sum / 2 ) ans ++ ; } return ans ; } int main ( ) { int arr [ ] = { 1 , -1 , 1 , -1 , 1 , -1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << cntWays ( arr , n ) ; return 0 ; }
Find N distinct numbers whose bitwise Or is equal to K | C ++ implementation of the approach ; Function to pre - calculate all the powers of 2 upto MAX ; Function to return the count of set bits in x ; To store the count of set bits ; Function to add num to the answer by setting all bit positions as 0 which are also 0 in K ; Bit i is 0 in K ; Function to find and print N distinct numbers whose bitwise OR is K ; Choosing K itself as one number ; Find the count of set bits in K ; Impossible to get N distinct integers ; Add i to the answer after setting all the bits as 0 which are 0 in K ; If N distinct numbers are generated ; Print the generated numbers ; Driver code ; Pre - calculate all the powers of 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE #define MAX  32 NEW_LINE ll pow2 [ MAX ] ; bool visited [ MAX ] ; vector < int > ans ; void power_2 ( ) { ll ans = 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { pow2 [ i ] = ans ; ans *= 2 ; } } int countSetBits ( ll x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } void add ( ll num ) { int point = 0 ; ll value = 0 ; for ( ll i = 0 ; i < MAX ; i ++ ) { if ( visited [ i ] ) continue ; else { if ( num & 1 ) { value += ( 1 << i ) ; } num /= 2 ; } } ans . push_back ( value ) ; } void solve ( ll n , ll k ) { ans . push_back ( k ) ; int countk = countSetBits ( k ) ; if ( pow2 [ countk ] < n ) { cout << -1 ; return ; } int count = 0 ; for ( ll i = 0 ; i < pow2 [ countk ] - 1 ; i ++ ) { add ( i ) ; count ++ ; if ( count == n ) break ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } int main ( ) { ll n = 3 , k = 5 ; power_2 ( ) ; solve ( n , k ) ; return 0 ; }
Count of all possible values of X such that A % X = B | C ++ implementation of the approach ; Function to return the count of all possible values for x such that ( A % x ) = B ; Case 1 ; Case 2 ; Case 3 ; Find the number of divisors of x which are greater than b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countX ( int a , int b ) { if ( b > a ) return 0 ; else if ( a == b ) return -1 ; else { int x = a - b , ans = 0 ; for ( int i = 1 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { int d1 = i , d2 = b - 1 ; if ( i * i != x ) d2 = x / i ; if ( d1 > b ) ans ++ ; if ( d2 > b ) ans ++ ; } } return ans ; } } int main ( ) { int a = 21 , b = 5 ; cout << countX ( a , b ) ; return 0 ; }
Find the Jaccard Index and Jaccard Distance between the two given sets | C ++ implementation of the approach ; Function to return the intersection set of s1 and s2 ; Find the intersection of the two sets ; Function to return the Jaccard index of two sets ; Sizes of both the sets ; Get the intersection set ; Size of the intersection set ; Calculate the Jaccard index using the formula ; Return the Jaccard index ; Function to return the Jaccard distance ; Calculate the Jaccard distance using the formula ; Return the Jaccard distance ; Driver code ; Elements of the 1 st set ; Elements of the 2 nd set ; Print the Jaccard index and Jaccard distance
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < int > intersection ( set < int > s1 , set < int > s2 ) { set < int > intersect ; set_intersection ( s1 . begin ( ) , s1 . end ( ) , s2 . begin ( ) , s2 . end ( ) , inserter ( intersect , intersect . begin ( ) ) ) ; return intersect ; } double jaccard_index ( set < int > s1 , set < int > s2 ) { double size_s1 = s1 . size ( ) ; double size_s2 = s2 . size ( ) ; set < int > intersect = intersection ( s1 , s2 ) ; double size_in = intersect . size ( ) ; double jaccard_in = size_in / ( size_s1 + size_s2 - size_in ) ; return jaccard_in ; } double jaccard_distance ( double jaccardIndex ) { double jaccard_dist = 1 - jaccardIndex ; return jaccard_dist ; } int main ( ) { set < int > s1 ; s1 . insert ( 1 ) ; s1 . insert ( 2 ) ; s1 . insert ( 3 ) ; s1 . insert ( 4 ) ; s1 . insert ( 5 ) ; set < int > s2 ; s2 . insert ( 4 ) ; s2 . insert ( 5 ) ; s2 . insert ( 6 ) ; s2 . insert ( 7 ) ; s2 . insert ( 8 ) ; s2 . insert ( 9 ) ; s2 . insert ( 10 ) ; double jaccardIndex = jaccard_index ( s1 , s2 ) ; cout << " Jaccard ▁ index ▁ = ▁ " << jaccardIndex << endl ; cout << " Jaccard ▁ distance ▁ = ▁ " << jaccard_distance ( jaccardIndex ) ; return 0 ; }
Find the sum of numbers from 1 to n excluding those which are powers of K | C ++ implementation of the approach ; Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll sumPowersK ( ll n , ll k ) { ll sum = 0 , num = 1 ; while ( num <= n ) { sum += num ; num *= k ; } return sum ; } ll getSum ( ll n , ll k ) { ll pwrK = sumPowersK ( n , k ) ; ll sumAll = ( n * ( n + 1 ) ) / 2 ; return ( sumAll - pwrK ) ; } int main ( ) { ll n = 10 , k = 3 ; cout << getSum ( n , k ) ; return 0 ; }
Maximum number of people that can be killed with strength P | C ++ implementation of the approach ; Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPeople ( int p ) { int tmp = 0 , count = 0 ; for ( int i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } int main ( ) { int p = 14 ; cout << maxPeople ( p ) ; return 0 ; }
Maximum number of people that can be killed with strength P | C ++ implementation of the approach ; Function to return the maximum number of people that can be killed ; Storing the sum beforehand so that it can be used in each query ; lower_bound returns an iterator pointing to the first element greater than or equal to your val ; Previous value ; Returns the index in array upto which killing is possible with strength P ; Driver code
#include <algorithm> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE static constexpr int kN = 1000000 ; int maxPeople ( int p ) { ll sums [ kN ] ; sums [ 0 ] = 0 ; for ( int i = 1 ; i < kN ; i ++ ) sums [ i ] = ( ll ) ( i * i ) + sums [ i - 1 ] ; auto it = std :: lower_bound ( sums , sums + kN , p ) ; if ( * it > p ) { -- it ; } return ( it - sums ) ; } int main ( ) { int p = 14 ; cout << maxPeople ( p ) ; return 0 ; }
Maximum number of people that can be killed with strength P | C ++ implementation of the approach ; Helper function which returns the sum of series ( 1 ^ 2 + 2 ^ 2 + ... + n ^ 2 ) ; maxPeople function which returns appropriate value using Binary Search in O ( logn ) ; Set the lower and higher values ; Calculate the mid using low and high ; Compare value with n ; Return the ans ; Driver code
#include <algorithm> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; long squareSeries ( long n ) { return ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } long maxPeople ( long n ) { long low = 0 ; long high = 1000000L ; long ans = 0L ; while ( low <= high ) { long mid = low + ( ( high - low ) / 2 ) ; long value = squareSeries ( mid ) ; if ( value <= n ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } int main ( ) { long p = 14 ; cout << maxPeople ( p ) ; return 0 ; }
Find the index which is the last to be reduced to zero after performing a given operation | C ++ implementation of the approach ; Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findIndex ( int a [ ] , int n , int k ) { int index = -1 , max_ceil = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + k - 1 ) / k ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= max_ceil ) { max_ceil = a [ i ] ; index = i ; } } return index ; } int main ( ) { int arr [ ] = { 31 , 12 , 25 , 27 , 32 , 19 } ; int K = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findIndex ( arr , N , K ) ; return 0 ; }
Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree | C ++ implementation of the approach ; Function to solve queries of the maximum xor value between the nodes in a given level L of a perfect binary tree and a given value X ; Initialize result ; Initialize array to store bits ; Initialize a copy of X and size of array ; Storing the bits of X in the array a [ ] ; Filling the array b [ ] ; Initializing variable which gives maximum xor ; Getting the maximum xor value ; Return the result ; Driver code ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  60 NEW_LINE int solveQuery ( int L , int X ) { int res ; int a [ MAXN ] , b [ L ] ; int ref = X , size_a = 0 ; while ( ref > 0 ) { a [ size_a ] = ref % 2 ; ref /= 2 ; size_a ++ ; } for ( int i = 0 ; i < min ( size_a , L ) ; i ++ ) { if ( a [ i ] == 1 ) b [ i ] = 0 ; else b [ i ] = 1 ; } for ( int i = min ( size_a , L ) ; i < L ; i ++ ) b [ i ] = 1 ; b [ L - 1 ] = 1 ; int temp = 0 , p = 1 ; for ( int i = 0 ; i < L ; i ++ ) { temp += b [ i ] * p ; p *= 2 ; } res = temp ^ X ; return res ; } int main ( ) { int queries [ ] [ 2 ] = { { 2 , 5 } , { 3 , 15 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << solveQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << endl ; return 0 ; }
Count index pairs which satisfy the given condition | CPP implementation of the approach ; Function to return the count of required index pairs ; To store the required count ; Array to store the left elements upto which current element is maximum ; Iterating through the whole permutation except first and last element ; If current element can be maximum in a subsegment ; Current maximum ; Iterating for smaller values then current maximum on left of it ; Storing left borders of the current maximum ; Iterating for smaller values then current maximum on right of it ; Condition satisfies ; Return count of subsegments ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Count_Segment ( int p [ ] , int n ) { int count = 0 ; int upto [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) upto [ i ] = 0 ; int j = 0 , curr = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { if ( p [ i ] > p [ i - 1 ] and p [ i ] > p [ i + 1 ] ) { curr = p [ i ] ; j = i - 1 ; while ( j >= 0 and p [ j ] < curr ) { upto [ p [ j ] ] = curr ; j -= 1 ; } j = i + 1 ; while ( j < n and p [ j ] < curr ) { if ( upto [ curr - p [ j ] ] == curr ) count += 1 ; j += 1 ; } } } return count ; } int main ( ) { int p [ ] = { 3 , 4 , 1 , 5 , 2 } ; int n = sizeof ( p ) / sizeof ( p [ 0 ] ) ; cout << ( Count_Segment ( p , n ) ) ; return 0 ; }
Check whether a number can be represented as sum of K distinct positive integers | C ++ implementation of the approach ; Function that returns true if n can be represented as the sum of exactly k distinct positive integers ; If n can be represented as 1 + 2 + 3 + ... + ( k - 1 ) + ( k + x ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool solve ( int n , int k ) { if ( n >= ( k * ( k + 1 ) ) / 2 ) { return true ; } return false ; } int main ( ) { int n = 12 , k = 4 ; if ( solve ( n , k ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether product of integers from a to b is positive , negative or zero | C ++ implementation of the approach ; Function to check whether the product of integers of the range [ a , b ] is positive , negative or zero ; If both a and b are positive then the product will be positive ; If a is negative and b is positive then the product will be zero ; If both a and b are negative then we have to find the count of integers in the range ; Total integers in the range ; If n is even then the resultant product is positive ; If n is odd then the resultant product is negative ; Driver code
#include <iostream> NEW_LINE using namespace std ; void solve ( long long int a , long long int b ) { if ( a > 0 && b > 0 ) { cout << " Positive " ; } else if ( a <= 0 && b >= 0 ) { cout << " Zero " << endl ; } else { long long int n = abs ( a - b ) + 1 ; if ( n % 2 == 0 ) { cout << " Positive " << endl ; } else { cout << " Negative " << endl ; } } } int main ( ) { int a = -10 , b = -2 ; solve ( a , b ) ; return 0 ; }
Bitwise AND of sub | C ++ implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int closetAND ( int arr [ ] , int n , int k ) { int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = min ( ans , abs ( k - X ) ) ; } } return ans ; } int main ( ) { int arr [ ] = { 4 , 7 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << closetAND ( arr , n , k ) ; return 0 ; }
Program to find the rate percentage from compound interest of consecutive years | C ++ implementation of the approach ; Function to return the required rate percentage ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Rate ( int N1 , int N2 ) { float rate = ( N2 - N1 ) * 100 / float ( N1 ) ; return rate ; } int main ( ) { int N1 = 100 , N2 = 120 ; cout << Rate ( N1 , N2 ) << " ▁ % " ; return 0 ; }
Find prime number K in an array such that ( A [ i ] % K ) is maximum | C ++ implementation of the approach ; Function to return the required prime number from the array ; 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 . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPrime ( int arr [ ] , int n ) { int max_val = * max_element ( arr , arr + n ) ; vector < bool > prime ( max_val + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; 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 maximum = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) maximum = max ( maximum , arr [ i ] ) ; } return maximum ; } int main ( ) { int arr [ ] = { 2 , 10 , 15 , 7 , 6 , 8 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getPrime ( arr , n ) ; return 0 ; }