Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
N is the length of array .



Explaining algorithm is mandatory. Only giving code and no explanation is not allowed. Also add your name at the bottom of answer so you get the due credits. Direct libraries in python are not allowed to be used as they skip the lagorithm parts sometimes.


Example Array : 4 -8 7 8 -100 15


Output : {7,8} is the largesr subarray with sum =15
in Algorithms and Problems by Expert (107,750 points) | 1,195 views

2 Answers

0 like 0 dislike
//Time = O(n)
//space = O(n)
//we will store the prefix sum as key, and the index as value in a map(index denotes the prefix sum till that point
//we will iterate the array,calculate the prefix sum till that point, if (prefixsum-k) is present in the map, then find the index of prefixsum-k, subtract the current counter and find the maximim length

using namespace std;
int main()
    int n,sum=0,k,x,j;
    vector<int> a(n);
    map<int,int> mp;
    for(int i=0;i<n;i++)
    int mx=INT_MIN;
    for(int i=0;i<n;i++)
        cout<<" no subarray where sum= k";
edited by anonymous
0 like 0 dislike
l=[1,5,6,7,8]   #l=list(map(int,input().split())) or l=[int(x) for x in input().split()]
k=11            #k=input()
le,c=0,0        #le for calculating length of the array,
for i in l:
for i in range(len(l)):    #i as starting index and j as ending index
    for j in range(i+1,len(l)+1):
        for i in range(i,j):
            #print('s =',s)
        if s==k:


Time Complexity : O(N^2) 

0 0
Your approach is not efficient . It uses 2 nested-for loops , hence the time complexity will be O(n*n) .