codeparrot/xlcost-text-to-code huggingface dataset:
Capacity To Ship Packages Within D Days | C ++ program for the above approach ; Function to check if the weights can be delivered in D days or not ; Stores the count of days required to ship all the weights if the maximum capacity is mx ; Traverse all the weights ; If total weight is more than the maximum capacity ; If days are more than D , then return false ; Return true for the days < D ; Function to find the least weight capacity of a boat to ship all the weights within D days ; Stores the total weights to be shipped ; Find the sum of weights ; Stores the maximum weight in the array that has to be shipped ; Store the ending value for the search space ; Store the required result ; Perform binary search ; Store the middle value ; If mid can be shipped , then update the result and end value of the search space ; Search for minimum value in the right part ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( int weight [ ] , int n , int D , int mx ) { int st = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += weight [ i ] ; if ( sum > mx ) { st ++ ; sum = weight [ i ] ; } if ( st > D ) return false ; } return true ; } void shipWithinDays ( int weight [ ] , int D , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += weight [ i ] ; int s = weight [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = max ( s , weight [ i ] ) ; } int e = sum ; int res = -1 ; while ( s <= e ) { int mid = s + ( e - s ) / 2 ; if ( isValid ( weight , n , D , mid ) ) { res = mid ; e = mid - 1 ; } else s = mid + 1 ; } cout << res ; } int main ( ) { int weight [ ] = { 9 , 8 , 10 } ; int D = 3 ; int N = sizeof ( weight ) / sizeof ( weight [ 0 ] ) ; shipWithinDays ( weight , D , N ) ; return 0 ; }
Shortest path for a thief to reach the Nth house avoiding policemen | C ++ program for the above approach ; Function to create graph edges where node A and B can be visited ; Visit all the connections ; If a policeman is at any point of connection , leave that connection . Insert the connect otherwise . ; Function to find the shortest path ; If police is at either at the 1 - st house or at N - th house ; The thief cannot reach the N - th house ; Stores Edges of graph ; Function call to store connections ; Stores wheather node is visited or not ; Stores distances from the root node ; Visit all nodes that are currently in the queue ; If current node is not visited already ; Driver Code ; N : Number of houses E : Number of edges ; Given positions ; Given Paths ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createGraph ( unordered_map < int , vector < int > > & adj , int paths [ ] [ 2 ] , int A [ ] , int N , int E ) { for ( int i = 0 ; i < E ; i ++ ) { if ( ! A [ paths [ i ] [ 0 ] ] && ! A [ paths [ i ] [ 1 ] ] ) { adj [ paths [ i ] [ 0 ] ] . push_back ( paths [ i ] [ 1 ] ) ; } } } int minPath ( int paths [ ] [ 2 ] , int A [ ] , int N , int E ) { if ( A [ 0 ] == 1 A [ N - 1 ] == 1 ) return -1 ; unordered_map < int , vector < int > > adj ; createGraph ( adj , paths , A , N , E ) ; vector < int > visited ( N , 0 ) ; int dist [ N ] ; dist [ 0 ] = 0 ; queue < int > q ; q . push ( 0 ) ; visited [ 0 ] = 1 ; while ( ! q . empty ( ) ) { int temp = q . front ( ) ; q . pop ( ) ; for ( auto x : adj [ temp ] ) { if ( ! visited [ x ] ) { q . push ( x ) ; visited [ x ] = 1 ; dist [ x ] = dist [ temp ] + 1 ; } } } if ( ! visited [ N - 1 ] ) return -1 ; else return dist [ N - 1 ] ; } int main ( ) { int N = 5 , E = 5 ; int A [ ] = { 0 , 1 , 0 , 0 , 0 } ; int paths [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 4 } , { 2 , 3 } , { 3 , 4 } } ; cout << minPath ( paths , A , N , E ) ; return 0 ; }
Find a point whose sum of distances from all given points on a line is K | C ++ program for the above approach ; Function to find the sum of distances of all points from a given point ; Stores sum of distances ; Traverse the array ; Return the sum ; Function to find such a point having sum of distances of all other points from this point equal to K ; If N is odd keep left as arr [ n / 2 ] else keep left as arr [ n / 2 - 1 ] + 1 ; ; Keep right as arr [ N - 1 ] ; Perform binary search in the right half ; Calculate the mid index of the range ; If temp is equal to K ; Print the value of mid ; If the value of K < temp ; Update right to mid - 1 ; If the value of K > temp ; Update left to mid + 1 ; Update the value of left ; Update the value of right ; Perform binary search on the left half ; Calculate the mid index of the range ; If temp is equal to K ; Print mid ; if K > temp ; Update right to mid - 1 ; If K < temp ; Update left to mid + 1 ; If no such point found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int * arr , int N , int pt ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += abs ( arr [ i ] - pt ) ; } return sum ; } void findPoint ( int * arr , int N , int K ) { int left ; if ( N % 2 ) { left = arr [ N / 2 ] ; } else { left = arr [ N / 2 - 1 ] + 1 ; } int right = arr [ N - 1 ] ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { cout << mid << endl ; return ; } else if ( K < temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } left = arr [ 0 ] ; right = arr [ N / 2 ] - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { cout << mid << endl ; return ; } else if ( K > temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } cout << " - 1" << endl ; } int main ( ) { int arr [ ] = { 1 , 3 , 6 , 7 , 11 } ; int K = 18 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPoint ( arr , N , K ) ; return 0 ; }
Generate an array consisting of most frequent greater elements present on the right side of each array element | C ++ program for the above approach ; Function to generate an array containing the most frequent greater element on the right side of each array element ; Stores the generated array ; Traverse the array arr [ ] ; Store the result for the current index and its frequency ; Iterate over the right subarray ; Store the frequency of the current array element ; If the frequencies are equal ; Update ans to smaller of the two elements ; If count of new element is more than count of ans ; Insert answer in the array ; Print the resultant array ; Driver Code ; Given Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArray ( int arr [ ] , int n ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { int ans = -1 , old_c = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { int curr_c = count ( & arr [ j ] , & arr [ n ] , arr [ j ] ) ; if ( curr_c == old_c ) { if ( arr [ j ] < ans ) ans = arr [ j ] ; } if ( curr_c > old_c ) { ans = arr [ j ] ; old_c = curr_c ; } } } v . push_back ( ans ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << v [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 4 , 5 , 2 , 25 , 10 , 5 , 10 , 3 , 10 , 5 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findArray ( arr , size ) ; return 0 ; }
Smallest index that splits an array into two subarrays with equal product | C ++ program for the above approach ; Function to find the smallest index that splits the array into two subarrays with equal product ; Stores the product of the array ; Traverse the given array ; Stores the product of left and the right subarrays ; Traverse the given array ; Update the products ; Check if product is equal ; Print resultant index ; If no partition exists , then print - 1. ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void prodEquilibrium ( int arr [ ] , int N ) { int product = 1 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } int left = 1 ; int right = product ; for ( int i = 0 ; i < N ; i ++ ) { left = left * arr [ i ] ; right = right / arr [ i ] ; if ( left == right ) { cout << i + 1 << endl ; return ; } } cout << -1 << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; prodEquilibrium ( arr , N ) ; return 0 ; }
Count triplets from a sorted array having difference between adjacent elements equal to D | C ++ program for the above approach ; Function to count the number of triplets having difference between adjacent elements equal to D ; Stores the frequency of array elements ; Stores the count of resultant triplets ; Traverse the array ; Check if arr [ i ] - D and arr [ i ] - 2 * D exists in the Hashmap or not ; Update the value of ans ; Increase the frequency of the current element ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int D , vector < int > & arr ) { unordered_map < int , int > freq ; int ans = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( freq . find ( arr [ i ] - D ) != freq . end ( ) && freq . find ( arr [ i ] - 2 * D ) != freq . end ( ) ) { ans += freq [ arr [ i ] - D ] * freq [ arr [ i ] - 2 * D ] ; } freq [ arr [ i ] ] ++ ; } return ans ; } int main ( ) { vector < int > arr { 1 , 2 , 4 , 5 , 7 , 8 , 10 } ; int D = 1 ; cout << countTriplets ( D , arr ) ; return 0 ; }
Find the array element having equal sum of Prime Numbers on its left and right | C ++ program for the above approach ; Function to find an index in the array having sum of prime numbers to its left and right equal ; Stores the maximum value present in the array ; Stores all positive elements which are <= max_value ; If 1 is present ; Remove 1 ; Sieve of Eratosthenes to store all prime numbers which are <= max_value in the Map ; Erase non - prime numbers ; Stores the sum of prime numbers from left ; Stores the sum of prime numbers to the left of each index ; Stores the sum of prime numbers to the left of the current index ; Add current value to the prime sum if the current value is prime ; Stores the sum of prime numbers from right ; Stores the sum of prime numbers to the right of each index ; Stores the sum of prime numbers to the right of the current index ; Add current value to the prime sum if the current value is prime ; Traverse through the two arrays to find the index ; Compare the values present at the current index ; Return the index where both the values are same ; No index is found . ; Driver Code ; Given array arr [ ] ; Size of Array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_index ( int arr [ ] , int N ) { int max_value = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { max_value = max ( max_value , arr [ i ] ) ; } map < int , int > store ; for ( int i = 1 ; i <= max_value ; i ++ ) { store [ i ] ++ ; } if ( store . find ( 1 ) != store . end ( ) ) { store . erase ( 1 ) ; } for ( int i = 2 ; i <= sqrt ( max_value ) ; i ++ ) { int multiple = 2 ; while ( ( i * multiple ) <= max_value ) { if ( store . find ( i * multiple ) != store . end ( ) ) { store . erase ( i * multiple ) ; } multiple ++ ; } } int prime_sum_from_left = 0 ; int first_array [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { first_array [ i ] = prime_sum_from_left ; if ( store . find ( arr [ i ] ) != store . end ( ) ) { prime_sum_from_left += arr [ i ] ; } } int prime_sum_from_right = 0 ; int second_array [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { second_array [ i ] = prime_sum_from_right ; if ( store . find ( arr [ i ] ) != store . end ( ) ) { prime_sum_from_right += arr [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( first_array [ i ] == second_array [ i ] ) { return i ; } } return -1 ; } int main ( ) { int arr [ ] = { 11 , 4 , 7 , 6 , 13 , 1 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << find_index ( arr , N ) ; return 0 ; }
Convert an array to reduced form | Set 3 ( Binary Search ) | C ++ program for the above approach ; Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Print the current index and break ; Update the value of l ; Update the value of r ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convert ( int arr [ ] , int n ) { int brr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; sort ( brr , brr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = 0 , r = n - 1 , mid ; while ( l <= r ) { mid = ( l + r ) / 2 ; if ( brr [ mid ] == arr [ i ] ) { cout << mid << ' ▁ ' ; break ; } else if ( brr [ mid ] < arr [ i ] ) { l = mid + 1 ; } else { r = mid - 1 ; } } } } int main ( ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; convert ( arr , N ) ; return 0 ; }
Find the array element having equal count of Prime Numbers on its left and right | C ++ program for the above approach ; Function to find the index of the array such that the count of prime numbers to its either ends are same ; Store the maximum value in the array ; Traverse the array arr [ ] ; Stores all the numbers ; Iterate over the range [ 1 , Max ] ; Increment the value of st [ i ] ; Removes 1 from the map St ; Perform Sieve of Prime Numbers ; While i * j is less than the maxValue ; If i * j is in map St ; Erase the value ( i * j ) ; Increment the value of j ; Stores the count of prime from index 0 to i ; Stores the count of prime numbers ; Traverse the array arr [ ] ; If arr [ i ] is present in the map st ; Stores the count of prime from index i to N - 1 ; Stores the count of prime numbers ; Iterate over the range [ 0 , N - 1 ] in reverse order ; If arr [ i ] is in map st ; Iterate over the range [ 0 , N - 1 ] ; If prefix [ i ] is equal to the Suffix [ i ] ; Return - 1 if no such index is present ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findIndex ( int arr [ ] , int N ) { int maxValue = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { maxValue = max ( maxValue , arr [ i ] ) ; } map < int , int > St ; for ( int i = 1 ; i <= maxValue ; i ++ ) { St [ i ] ++ ; } if ( St . find ( 1 ) != St . end ( ) ) { St . erase ( 1 ) ; } for ( int i = 2 ; i <= sqrt ( maxValue ) ; i ++ ) { int j = 2 ; while ( ( i * j ) <= maxValue ) { if ( St . find ( i * j ) != St . end ( ) ) { St . erase ( i * j ) ; } j ++ ; } } int LeftCount = 0 ; int Prefix [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Prefix [ i ] = LeftCount ; if ( St . find ( arr [ i ] ) != St . end ( ) ) { LeftCount ++ ; } } int RightCount = 0 ; int Suffix [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { Suffix [ i ] = RightCount ; if ( St . find ( arr [ i ] ) != St . end ( ) ) { RightCount ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( Prefix [ i ] == Suffix [ i ] ) { return i ; } } return -1 ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 7 , 5 , 10 , 1 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findIndex ( arr , N ) ; return 0 ; }
Numbers of pairs from an array whose average is also present in the array | C ++ program for the above approach ; Function to count the number of pairs from the array having sum S ; Stores the total count of pairs whose sum is 2 * S ; Generate all possible pairs and check their sums ; If the sum is S , then increment the count ; Return the total count of pairs ; Function to count of pairs having whose average exists in the array ; Initialize the count ; Use set to remove duplicates ; Add elements in the set ; For every sum , count all possible pairs ; Return the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCountPairs ( vector < int > arr , int N , int S ) { int count = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < arr . size ( ) ; j ++ ) { if ( ( arr [ i ] + arr [ j ] ) == S ) count ++ ; } } return count ; } int countPairs ( vector < int > arr , int N ) { int count = 0 ; unordered_set < int > S ; for ( int i = 0 ; i < N ; i ++ ) S . insert ( arr [ i ] ) ; for ( int ele : S ) { int sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } int main ( ) { vector < int > arr = { 4 , 2 , 5 , 1 , 3 , 5 } ; int N = arr . size ( ) ; cout << countPairs ( arr , N ) ; return 0 ; }
Count numbers up to N having exactly 5 divisors | C ++ program for the above approach ; Function to calculate the value of ( x ^ y ) using binary exponentiation ; Stores the value of x ^ y ; Base Case ; If y is odd multiply x with result ; Otherwise , divide y by 2 ; Function to perform the Sieve Of Eratosthenes to find the prime number over the range [ 1 , 10 ^ 5 ] ; If prime [ p ] is not changed then it is a prime ; Set all the multiples of p to non - prime ; Iterate over the range [ 1 , MAX ] ; Store all the prime number ; Function to find the primes having only 5 divisors ; Base Case ; First value of the pair has the prime number and the second value has the count of primes till that prime numbers ; Precomputing all the primes ; Perform the Binary search ; Calculate the fourth power of the curr and prev ; Return value of mid ; Return value of mid - 1 ; Update the value of high ; Update the value of low ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE const int MAX = 1e5 ; using namespace std ; ll power ( ll x , unsigned ll y ) { ll res = 1 ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } void SieveOfEratosthenes ( vector < pair < ll , ll > > & v ) { bool prime [ MAX + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = 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 ; } } int num = 1 ; for ( int i = 1 ; i <= MAX ; i ++ ) { if ( prime [ i ] ) { v . push_back ( { i , num } ) ; num ++ ; } } } int countIntegers ( ll n ) { if ( n < 16 ) { return 0 ; } vector < pair < ll , ll > > v ; SieveOfEratosthenes ( v ) ; int low = 0 ; int high = v . size ( ) - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; ll curr = power ( v [ mid ] . first , 4 ) ; ll prev = power ( v [ mid - 1 ] . first , 4 ) ; if ( curr == n ) { return v [ mid ] . second ; } else if ( curr > n and prev <= n ) { return v [ mid - 1 ] . second ; } else if ( curr > n ) { high = mid - 1 ; } else { low = mid + 1 ; } } return 0 ; } int main ( ) { ll N = 100 ; cout << countIntegers ( N ) ; return 0 ; }
Sort array of strings after sorting each string after removing characters whose frequencies are not a powers of 2 | C ++ program for the above approach ; Function to check if N is power of 2 or not ; Base Case ; Return true if N is power of 2 ; Function to print array of strings in ascending order ; Sort strings in ascending order ; Print the array ; Function to sort the strings after modifying each string according to the given conditions ; Store the frequency of each characters of the string ; Stores the required array of strings ; Traverse the array of strings ; Temporary string ; Stores frequency of each alphabet of the string ; Update frequency of S [ i ] [ j ] ; Traverse the map freq ; Check if the frequency of i . first is a power of 2 ; Update string st ; Clear the map ; Null string ; Sort the string in descending order ; Update res ; Print the array of strings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( ceil ( log2 ( n ) ) == floor ( log2 ( n ) ) ) ; } void printArray ( vector < string > res ) { sort ( res . begin ( ) , res . end ( ) ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { cout << res [ i ] << " ▁ " ; } } void sortedStrings ( string S [ ] , int N ) { unordered_map < char , int > freq ; vector < string > res ; for ( int i = 0 ; i < N ; i ++ ) { string st = " " ; for ( int j = 0 ; j < S [ i ] . size ( ) ; j ++ ) { freq [ S [ i ] [ j ] ] ++ ; } for ( auto i : freq ) { if ( isPowerOfTwo ( i . second ) ) { for ( int j = 0 ; j < i . second ; j ++ ) { st += i . first ; } } } freq . clear ( ) ; if ( st . size ( ) == 0 ) continue ; sort ( st . begin ( ) , st . end ( ) , greater < char > ( ) ) ; res . push_back ( st ) ; } printArray ( res ) ; } int main ( ) { string arr [ ] = { " aaacbb " , " geeks " , " aaa " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortedStrings ( arr , N ) ; return 0 ; }
Modify string by replacing all occurrences of given characters by specified replacing characters | C ++ program for the above approach ; Function to modify given string by replacing characters ; Store the size of string and the number of pairs ; Initialize 2 character arrays ; Traverse the string s Update arrays arr [ ] and brr [ ] ; Traverse the array of pairs p ; a -> Character to be replaced b -> Replacing character ; Iterate over the range [ 0 , 25 ] ; If it is equal to current character , then replace it in the array b ; Print the array brr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void replaceCharacters ( string s , vector < vector < char > > p ) { int n = s . size ( ) , k = p . size ( ) ; char arr [ 26 ] ; char brr [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s [ i ] - ' a ' ] = s [ i ] ; brr [ s [ i ] - ' a ' ] = s [ i ] ; } for ( int j = 0 ; j < k ; j ++ ) { char a = p [ j ] [ 0 ] , b = p [ j ] [ 1 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == a ) { brr [ i ] = b ; } } } for ( int i = 0 ; i < n ; i ++ ) { cout << brr [ s [ i ] - ' a ' ] ; } } int main ( ) { string S = " aabbgg " ; vector < vector < char > > P { { ' a ' , ' b ' } , { ' b ' , ' g ' } , { ' g ' , ' a ' } } ; replaceCharacters ( S , P ) ; return 0 ; }
Smallest Semi | C ++ program for the above approach ; Function to find all the prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the smallest semi - prime number having a difference between any of its two divisors at least N ; Stores the prime numbers ; Fill the prime array ; Initialize the first divisor ; Find the value of the first prime number ; Initialize the second divisor ; Find the second prime number ; Print the semi - prime number ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define MAX  100001 NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool prime [ ] ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } void smallestSemiPrime ( int n ) { bool prime [ MAX ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime ) ; int num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } int num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } cout << num1 * 1LL * num2 ; } int main ( ) { int N = 2 ; smallestSemiPrime ( N ) ; return 0 ; }
Check if a number can be represented as product of two positive perfect cubes | C ++ program for the above approach ; Function to check if N can be represented as the product of two perfect cubes or not ; Stores the perfect cubes ; Traverse the Map ; Stores the first number ; Stores the second number ; Search the pair for the first number to obtain product N from the Map ; If N cannot be represented as the product of the two positive perfect cubes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void productOfTwoPerfectCubes ( int N ) { map < int , int > cubes ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes [ i * i * i ] = i ; for ( auto itr = cubes . begin ( ) ; itr != cubes . end ( ) ; itr ++ ) { int firstNumber = itr -> first ; if ( N % itr -> first == 0 ) { int secondNumber = N / itr -> first ; if ( cubes . find ( secondNumber ) != cubes . end ( ) ) { cout << " Yes " ; return ; } } } cout << " No " ; } int main ( ) { int N = 216 ; productOfTwoPerfectCubes ( N ) ; return 0 ; }