codeparrot/xlcost-text-to-code huggingface dataset:
Number of edges in a perfect binary tree with N levels | C ++ implementation of the approach ; Function to return the count of edges in an n - level perfect binary tree ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntEdges ( int n ) { int edges = pow ( 2 , n ) - 2 ; return edges ; } int main ( ) { int n = 4 ; cout << cntEdges ( n ) ; return 0 ; }
Number of cells in the Nth order figure | C ++ implementation of the approach ; Function to return the number of cells in the nth order figure of the given type ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntCells ( int n ) { int cells = pow ( n , 2 ) + pow ( n - 1 , 2 ) ; return cells ; } int main ( ) { int n = 3 ; cout << cntCells ( n ) ; return 0 ; }
Sum of all the numbers in the Nth parenthesis | C ++ implementation of the approach ; Function to return the sum of the numbers in the nth parenthesis ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { return pow ( n , 3 ) ; } int main ( ) { int n = 3 ; cout << findSum ( n ) ; return 0 ; }
Find the count of natural Hexadecimal numbers of size N | C ++ implementation of the above approach ; Function to return the count of n - digit natural hexadecimal numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { return 15 * pow ( 16 , n - 1 ) ; } int main ( ) { int n = 2 ; cout << count ( n ) ; return 0 ; }
Largest Even and Odd N | C ++ implementation of the approach ; Function to print the largest n - digit even and odd numbers in octal number system ; Append '7' ( N - 1 ) times ; Append '6' for an even number ; Append '7' for an odd number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int n ) { string ans = string ( n - 1 , '7' ) ; string even = ans + '6' ; string odd = ans + '7' ; cout << " Even ▁ : ▁ " << even << endl ; cout << " Odd ▁ : ▁ " << odd << endl ; } int main ( ) { int n = 4 ; findNumbers ( n ) ; return 0 ; }
Nth term of a Custom Fibonacci series | C ++ implementation of the Custom Fibonacci series ; Function to return the nth term of the required sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nth_term ( int a , int b , int n ) { int z = 0 ; if ( n % 6 == 1 ) z = a ; else if ( n % 6 == 2 ) z = b ; else if ( n % 6 == 3 ) z = b - a ; else if ( n % 6 == 4 ) z = - a ; else if ( n % 6 == 5 ) z = - b ; if ( n % 6 == 0 ) z = - ( b - a ) ; return z ; } int main ( ) { int a = 10 , b = 17 , n = 3 ; cout << nth_term ( a , b , n ) ; return 0 ; }
Check whether the given integers a , b , c and d are in proportion | C ++ implementation of the approach ; Function that returns true if the given four integers are in proportion ; Array will consist of only four integers ; Sort the array ; Find the product of extremes and means ; If the products are equal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool inProportion ( int arr [ ] ) { int n = 4 ; sort ( arr , arr + n ) ; long extremes = ( long ) arr [ 0 ] * ( long ) arr [ 3 ] ; long means = ( long ) arr [ 1 ] * ( long ) arr [ 2 ] ; if ( extremes == means ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 2 } ; if ( inProportion ( arr ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the deleted value from the array when average of original elements is given | C ++ implementation of the approach ; Function to return the missing element ; Find the sum of the array elements ; The numerator and the denominator of the equation ; If not divisible then X is not an integer it is a floating point number ; Return X ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMissing ( int arr [ ] , int n , int k , int avg ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int num = ( avg * ( n + k ) ) - sum ; int den = k ; if ( num % den != 0 ) return -1 ; return ( num / den ) ; } int main ( ) { int k = 3 , avg = 4 ; int arr [ ] = { 2 , 7 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findMissing ( arr , n , k , avg ) ; return 0 ; }
Count of N | C ++ implementation of the approach ; Function to return the factorial of n ; Function to return the count of n - digit numbers with all distinct digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } int countNum ( int n ) { if ( n > 10 ) return 0 ; return ( 9 * factorial ( 9 ) / factorial ( 10 - n ) ) ; } int main ( ) { int n = 3 ; cout << countNum ( n ) ; return 0 ; }
Maximum number of distinct positive integers that can be used to represent N | C ++ implementation of the approach ; Function to return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { return int ( ( -1 + sqrt ( 1 + 8 * n ) ) / 2 ) ; } int main ( ) { int n = 10 ; cout << count ( n ) ; return 0 ; }
Find the previous fibonacci number | C ++ implementation of the approach ; Function to return the previous fibonacci number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int previousFibonacci ( int n ) { double a = n / ( ( 1 + sqrt ( 5 ) ) / 2.0 ) ; return round ( a ) ; } int main ( ) { int n = 8 ; cout << ( previousFibonacci ( n ) ) ; }
Find the quadratic equation from the given roots | C ++ implementation of the approach ; Function to find the quadratic equation whose roots are a and b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findEquation ( int a , int b ) { int sum = ( a + b ) ; int product = ( a * b ) ; cout << " x ^ 2 ▁ - ▁ ( " << sum << " x ) ▁ + ▁ ( " << product << " ) ▁ = ▁ 0" ; } int main ( ) { int a = 2 , b = 3 ; findEquation ( a , b ) ; return 0 ; }
Smallest N digit number which is a perfect fourth power | C ++ implementation of the approach ; Function to return the smallest n - digit number which is a perfect fourth power ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cal ( int n ) { double res = pow ( ceil ( ( pow ( pow ( 10 , ( n - 1 ) ) , 1 / 4 ) ) ) , 4 ) ; return ( int ) res ; } int main ( ) { int n = 1 ; cout << ( cal ( n ) ) ; }
Count of 0 s in an N | C ++ implementation of the approach ; Function to return the count of 0 s in an n - level hexagon ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { return 3 * n * ( n - 1 ) + 1 ; } int main ( ) { int n = 3 ; cout << count ( n ) ; return 0 ; }
Number of words that can be made using exactly P consonants and Q vowels from the given string | C ++ implementation of the approach ; Function to return the value of nCk ; Function to return the factorial of n ; Function that returns true if ch is a vowel ; Function to return the number of words possible ; To store the count of vowels and consonanats in the given string ; If current character is a vowel ; Find the total possible words ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE lli binomialCoeff ( lli n , lli k ) { if ( k == 0 k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } lli fact ( lli n ) { if ( n >= 1 ) return n * fact ( n - 1 ) ; else return 1 ; } bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } return false ; } lli countWords ( string s , int p , int q ) { lli countc = 0 , countv = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s [ i ] ) ) countv ++ ; else countc ++ ; } lli a = binomialCoeff ( countc , p ) ; lli b = binomialCoeff ( countv , q ) ; lli c = fact ( p + q ) ; lli ans = ( a * b ) * c ; return ans ; } int main ( ) { string s = " crackathon " ; int p = 4 , q = 3 ; cout << countWords ( s , p , q ) ; return 0 ; }
Count of elements on the left which are divisible by current element | C ++ implementation of the approach ; Utility function to print the elements of the array ; Function to generate and print the required array ; For every element of the array ; To store the count of elements on the left that the current element divides ; Print the generated array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void generateArr ( int A [ ] , int n ) { int B [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( A [ j ] % A [ i ] == 0 ) cnt ++ ; } B [ i ] = cnt ; } printArr ( B , n ) ; } int main ( ) { int A [ ] = { 3 , 5 , 1 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; generateArr ( A , n ) ; return 0 ; }
Represent the given number as the sum of two composite numbers | C ++ implementation of the approach ; Function to find two composite numbers which when added give sum as n ; Only 8 and 10 can be represented as the sum of two composite integers ; If n is even ; If n is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNums ( int n ) { if ( n <= 11 ) { if ( n == 8 ) cout << "4 ▁ 4" ; if ( n == 10 ) cout << "4 ▁ 6" ; else cout << " - 1" ; return ; } if ( n % 2 == 0 ) cout << "4 ▁ " << ( n - 4 ) ; else cout << "9 ▁ " << ( n - 9 ) ; } int main ( ) { int n = 13 ; findNums ( n ) ; return 0 ; }
Count of N | C ++ implementation of the approach ; Function to return the count of possible numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { return pow ( 2 , n - 1 ) ; } int main ( ) { int n = 4 ; cout << count ( n ) ; return 0 ; }
Find the next fibonacci number | C ++ implementation of the approach ; Function to return the next fibonacci number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextFibonacci ( int n ) { double a = n * ( 1 + sqrt ( 5 ) ) / 2.0 ; return round ( a ) ; } int main ( ) { int n = 5 ; cout << nextFibonacci ( n ) ; }
Sum of all the numbers present at given level in Pascal 's triangle | C ++ implementation of the above approach ; Function to find sum of numbers at Lth level in Pascals Triangle ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int h ) { return pow ( 2 , h - 1 ) ; } int main ( ) { int L = 3 ; cout << sum ( L ) ; return 0 ; }
Product of values of all possible non | C ++ implementation of the approach ; Function to find product of all elements in all subsets ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int product ( int a [ ] , int n ) { int ans = 1 ; int val = pow ( 2 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { ans *= pow ( a [ i ] , val ) ; } return ans ; } int main ( ) { int n = 2 ; int a [ ] = { 3 , 7 } ; cout << product ( a , n ) ; return 0 ; }
Program to find Nth odd Fibonacci Number | C ++ program for Nth odd fibonacci number ; Function to find nth odd fibonacci number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oddFib ( int n ) { n = ( 3 * n + 1 ) / 2 ; int a = -1 , b = 1 , c , i ; for ( i = 1 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } int main ( ) { int n = 4 ; cout << oddFib ( n ) ; return 0 ; }
Find a pair ( n , r ) in an integer array such that value of nPr is maximum | C ++ implementation of the approach ; Function to print the pair ( n , r ) such that nPr is maximum possible ; There should be atleast 2 elements ; Findex the largest 2 elements ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findPair ( int arr [ ] , int n ) { if ( n < 2 ) { cout << " - 1" ; return ; } int i , first , second ; first = second = -1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } cout << " n ▁ = ▁ " << first << " ▁ and ▁ r ▁ = ▁ " << second ; } int main ( ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n ) ; return 0 ; }
Sum of values of all possible non | C ++ implementation of the approach ; Function to return the required sum ; Find the sum of the array elements ; Every element appears 2 ^ ( n - 1 ) times ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } sum = sum * pow ( 2 , n - 1 ) ; return sum ; } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << sum ( arr , n ) ; return 0 ; }
Printing the Triangle Pattern using last term N | C ++ code for printing the Triangle Pattern using last term N ; Function to demonstrate printing pattern ; number of spaces ; character to be printed ; outer loop to handle number of rows n in this case ; inner loop to handle number spaces values changing acc . to requirement ; decrementing k after each loop ; inner loop to handle number of columns values changing acc . to outer loop ; printing stars ; ending line after each row ; Function to find the max height or the number of lines in the triangle pattern ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void triangle ( int n ) { int k = 2 * n - 2 ; int ch = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) cout << " ▁ " ; k = k - 1 ; for ( int j = 0 ; j <= i ; j ++ ) { cout << ch ++ << " ▁ " ; } cout << endl ; } } int maxHeight ( int n ) { return ( ( ( int ) sqrt ( 1 + 8.0 * n ) ) - 1 ) / 2 ; } int main ( ) { int N = 9 ; triangle ( maxHeight ( N ) ) ; return 0 ; }
Number of ways in which N can be represented as the sum of two positive integers | C ++ implementation of the approach ; Function to return the number of distinct ways to represent n as the sum of two integers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ways ( int n ) { return n / 2 ; } int main ( ) { int n = 2 ; cout << ways ( n ) ; return 0 ; }
Number of ways to erase exactly one element in the Binary Array to make XOR zero | C ++ program to find the number of ways to erase exactly one element from this array to make XOR zero ; Function to find the number of ways ; Calculate the number of 1 ' s ▁ and ▁ 0' s ; Considering the 4 cases ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int no_of_ways ( int a [ ] , int n ) { int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } int main ( ) { int n = 4 ; int a1 [ 4 ] = { 1 , 1 , 0 , 0 } ; cout << no_of_ways ( a1 , n ) << endl ; n = 5 ; int a2 [ 5 ] = { 1 , 1 , 1 , 0 , 0 } ; cout << no_of_ways ( a2 , n ) << endl ; n = 5 ; int a3 [ 5 ] = { 1 , 1 , 0 , 0 , 0 } ; cout << no_of_ways ( a3 , n ) << endl ; n = 6 ; int a4 [ 6 ] = { 1 , 1 , 1 , 0 , 0 , 0 } ; cout << no_of_ways ( a4 , n ) << endl ; return 0 ; }
Minimum absolute difference between N and any power of 2 | C ++ implementation of the approach ; Function to return the highest power of 2 less than or equal to n ; Function to return the smallest power of 2 greater than or equal to n ; Function that returns the minimum absolute difference between n and any power of 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prevPowerof2 ( int n ) { int p = ( int ) log2 ( n ) ; return ( int ) pow ( 2 , p ) ; } int nextPowerOf2 ( int n ) { int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } int minDiff ( int n ) { int low = prevPowerof2 ( n ) ; int high = nextPowerOf2 ( n ) ; return min ( n - low , high - n ) ; } int main ( ) { int n = 6 ; cout << minDiff ( n ) ; return 0 ; }
Maximum possible number with the given operation | C ++ implementation of the approach ; Function to return the maximum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits greater than or equal to 5 need not to be changed as changing them will lead to a smaller number ; The resulting integer cannot have leading zero ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string maxInt ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] < '5' ) { str [ i ] = ( '9' - str [ i ] ) + '0' ; } } if ( str [ 0 ] == '0' ) str [ 0 ] = '9' ; return str ; } int main ( ) { string str = "42" ; cout << maxInt ( str ) ; return 0 ; }
Find the ratio of number of elements in two Arrays from their individual and combined average | C ++ program to Find the Ratio of number of Elements in two Arrays from their individual and combined Average ; C ++ function to find the ratio of number of array elements ; calculating GCD of them ; make neumarator and denominator coprime ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FindRatio ( int a , int b , int c ) { int up = abs ( b - c ) ; int down = abs ( c - a ) ; int g = __gcd ( up , down ) ; up /= g ; down /= g ; cout << up << " : " << down << " STRNEWLINE " ; } int main ( ) { int a = 4 , b = 10 , c = 6 ; FindRatio ( a , b , c ) ; return 0 ; }
Maximum distance between two 1 's in Binary representation of N | C ++ program to find the Maximum distance between two 1 's in Binary representation of N ; Compute the binary representation ; if N is a power of 2 then return - 1 ; else find the distance between the first position of 1 and last position of 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longest_gap ( int N ) { int distance = 0 , count = 0 , first_1 = -1 , last_1 = -1 ; while ( N ) { count ++ ; int r = N & 1 ; if ( r == 1 ) { first_1 = first_1 == -1 ? count : first_1 ; last_1 = count ; } N = N / 2 ; } if ( last_1 <= first_1 ) { return -1 ; } else { distance = ( last_1 - first_1 - 1 ) ; return distance ; } } int main ( ) { int N = 131 ; cout << longest_gap ( N ) << endl ; N = 8 ; cout << longest_gap ( N ) << endl ; N = 17 ; cout << longest_gap ( N ) << endl ; N = 33 ; cout << longest_gap ( N ) << endl ; return 0 ; }
Check if it is possible to move from ( 0 , 0 ) to ( X , Y ) in exactly K steps | C ++ implementation of the approach ; Function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; Minimum moves required ; If possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int x , int y , int k ) { int minMoves = abs ( x ) + abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } int main ( ) { int x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether N is a Factorion or not | C ++ implementation of the approach ; Function that returns true if n is a Factorion ; fact [ i ] will store i ! ; A copy of the given integer ; To store the sum of factorials of the digits of n ; Get the last digit ; Add the factorial of the current digit to the sum ; Remove the last digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE bool isFactorion ( int n ) { int fact [ MAX ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; int org = n ; int sum = 0 ; while ( n > 0 ) { int d = n % 10 ; sum += fact [ d ] ; n /= 10 ; } if ( sum == org ) return true ; return false ; } int main ( ) { int n = 40585 ; if ( isFactorion ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the possible permutation of the bits of N | C ++ implementation of the approach ; Function that returns true if it is possible to arrange the bits of n in alternate fashion ; To store the count of 1 s in the binary representation of n ; If the number set bits and the number of unset bits is equal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int TOTAL_BITS = 32 ; bool isPossible ( int n ) { int cnt = __builtin_popcount ( n ) ; if ( cnt == TOTAL_BITS / 2 ) return true ; return false ; } int main ( ) { int n = 524280 ; if ( isPossible ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if two Integer are anagrams of each other | C ++ implementation of the approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagarams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int TEN = 10 ; void updateFreq ( int n , int freq [ ] ) { while ( n ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } bool areAnagrams ( int a , int b ) { int freqA [ TEN ] = { 0 } ; int freqB [ TEN ] = { 0 } ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } int main ( ) { int a = 240 , b = 204 ; if ( areAnagrams ( a , b ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible | C ++ implementation of the approach ; Function to find the permutation of the first N natural numbers such that the sum of ( i % Pi ) is maximum possible and return the maximum sum ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Max_Sum ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } int main ( ) { int n = 8 ; cout << Max_Sum ( n ) ; return 0 ; }
Check if the number formed by the last digits of N numbers is divisible by 10 or not | C ++ implementation of the approach ; Function that returns true if the number formed by the last digits of all the elements is divisible by 10 ; Last digit of the last element ; Number formed will be divisible by 10 ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isDivisible ( int arr [ ] , int n ) { int lastDigit = arr [ n - 1 ] % 10 ; if ( lastDigit == 0 ) return true ; return false ; } int main ( ) { int arr [ ] = { 12 , 65 , 46 , 37 , 99 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isDivisible ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of Multiples of A , B or C less than or equal to N | C ++ implementation of the approach ; Function to return the gcd of a and b ; Function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; Calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } long divTermCount ( long a , long b , long c , long num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ; } int main ( ) { long a = 7 , b = 3 , c = 5 , n = 100 ; cout << divTermCount ( a , b , c , n ) ; return 0 ; }
Array containing power of 2 whose XOR and Sum of elements equals X | C ++ implementation of the above approach ; Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < long > getArray ( int n ) { vector < long > ans ; long p2 = 1 ; while ( n > 0 ) { if ( n & 1 ) ans . push_back ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } int main ( ) { long n = 15 ; vector < long > ans = getArray ( n ) ; for ( int i : ans ) cout << i << " ▁ " ; return 0 ; }