Minimum elements to be inserted such that no Subarray has sum 0
Given an array arr[] of N integers such that no element is 0 in that array, the task is to find the minimum number of elements to be inserted such that no subarray of the new array has sum 0.
Examples:
Input: N = 3, arr[] = {1, -1, 1}
Output: 2
Explanation: As in the array the sum of first two element is 0 that is 1+(-1) = 0.
To avoid this, insert an element. Insert 10 at position 2.
The array become {1, 10, -1, 1}.
Now the sum of last 2 element is 0. To avoid this insert an element.
Insert 10 at position 4. Array become {1, 10, -1, 10, 1}.
Now, no subarray have sum 0.
So we have to insert minimum 2 integers in the array.
Input: N = 4, arr[] = {-2, 1, 2, 3}
Output:
Explanation: No array is there whose sum is 0.
So no need to insert element.
Approach: The problem can be solved based on the following observation.
Observations:
- If we get any subarray with sum 0. Then insert an element just 1 place before the position where you got sum 0. So that sum will not remain 0 and start doing sum again.
- Do the same process till the end and print how many times you have inserted element.
Follow the steps to solve the problem:
- Take an unordered_map to store the sum detail to know if there is a subarray with sum 0.
- Take a variable ans and initialize it to 0 to store the minimum number of elements to be inserted.
- Traverse the array and add every element into the array and also note the sum detail in the map.
- If, you got sum 0 (which can be found by checking if the sum is already present in the map):
- Then increase the ans by 1 and change the sum to the current element value because you have taken care of the previous elements and also remove the entries of the sum from the map.
- Repeat this step whenever there is a subarray with sum 0.
- In last return ans.
Below is the implementation for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int getElements( int N, int arr[])
{
unordered_map< long long int , int > forSum;
int ans = 0;
long long int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
forSum[sum]++;
if (sum == 0 || forSum[sum] > 1) {
ans++;
sum = arr[i];
forSum.clear();
forSum[sum]++;
}
}
return ans;
}
int main()
{
int N = 3;
int arr[] = { 1, -1, 1 };
cout << getElements(N, arr) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int getElements( int N, int arr[])
{
HashMap<Integer, Integer> forSum
= new HashMap<Integer, Integer>();
int ans = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += arr[i];
if (forSum.get(sum) != null )
forSum.put(sum, forSum.get(sum) + 1 );
else
forSum.put(sum, 1 );
if (sum == 0
|| (forSum.get(sum) != null
&& forSum.get(sum) > 1 )) {
ans++;
sum = arr[i];
forSum.clear();
forSum.put(sum, 1 );
}
}
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
int arr[] = { 1 , - 1 , 1 };
System.out.print(getElements(N, arr));
}
}
|
Python3
def getElements(N, arr):
forSum = {}
ans = 0 ;
sum = 0 ;
for i in range (N):
sum + = arr[i];
if ( sum in forSum):
forSum[ sum ] + = 1
else :
forSum[ sum ] = 1
if ( sum = = 0 or forSum[ sum ] > 1 ):
ans + = 1
sum = arr[i];
forSum.clear();
if ( sum in forSum):
forSum[ sum ] + = 1
else :
forSum[ sum ] = 1
return ans;
N = 3 ;
arr = [ 1 , - 1 , 1 ];
print (getElements(N, arr));
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int getElements( int N, int [] arr)
{
Dictionary< int ,
int > forSum = new Dictionary< int , int >();
int ans = 0;
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
forSum.Add(sum, 1);
if (sum == 0 || forSum[sum] > 1) {
ans++;
sum = arr[i];
forSum.Clear();
forSum.Add(sum, 1);
}
}
return ans;
}
public static void Main()
{
int N = 3;
int [] arr = { 1, -1, 1 };
Console.Write(getElements(N, arr));
}
}
|
Javascript
<script>
function getElements(N, arr)
{
var forSum = new Map();
let ans = 0;
let sum = 0;
for (let i = 0; i < N; i++) {
sum += arr[i];
forSum[sum]++;
if (sum == 0 || forSum[sum] > 1) {
ans++;
sum = arr[i];
forSum.clear();
forSum[sum]++;
}
}
return ans;
}
let N = 3;
let arr = [ 1, -1, 1 ];
document.write(getElements(N, arr));
</script>
|
Time Complexity: O(N) because the array is traversed only once and at most N elements are cleared from the map.
Auxiliary Space: O(N)
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...