Find array elements equal to sum of any subarray of at least size 2
Last Updated :
02 Sep, 2022
Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 3, 5, 6
Explanation:
The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectively.
Input: arr[] = {5, 6, 10, 1, 3, 4, 8, 16}
Output: 4, 8, 16
Explanation:
The elements 4, 8, 16 are equal to the sum of subarrays {1, 3}, {1, 3, 4}, {1, 3, 4, 8} respectively
Approach: The idea is to use a prefix sum array to solve the given problem. Create a prefix[] array that stores the prefix sum of all its preceding elements for every index. Store the sum of all subarrays in a map and search if any array element is present in the map or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findElements( int * arr, int n)
{
if (n == 1)
return ;
int pre[n];
pre[0] = arr[0];
for ( int i = 1; i < n; i++)
pre[i] = arr[i] + pre[i - 1];
unordered_map< int , bool > mp;
for ( int i = 1; i < n - 1; i++) {
mp[pre[i]] = true ;
for ( int j = i + 1; j < n; j++) {
mp[pre[j] - pre[i - 1]] = true ;
}
}
for ( int i = 0; i < n; i++) {
if (mp[arr[i]]) {
cout << arr[i] << " " ;
}
}
cout << endl;
}
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
findElements(arr1, n1);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findElements( int [] arr, int n)
{
if (n == 1 )
return ;
int pre[] = new int [n];
pre[ 0 ] = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
pre[i] = arr[i] + pre[i - 1 ];
HashMap<Integer, Boolean> mp = new HashMap<>();
for ( int i = 1 ; i < n - 1 ; i++)
{
mp.put(pre[i], true );
for ( int j = i + 1 ; j < n; j++)
{
mp.put(pre[j] - pre[i - 1 ], true );
}
}
for ( int i = 0 ; i < n; i++)
{
if (mp.get(arr[i]) != null )
{
System.out.print(arr[i] + " " );
}
}
System.out.println();
}
public static void main(String[] args)
{
int arr1[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int n1 = arr1.length;
findElements(arr1, n1);
}
}
|
Python3
def findElements(arr, n):
if (n = = 1 ):
return ;
pre = [ 0 ] * n;
pre[ 0 ] = arr[ 0 ];
for i in range ( 1 , n):
pre[i] = arr[i] + pre[i - 1 ];
mp = {};
for i in range ( 1 , n - 1 ):
mp[pre[i]] = True ;
for j in range (i + 1 , n):
mp[pre[j] - pre[i - 1 ]] = True ;
for i in range (n):
if arr[i] in mp:
print (arr[i], end = " " );
else :
pass
print ()
if __name__ = = "__main__" :
arr1 = [ 1 , 2 , 3 , 4 , 5 , 6 ];
n1 = len (arr1);
findElements(arr1, n1);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findElements( int [] arr,
int n)
{
if (n == 1)
return ;
int []pre = new int [n];
pre[0] = arr[0];
for ( int i = 1; i < n; i++)
pre[i] = arr[i] + pre[i - 1];
Dictionary< int ,
Boolean> mp = new Dictionary< int ,
Boolean>();
for ( int i = 1; i < n - 1; i++)
{
if (!mp.ContainsKey(pre[i]))
mp.Add(pre[i], true );
for ( int j = i + 1; j < n; j++)
{
if (!mp.ContainsKey(pre[j] - pre[i - 1]))
mp.Add(pre[j] - pre[i - 1], true );
}
}
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
Console.Write(arr[i] + " " );
}
}
Console.WriteLine();
}
public static void Main(String[] args)
{
int []arr1 = {1, 2, 3, 4, 5, 6};
int n1 = arr1.Length;
findElements(arr1, n1);
}
}
|
Javascript
<script>
function findElements(arr, n)
{
if (n == 1)
return ;
let pre = Array.from({length: n}, (_, i) => 0);
pre[0] = arr[0];
for (let i = 1; i < n; i++)
pre[i] = arr[i] + pre[i - 1];
let mp = new Map();
for (let i = 1; i < n - 1; i++)
{
mp.set(pre[i], true );
for (let j = i + 1; j < n; j++)
{
mp.set(pre[j] - pre[i - 1], true );
}
}
for (let i = 0; i < n; i++)
{
if (mp.get(arr[i]) != null )
{
document.write(arr[i] + " " );
}
}
document.write( "<br/>" );
}
let arr1 = [ 1, 2, 3, 4, 5, 6 ];
let n1 = arr1.length;
findElements(arr1, n1);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...