VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-array-element-having-equal-count-of-prime-numbers-on-its-left-and-right/

⇱ Find the array element having equal count of Prime Numbers on its left and right - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the array element having equal count of Prime Numbers on its left and right

Last Updated : 23 Jul, 2025

Given an array arr[] consisting of N positive integers, the task is to find an index from the array having count of prime numbers present on its left and right are equal.

Examples:

Input: arr[] = {2, 3, 4, 7, 5, 10, 1, 8}
Output: 2
Explanation: 
Consider the index 2, then the prime numbers to its left are {2, 3} and the prime numbers to its right are {7, 5}.
As, the count of prime numbers to the left and right index is 2, which is equal. Therefore, print 2.

Input: arr[] = {8, 10, 2, 7, 3}
Output: 3

Naive Approach: The simplest approach to solve the given problem is to count the prime numbers to the left and right of each array element by traversing the array and print the index at which the count of prime numbers are found to be equal. 
Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized using the Sieve of Eratosthenes and prefix sum technique to pre-store the prime numbers left and right to an array element. Follow the steps below to solve the problem:

  • First, find the maximum value of the array arr[] and store it in a variable, say maxValue.
  • Initialize a map < int, int>, say St, to store all the prime numbers in it.
  • Iterate over the range [2, maxValue] and push all the values in St.
  • Iterate over the range [2, maxValue] and perform the following operations:
    • Initialize a variable j as 1 and iterate while the i*j is less than or equal to the maxValue.
    • In each iteration of the above steps remove i*j from St and increment j by 1.
  • Initialize two variables, say LeftCount and RightCount, to store the count of prime numbers in a prefix array and in a suffix array respectively.
  • Initialize two arrays, say Prefix[] and Suffix[], to store the prefix sum array and suffix sum array of the count of prime numbers of the array arr[].
  • Traverse the array arr[] and perform the following operations:
    • Assign LeftCount to Prefix[i].
    • If the value of arr[i] is present in St, then increment the value of LeftCount by 1.
  • Iterate over the range [0, N-1] in reverse and perform the following operations:
    • Assign RightCount to Suffix[i].
    • If arr[i] is present in St, then increment the value of RightCount by 1.
  • Iterate over the range [0, N - 1] and if the value of Prefix[i] is equal to the value of Suffix[i], then print the index i as the answer and break out of the loop.
  • After completing the above steps, if none of the above cases satisfy then print -1.

Below is the implementation of the above approach:


Output: 
2

 

Time Complexity: O(N * log N)
Auxiliary Space: O(N)

Comment