Find the largest continuous sub-array of an array V of size N, with sum equal to S.


  • 0
    S

    find the largest continuous sub-array of an array V of size N, with sum equal to S.


  • 0
    S
    #include <iostream>
    #include<map>
    using namespace std;
    
    int printSubarray(int arr[], int n , int givenSum)
    {
       int sum=0;
       map<int,int> MAP;
       int maxI=0;
    
        for(int i=0;i<n;i++)
        {
            sum += arr[i];
    
    
    
           if(MAP.count(sum-givenSum)){//0 and 1//in case of map
    				cout<<"index"<<MAP[sum-givenSum]+1<<":_"<<i<<endl;
    			    maxI=max(maxI,i-MAP[sum-givenSum]);
    			}
           else if( sum == givenSum ){
            	   cout<<"index"<<"0"<<":++"<<i<<endl;
        	       maxI=max(maxI,i+1);
    
        	}
            else
            	MAP[sum] = i;
        }
    
        return maxI;
    }
    
    int main()
    {
    	int arr[]={4,-2,3,0,0,7,1,-3,0,0,8,-3,0,0,0,0,1,-1};
    	int n = sizeof(arr)/sizeof(arr[0]);
    	int k=5;
    	cout<<"maxI"<<printZeroSumSubarray(arr,n,k );
    	return 0;
    }
    
    
    
    
    O/P :
    
    index0:++2
    index0:++3
    index0:++4
    index7:_10
    index10:_11
    index10:_12
    index10:_13
    index10:_14
    index10:_15
    index10:_17
    maxI8
    
    I am not able to handle the case from pos  5 to 9 as it is also come into subarray with sum K 
    
    Can anyone help in this

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.