Given an array arr[] of size N, the task is to find the index in the given array where the sum of the prime numbers present to its left is equal to the sum of the prime numbers present to its right.
Examples:
Input: arr[] = {11, 4, 7, 6, 13, 1, 5} Output: 3 Explanation: Sum of prime numbers to left of index 3 = 11 + 7 = 18 Sum of prime numbers to right of index 3 = 13 + 5 = 18
Input: arr[] = {5, 2, 1, 7} Output: 2 Explanation: Sum of prime numbers to left of index 2 = 5 + 2 = 7 Sum of prime numbers to right of index 2 = 7
Naive Approach: The simplest approach is to traverse the array and check for the given condition for every index in the range [0, N – 1]. If the condition is found to be true for any index, then print the value of that index.
Time Complexity: O(N2*?M), where M is the largest element in the array 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 sum of prime numbers left and right to an array element. Follow the steps below to solve the problem:
- Traverse through the array to find the maximum value present in the array.
- Use Sieve of Eratosthenes to find out the prime numbers which are less than or equal to the maximum value present in the array. Store these elements in a Map.
- Initialize an array, say first_array. Traverse the array and store the sum of all prime numbers up to ith index at first_array[i].
- Initialize an array, say second_array. Traverse the array in reverse and store the sum of all elements up to ith index at second_array[i].
- Traverse the arrays first_array and second_array and check if at any index, both their values are equal or not. If found to be true, return that index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_index( int arr[], int N)
{
int max_value = INT_MIN;
for ( int i = 0; i < N; i++) {
max_value = max(max_value, arr[i]);
}
map< int , int > store;
for ( int i = 1; i <= max_value; i++) {
store[i]++;
}
if (store.find(1) != store.end()) {
store.erase(1);
}
for ( int i = 2; i <= sqrt (max_value); i++) {
int multiple = 2;
while ((i * multiple) <= max_value) {
if (store.find(i * multiple) != store.end()) {
store.erase(i * multiple);
}
multiple++;
}
}
int prime_sum_from_left = 0;
int first_array[N];
for ( int i = 0; i < N; i++) {
first_array[i] = prime_sum_from_left;
if (store.find(arr[i]) != store.end()) {
prime_sum_from_left += arr[i];
}
}
int prime_sum_from_right = 0;
int second_array[N];
for ( int i = N - 1; i >= 0; i--) {
second_array[i] = prime_sum_from_right;
if (store.find(arr[i]) != store.end()) {
prime_sum_from_right += arr[i];
}
}
for ( int i = 0; i < N; i++) {
if (first_array[i] == second_array[i]) {
return i;
}
}
return -1;
}
int main()
{
int arr[] = { 11, 4, 7, 6, 13, 1, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << find_index(arr, N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static int find_index( int arr[], int N)
{
int max_value = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++)
{
max_value = Math.max(max_value, arr[i]);
}
Map<Integer, Integer> store= new HashMap<>();
for ( int i = 1 ; i <= max_value; i++)
{
store.put(i, store.getOrDefault(i, 0 ) + 1 );
}
if (store.containsKey( 1 ))
{
store.remove( 1 );
}
for ( int i = 2 ; i <= Math.sqrt(max_value); i++)
{
int multiple = 2 ;
while ((i * multiple) <= max_value)
{
if (store.containsKey(i * multiple))
{
store.remove(i * multiple);
}
multiple++;
}
}
int prime_sum_from_left = 0 ;
int [] first_array = new int [N];
for ( int i = 0 ; i < N; i++)
{
first_array[i] = prime_sum_from_left;
if (store.containsKey(arr[i]))
{
prime_sum_from_left += arr[i];
}
}
int prime_sum_from_right = 0 ;
int [] second_array= new int [N];
for ( int i = N - 1 ; i >= 0 ; i--)
{
second_array[i] = prime_sum_from_right;
if (store.containsKey(arr[i]))
{
prime_sum_from_right += arr[i];
}
}
for ( int i = 0 ; i < N; i++)
{
if (first_array[i] == second_array[i])
{
return i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 11 , 4 , 7 , 6 , 13 , 1 , 5 };
int N = arr.length;
System.out.println(find_index(arr, N));
}
}
|
Python3
from math import sqrt
def find_index(arr, N):
max_value = - 10 * * 9
for i in range (N):
max_value = max (max_value, arr[i])
store = {}
for i in range ( 1 , max_value + 1 ):
store[i] = store.get(i, 0 ) + 1
if ( 1 in store):
del store[ 1 ]
for i in range ( 2 , int (sqrt(max_value)) + 1 ):
multiple = 2
while ((i * multiple) < = max_value):
if (i * multiple in store):
del store[i * multiple]
multiple + = 1
prime_sum_from_left = 0
first_array = [ 0 ] * N
for i in range (N):
first_array[i] = prime_sum_from_left
if arr[i] in store:
prime_sum_from_left + = arr[i]
prime_sum_from_right = 0
second_array = [ 0 ] * N
for i in range (N - 1 , - 1 , - 1 ):
second_array[i] = prime_sum_from_right
if (arr[i] in store):
prime_sum_from_right + = arr[i]
for i in range (N):
if (first_array[i] = = second_array[i]):
return i
return - 1
if __name__ = = '__main__' :
arr = [ 11 , 4 , 7 , 6 , 13 , 1 , 5 ]
N = len (arr)
print (find_index(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int find_index( int [] arr, int N)
{
int max_value = Int32.MinValue;
for ( int i = 0; i < N; i++)
{
max_value = Math.Max(max_value, arr[i]);
}
Dictionary< int ,
int > store = new Dictionary< int ,
int >();
for ( int i = 1; i <= max_value; i++)
{
if (!store.ContainsKey(i))
store[i] = 0;
store[i]++;
}
if (store.ContainsKey(1))
{
store.Remove(1);
}
for ( int i = 2; i <= Math.Sqrt(max_value); i++)
{
int multiple = 2;
while ((i * multiple) <= max_value)
{
if (store.ContainsKey(i * multiple))
{
store.Remove(i * multiple);
}
multiple++;
}
}
int prime_sum_from_left = 0;
int [] first_array = new int [N];
for ( int i = 0; i < N; i++)
{
first_array[i] = prime_sum_from_left;
if (store.ContainsKey(arr[i]))
{
prime_sum_from_left += arr[i];
}
}
int prime_sum_from_right = 0;
int [] second_array = new int [N];
for ( int i = N - 1; i >= 0; i--)
{
second_array[i] = prime_sum_from_right;
if (store.ContainsKey(arr[i]))
{
prime_sum_from_right += arr[i];
}
}
for ( int i = 0; i < N; i++)
{
if (first_array[i] == second_array[i])
{
return i;
}
}
return -1;
}
public static void Main()
{
int [] arr = { 11, 4, 7, 6, 13, 1, 5 };
int N = arr.Length;
Console.WriteLine(find_index(arr, N));
}
}
|
Javascript
<script>
function find_index(arr,N)
{
let max_value = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
max_value = Math.max(max_value, arr[i]);
}
let store= new Map();
for (let i = 1; i <= max_value; i++) {
if (!store.has(i))
store.set(i,0);
store.set(i,store.get(i)+1);
}
if (store.has(1)) {
store. delete (1);
}
for (let i = 2; i <= Math.sqrt(max_value); i++) {
let multiple = 2;
while ((i * multiple) <= max_value) {
if (store.has(i * multiple)) {
store. delete (i * multiple);
}
multiple++;
}
}
let prime_sum_from_left = 0;
let first_array= new Array(N);
for (let i = 0; i < N; i++) {
first_array[i] = prime_sum_from_left;
if (store.has(arr[i])) {
prime_sum_from_left += arr[i];
}
}
let prime_sum_from_right = 0;
let second_array= new Array(N);
for (let i = N - 1; i >= 0; i--) {
second_array[i] = prime_sum_from_right;
if (store.has(arr[i]) ) {
prime_sum_from_right += arr[i];
}
}
for (let i = 0; i < N; i++) {
if (first_array[i] == second_array[i]) {
return i;
}
}
return -1;
}
let arr=[11, 4, 7, 6, 13, 1, 5];
let N=arr.length;
document.write(find_index(arr, N));
</script>
|
Time Complexity: O(N + max(arr[])loglog(max(arr[])) Auxiliary Space: O(max(arr[]) + N)
|