codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number is an Achilles number or not | Program to check if the given number is an Achilles Number ; function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } bool isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = log ( a ) / log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } bool isAchillesNumber ( int n ) { if ( isPowerful ( n ) && ! isPower ( n ) ) return true ; else return false ; } int main ( ) { int n = 72 ; if ( isAchillesNumber ( n ) ) cout << " YES " << endl ; else cout << " NO " << endl ; n = 36 ; if ( isAchillesNumber ( n ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Program to find count of numbers having odd number of divisors in given range | C ++ program to find count of numbers having odd number of divisors in given range ; Function to count numbers having odd number of divisors in range [ A , B ] ; variable to odd divisor count ; iterate from a to b and count their number of divisors ; variable to divisor count ; if count of divisor is odd then increase res by 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int OddDivCount ( int a , int b ) { int res = 0 ; for ( int i = a ; i <= b ; ++ i ) { int divCount = 0 ; for ( int j = 1 ; j <= i ; ++ j ) { if ( i % j == 0 ) { ++ divCount ; } } if ( divCount % 2 ) { ++ res ; } } return res ; } int main ( ) { int a = 1 , b = 10 ; cout << OddDivCount ( a , b ) << endl ; return 0 ; }
Largest factor of a given number which is a perfect square | C ++ program to find the largest factor of a number which is also a perfect square ; Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int largestSquareFactor ( int num ) { int answer = 1 ; for ( int i = 2 ; i < sqrt ( num ) ; ++ i ) { int cnt = 0 ; int j = i ; while ( num % j == 0 ) { cnt ++ ; j *= i ; } if ( cnt & 1 ) { cnt -- ; answer *= pow ( i , cnt ) ; } else { answer *= pow ( i , cnt ) ; } } return answer ; } int main ( ) { int N = 420 ; cout << largestSquareFactor ( N ) ; return 0 ; }
Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | CPP program to find Nth term of the series 2 + 6 + 13 + 23 + 36 + ... ; calculate Nth term of given series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Nth_Term ( int n ) { return ( 3 * pow ( n , 2 ) - n + 2 ) / ( 2 ) ; } int main ( ) { int N = 5 ; cout << Nth_Term ( N ) << endl ; }
All possible numbers of N digits and base B without leading zeros | C ++ implementation of the approach ; function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPermutations ( int N , int B ) { int x = pow ( B , N ) ; int y = pow ( B , N - 1 ) ; cout << x - y << " STRNEWLINE " ; } int main ( ) { int N = 6 ; int B = 4 ; countPermutations ( N , B ) ; return 0 ; }
Absolute difference between the Product of Non | C ++ program to find the Absolute Difference between the Product of Non - Prime numbers and Prime numbers of an Array ; Function to find the difference between the product of non - primes and the product of primes of an 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 ; Store the product of primes in P1 and the product of non primes in P2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateDifference ( 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 P1 = 1 , P2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { P1 *= arr [ i ] ; } else if ( arr [ i ] != 1 ) { P2 *= arr [ i ] ; } } return abs ( P2 - P1 ) ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << calculateDifference ( arr , n ) ; return 0 ; }
Maximum count of equal numbers in an array after performing given operations | CPP program to find the maximum number of equal numbers in an array ; Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver Code ; size of an array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int EqualNumbers ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; if ( sum % n ) return n - 1 ; return n ; } int main ( ) { int a [ ] = { 1 , 4 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << EqualNumbers ( a , n ) ; return 0 ; }
Count number of ordered pairs with Even and Odd Sums | C ++ implementation of the above approach ; function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_odd_pair ( int n , int a [ ] ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } int ans = odd * even * 2 ; return ans ; } int count_even_pair ( int odd_sum_pairs , int n ) { int total_pairs = ( n * ( n - 1 ) ) ; int ans = total_pairs - odd_sum_pairs ; return ans ; } int main ( ) { int n = 6 ; int a [ ] = { 2 , 4 , 5 , 9 , 1 , 8 } ; int odd_sum_pairs = count_odd_pair ( n , a ) ; int even_sum_pairs = count_even_pair ( odd_sum_pairs , n ) ; cout << " Even ▁ Sum ▁ Pairs ▁ = ▁ " << even_sum_pairs << endl ; cout << " Odd ▁ Sum ▁ Pairs = ▁ " << odd_sum_pairs << endl ; return 0 ; }
Steps required to visit M points in order on a circular ring of N points | C ++ implementation of the approach ; Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSteps ( int n , int m , int a [ ] ) { int cur = 1 ; int steps = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( a [ i ] >= cur ) steps += ( a [ i ] - cur ) ; else steps += ( n - cur + a [ i ] ) ; cur = a [ i ] ; } return steps ; } int main ( ) { int n = 3 , m = 3 ; int a [ ] = { 2 , 1 , 2 } ; cout << findSteps ( n , m , a ) ; }
Program to Convert Hexadecimal Number to Binary | C ++ program to convert Hexadecimal number to Binary ; function to convert Hexadecimal to Binary Number ; driver code ; Get the Hexadecimal number ; Convert HexaDecimal to Binary
#include <bits/stdc++.h> NEW_LINE using namespace std ; void HexToBin ( string hexdec ) { long int i = 0 ; while ( hexdec [ i ] ) { switch ( hexdec [ i ] ) { case '0' : cout << "0000" ; break ; case '1' : cout << "0001" ; break ; case '2' : cout << "0010" ; break ; case '3' : cout << "0011" ; break ; case '4' : cout << "0100" ; break ; case '5' : cout << "0101" ; break ; case '6' : cout << "0110" ; break ; case '7' : cout << "0111" ; break ; case '8' : cout << "1000" ; break ; case '9' : cout << "1001" ; break ; case ' A ' : case ' a ' : cout << "1010" ; break ; case ' B ' : case ' b ' : cout << "1011" ; break ; case ' C ' : case ' c ' : cout << "1100" ; break ; case ' D ' : case ' d ' : cout << "1101" ; break ; case ' E ' : case ' e ' : cout << "1110" ; break ; case ' F ' : case ' f ' : cout << "1111" ; break ; default : cout << " Invalid hexadecimal digit " << hexdec [ i ] ; } i ++ ; } } int main ( ) { char hexdec [ 100 ] = "1AC5" ; cout << " Equivalent Binary value is : " HexToBin ( hexdec ) ; return 0 ; }
Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | C ++ program to Count unordered pairs ( i , j ) in array such that product of a [ i ] and a [ j ] can be expressed as power of two ; Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int x ) { return x && ( ! ( x & ( x - 1 ) ) ) ; } void Count_pairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) ) count ++ ; } int ans = ( count * ( count - 1 ) ) / 2 ; cout << ans << " STRNEWLINE " ; } int main ( ) { int a [ ] = { 2 , 5 , 8 , 16 , 128 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; Count_pairs ( a , n ) ; return 0 ; }
Ways of dividing a group into two halves such that two elements are in different groups | CPP Program to count Number of ways in which two Beautiful girls are in different group ; This function will return the factorial of a given number ; This function will calculate nCr of given n and r ; This function will Calculate number of ways ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = result * i ; return result ; } int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } int calculate_result ( int n ) { int result = 2 * nCr ( ( n - 2 ) , ( n / 2 - 1 ) ) ; return result ; } int main ( void ) { int a = 2 , b = 4 ; cout << calculate_result ( 2 * a ) << endl ; cout << calculate_result ( 2 * b ) << endl ; return 0 ; }
Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | CPP program to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PossibleValues ( int b , int x , int n ) { int leastdivisible = ( b / x + 1 ) * x ; int flag = 1 ; while ( leastdivisible <= n ) { if ( leastdivisible - b >= 1 ) { cout << leastdivisible - b << " ▁ " ; leastdivisible += x ; flag = 0 ; } else break ; } if ( flag ) cout << -1 ; } int main ( ) { int b = 10 , x = 6 , n = 40 ; PossibleValues ( b , x , n ) ; return 0 ; }
Check if all sub | C ++ program to check if all sub - numbers have distinct Digit product ; Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; set to maintain digit products ; Finding all possible subarrays ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digitProduct ( int digits [ ] , int start , int end ) { int pro = 1 ; for ( int i = start ; i <= end ; i ++ ) { pro *= digits [ i ] ; } return pro ; } bool isDistinct ( int N ) { string s = to_string ( N ) ; int len = s . length ( ) ; int digits [ len ] ; unordered_set < int > products ; for ( int i = 0 ; i < len ; i ++ ) { digits [ i ] = s [ i ] - '0' ; } for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { int val = digitProduct ( digits , i , j ) ; if ( products . find ( val ) != products . end ( ) ) return false ; else products . insert ( val ) ; } } return true ; } int main ( ) { int N = 324 ; if ( isDistinct ( N ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Ulam Number Sequence | Cpp code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; Set to search specific Ulam number efficiently ; push First 2 two term of the sequence in the array and set for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; Check if i - arr [ j ] exist in the array or not using set If yes , Then i can be represented as sum of arr [ j ] + ( i - arr [ j ] ) ; if Count is greater than 2 break the loop ; If count is 2 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Driver code ; pre compute Ulam Number sequence ; print nth Ulam number
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10000 NEW_LINE vector < int > arr ; void ulam ( ) { unordered_set < int > s ; arr . push_back ( 1 ) ; s . insert ( 1 ) ; arr . push_back ( 2 ) ; s . insert ( 2 ) ; for ( int i = 3 ; i < MAX ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( s . find ( i - arr [ j ] ) != s . end ( ) && arr [ j ] != ( i - arr [ j ] ) ) count ++ ; if ( count > 2 ) break ; } if ( count == 2 ) { arr . push_back ( i ) ; s . insert ( i ) ; } } } int main ( ) { ulam ( ) ; int n = 9 ; cout << arr [ n - 1 ] ; return 0 ; }
Hilbert Number | CPP program to find nth hilbert Number ; Utility function to return Nth Hilbert Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long nthHilbertNumber ( int n ) { return 4 * ( n - 1 ) + 1 ; } int main ( ) { int n = 5 ; cout << nthHilbertNumber ( n ) ; return 0 ; }
Program to find the nth Kynea number | CPP code to find nth Kynea number ; Function to calculate nth kynea number ; Firstly calculate 2 ^ n + 1 ; Now calculate ( 2 ^ n + 1 ) ^ 2 ; Now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth Kynea number ; Driver Program ; print nth kynea number
#include <bits/stdc++.h> NEW_LINE using namespace std ; long nthKyneaNumber ( int n ) { n = ( 1 << n ) + 1 ; n = n * n ; n = n - 2 ; return n ; } int main ( ) { int n = 8 ; cout << nthKyneaNumber ( n ) ; return 0 ; }
Program to find the nth Kynea number | CPP code to find nth Kynea number ; Function to calculate nth kynea number ; Calculate nth kynea number ; Driver Program ; print nth kynea number
#include <bits/stdc++.h> NEW_LINE using namespace std ; long nthKyneaNumber ( int n ) { return ( ( 1 << ( 2 * n ) ) + ( 1 << ( n + 1 ) ) - 1 ) ; } int main ( ) { int n = 2 ; cout << nthKyneaNumber ( n ) ; return 0 ; }
Program to check whether a number is Proth number or not | CPP program to check Proth number ; Utility function to check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that k is odd number and n / k is a power of 2 greater than k ; Driver code ; Get n ; Check n for Proth Number
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int n ) { return ( n && ! ( n & ( n - 1 ) ) ) ; } bool isProthNumber ( int n ) { int k = 1 ; while ( k < ( n / k ) ) { if ( n % k == 0 ) { if ( isPowerOfTwo ( n / k ) ) return true ; } k = k + 2 ; } return false ; } int main ( ) { int n = 25 ; if ( isProthNumber ( n - 1 ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Find last two digits of sum of N factorials | C ++ program to find the unit place digit of the first N natural numbers factorials ; Function to find the unit ' s ▁ and ▁ ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 else ( N >= 10 ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define ll  long int NEW_LINE int get_last_two_digit ( long long int N ) { if ( N <= 10 ) { ll ans = 0 , fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fac = fac * i ; ans += fac ; } return ans % 100 ; } return 13 ; } int main ( ) { long long int N = 1 ; for ( N = 1 ; N <= 10 ; N ++ ) cout << " For ▁ N ▁ = ▁ " << N << " ▁ : ▁ " << get_last_two_digit ( N ) << endl ; return 0 ; }
Check whether product of ' n ' numbers is even or odd | C ++ implementation to check whether product of ' n ' numbers is even or odd ; function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isProductEven ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) == 0 ) return true ; return false ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isProductEven ( arr , n ) ) cout << " Even " ; else cout << " Odd " ; return 0 ; }
Sum of squares of Fibonacci numbers | C ++ Program to find sum of squares of Fibonacci numbers ; Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSquareSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ n + 1 ] ; fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ; int sum = ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += ( fibo [ i ] * fibo [ i ] ) ; } return sum ; } int main ( ) { int n = 6 ; cout << " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " << calculateSquareSum ( n ) << endl ; return 0 ; }
Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | C ++ implementation of the approach ; function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int fnMod ( int n ) { int rem = n % 4 ; if ( rem == 0 rem == 3 ) return 0 ; else if ( rem == 1 rem == 2 ) return 1 ; } int main ( ) { int n = 6 ; cout << fnMod ( n ) ; return 0 ; }
Minimum increment operations to make the array in increasing order | C ++ program to find minimum moves required to make the array in increasing order ; function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumMoves ( int a [ ] , int n , int x ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= a [ i - 1 ] ) { int p = ( a [ i - 1 ] - a [ i ] ) / x + 1 ; ans += p ; a [ i ] += p * x ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 3 , 2 } ; int x = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinimumMoves ( arr , n , x ) ; return 0 ; }
Check if a large number is divisible by 2 , 3 and 5 or not | CPP program to Check if a large number is divisible by 2 , 3 and 5 or not . ; function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SumOfDigits ( string str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( str [ i ] - '0' ) ; return sum ; } bool Divisible ( string str , int n ) { if ( SumOfDigits ( str , n ) % 3 == 0 and str [ n - 1 ] == '0' ) return true ; return false ; } int main ( ) { string str = "263730746028908374890" ; int n = str . size ( ) ; if ( Divisible ( str , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Count all the numbers in a range with smallest factor as K | C ++ program to find the count of numbers in a range whose smallest factor is K ; Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; Function to find count of numbers in range [ a , b ] with smallest factor as K ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int k ) { if ( k <= 1 ) return false ; for ( int i = 2 ; i < k ; i ++ ) if ( k % i == 0 ) return false ; return true ; } int check ( int num , int k ) { int flag = 1 ; for ( int i = 2 ; i < k ; i ++ ) { if ( num % i == 0 ) flag = 0 ; } if ( flag == 1 ) { if ( num % k == 0 ) return 1 ; else return 0 ; } else return 0 ; } int findCount ( int a , int b , int k ) { int count = 0 ; if ( ! isPrime ( k ) ) return 0 ; else { int ans ; for ( int i = a ; i <= b ; i ++ ) { ans = check ( i , k ) ; if ( ans == 1 ) count ++ ; else continue ; } } return count ; } int main ( ) { int a = 2020 , b = 6300 , k = 29 ; cout << findCount ( a , b , k ) ; return 0 ; }
Largest number with the given set of N digits that is divisible by 2 , 3 and 5 | C ++ implementation of above approach ; Function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner ; if there is just one element 0 ; find the remainder of the sum of digits when divided by 3 ; there can a remainder as 1 or 2 ; traverse from the end of the digits ; first element which has the same remainder remove it ; if there is no element which has a same remainder as y ; subtract it by 3 ( could be one or two ) ; delete two minimal digits which has a remainder as y ; print all the digits as a single integer ; Driver code ; initialize the number of set of digits ; initialize all the set of digits in a vector
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE int findLargest ( int n , vector < int > & v ) { int flag = 0 ; ll sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ i ] == 0 ) flag = 1 ; sum += v [ i ] ; } if ( ! flag ) cout << " Not ▁ possible " << endl ; else { sort ( v . begin ( ) , v . end ( ) , greater < int > ( ) ) ; if ( v [ 0 ] == 0 ) { cout << "0" << endl ; return 0 ; } else { int flag = 0 ; int y = sum % 3 ; if ( y != 0 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( v [ i ] % 3 == y ) { v . erase ( v . begin ( ) + i ) ; flag = 1 ; break ; } } if ( flag == 0 ) { y = 3 - y ; int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( v [ i ] % 3 == y ) { v . erase ( v . begin ( ) + i ) ; cnt ++ ; if ( cnt >= 2 ) break ; } } } } if ( * v . begin ( ) == 0 ) cout << "0" << endl ; else for ( int i : v ) { cout << i ; } } } } int main ( ) { int n = 11 ; vector < int > v { 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 } ; findLargest ( n , v ) ; return 0 ; }