Minimum sum of subarray | Practice | GeeksforGeeks

Given an array of integers of size N, for all, i's [1, N], the task is to find the minimum subarray sum in the subarray [i, N].
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains a single integer N.
3. The second line contains N space-separated positive integers represents array.

Output: For each test case, print N space-separated integers

Constraints:
1. 1 <= T <= 10
2. 1 <= N <= 100000
3. -10000 <= arr[i] <= 10000

Example:
Input:
2
3
3 -1 -2
5
5 -3 -2 9 4
Output:
-3 -3 -2
-5 -5 -2 4 4 
Explanation:
Test case 1: 
1. i = 1, subarray is [3, -1, -2], all possible subarrays are [3], [-1], [-2], [3, -1], [-1, -2], [3, -1, -2]. Minnimum sum is -3 [-1, -2].
2. i = 2, subarray is [-1, -2], all possible subarrays are [-1], [-2], [-1, -2]. Minnimum sum is -3 [-1, -2].
3. i = 3, subarray is [-2], all possible subarrays are [-2]. Minnimum sum is -2[-2].
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(N) and space complexity as O(N) is as follows :- 
 
Execution Time :- 0.0 / 2.2 sec
 
#include <bits/stdc++.h>#define ll long long int#define ld long double#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);#define mod ((int)1e9+7)#define inf 1e18#define hell 1e15#define p_b push_backusing namespace std;
vector<int> helper(int arr[],int n){   vector<int> ans(n);   int min_so_far = arr[n-1];   int curr_so_far = arr[n-1];   ans[n-1] = min_so_far;   for(int i = n-2; i >= 0; i--){       curr_so_far = min(arr[i],curr_so_far+arr[i]);       min_so_far = min(min_so_far,curr_so_far);       ans[i] = min_so_far;   }   return (ans);}
int main(int args,char **ch){   FIO   short t; cin>>t;   while(t--){       int n; cin>>n;       int arr[n];       for(int i = 0; i < n; i++)           cin>>arr[i];       vector<int> ans = helper(arr,n);       for(auto x: ans) cout<<x<<" ";       cout<<endl;   }      return (0);}
0
imranwahid4 months ago
Easy C++ solution using Kadane's algorithm
0
20je01176 months ago
# include <bits/stdc++.h> # define int long long using namespace std; int find(int start,vector<int>&v){int curr_min=INT_MAX;int min_so_far=INT_MAX;for(int i=start;i<v.size();++i){if(curr_min>0){   curr_min=v[i];}else{   curr_min+=v[i];}min_so_far=min(min_so_far,curr_min);}return min_so_far;}int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);//this is for finding minimum subarray sum of an arrayint t;cin>>t;while(t--){   int n;cin>>n;   vector<int>v(n);   for(int i=0;i<n;++i){       cin>>v[i];   }vector<int>result(n);for(int i=0;i<n;++i){   result[i]=find(i,v);}for(auto val:result){   cout<<val<<" ";}cout<<endl;}
return 0;}
+1
jasmeenkaursyali7 months ago
#include <iostream>using namespace std;
int main() {int t;cin>>t;while(t--){    int n; cin>>n;    int arr[n];    for(int i=0;i<n;i++)        cin>>arr[i];             for(int j=0;j<n;j++){       int mi=100000;        int curr=0;                for(int i=j;i<n;i++){            if(arr[i]+curr<=arr[i])            curr = arr[i]+curr;            else            curr = arr[i];                        mi = min(curr,mi);        }        cout<<mi<<" ";     }      cout<<endl;       }return 0;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.