codeparrot/xlcost-text-to-code huggingface dataset:
Sum of series formed by difference between product and sum of N natural numbers | C ++ program to calculate the sum upto the Nth term of the given series ; Recursive Function to calculate the sum upto Nth term ; If N - th term is calculated ; Update multi to store product upto K ; Update add to store sum upto K ; Update prevSum to store sum upto K ; Proceed to next K ; Function to calculate and return the Sum upto Nth term ; Recursive Function ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesSumUtil ( int k , int n , int prevSum , int multi , int add ) { if ( k == n + 1 ) { return prevSum ; } multi = multi * k ; add = add + k ; prevSum = prevSum + multi - add ; return seriesSumUtil ( k + 1 , n , prevSum , multi , add ) ; } int seriesSum ( int n ) { if ( n == 1 ) return 0 ; int prevSum = 0 ; int multi = 1 ; int add = 1 ; return seriesSumUtil ( 2 , n , prevSum , multi , add ) ; } int main ( ) { int N = 5 ; cout << seriesSum ( N ) << " ▁ " ; }
Count of total bits toggled / flipped in binary representation of 0 to N | C ++ program to count the number of toggles required to generate all numbers from 0 to N ; Function to count and print the required number of toggles ; Store the count of toggles ; Add the contribution of the current LSB ; Update N ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; void solve ( ll N ) { ll ans = 0 ; while ( N != 0 ) { ans += N ; N /= 2 ; } cout << ans << endl ; } int main ( ) { ll N = 5 ; solve ( N ) ; return 0 ; }
Maximum Bitwise AND pair ( X , Y ) from given range such that X and Y can be same | C ++ implementation to find the Maximum Bitwise AND pair ( X , Y ) from given range such that X and Y can be same ; Function to return the maximum bitwise AND ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumAND ( int L , int R ) { return R ; } int main ( ) { int l = 3 ; int r = 7 ; cout << maximumAND ( l , r ) ; return 0 ; }
Check if K distinct array elements form an odd sum | C ++ program for above approach ; Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool oddSum ( vector < int > & A , int N , int K ) { set < int > Odd ; set < int > Even ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . insert ( A [ i ] ) ; } else { Odd . insert ( A [ i ] ) ; } } if ( Odd . size ( ) >= K ) return true ; bool flag = false ; for ( int i = 1 ; i < K ; i += 2 ) { int needed = K - i ; if ( needed <= Even . size ( ) ) { return true ; } } return flag ; } int main ( ) { int K = 5 ; vector < int > A = { 12 , 1 , 7 , 7 , 26 , 18 } ; int N = 3 ; if ( oddSum ( A , N , K ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Count of elements not divisible by any other elements of Array | C ++ program for the above approach ; Function to count the number of elements of array which are not divisible by any other element of same array ; Length for boolean array ; Hash map for storing the element and it 's frequency ; Update the maximum element ; Boolean array of size of the max element + 1 ; Marking the multiples as false ; To store the final count ; Traverse boolean array ; Check if i is not divisible by any other array elements and appears in the array only once ; Return the final Count ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countEle ( int a [ ] , int n ) { int len = 0 ; unordered_map < int , int > hmap ; for ( int i = 0 ; i < n ; i ++ ) { len = max ( len , a [ i ] ) ; hmap [ a [ i ] ] ++ ; } bool v [ len + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) { v [ i ] = true ; } for ( int i = 0 ; i < n ; i ++ ) { if ( v [ a [ i ] ] == false ) continue ; for ( int j = 2 * a [ i ] ; j <= len ; j += a [ i ] ) { v [ j ] = false ; } } int count = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { if ( v [ i ] == true && hmap . count ( i ) == 1 && hmap [ i ] == 1 ) { count += 1 ; } } return count ; } int main ( ) { int arr [ ] = { 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << countEle ( arr , n ) ; return 0 ; }
Smallest N digit number divisible by N | C ++ program for the above approach ; Function to find the smallest N - digit number divisible by N ; Find largest n digit number ; Find smallest n digit number ; If i is divisible by N , then print i and return ; ; Driver Code ; Given Number ; Function Call
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { int L = pow ( 10 , N ) - 1 ; int S = pow ( 10 , N - 1 ) ; for ( int i = S ; i <= L ; i ++ ) { if ( i % N == 0 ) { cout << i ; return ; } } } int main ( ) { int N = 2 ; smallestNumber ( N ) ; return 0 ; }
Number less than equals to N with maximum product of prime factors | C ++ program for the above approach ; Function to find the smallest number having a maximum product of prime factors ; Declare the array arr [ ] ; Initialise array with 1 ; Iterate from [ 2 , N ] ; If value at index i is 1 , then i is prime and make update array at index for all multiples of i ; Initialise maxValue ; Find number having maximum product of prime factor <= N ; Find the maximum value ; Driven Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPrimefactorNum ( int N ) { int arr [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) arr [ i ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] == 1 ) { for ( int j = i ; j <= N ; j += i ) { arr [ j ] *= i ; } } } int maxValue = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] > maxValue ) { maxValue = i ; } } return maxValue ; } int main ( ) { int N = 20 ; cout << maxPrimefactorNum ( N ) ; return 0 ; }
Count of elements to be multiplied with integers to make each pair of Array a perfect square | C ++ program to find the minimum number of steps to modify the array such that the product of any two numbers in the array is a perfect square ; Function to find the smallest prime factor of the elements ; Initializing the first element of the array ; Loop to add the remaining elements to the array ; Marking the smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all the numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to find the minimum number of steps to modify the array such that the product of any two numbers in the array is a perfect square ; Map created to store the unique prime numbers ; Variable to store the minimum number of operations ; Loop to store every unique prime number ; Erasing 1 as a key because it is not a prime number ; Iterating through the hash ; Two variables used for counting the frequency of prime is even or odd ; First prime number ; Iterating the number D ; check if prime is a factor of the element in the array ; Loop for calculating the frequency of the element ; Check for frequency odd or even ; If it is not a factor of the element , then it is automatically even ; Storing the minimum of two variable even or odd ; Driver code ; Input array ; Creating shortest prime factorisation array
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; void spf_array ( int spf [ ] ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } int minimum_operation ( int b [ ] , int d , int spf [ ] ) { unordered_map < int , int > m ; int i = 0 ; int c = 0 ; for ( i = 0 ; i < d ; i ++ ) { int x = b [ i ] ; while ( x != 1 ) { x = x / spf [ x ] ; if ( m [ spf [ x ] ] == 0 ) { m [ spf [ x ] ] = 1 ; } } } m . erase ( 1 ) ; for ( auto x : m ) { int e = 0 , o = 0 ; int j = x . first ; for ( i = 0 ; i < d ; i ++ ) { if ( b [ i ] % j == 0 ) { int h = 0 ; int g = b [ i ] ; while ( g != 0 ) { if ( g % j != 0 ) { break ; } g = g / j ; h = h + 1 ; } if ( h % 2 == 0 ) { e = e + 1 ; } else { o = o + 1 ; } } else { e = e + 1 ; } } c = c + min ( o , e ) ; } return c ; } int main ( ) { int spf [ 1001 ] ; int b [ ] = { 1 , 4 , 6 } ; int d = sizeof ( b ) / sizeof ( b [ 0 ] ) ; spf_array ( spf ) ; cout << minimum_operation ( b , d , spf ) << endl ; }
Zuckerman Numbers | C ++ implementation to check if N is a Zuckerman number ; Function to get product of digits ; Function to check if N is an Zuckerman number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } bool isZuckerman ( int n ) { return n % getProduct ( n ) == 0 ; } int main ( ) { int n = 115 ; if ( isZuckerman ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Additive Prime Number | C ++ program for the above approach ; Check if N is prime or not ; Corner Cases ; This is checked to skip middle five numbers ; Function to get sum of digits ; Return the sum of digits ; Function to check whether the given number is Additive Prime number or not ; If number is not prime ; Check if sum of digits is prime or not ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } bool isAdditivePrime ( int n ) { if ( ! isPrime ( n ) ) return false ; return isPrime ( getSum ( n ) ) ; } int main ( ) { int N = 23 ; if ( isAdditivePrime ( N ) ) cout << " Yes " ; else cout << " No " ; }
Straight | C ++ program for the above approach ; Function to check if N is a Straight Line number or not ; N must be > 99 ; Difference between consecutive digits must be same ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isStraighLineNum ( int N ) { if ( N <= 99 ) return false ; string str = to_string ( N ) ; int d = str [ 1 ] - str [ 0 ] ; for ( int i = 2 ; i < str . length ( ) ; i ++ ) if ( str [ i ] - str [ i - 1 ] != d ) return false ; return true ; } int main ( ) { int N = 135 ; if ( isStraighLineNum ( n ) ) cout << " Yes " ; else cout << " No " ; }
Second | C ++ implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findNthTerm ( int n ) { cout << pow ( 2 , n ) - 2 * n << endl ; } int main ( ) { int N = 4 ; findNthTerm ( N ) ; return 0 ; }
Alternating Numbers | C ++ program for the above approach ; Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isEvenOddForm ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 0 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 1 ) return false ; } return true ; } bool isOddEvenForm ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 1 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 0 ) return false ; } return true ; } bool isAlternating ( int n ) { string str = to_string ( n ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } int main ( ) { int N = 129 ; if ( isAlternating ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Ormiston prime Pairs | C ++ implementation to check Ormiston prime ; Function to check if the number is a prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; 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 anagrams 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 ; Returns true if n1 and n2 are Ormiston primes ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } 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 ; } bool OrmistonPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && areAnagrams ( n1 , n2 ) ) ; } int main ( ) { int n1 = 1913 , n2 = 1931 ; if ( OrmistonPrime ( n1 , n2 ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Decakismyriagon Number | C ++ program for the above approach ; Function to find the N - th Decakismyriagon Number ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int DecakismyriagonNum ( int N ) { return ( 99998 * N * N - 99996 * N ) / 2 ; } int main ( ) { int N = 3 ; cout << DecakismyriagonNum ( N ) ; return 0 ; }
Untouchable Number | C ++ program for the above approach ; Function to calculate sum of all proper divisors of num ; Final result of summation of divisors ; Find all divisors of num ; If ' i ' is divisor of ' num ' ; If both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Function to check if N is a Untouchable Number ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int num ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result + 1 ) ; } bool isUntouchable ( int n ) { for ( int i = 1 ; i <= 2 * n ; i ++ ) { if ( divSum ( i ) == n ) return false ; } return true ; } int main ( ) { int N = 52 ; if ( isUntouchable ( n ) ) cout << " Yes " ; else cout << " No " ; }
Zygodrome Number | C ++ implementation to check if N is an zygodrome number ; Function to check if N is an zygodrome number ; convert N to string ; Adding a space at the beginning and end of the string ; Traverse the string ; If any character is not same as prev and next then return false ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool iszygodromeNum ( int N ) { string s = to_string ( N ) ; s = ' ▁ ' + s + ' ▁ ' ; for ( int i = 1 ; i < s . size ( ) - 1 ; i ++ ) { if ( s [ i ] != s [ i - 1 ] && s [ i ] != s [ i + 1 ] ) { return false ; } } return true ; } int main ( ) { int n = 1122 ; if ( iszygodromeNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Loeschian Number | C ++ program for the above approach ; Function to check if N is a Loeschian Number ; Iterate [ 0 , sqrt ( N ) ] for x ; Iterate [ 0 , sqrt ( N ) ] for y ; Check the given criteria ; If no such pair found then return false ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isLoeschian ( int n ) { for ( int x = 1 ; x <= sqrt ( n ) ; x ++ ) { for ( int y = 1 ; y <= sqrt ( n ) ; y ++ ) { if ( x * x + x * y + y * y == n ) return true ; } } return false ; } int main ( ) { int N = 19 ; if ( isLoeschian ( n ) ) cout << " Yes " ; else cout << " No " ; }
Admirable Numbers | C ++ implementation to check if N is an admirable number ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add ; Add 1 and n to result as above loop considers proper divisors greater ; Function to check if there exists a proper divisor D ' ▁ of ▁ N ▁ such ▁ that ▁ sigma ( n ) -2D ' = 2 N ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Check 1 since 1 is also a divisor ; Function to check if N is an admirable number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } bool check ( int num ) { int sigmaN = divSum ( num ) ; for ( int i = 2 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) { if ( sigmaN - 2 * i == 2 * num ) return true ; } else { if ( sigmaN - 2 * i == 2 * num ) return true ; if ( sigmaN - 2 * ( num / i ) == 2 * num ) return true ; } } } if ( sigmaN - 2 * 1 == 2 * num ) return true ; return false ; } bool isAdmirableNum ( int N ) { return check ( N ) ; } int main ( ) { int n = 12 ; if ( isAdmirableNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Program to print the series 2 , 15 , 41 , 80 , 132 , 197 … till N terms | C ++ implementation to print the given with the given Nth term ; Function to print the series ; Generate the ith term and ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void printSeries ( int N ) { int ith_term = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { ith_term = ( 13 * i * ( i - 1 ) ) / 2 + 2 ; cout << ith_term << " , ▁ " ; } } int main ( ) { int N = 7 ; printSeries ( N ) ; return 0 ; }
Possible pairs forming a Pythagorean Triple with a given value | C ++ program to compute all the possible pairs that forms a pythagorean triple with a given value ; Function to generate all possible pairs ; Vector to store all the possible pairs ; Checking all the possible pair in the range of [ 1 , c ) ; If the pair satisfies the condition push it in the vector ; Driver Program ; If no valid pair exist ; Print all valid pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < int , int > > Pairs ( int C ) { vector < pair < int , int > > ans ; for ( int i = 1 ; i < C ; i ++ ) { for ( int j = i + 1 ; j < C ; j ++ ) { if ( ( i * i ) + ( j * j ) == ( C * C ) ) { ans . push_back ( make_pair ( i , j ) ) ; } } } return ans ; } int main ( ) { int C = 13 ; vector < pair < int , int > > ans = Pairs ( C ) ; if ( ans . size ( ) == 0 ) { cout << " No ▁ valid ▁ pair ▁ exist " << endl ; return 0 ; } for ( auto i = ans . begin ( ) ; i != ans . end ( ) ; i ++ ) { cout << " ( " << i -> first << " , ▁ " << i -> second << " ) " << endl ; } return 0 ; }
How to calculate strike rate of a batsman | C ++ program to calculate strike rate of a batsman ; function to calculate strike rate of a batsman ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float strikerate ( int bowls , int runs ) { float z ; z = ( float ( runs ) / bowls ) * 100 ; return z ; } int main ( ) { int A , B ; A = 264 ; B = 173 ; cout << strikerate ( B , A ) << endl ; return 0 ; }
Check if a number exists with X divisors out of which Y are composite | C ++ program to check if a number exists having exactly X positive divisors out of which Y are composite divisors ; Count the number of times 2 divides N ; check for all possible numbers that can divide it ; if N at the end is a prime number . ; Function to check if any such number exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorize ( int N ) { int count = 0 ; int cnt = 0 ; while ( ( N % 2 ) == 0 ) { N = N / 2 ; count ++ ; } cnt = cnt + count ; for ( int i = 3 ; i <= sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } cnt = cnt + count ; } if ( N > 2 ) cnt = cnt + 1 ; return cnt ; } void ifNumberExists ( int X , int Y ) { int C , dsum ; C = X - Y - 1 ; dsum = factorize ( X ) ; if ( dsum >= C ) cout << " YES ▁ STRNEWLINE " ; else cout << " NO ▁ STRNEWLINE " ; } int main ( ) { int X , Y ; X = 6 ; Y = 4 ; ifNumberExists ( X , Y ) ; return 0 ; }
Nearest prime number in the array of every array element | C ++ program to find nearest prime number in the array for all array elements ; Create a boolean array and set all entries it as false . A value in prime [ i ] will be true if i is not a prime , else false ; Sieve of Eratosthenes function ; Update all multiples of i greater than or equal to the square of it numbers which are multiple of i and are less than i ^ 2 are already been marked . ; Function to find nearest prime number for all elements ; Compute and store all prime numbers up to maxm ; Store the indices of all primes ; If no primes are present in the array ; Store the current prime ; If the no further primes exist in the array ; For all indices less than that of the current prime ; If the current prime is nearer ; If the next prime is nearer ; Make the next prime as the current ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define max  10000000 NEW_LINE bool prime [ max ] = { false } ; void SieveOfEratosthenes ( int maxm ) { prime [ 0 ] = prime [ 1 ] = true ; for ( int i = 2 ; i * i <= maxm ; i ++ ) { if ( ! prime [ i ] ) { for ( int j = i * i ; j <= maxm ; j += i ) { prime [ j ] = true ; } } } } void print_nearest_prime ( int arr [ ] , int N ) { int maxm = * max_element ( arr , arr + N ) ; SieveOfEratosthenes ( maxm ) ; vector < int > primes ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! prime [ arr [ i ] ] ) primes . push_back ( i ) ; } if ( primes . size ( ) == 0 ) { for ( int i = 0 ; i < N ; i ++ ) { cout << -1 << " ▁ " ; } return ; } int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( curr == primes . size ( ) - 1 || i <= primes [ curr ] ) { cout << arr [ primes [ curr ] ] << " ▁ " ; continue ; } if ( abs ( primes [ curr ] - i ) < abs ( primes [ curr + 1 ] - i ) ) { cout << arr [ primes [ curr ] ] << " ▁ " ; } else { curr ++ ; cout << arr [ primes [ curr ] ] << " ▁ " ; } } } int main ( ) { int N = 6 ; int arr [ ] = { 8 , 7 , 12 , 15 , 3 , 11 } ; print_nearest_prime ( arr , N ) ; return 0 ; }
Find position of given term in a series formed with only digits 4 and 7 allowed | C ++ program for the above approach ; Function to find the position of the number N ; To store the position of N ; Iterate through all digit of N ; If current digit is 7 ; If current digit is 4 ; Print the final position ; Driver Code ; Given number of the series ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPosition ( int n ) { int i = 0 ; int pos = 0 ; while ( n > 0 ) { if ( n % 10 == 7 ) { pos = pos + pow ( 2 , i + 1 ) ; } else { pos = pos + pow ( 2 , i ) ; } i ++ ; n = n / 10 ; } cout << pos ; } int main ( ) { int N = 777 ; findPosition ( N ) ; return 0 ; }
Average of Cubes of first N natural numbers | C ++ program for the above approach ; Function to find average of cubes ; Storing sum of cubes of numbers in sum ; Calculate sum of cubes ; Return average ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findAverageOfCube ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i * i * i ; } return sum / n ; } int main ( ) { int n = 3 ; cout << findAverageOfCube ( n ) ; }