Given an **array A** containing numbers from **2 to N.**. A special type of sieving is done on it .

The procedure of sieving is as follows:

- Create an array with elements as consecutive integers from 2 to N and mark every element in the array as unmarked.
- Let an integer
**Q = N**and**mark**all the proper divisors of**Q**except Q itself in the array. - Find the largest number unmarked less than Q and assign Q to it, and repeat from step 2. If there are no more unmarked elements then stop.

Find the number of unmarked integers after sieving.

**Examples:**

Input : N = 5 Output : Number of unmarked elements = 3 Explanation : We create array A[] = { 2, 3, 4, 5 }. 2 is marked as it is a proper divisor of 4. Input : N = 4 Output : Number of unmarked elements = 2

**Naive Approach:**

One basic approach is to run two loops. Outer loop to traverse the whole array and inner loop for traversing from 2 – Q to unmark all the proper divisors of Q by checking **a[i] % Q = 0.**

**Time Complexity:** O(N^2)

**Efficient Approach:**

A simple observation suggests that actually there is no need to do sieving here instead, the value of N will determine the answer.

**Case 1:** If N is **Odd** then number of unmarked elements will be (N/2) + 1.

**Case 2:** If N is **Even** then number of unmarked elements will be (N/2).

**Time Complexity:** O(1)

**Examples:**

Input : N = 5

Output : 3

A[] = { 2, 3, 4, 5 }

Steps:

1.) Q = 5 : Mark All the proper divisors of Q, here no element is there so every element remains unmarked.

3.) Q = 4 : Mark all the proper divisors of Q. Here 2 gets marked and unmarked elements are {3, 4, 5}.

5.) Q = 3 :

6.) Now no further marking can be done so stop here

So number of unmarked elements are 3 i.e {3, 4, 5}

In case of ODD value of N result should be (N/2)+1 = (3/2)+1 = (5/2)+1 = 2+1= 3.Input : N = 4

Output : 2

A[] = { 2, 3, 4 }

Steps:

1.) Q = 4 Mark all the proper divisors of Q. So here 2 gets marked and unmarked elements are {3, 4}

3.) Q = 3

4.) Now no further marking can be done so stop here

So number of unmarked element after sieving are {3, 4} = 2

In case of EVEN value of N result should be (N/2) = (4/2) = 2

## C++

`// C++ Program to determine the ` `// number of unmarked integers in ` `// a special sieve ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countUnmarked(` `int` `N) ` `{ ` ` ` `if` `(N % 2 == 0) ` ` ` `return` `N/2; ` ` ` `else` ` ` `return` `N/2 + 1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `cout << ` `"Number of unmarked elements: "` ` ` `<< countUnmarked(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to determine ` `// the number of unmarked ` `// integers in a special sieve ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `countUnmarked(` `int` `N) ` `{ ` ` ` `if` `(N % ` `2` `== ` `0` `) ` ` ` `return` `N / ` `2` `; ` ` ` `else` ` ` `return` `N / ` `2` `+ ` `1` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` `System.out.println(` `"Number of unmarked "` `+ ` ` ` `"elements: "` `+ ` ` ` `countUnmarked(N)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to determine ` `# the number of unmarked ` `# integers in a special sieve ` `def` `countUnmarked(N): ` ` ` `if` `(N ` `%` `2` `=` `=` `0` `): ` ` ` `return` `N ` `/` `2` `; ` ` ` `else` `: ` ` ` `return` `N ` `/` `2` `+` `1` `; ` ` ` `# Driver Code ` `N ` `=` `4` `; ` `print` `(` `"Number of unmarked elements:"` `, ` ` ` `int` `(countUnmarked(N))); ` ` ` `# This code is contributed ` `# by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to determine ` `// the number of unmarked ` `// integers in a special sieve ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `countUnmarked(` `int` `N) ` `{ ` ` ` `if` `(N % 2 == 0) ` ` ` `return` `N / 2; ` ` ` `else` ` ` `return` `N / 2 + 1; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `N = 4; ` ` ` `Console.WriteLine(` `"Number of unmarked "` `+ ` ` ` `"elements: "` `+ ` ` ` `countUnmarked(N)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to determine ` `// the number of unmarked ` `// integers in a special sieve ` ` ` `function` `countUnmarked(` `$N` `) ` `{ ` ` ` `if` `(` `$N` `% 2 == 0) ` ` ` `return` `$N` `/ 2; ` ` ` `else` ` ` `return` `$N` `/ 2 + 1; ` `} ` ` ` `// Driver Code ` `$N` `= 4; ` `echo` `"Number of unmarked elements: "` `, ` ` ` `countUnmarked(` `$N` `); ` ` ` `// This code is contributed ` `// by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Number of unmarked elements: 2

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:

- Sieve of Eratosthenes
- Segmented Sieve
- Sieve of Sundaram to print all primes smaller than n
- Sieve of Atkin
- Sieve of Eratosthenes in 0(n) time complexity
- Prime Factorization using Sieve O(log n) for multiple queries
- Sum of all Primes in a given range using Sieve of Eratosthenes
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Bitwise Sieve
- Segmented Sieve (Print Primes in a Range)
- Longest sub-array of Prime Numbers using Segmented Sieve
- Median in a stream of integers (running integers)
- Minimum product of k integers in an array of positive Integers
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Find Largest Special Prime which is less than or equal to a given number
- Count the number of special permutations
- Smallest Special Prime which is greater than or equal to a given number
- Count number of Special Set
- Special prime numbers

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.