maximum subarray whose sum equals 0

后端 未结 12 1330
礼貌的吻别
礼貌的吻别 2020-12-04 08:14

An array contains both positive and negative elements, find the maximum subarray whose sum equals 0.

相关标签:
12条回答
  • 2020-12-04 08:40

    Hope this will help.

    int v[DIM] = {2, -3,  1,  2,  3,  1,  4, -6,  7, -5, -1};
    int i,j,sum=0,counter=0;
    
        for (i=0; i<DIM; i++) {
            sum = v[i];
            counter=0;
            for (j=i+1; j<DIM;j++) {
                sum += v[j];
                counter++;
                if (sum == 0) {
                    printf("Sub-array starting from index %d, length %d.\n",(j-counter),counter +1);
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-04 08:41
    1. Given A[i]
      A[i] | 2 |  1 | -1 | 0 | 2 | -1 | -1
    -------+---|----|--------|---|----|---
    sum[i] | 2 |  3 |  2 | 2 | 4 |  3 |  2
    
    2. sum[i] = A[0] + A[1] + ...+ A[i]
    3. build a map<Integer, Set>
    4. loop through array sum, and lookup map to get the set and generate set, and push <sum[i], i> into map.
    
    Complexity O(n)
    
    0 讨论(0)
  • 2020-12-04 08:42

    Hope this help you.

     private static void subArrayZeroSum(int array[] , int findSum){
        Map<Integer,HashSet<Integer>> map = new HashMap<Integer,HashSet<Integer>>();
        int sum = 0;
       for(int index = 0 ; index < array.length ; index ++){
           sum +=array[index];
           if(array[index] == findSum){
               System.out.println(" ["+index+"]");
           }
           if(sum == findSum && index > 0){
               System.out.println(" [ 0 , "+index+" ]");
           }
           if(map.containsKey(sum)){
               HashSet<Integer> set = map.get(sum);
               if(set == null)
                   set = new HashSet<Integer>();
    
               set.add(index);
               map.put(sum, set);
    
               for(int val : set){
    
                   if(val + 1 != index && (val + 1) < index){
                      System.out.println("["+(val + 1) +","+index+" ]");
                   }
               }
    
            }else{
                HashSet<Integer> set = map.get(sum);
                   if(set == null)
                       set = new HashSet<Integer>();
                   set.add(index);
                map.put(sum, set);
            }
       }        
    }
    
    0 讨论(0)
  • 2020-12-04 08:43

    One of the solution:

    Let's say we have an array of integer, int[] arr = {2,1,-1,-2};

    We will traverse using the for loop until we find the number < 0 OR <= 0 i = 2;

    With the inner loop, we will traverse assign the value to j = i-1 So, We can able to find the positive value.

    for(int i = 0; i<arr.length; i++){
          int j = 0;
          int sum = arr[i];
          if(arr[i] < 0){
          j = i - 1;
    }
    

    We will have one sum variable, which maintaining the sum of arr[i] and arr[j] and updating the result.

    If the sum is < 0 then, we have to move left side of the array and so, we will decrement the j by one, j--

    for(j = i-1; j>=0; j--) {
          sum = sum + arr[j];
          if(sum == 0){
          System.out.println("Index from j=" + j+ " to i=" + i);
          return true;
       }
    }
    

    If the sum is > 0 then, we have to move right side of the array and so, we will increment the i

    When we find the sum == 0 then we can print the j and i index and return or break the loop.

    And so, It's complete in a linear time. As well we don't need to use any other data structure as well.

    0 讨论(0)
  • 2020-12-04 08:44

    An array contains positive and negative numbers. Find the sub-array that has the maximum sum

    public static int findMaxSubArray(int[] array)
    {
        int max=0,cumulativeSum=0,i=0,start=0,end=0,savepoint=0;
        while(i<array.length)
        {
            if(cumulativeSum+array[i]<0)
            {
                cumulativeSum=0;
                savepoint=start;
                start=i+1;
            }
            else
                cumulativeSum=cumulativeSum+array[i];
            if(cumulativeSum>max)
            {
                    max=cumulativeSum;
                    savepoint=start;
                    end=i;
            }
            i++;
        }
    
        System.out.println("Max : "+max+"  Start indices : "+savepoint+"  end indices : "+end);
        return max;
    
    }
    
    0 讨论(0)
  • 2020-12-04 08:49

    Below codes can find out every possible sub-array that has a sum being a given number, and (of course) it can find out the shortest and longest sub-array of that kind.

    public static void findGivenSumSubarray(int arr[], int givenSum) {
        int sum = 0;
        int sStart = 0, sEnd = Integer.MAX_VALUE - 1;  // Start & end position of the shortest sub-array
        int lStart = Integer.MAX_VALUE - 1, lEnd = 0;  // Start & end position of the longest  sub-array
    
        HashMap<Integer, ArrayList<Integer>> sums = new HashMap<>();
        ArrayList<Integer> indices = new ArrayList<>();
    
        indices.add(-1);
        sums.put(0, indices);
    
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
            indices = sums.get(sum - givenSum);
            if(indices != null) {
                for(int index : indices) {
                    System.out.println("From #" + (index + 1) + " to #" + i);
                }
                if(i - indices.get(indices.size() - 1) < (sEnd - sStart + 1)) {
                    sStart = indices.get(indices.size() - 1) + 1;
                    sEnd = i;
                }
                if(i - indices.get(0) > (lEnd - lStart + 1)) {
                    lStart = indices.get(0) + 1;
                    lEnd = i;
                }
            }
            indices = sums.get(sum);
            if(indices == null) {
                indices = new ArrayList<>();
            }
            indices.add(i);
            sums.put(sum, indices);
        }
    
        System.out.println("Shortest sub-arry: Length = " + (sEnd - sStart + 1) + ", [" + sStart + " - " + sEnd + "]");
        System.out.println("Longest  sub-arry: Length = " + (lEnd - lStart + 1) + ", [" + lStart + " - " + lEnd + "]");
    }
    
    0 讨论(0)
提交回复
热议问题