Minimum Subarray sum with atleast one repeated value.
Last Updated :
06 Dec, 2023
Given an integer array arr[], the task is to find a minimum subarray sum that has at least one repeated value. If no such subarray is present print -1.
Examples:
Input: arr[] = {1, 3, 2, 1, 8, 2}
Output: 7
Explanation: There were 2 possible subarray’s {1, 3, 2, 1} with sum = 7 and {2, 1, 8, 2} with sum = 13, so the answer is 7
Input: arr[] = {5, 3, 2, 4, 7}
Output: -1
Explanation: As all the numbers are unique No such subarray is present, so the answer is -1.
Approach: To solve the problem follow the below idea:
Approach is pretty simple we pre-compute the prefix sum array and then we iterate over the array arr[] and check the last occurence index of the current element (if present) and with the help of prefix sum array calculate the sum of the that subarray part then update minimum answer accordingly,also we need to update the last occurance index of the current element.
Steps to code the above approach:
- Initialize an array to compute the prefix sum array
- Declare an unordered map of int, int to store the last occurrence of integers present in the array
- Compute the prefix sum array
- Iterate through the original array and search in the unordered map for the last occurrence of the current value(if present )
- Calculate the sum of the subarray sum using prefix sum array by the current index value in prefix sum – the last occurrence value in prefix sum + current value and calculate the minimum answer by comparing.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSum(vector< int > vec)
{
int n = vec.size();
long ans = LONG_MAX;
vector< long > prefixSum(vec.size());
for ( int i = 0; i < vec.size(); i++) {
if (i == 0) {
prefixSum[i] = vec[i];
}
else {
prefixSum[i] = prefixSum[i - 1] + vec[i];
}
}
unordered_map< int , int > umap;
for ( int i = 0; i < vec.size(); i++) {
if (umap.find(vec[i]) != umap.end()) {
long val = umap[vec[i]];
long cc
= prefixSum[i] - prefixSum[val] + vec[val];
ans = min(ans, cc);
}
umap[vec[i]] = i;
}
if (ans == LONG_MAX)
return -1;
return ans;
}
int main()
{
vector< int > arr = { 1, 3, 2, 1, 8, 2 };
cout << minSum(arr);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static int minSum( int [] arr) {
int n = arr.length;
long ans = Long.MAX_VALUE;
long [] prefixSum = new long [arr.length];
for ( int i = 0 ; i < arr.length; i++) {
if (i == 0 ) {
prefixSum[i] = arr[i];
} else {
prefixSum[i] = prefixSum[i - 1 ] + arr[i];
}
}
Map<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
int val = map.get(arr[i]);
long cc = prefixSum[i] - prefixSum[val] + arr[val];
ans = Math.min(ans, cc);
}
map.put(arr[i], i);
}
if (ans == Long.MAX_VALUE)
return - 1 ;
return ( int ) ans;
}
public static void main(String[] args) {
int [] arr = { 1 , 3 , 2 , 1 , 8 , 2 };
System.out.println(minSum(arr));
}
}
|
Python3
def GFG(arr):
n = len (arr)
ans = float ( 'inf' )
prefixSum = [ 0 ] * n
for i in range (n):
if i = = 0 :
prefixSum[i] = arr[i]
else :
prefixSum[i] = prefixSum[i - 1 ] + arr[i]
dictionary = {}
for i in range (n):
if arr[i] in dictionary:
val = dictionary[arr[i]]
cc = prefixSum[i] - prefixSum[val] + arr[val]
ans = min (ans, cc)
dictionary[arr[i]] = i
if ans = = float ( 'inf' ):
return - 1
return ans
arr = [ 1 , 3 , 2 , 1 , 8 , 2 ]
print (GFG(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int MinSum( int [] arr)
{
int n = arr.Length;
long ans = long .MaxValue;
long [] prefixSum = new long [arr.Length];
for ( int i = 0; i < arr.Length; i++) {
if (i == 0) {
prefixSum[i] = arr[i];
}
else {
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
}
Dictionary< int , int > map
= new Dictionary< int , int >();
for ( int i = 0; i < arr.Length; i++) {
if (map.ContainsKey(arr[i])) {
int val = map[arr[i]];
long cc = prefixSum[i] - prefixSum[val]
+ arr[val];
ans = Math.Min(ans, cc);
}
map[arr[i]] = i;
}
if (ans == long .MaxValue)
return -1;
return ( int )ans;
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 2, 1, 8, 2 };
Console.WriteLine(MinSum(arr));
}
}
|
Javascript
function GFG(arr) {
const n = arr.length;
let ans = Number.MAX_SAFE_INTEGER;
const prefixSum = new Array(n);
for (let i = 0; i < n; i++) {
if (i === 0) {
prefixSum[i] = arr[i];
} else {
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
}
const map = new Map();
for (let i = 0; i < n; i++) {
if (map.has(arr[i])) {
const val = map.get(arr[i]);
const cc = prefixSum[i] - prefixSum[val] + arr[val];
ans = Math.min(ans, cc);
}
map.set(arr[i], i);
}
if (ans === Number.MAX_SAFE_INTEGER) {
return -1;
}
return ans;
}
const arr = [1, 3, 2, 1, 8, 2];
console.log(GFG(arr));
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...