Given an integer N, the task is to generate an array of size N with the following properties:
- No two elements divide each other.
- Every odd subset has odd sum and every even subset has even sum.
Examples:
Input: N = 3
Output: 3 5 7
No two element divide each other and the sum
of all the odd subsets {3}, {5}, {7} and {3, 5, 7} is odd.
Sum of all the even subsets is even i.e. {3, 5}, {3, 7} and {5, 7}Input: N = 6
Output: 3 5 7 11 13 17
Approach: In order to satisfy the condition when every odd subset has odd sum and even subset has even sum, every element has to be odd and in order for any two elements to not divide each other they must be prime. So, the task now is to find the first N odd prime numbers.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MAX 1000000 // 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. bool prime[MAX + 1]; void SieveOfEratosthenes() { memset(prime, true, sizeof(prime)); prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Set all multiples of p to non-prime for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } } // Function to find the first // n odd prime numbers void solve(int n) { // To store the current count // of prime numbers int count = 0; // Starting with 3 as 2 is // an even prime number for (int i = 3; count < n; i++) { // If i is prime if (prime[i]) { // Print i and increment count cout << i << " "; count++; } } } // Driver code int main() { // Create the sieve SieveOfEratosthenes(); int n = 6; solve(n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { static int MAX = 1000000; // 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. static boolean []prime = new boolean[MAX + 1]; static void SieveOfEratosthenes() { for (int i = 0; i <= MAX; i ++) prime[i] = true; prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Set all multiples of p to non-prime for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } } // Function to find the first // n odd prime numbers static void solve(int n) { // To store the current count // of prime numbers int count = 0; // Starting with 3 as 2 is // an even prime number for (int i = 3; count < n; i++) { // If i is prime if (prime[i]) { // Print i and increment count System.out.print(i + " "); count++; } } } // Driver code public static void main(String[] args) { // Create the sieve SieveOfEratosthenes(); int n = 6; solve(n); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach from math import sqrt MAX = 1000000 # 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. prime = [True] * (MAX + 1); def SieveOfEratosthenes() : prime[1] = False; for p in range(2, int(sqrt(MAX)) + 1) : # If prime[p] is not changed, # then it is a prime if (prime[p] == True) : # Set all multiples of p to non-prime for i in range(p * 2, MAX + 1, p) : prime[i] = False; # Function to find the first # n odd prime numbers def solve(n) : # To store the current count # of prime numbers count = 0; i = 3; # Starting with 3 as 2 is # an even prime number while count < n : # If i is prime if (prime[i]) : # Print i and increment count print(i, end = " "); count += 1; i += 1 # Driver code if __name__ == "__main__" : # Create the sieve SieveOfEratosthenes(); n = 6; solve(n); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approach using System; class GFG { static int MAX = 1000000; // 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. static bool []prime = new bool[MAX + 1]; static void SieveOfEratosthenes() { for (int i = 0; i <= MAX; i ++) prime[i] = true; prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Set all multiples of p to non-prime for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } } // Function to find the first // n odd prime numbers static void solve(int n) { // To store the current count // of prime numbers int count = 0; // Starting with 3 as 2 is // an even prime number for (int i = 3; count < n; i++) { // If i is prime if (prime[i]) { // Print i and increment count Console.Write(i + " "); count++; } } } // Driver code public static void Main(String[] args) { // Create the sieve SieveOfEratosthenes(); int n = 6; solve(n); } } // This code is contributed by 29AjayKumar |
3 5 7 11 13 17
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count possible permutations of given array satisfying the given conditions
- Count valid pairs in the array satisfying given conditions
- Split N as the sum of K numbers satisfying the given conditions
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Generate an array using given conditions from a given array
- Generate elements of the array following given conditions
- Generate an array of size K which satisfies the given conditions
- Count permutations of all integers upto N that can form an acyclic graph based on given conditions
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Check if elements of an array can be arranged satisfying the given condition
- Count of pairs satisfying the given condition
- Smallest positive integer X satisfying the given equation
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Maximum possible sum of squares of stack elements satisfying the given properties
- Generate minimum sum sequence of integers with even elements greater
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Find x and y satisfying ax + by = n
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

