# Number of 0s and 1s at prime positions in the given array

Given an array arr[] of size N where each element is either 0 or 1. The task is to find the count of 0s and 1s which are at prime indices.

Examples:

Input: arr[] = {1, 0, 1, 0, 1}
Output:
Number of 0s = 1
Number of 1s = 1

Input: arr[] = {1, 0, 1, 1}
Output:
Number of 0s = 0
Number of 1s = 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse the array and for every 0 encountered update the count of 0s if the current index is prime and update the count of 1s for all the 1s which are at prime indices.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true ` `// if n is prime ` `bool` `isPrime(``int` `n) ` `{ ` `    ``if` `(n <= 1) ` `        ``return` `false``; ` ` `  `    ``// Check from 2 to n ` `    ``for` `(``int` `i = 2; i < n; i++)  ` `    ``{ ` `        ``if` `(n % i == 0) ` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Function to find the count ` `// of 0s and 1s at prime indices ` `void` `countPrimePosition(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// To store the count of 0s and 1s ` `    ``int` `c0 = 0, c1 = 0; ` `     `  `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` ` `  `        ``// If current 0 is at ` `        ``// prime position ` `        ``if` `(arr[i] == 0 && isPrime(i)) ` `            ``c0++; ` ` `  `        ``// If current 1 is at ` `        ``// prime position ` `        ``if` `(arr[i] == 1 && isPrime(i)) ` `            ``c1++; ` `    ``} ` `    ``cout << ``"Number of 0s = "` `<< c0 << endl; ` `    ``cout << ``"Number of 1s = "` `<< c1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr [] = { 1, 0, 1, 0, 1 }; ` `     `  `    ``int` `n = ``sizeof``(arr)/ ``sizeof``(arr); ` `    ``countPrimePosition(arr, n); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by ihritik `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function that returns true ` `    ``// if n is prime ` `    ``static` `boolean` `isPrime(``int` `n) ` `    ``{ ` `        ``if` `(n <= ``1``) ` `            ``return` `false``; ` ` `  `        ``// Check from 2 to n ` `        ``for` `(``int` `i = ``2``; i < n; i++) { ` `            ``if` `(n % i == ``0``) ` `                ``return` `false``; ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Function to find the count ` `    ``// of 0s and 1s at prime indices ` `    ``static` `void` `countPrimePosition(``int` `arr[]) ` `    ``{ ` ` `  `        ``// To store the count of 0s and 1s ` `        ``int` `c0 = ``0``, c1 = ``0``; ` `        ``int` `n = arr.length; ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``// If current 0 is at ` `            ``// prime position ` `            ``if` `(arr[i] == ``0` `&& isPrime(i)) ` `                ``c0++; ` ` `  `            ``// If current 1 is at ` `            ``// prime position ` `            ``if` `(arr[i] == ``1` `&& isPrime(i)) ` `                ``c1++; ` `        ``} ` `        ``System.out.println(``"Number of 0s = "` `+ c0); ` `        ``System.out.println(``"Number of 1s = "` `+ c1); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { ``1``, ``0``, ``1``, ``0``, ``1` `}; ` `        ``countPrimePosition(arr); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function that returns true  ` `# if n is prime  ` `def` `isPrime(n) :  ` `     `  `    ``if` `(n <``=` `1``) : ` `        ``return` `False``;  ` ` `  `    ``# Check from 2 to n  ` `    ``for` `i ``in` `range``(``2``, n) :  ` `        ``if` `(n ``%` `i ``=``=` `0``) : ` `            ``return` `False``;  ` `         `  `    ``return` `True``;  ` ` `  `# Function to find the count  ` `# of 0s and 1s at prime indices  ` `def` `countPrimePosition(arr) : ` ` `  `    ``# To store the count of 0s and 1s  ` `    ``c0 ``=` `0``; c1 ``=` `0``;  ` `    ``n ``=` `len``(arr);  ` `     `  `    ``for` `i ``in` `range``(n) :  ` `     `  `        ``# If current 0 is at  ` `        ``# prime position  ` `        ``if` `(arr[i] ``=``=` `0` `and` `isPrime(i)) : ` `            ``c0 ``+``=` `1``;  ` `     `  `        ``# If current 1 is at  ` `        ``# prime position  ` `        ``if` `(arr[i] ``=``=` `1` `and` `isPrime(i)) : ` `            ``c1 ``+``=` `1``;  ` `             `  `    ``print``(``"Number of 0s ="``, c0);  ` `    ``print``(``"Number of 1s ="``, c1);  ` `         `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``arr ``=` `[ ``1``, ``0``, ``1``, ``0``, ``1` `];  ` `    ``countPrimePosition(arr);  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function that returns true ` `    ``// if n is prime ` `    ``static` `bool` `isPrime(``int` `n) ` `    ``{ ` `        ``if` `(n <= 1) ` `            ``return` `false``; ` ` `  `        ``// Check from 2 to n ` `        ``for` `(``int` `i = 2; i < n; i++)  ` `        ``{ ` `            ``if` `((n % i) == 0) ` `                ``return` `false``; ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Function to find the count ` `    ``// of 0s and 1s at prime indices ` `    ``static` `void` `countPrimePosition(``int` `[]arr) ` `    ``{ ` ` `  `        ``// To store the count of 0s and 1s ` `        ``int` `c0 = 0, c1 = 0; ` `        ``int` `n = arr.Length; ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` ` `  `            ``// If current 0 is at ` `            ``// prime position ` `            ``if` `((arr[i] == 0) && (isPrime(i))) ` `                ``c0++; ` ` `  `            ``// If current 1 is at ` `            ``// prime position ` `            ``if` `((arr[i] == 1) && (isPrime(i))) ` `                ``c1++; ` `        ``} ` `        ``Console.WriteLine(``"Number of 0s = "` `+ c0); ` `        ``Console.WriteLine(``"Number of 1s = "` `+ c1); ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `         `  `        ``int``[] arr = { 1, 0, 1, 0, 1 }; ` `        ``countPrimePosition(arr); ` `    ``} ` `} ` ` `  `// This code is contributed by ajit.  `

Output:

```Number of 0s = 1
Number of 1s = 1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : jit_t, AnkitRai01, ihritik

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.