Monday, 15 July 2019

largest subarray of 0's and 1's

/*You are required to complete this method*/
int largeLen(int arr[], int n) 
{
int sum = 0; 
int maxsize = -1, startindex;
      for (int i = 0; i < n-1; i++)                // Pick a starting point as i 

sum = (arr[i] == 0)? -1 : 1; 
                for (int j = i+1; j < n; j++)   // Consider all subarrays                                                                             starting from i
                   
(arr[j] == 0)? (sum += -1): (sum += 1); 

// If this is a 0 sum subarray, then 
// compare it with maximum size subarray 
// calculated so far 
              if (sum == 0 && maxsize < j-i+1) 


maxsize = j - i +1; 
startindex = i; 

    } 

if (maxsize == -1)  maxsize=0;
return maxsize; 
}

No comments:

Post a Comment