codeparrot/xlcost-text-to-code huggingface dataset:
Count of Ks in the Array for a given range of indices after array updates for Q queries | C ++ program for the above approach ; Function to perform all the queries ; Stores the count of 0 s ; Count the number of 0 s for query of type 1 ; Update the array element for query of type 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void performQueries ( int n , int q , int k , vector < int > & arr , vector < vector < int > > & query ) { for ( int i = 1 ; i <= q ; i ++ ) { int count = 0 ; if ( query [ i - 1 ] [ 0 ] == 1 ) { for ( int j = query [ i - 1 ] [ 1 ] ; j <= query [ i - 1 ] [ 2 ] ; j ++ ) { if ( arr [ j ] == k ) count ++ ; } cout << count << endl ; } else { = arr [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; } } } int main ( ) { vector < int > arr = { 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 } ; int Q = 5 ; vector < vector < int > > query = { { 1 , 5 , 14 } , { 2 , 6 , 1 } , { 1 , 0 , 8 } , { 2 , 13 , 0 } , { 1 , 6 , 18 } } ; int N = arr . size ( ) ; int K = 0 ; performQueries ( N , Q , K , arr , query ) ; return 0 ; }
Find smallest value of K such that bitwise AND of numbers in range [ N , N | C ++ program to find smallest value of K such that bitwise AND of numbers in range [ N , N - K ] is 0 ; Function is to find the largest no which gives the sequence n & ( n - 1 ) & ( n - 2 ) & ... . . & ( n - k ) = 0. ; Since , we need the largest no , we start from n itself , till 0 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findSmallestNumK ( int n ) { int cummAnd = n ; int i = n - 1 ; while ( cummAnd != 0 ) { cummAnd = cummAnd & i ; if ( cummAnd == 0 ) { return i ; } i -- ; } return -1 ; } int main ( ) { int N = 17 ; int lastNum = findSmallestNumK ( N ) ; int K = lastNum == -1 ? lastNum : N - lastNum ; cout << K << " STRNEWLINE " ; return 0 ; }
Program to find the value of P ( N + r ) for a polynomial of a degree N such that P ( i ) = 1 for 1 Ã ¢ â €°¤ i Ã ¢ â €°¤ N and P ( N + 1 ) = a | C ++ program for the above approach ; Function to calculate factorial of N ; Base Case ; Otherwise , recursively calculate the factorial ; Function to find the value of P ( n + r ) for polynomial P ( X ) ; Stores the value of k ; Store the required answer ; Iterate in the range [ 1 , N ] and multiply ( n + r - i ) with answer ; Add the constant value C as 1 ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { if ( n == 1 n == 0 ) return 1 ; else return n * fact ( n - 1 ) ; } int findValue ( int n , int r , int a ) { int k = ( a - 1 ) / fact ( n ) ; int answer = k ; for ( int i = 1 ; i < n + 1 ; i ++ ) answer = answer * ( n + r - i ) ; answer = answer + 1 ; return answer ; } int main ( ) { int N = 1 ; int A = 2 ; int R = 3 ; cout << ( findValue ( N , R , A ) ) ; return 0 ; }
Find winner in game of N balls , in which a player can remove any balls in range [ A , B ] in a single move | C ++ program of the above approach ; Function to find the winner of the game ; Stores sum of A and B ; If N is of the form m * ( A + B ) + y ; Otherwise , ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string NimGame ( int N , int A , int B ) { int sum = A + B ; if ( N % sum <= A - 1 ) return " Bob " ; else return " Alice " ; } int main ( ) { int N = 3 , A = 1 , B = 2 ; cout << NimGame ( N , A , B ) << endl ; return 0 ; }
Find Nth number in a sequence which is not a multiple of a given number | C ++ program for the above approach ; Function to find Nth number not a multiple of A in range [ L , R ] ; Calculate the Nth no ; Check for the edge case ; Driver Code ; Input parameters ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNo ( int A , int N , int L , int R ) { int ans = L - 1 + N + floor ( ( N - 1 ) / ( A - 1 ) ) ; if ( ans % A == 0 ) { ans = ans + 1 ; } cout << ans << endl ; } int main ( ) { int A = 5 , N = 10 , L = 4 , R = 20 ; countNo ( A , N , L , R ) ; return 0 ; }
Count of paths in given Binary Tree with odd bitwise AND for Q queries | C ++ implementation to count paths in Binary Tree with odd bitwise AND ; Function to count number of paths in binary tree such that bitwise AND of all nodes is Odd ; vector dp to store the count of bitwise odd paths till that vertex ; Precomputing for each value ; check for odd value ; Number of odd elements will be + 1 till the parent node ; For even case ; Since node is even Number of odd elements will be 0 ; Even value node will not contribute in answer hence dp [ i ] = previous answer ; Printing the answer for each query ; Driver code ; vector to store queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void compute ( vector < int > query ) { vector < int > v ( 100001 ) , dp ( 100001 ) ; v [ 1 ] = 1 , v [ 2 ] = 0 ; dp [ 1 ] = 0 , dp [ 2 ] = 0 ; for ( int i = 3 ; i < 100001 ; i ++ ) { if ( i % 2 != 0 ) { if ( ( i / 2 ) % 2 == 0 ) { v [ i ] = 1 ; dp [ i ] = dp [ i - 1 ] ; } else { v [ i ] = v [ i / 2 ] + 1 ; dp [ i ] = dp [ i - 1 ] + v [ i ] - 1 ; } } else { v [ i ] = 0 ; dp [ i ] = dp [ i - 1 ] ; } } for ( auto x : query ) cout << dp [ x ] << endl ; } int main ( ) { vector < int > query = { 5 , 2 } ; compute ( query ) ; return 0 ; }
Number of cycles formed by joining vertices of n sided polygon at the center | C ++ program for the above approach ; Function to calculate number of cycles ; BigInteger is used here if N = 10 ^ 9 then multiply will result into value greater than 10 ^ 18 ; BigInteger multiply function ; Return the final result ; Driver code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCycles ( int N ) { int res = 0 ; int finalResult = 0 ; int val = 2 * N - 1 ; int s = val ; res = ( N - 1 ) * ( N - 2 ) ; finalResult = res + s ; return finalResult ; } int main ( ) { int N = 5 ; cout << findCycles ( N ) << endl ; return 0 ; }
Split a given array into K subarrays minimizing the difference between their maximum and minimum | C ++ program for the above approach ; Function to find the subarray ; Add the difference to vectors ; Sort vector to find minimum k ; Initialize result ; Adding first k - 1 values ; Return the minimized sum ; Driver Code ; Given array arr [ ] ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( int a [ ] , int n , int k ) { vector < int > v ; for ( int i = 1 ; i < n ; ++ i ) { v . push_back ( a [ i - 1 ] - a [ i ] ) ; } sort ( v . begin ( ) , v . end ( ) ) ; int res = a [ n - 1 ] - a [ 0 ] ; for ( int i = 0 ; i < k - 1 ; ++ i ) { res += v [ i ] ; } return res ; } int main ( ) { int arr [ ] = { 4 , 8 , 15 , 16 , 23 , 42 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int K = 3 ; cout << find ( arr , N , K ) << endl ; return 0 ; }
Last digit of a number raised to last digit of N factorial | C ++ program for the above approach ; Function to find a ^ b using binary exponentiation ; Initialise result ; If b is odd then , multiply result by a ; b must be even now Change b to b / 2 ; Change a = a ^ 2 ; Function to find the last digit of the given equation ; To store cyclicity ; Store cyclicity from 1 - 10 ; Observation 1 ; Observation 3 ; To store the last digits of factorial 2 , 3 , and 4 ; Find the last digit of X ; Step 1 ; Divide a [ N ] by cyclicity of v ; If remainder is 0 ; Step 1.1 ; Step 1.2 ; Step 1.3 ; If r is non - zero , then return ( l ^ r ) % 10 ; Else return 0 ; Else return 1 ; Driver Code ; Given Numbers ; Function Call ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; long power ( long a , long b , long c ) { long result = 1 ; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { result = ( result * a ) % c ; } b /= 2 ; a = ( a * a ) % c ; } return result ; } long calculate ( long X , long N ) { int a [ 10 ] ; int cyclicity [ 11 ] ; cyclicity [ 1 ] = 1 ; cyclicity [ 2 ] = 4 ; cyclicity [ 3 ] = 4 ; cyclicity [ 4 ] = 2 ; cyclicity [ 5 ] = 1 ; cyclicity [ 6 ] = 1 ; cyclicity [ 7 ] = 4 ; cyclicity [ 8 ] = 4 ; cyclicity [ 9 ] = 2 ; cyclicity [ 10 ] = 1 ; if ( N == 0 N == 1 ) { return ( X % 10 ) ; } else if ( N == 2 N == 3 N == 4 ) { long temp = ( long ) 1e18 ; a [ 2 ] = 2 ; a [ 3 ] = 6 ; a [ 4 ] = 4 ; long v = X % 10 ; if ( v != 0 ) { int u = cyclicity [ ( int ) v ] ; int r = a [ ( int ) N ] % u ; if ( r == 0 ) { if ( v == 2 v == 4 v == 6 v == 8 ) { return 6 ; } else if ( v == 5 ) { return 5 ; } else if ( v == 1 v == 3 v == 7 v == 9 ) { return 1 ; } } else { return ( power ( v , r , temp ) % 10 ) ; } } else { return 0 ; } } return 1 ; } int main ( ) { int X = 18 ; int N = 4 ; long result = calculate ( X , N ) ; cout << result ; }
Maximize 3 rd element sum in quadruplet sets formed from given Array | C ++ code to Maximize 3 rd element sum in quadruplet sets formed from given Array ; Function to find the maximum possible value of Y ; pairs contain count of minimum elements that will be utilized at place of Z . it is equal to count of possible pairs that is size of array divided by 4 ; sorting the array in descending order so as to bring values with minimal difference closer to arr [ i ] ; here , i + 2 acts as a pointer that points to the third value of every possible quadruplet ; returning the optimally maximum possible value ; Driver code ; array declaration ; size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int formQuadruplets ( int arr [ ] , int n ) { int ans = 0 , pairs = 0 ; pairs = n / 4 ; sort ( arr , arr + n , greater < int > ( ) ) ; for ( int i = 0 ; i < n - pairs ; i += 3 ) { ans += arr [ i + 2 ] ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 1 , 7 , 5 , 5 , 4 , 1 , 1 , 3 , 3 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << formQuadruplets ( arr , n ) << endl ; return 0 ; }
Minimum concatenation required to get strictly LIS for array with repetitive elements | Set | C ++ implementation to Find the minimum concatenation required to get strictly Longest Increasing Subsequence for the given array with repetitive elements ; ordered map containing value and a vector containing index of it 's occurrences ; Mapping index with their values in ordered map ; k refers to present minimum index ; Stores the number of concatenation required ; Iterate over map m ; it . second . back refers to the last element of corresponding vector ; find the index of next minimum element in the sequence ; Return the final answer ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LIS ( int arr [ ] , int n ) { map < int , vector < int > > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] . push_back ( i ) ; int k = n ; int ans = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) { if ( it -> second . back ( ) < k ) { k = it -> second [ 0 ] ; ans += 1 ; } else k = * lower_bound ( it -> second . begin ( ) , it -> second . end ( ) , k ) ; } cout << ans << endl ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; LIS ( arr , n ) ; return 0 ; }
Two Balls Reachability Game | C ++ program to Find is it possible to have X white and Y black balls at the end . ; Recursive function to return gcd of a and b ; Function returns if it 's  possible to have X white and Y black balls or not. ; Finding gcd of ( x , y ) and ( a , b ) ; If gcd is same , it 's always  possible to reach (x, y) ; Here it 's never possible  if gcd is not same ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void IsPossible ( int a , int b , int x , int y ) { int final = gcd ( x , y ) ; int initial = gcd ( a , b ) ; if ( initial == final ) { cout << " POSSIBLE STRNEWLINE " ; } else { cout << " NOT ▁ POSSIBLE STRNEWLINE " ; } } int main ( ) { int A = 1 , B = 2 , X = 4 , Y = 11 ; IsPossible ( A , B , X , Y ) ; A = 2 , B = 2 , X = 3 , Y = 6 ; IsPossible ( A , B , X , Y ) ; return 0 ; }
Number of ways to color N | C ++ program for the above approach ; Function to count the ways to color block ; For storing powers of 2 ; For storing binomial coefficient values ; Calculating binomial coefficient using DP ; Calculating powers of 2 ; Sort the indices to calculate length of each section ; Initialise answer to 1 ; Find the length of each section ; Merge this section ; Return the final count ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1000000007 ; int waysToColor ( int arr [ ] , int n , int k ) { int powOf2 [ 500 ] = { 0 } ; int c [ 500 ] [ 500 ] ; for ( int i = 0 ; i <= n ; i ++ ) { c [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { c [ i ] [ j ] = ( c [ i - 1 ] [ j ] + c [ i - 1 ] [ j - 1 ] ) % mod ; } } powOf2 [ 0 ] = powOf2 [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { powOf2 [ i ] = powOf2 [ i - 1 ] * 2 % mod ; } int rem = n - k ; arr [ k ++ ] = n + 1 ; sort ( arr , arr + k ) ; int answer = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ i ] - ( i - 1 >= 0 ? arr [ i - 1 ] : 0 ) - 1 ; answer *= c [ rem ] [ x ] % mod * ( i != 0 && i != k - 1 ? powOf2 [ x ] : 1 ) % mod ; rem -= x ; } return answer ; } int main ( ) { int N = 6 ; int K = 3 ; int arr [ K ] = { 1 , 2 , 6 } ; cout << waysToColor ( arr , N , K ) ; return 0 ; }
Rearrange array such that difference of adjacent elements is in descending order | C ++ implementation to Rearrange array such that difference of adjacent elements is in descending order ; Function to print array in given order ; Sort the array ; Check elements till the middle index ; check if length is odd print the middle index at last ; Print the remaining elements in the described order ; Driver code ; array declaration ; size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int * a , int n ) { sort ( a , a + n ) ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( i == j ) { cout << a [ i ] << " ▁ " ; } else { cout << a [ j ] << " ▁ " ; cout << a [ i ] << " ▁ " ; } i = i + 1 ; j = j - 1 ; } cout << endl ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; printArray ( arr1 , n1 ) ; }
Find the Smallest number that divides X ^ X | C ++ implementation of above approach ; Function to find the required smallest number ; Finding smallest number that divides n ; i divides n and return this value immediately ; If n is a prime number then answer should be n , As we can 't take 1 as our answer. ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SmallestDiv ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return i ; } } return n ; } int main ( ) { int X = 385 ; int ans = SmallestDiv ( X ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Find an array of size N that satisfies the given conditions | C ++ implementation of the approach ; Utility function to print the contents of an array ; Function to generate and print the required array ; Initially all the positions are empty ; To store the count of positions i such that arr [ i ] = s ; To store the final array elements ; Set arr [ i ] = s and the gap between them is exactly 2 so in for loop we use i += 2 ; Mark the i 'th position as visited  as we put arr[i] = s ; Increment the count ; Finding the next odd number after s ; If the i 'th position is not visited  it means we did not put any value  at position i so we put 1 now ; Print the final 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 findArray ( int n , int k , int s ) { int vis [ n ] = { 0 } ; int cnt = 0 ; int arr [ n ] ; for ( int i = 0 ; i < n && cnt < k ; i += 2 ) { arr [ i ] = s ; vis [ i ] = 1 ; cnt ++ ; } int val = s ; if ( s % 2 == 0 ) val ++ ; else val = val + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { arr [ i ] = val ; } } printArr ( arr , n ) ; } int main ( ) { int n = 8 , k = 3 , s = 12 ; findArray ( n , k , s ) ; return 0 ; }
Sum of numbers in a range [ L , R ] whose count of divisors is prime | C ++ implementation of the approach ; prime [ i ] stores 1 if i is prime ; divi [ i ] stores the count of divisors of i ; sum [ i ] will store the sum of all the integers from 0 to i whose count of divisors is prime ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be 0 if i is Not a prime , else true . ; 0 and 1 is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count the divisors ; For each number i we will go to each of the multiple of i and update the count of divisor of its multiple j as i is one of the factor of j ; Function for pre - computation ; If count of divisors of i is prime ; taking prefix sum ; Driver code ; Find all the prime numbers till N ; Update the count of divisors of all the numbers till N ; Precomputation for the prefix sum array ; Perform query
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 100000 ; int prime [ N ] ; int divi [ N ] ; int sum [ N ] ; void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < N ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * p ; i < N ; i += p ) prime [ i ] = 0 ; } } } void DivisorCount ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { divi [ j ] ++ ; } } } void pre ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( prime [ divi [ i ] ] == 1 ) { sum [ i ] = i ; } } for ( int i = 1 ; i < N ; i ++ ) sum [ i ] += sum [ i - 1 ] ; } int main ( ) { int l = 5 , r = 8 ; SieveOfEratosthenes ( ) ; DivisorCount ( ) ; pre ( ) ; cout << sum [ r ] - sum [ l - 1 ] ; return 0 ; }
Count total number of even sum sequences | C ++ implementation of the approach ; Iterative function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd then multiply x with the result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to return n ^ ( - 1 ) mod p ; Function to return ( nCr % p ) using Fermat 's little theorem ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the count of odd numbers from 1 to n ; Function to return the count of even numbers from 1 to n ; Function to return the count of the required sequences ; Take i even and n - i odd numbers ; Number of odd numbers must be even ; Total ways of placing n - i odd numbers in the sequence of n numbers ; Add this number to the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  1000000007 NEW_LINE #define ll  long long int NEW_LINE ll power ( ll x , ll y , ll p ) { ll res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll modInverse ( ll n , ll p ) { return power ( n , p - 2 , p ) ; } ll nCrModPFermat ( ll n , ll r , ll p ) { if ( r == 0 ) return 1 ; ll fac [ n + 1 ] ; fac [ 0 ] = 1 ; for ( ll i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p ; } ll countOdd ( ll n ) { ll x = n / 2 ; if ( n % 2 == 1 ) x ++ ; return x ; } ll counteEven ( ll n ) { ll x = n / 2 ; return x ; } ll CountEvenSumSequences ( ll n ) { ll count = 0 ; for ( ll i = 0 ; i <= n ; i ++ ) { ll even = i , odd = n - i ; if ( odd % 2 == 1 ) continue ; ll tot = ( power ( countOdd ( n ) , odd , M ) * nCrModPFermat ( n , odd , M ) ) % M ; tot = ( tot * power ( counteEven ( n ) , i , M ) ) % M ; count += tot ; count %= M ; } return count ; } int main ( ) { ll n = 5 ; cout << CountEvenSumSequences ( n ) ; return 0 ; }
Find the sum of prime numbers in the Kth array | C ++ implementation of the approach ; To store whether a number is prime or not ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the sum of primes in the Kth array ; Update vector v to store all the prime numbers upto MAX ; To store the sum of primes in the kth array ; Count of primes which are in the arrays from 1 to k - 1 ; k is the number of primes in the kth array ; A prime has been added to the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE bool prime [ MAX ] ; void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } int sumPrime ( int k ) { SieveOfEratosthenes ( ) ; vector < int > v ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) v . push_back ( i ) ; } int sum = 0 ; int skip = ( k * ( k - 1 ) ) / 2 ; while ( k > 0 ) { sum += v [ skip ] ; skip ++ ; k -- ; } return sum ; } int main ( ) { int k = 3 ; cout << sumPrime ( k ) ; return 0 ; }
Count all substrings having character K | C ++ implementation of the approach ; Function to return the index of the next occurrence of character ch in str starting from the given index ; Return the index of the first occurrence of ch ; No occurrence found ; Function to return the count of all the substrings of str which contain the character ch at least one ; To store the count of valid substrings ; Index of the first occurrence of ch in str ; No occurrence of ch after index i in str ; Substrings starting at index i and ending at indices j , j + 1 , ... , n - 1 are all valid substring ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextOccurrence ( string str , int n , int start , char ch ) { for ( int i = start ; i < n ; i ++ ) { if ( str [ i ] == ch ) return i ; } return -1 ; } int countSubStr ( string str , int n , char ch ) { int cnt = 0 ; int j = nextOccurrence ( str , n , 0 , ch ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j != -1 && j < i ) { j = nextOccurrence ( str , n , j + 1 , ch ) ; } if ( j == -1 ) break ; cnt += ( n - j ) ; } return cnt ; } int main ( ) { string str = " geeksforgeeks " ; int n = str . length ( ) ; char ch = ' k ' ; cout << countSubStr ( str , n , ch ) ; return 0 ; }
Check whether bitwise AND of N numbers is Even or Odd | C ++ implementation of the approach ; Function to check if the bitwise AND of the array elements is even or odd ; If at least an even element is present then the bitwise AND of the array elements will be even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkEvenOdd ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { cout << " Even " ; return ; } } cout << " Odd " ; } int main ( ) { int arr [ ] = { 2 , 12 , 20 , 36 , 38 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkEvenOdd ( arr , n ) ; return 0 ; }
Find number of magical pairs of string of length L | C ++ implementation of the approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; Y must be even now y = y >> 1 ; y = y / 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , unsigned int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int main ( ) { int L = 2 , P = pow ( 10 , 9 ) ; int ans = power ( 325 , L , P ) ; cout << ans << " STRNEWLINE " ; return 0 ; }