Generate N integers satisfying the given conditions
Last Updated :
27 Mar, 2022
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 an odd sum and every even subset has an 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 an odd sum and even a subset has an 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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
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] == true ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
void solve( int n)
{
int count = 0;
for ( int i = 3; count < n; i++) {
if (prime[i]) {
cout << i << " " ;
count++;
}
}
}
int main()
{
SieveOfEratosthenes();
int n = 6;
solve(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 1000000 ;
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] == true )
{
for ( int i = p * 2 ; i <= MAX; i += p)
prime[i] = false ;
}
}
}
static void solve( int n)
{
int count = 0 ;
for ( int i = 3 ; count < n; i++)
{
if (prime[i])
{
System.out.print(i + " " );
count++;
}
}
}
public static void main(String[] args)
{
SieveOfEratosthenes();
int n = 6 ;
solve(n);
}
}
|
Python3
from math import sqrt
MAX = 1000000
prime = [ True ] * ( MAX + 1 );
def SieveOfEratosthenes() :
prime[ 1 ] = False ;
for p in range ( 2 , int (sqrt( MAX )) + 1 ) :
if (prime[p] = = True ) :
for i in range (p * 2 , MAX + 1 , p) :
prime[i] = False ;
def solve(n) :
count = 0 ;
i = 3 ;
while count < n :
if (prime[i]) :
print (i, end = " " );
count + = 1 ;
i + = 1
if __name__ = = "__main__" :
SieveOfEratosthenes();
n = 6 ;
solve(n);
|
C#
using System;
class GFG
{
static int MAX = 1000000;
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] == true )
{
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
static void solve( int n)
{
int count = 0;
for ( int i = 3; count < n; i++)
{
if (prime[i])
{
Console.Write(i + " " );
count++;
}
}
}
public static void Main(String[] args)
{
SieveOfEratosthenes();
int n = 6;
solve(n);
}
}
|
Javascript
<script>
const MAX = 1000000;
let prime = new Array(MAX + 1).fill( true );
function SieveOfEratosthenes()
{
prime[1] = false ;
for (let p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for (let i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
function solve(n)
{
let count = 0;
for (let i = 3; count < n; i++) {
if (prime[i]) {
document.write(i + " " );
count++;
}
}
}
SieveOfEratosthenes();
let n = 6;
solve(n);
</script>
|
Time Complexity: O(n + MAX3/2)
Auxiliary Space: O(MAX)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...