codeparrot/xlcost-text-to-code huggingface dataset:
Icosikaipentagon Number | C ++ program to find the N - th Icosikaipentagon Number ; Function to find the N - th icosikaipentagon Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int icosikaipentagonNum ( int N ) { return ( 23 * N * N - 21 * N ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ icosikaipentagon ▁ Number ▁ is ▁ " << icosikaipentagonNum ( n ) ; return 0 ; }
Chiliagon Number | C ++ program for above approach ; Finding the nth chiliagon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " << chiliagonNum ( n ) ; return 0 ; }
Pentacontagon number | C ++ program for above approach ; Finding the nth pentacontagon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " << pentacontagonNum ( n ) ; return 0 ; }
Number formed by adding product of its max and min digit K times | C ++ Code for the above approach ; function that returns the product of maximum and minimum digit of N number . ; finds the last digit . ; Moves to next digit ; Function to find the formed number ; K -- ; M ( 1 ) = N ; check if minimum digit is 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n ) { int r = n % 10 ; largest = max ( r , largest ) ; smallest = min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K -- ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } int main ( ) { int N = 487 , K = 100000000 ; cout << formed_no ( N , K ) << endl ; return 0 ; }
Logarithm tricks for Competitive Programming | C ++ implementation count the number of digits in a number ; Function to count the number of digits in a number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigit ( long long n ) { return floor ( log10 ( n ) + 1 ) ; } int main ( ) { double N = 80 ; cout << countDigit ( N ) ; return 0 ; }
Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | C ++ implementation to find sum of series of 1 + x ^ 2 + x ^ 3 + ... . + x ^ n ; Function to find the sum of the series and print N terms of the given series ; First Term ; Loop to print the N terms of the series and find their sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; cout << total << " ▁ " ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; cout << multi << " ▁ " ; multi = multi * x ; } cout << " STRNEWLINE " ; return total ; } int main ( ) { int x = 2 ; int n = 5 ; cout << fixed << setprecision ( 2 ) << sum ( x , n ) ; return 0 ; }
Find the remainder when N is divided by 4 using Bitwise AND operator | C ++ implementation to find N modulo 4 using Bitwise AND operator ; Function to find the remainder ; Bitwise AND with 3 ; Return x ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRemainder ( int n ) { int x = n & 3 ; return x ; } int main ( ) { int N = 43 ; int ans = findRemainder ( N ) ; cout << ans << endl ; return 0 ; }
Find all Autobiographical Numbers with given number of digits | C ++ implementation to find Autobiographical numbers with length N ; Function to return if the number is autobiographical or not ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; Initialise count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAutoBio ( int num ) { string autoStr ; int index , number , i , j , cnt ; autoStr = to_string ( num ) ; for ( int i = 0 ; i < autoStr . size ( ) ; i ++ ) { index = autoStr . at ( i ) - '0' ; cnt = 0 ; for ( j = 0 ; j < autoStr . size ( ) ; j ++ ) { number = autoStr . at ( j ) - '0' ; if ( number == i ) cnt ++ ; } if ( index != cnt ) return false ; } return true ; } void findAutoBios ( int n ) { int high , low , i , flag = 0 ; low = pow ( 10 , n - 1 ) ; high = pow ( 10 , n ) - 1 ; for ( i = low ; i <= high ; i ++ ) { if ( isAutoBio ( i ) ) { flag = 1 ; cout << i << " , ▁ " ; } } if ( ! flag ) cout << " There ▁ is ▁ no ▁ " << " Autobiographical ▁ number " << " ▁ with ▁ " << n << " ▁ digits STRNEWLINE " ; } int main ( ) { int N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; return 0 ; }
Check whether the number can be made palindromic after adding K | C ++ program to check whether the number can be made palindrome number after adding K ; Function to check whether a number is a palindrome or not ; Convert num to string ; Comparing kth character from the beginning and N - kth character from the end . If all the characters match , then the number is a palindrome ; If all the above conditions satisfy , it means that the number is a palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPalindrome ( int num ) { string str = to_string ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { cout << " No " ; return ; } l ++ ; r -- ; } cout << " Yes " ; return ; } int main ( ) { int n = 19 , k = 3 ; checkPalindrome ( n + k ) ; return 0 ; }
Count of subsets with sum equal to X using Recursion | C ++ program to print the count of subsets with sum equal to the given value X ; Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to 0 and returning the count ; Recursively calling the function for two cases Either the element can be counted in the subset If the element is counted , then the remaining sum to be checked is sum - the selected element If the element is not included , then the remaining sum to be checked is the total sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int subsetSum ( int arr [ ] , int n , int i , int sum , int count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << subsetSum ( arr , n , 0 , sum , 0 ) ; }
Distinct Prime Factors of an Array | cppimplementation of the above approach ; Function to return an array of prime numbers upto n using Sieve of Eratosthenes ; Function to return distinct prime factors from the given array ; Creating an empty array to store distinct prime factors ; Iterating through all the prime numbers and check if any of the prime numbers is a factor of the given input array ; Driver code ; Finding prime numbers upto 10000 using Sieve of Eratosthenes
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > sieve ( int n ) { vector < int > prime ( n + 1 , 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime [ p ] == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime [ i ] = 1 ; } p += 1 ; } vector < int > allPrimes ; for ( int i = 2 ; i < n ; i ++ ) if ( prime [ i ] == 0 ) allPrimes . push_back ( i ) ; return allPrimes ; } vector < int > distPrime ( vector < int > arr , vector < int > allPrimes ) { vector < int > list1 ; for ( int i : allPrimes ) { for ( int j : arr ) { if ( j % i == 0 ) { list1 . push_back ( i ) ; break ; } } } return list1 ; } int main ( ) { vector < int > allPrimes = sieve ( 10000 ) ; vector < int > arr = { 15 , 30 , 60 } ; vector < int > ans = distPrime ( arr , allPrimes ) ; cout << " [ " ; for ( int i : ans ) cout << i << " ▁ " ; cout << " ] " ; }
Sum of the count of number of adjacent squares in an M X N grid | C ++ implementation of the above approach ; function to calculate the sum of all cells adjacent value ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int m , int n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } int main ( ) { int m = 3 , n = 2 ; cout << sum ( m , n ) ; return 0 ; }
Count pairs in an array such that the absolute difference between them is ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¦¡¬°¥ K | C ++ implementation of the approach ; Function to return the count of required pairs ; Sort the given array ; To store the required count ; Update j such that it is always > i ; Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j ) ; Update the count of valid pairs ; Get to the next element to repeat the steps ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int cnt = 0 ; int i = 0 , j = 1 ; while ( i < n && j < n ) { j = ( j <= i ) ? ( i + 1 ) : j ; while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ; cnt += ( n - j ) ; i ++ ; } return cnt ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << count ( arr , n , k ) ; return 0 ; }
Find the volume of rectangular right wedge | CPP program to find volume of rectangular right wedge ; function to return volume of rectangular right wedge ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double volumeRec ( double a , double b , double e , double h ) { return ( ( ( b * h ) / 6 ) * ( 2 * a + e ) ) ; } int main ( ) { double a = 2 ; double b = 5 ; double e = 5 ; double h = 6 ; printf ( " Volume ▁ = ▁ % .1f " , volumeRec ( a , b , e , h ) ) ; return 0 ; }
Count squares with odd side length in Chessboard | C ++ implementation of the approach ; Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_square ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { int k = n - i + 1 ; count += ( k * k ) ; } return count ; } int main ( ) { int N = 8 ; cout << count_square ( N ) ; return 0 ; }
Count of elements whose absolute difference with the sum of all the other elements is greater than k | C ++ implementation of the approach ; Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static int countAnomalies ( int arr [ ] , int n , int k ) { int cnt = 0 ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 1 ; cout << countAnomalies ( arr , n , k ) ; }
Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | C ++ implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Driver code ; Function call ; Required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int d [ N ] , pre [ N ] ; void Positive_Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } int main ( ) { Positive_Divisors ( ) ; int n = 15 ; cout << pre [ n ] << endl ; return 0 ; }
Length of the smallest number which is divisible by K and formed by using 1 's only | C ++ implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return -1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) return len ; } return -1 ; } int main ( ) { int K = 7 ; cout << numLen ( K ) ; return 0 ; }
Find if the given number is present in the infinite sequence or not | C ++ implementation of the approach ; Function that returns true if the sequence will contain B ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool doesContainB ( int a , int b , int c ) { if ( a == b ) return true ; if ( ( b - a ) * c > 0 && ( b - a ) % c == 0 ) return true ; return false ; } int main ( ) { int a = 1 , b = 7 , c = 3 ; if ( doesContainB ( a , b , c ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | C ++ program to find the required permutation of first 2 * N natural numbers ; Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int n , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { int x = 2 * i - 1 ; int y = 2 * i ; if ( i <= k ) cout << y << " ▁ " << x << " ▁ " ; else cout << x << " ▁ " << y << " ▁ " ; } } int main ( ) { int n = 2 , k = 1 ; printPermutation ( n , k ) ; return 0 ; }
Maximize the sum of products of the degrees between any two vertices of the tree | C ++ implementation of above approach ; Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll maxSum ( int N ) { ll ans = 0 ; for ( int u = 1 ; u <= N ; u ++ ) { for ( int v = 1 ; v <= N ; v ++ ) { if ( u == v ) continue ; int degreeU = 2 ; if ( u == 1 u == N ) degreeU = 1 ; int degreeV = 2 ; if ( v == 1 v == N ) degreeV = 1 ; ans += ( degreeU * degreeV ) ; } } return ans ; } int main ( ) { int N = 6 ; cout << maxSum ( N ) ; }
Find integers that divides maximum number of elements of the array | CPP implementation of the approach ; Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumFactor ( vector < int > arr ) { int n = arr . size ( ) ; vector < int > rank ; vector < int > factors ; int max = * max_element ( arr . begin ( ) , arr . end ( ) ) ; for ( int i = 2 ; i <= max ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % i == 0 ) count += 1 ; rank . push_back ( count ) ; factors . push_back ( i ) ; } } int m = * max_element ( rank . begin ( ) , rank . end ( ) ) ; for ( int i = 0 ; i < rank . size ( ) ; i ++ ) { if ( rank [ i ] == m ) cout << factors [ i ] << " ▁ " ; } } int main ( ) { vector < int > arr = { 120 , 15 , 24 , 63 , 18 } ; maximumFactor ( arr ) ; }
Natural Numbers | CPP program to find sum of first n natural numbers . ; Returns sum of first n natural numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; }
Median | CPP program to find median ; Function for calculating median ; First we sort the array ; check for even case ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findMedian ( int a [ ] , int n ) { sort ( a , a + n ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Median ▁ = ▁ " << findMedian ( a , n ) << endl ; return 0 ; }
Mean | CPP program to find mean ; Function for calculating mean ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findMean ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Mean ▁ = ▁ " << findMean ( a , n ) << endl ; return 0 ; }
Check if the array has an element which is equal to product of remaining elements | C ++ implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square ; then check if its square root exist in the array or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckArray ( int arr [ ] , int n ) { int prod = 1 ; unordered_set < int > freq ; for ( int i = 0 ; i < n ; ++ i ) { freq . insert ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = sqrt ( prod ) ; if ( root * root == prod ) if ( freq . find ( root ) != freq . end ( ) ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( CheckArray ( arr , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Find minimum area of rectangle with given set of coordinates | C ++ Implementation of above approach ; function to find minimum area of Rectangle ; creating empty columns ; fill columns with coordinates ; check if rectangle can be formed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minAreaRect ( vector < vector < int > > A ) { map < int , vector < int > > columns ; for ( auto i : A ) columns [ i [ 0 ] ] . push_back ( i [ 1 ] ) ; map < pair < int , int > , int > lastx ; int ans = INT_MAX ; for ( auto x : columns ) { vector < int > column = x . second ; sort ( column . begin ( ) , column . end ( ) ) ; for ( int j = 0 ; j < column . size ( ) ; j ++ ) { for ( int i = 0 ; i < j ; i ++ ) { int y1 = column [ i ] ; if ( lastx . find ( { y1 , column [ j ] } ) != lastx . end ( ) ) { ans = min ( ans , ( x . first - lastx [ { y1 , column [ j ] } ] ) * ( column [ j ] - column [ i ] ) ) ; } lastx [ { y1 , column [ j ] } ] = x . first ; } } } if ( ans < INT_MAX ) return ans ; else return 0 ; } int main ( ) { vector < vector < int > > A = { { 1 , 1 } , { 1 , 3 } , { 3 , 1 } , { 3 , 3 } , { 2 , 2 } } ; cout << ( minAreaRect ( A ) ) ; return 0 ; }
Check whether a number has consecutive 0 's in the given base or not | C ++ implementation of the above approach ; Function to convert N into base K ; Weight of each digit ; Function to check for consecutive 0 ; Flag to check if there are consecutive zero or not ; If there are two consecutive zero then returning False ; We first convert to given base , then check if the converted number has two consecutive 0 s or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int toK ( int N , int K ) { int w = 1 ; int s = 0 ; while ( N != 0 ) { int r = N % K ; N = N / K ; s = r * w + s ; w *= 10 ; } return s ; } bool check ( int N ) { bool fl = false ; while ( N != 0 ) { int r = N % 10 ; N = N / 10 ; if ( fl == true and r == 0 ) return false ; if ( r > 0 ) { fl = false ; continue ; } fl = true ; } return true ; } void hasConsecutiveZeroes ( int N , int K ) { int z = toK ( N , K ) ; if ( check ( z ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int N = 15 ; int K = 8 ; hasConsecutiveZeroes ( N , K ) ; }
Sum of every Kâ €™ th prime number in an array | C ++ implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all the entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; Driver code ; create the sieve
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE bool prime [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } void SumOfKthPrimes ( int arr [ ] , int n , int k ) { int c = 0 ; long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } cout << sum << endl ; } int main ( ) { SieveOfEratosthenes ( ) ; int arr [ ] = { 2 , 3 , 5 , 7 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; SumOfKthPrimes ( arr , n , k ) ; return 0 ; }
Find the super power of a given Number | CPP for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver program
#include <bits/stdc++.h> NEW_LINE #define MAX  100000 NEW_LINE using namespace std ; bool prime [ 100002 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == true ) for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } int superpower ( int n ) { SieveOfEratosthenes ( ) ; int superPower = 0 , factor = 0 ; int i = 2 ; while ( n > 1 && i <= MAX ) { if ( prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } int main ( ) { int n = 256 ; cout << superpower ( n ) ; return 0 ; }