Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Find the maximal of maximum sum of contiguous subarray by deleting no more than one elemnent?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 694
    Answer it

    You're given an array of N integer numbers.
    The maximal sum of the array is the maximal sum of the elements of a nonempty consecutive subarray of this array. For example, the maximal sum of the array [1, -2, 3, -2, 5] is 6 because the sum of the subarray [3, -2, 5] is 6 and it is impossible to achieve greater subarray sum.
    Now you're allowed to remove no more than one element from the given array. What is the maximal possible maximal sum of the resulting array you can achieve by doing so?

 1 Answer(s)

  • Hi utkarsh , I've implemented the solution for above problem in C# ...

    using System;

    using System.Collections;

    class Program

    {

    static ArrayList mainList = new ArrayList();
    
    
    static void Main(){
        int []arr = new int[]{1, -2, 3, -2, 5};
    
        for(int i = 0; i < arr.Length; i++){
            int count = i + 1;
            ArrayList temp = new ArrayList();
            for(int j = i; j < arr.Length ; j++){
                for(int  k = i; k< count ; k++){
                    temp.Add(arr[k]);
                }
                count ++;
                mainList.Add(temp.Clone());
                temp.Clear();
            }
        }
        ShowContents();
    }
    
    static void ShowContents(){
        float  sum = int.MinValue, pos;
    
        for (int i = 0; i < mainList.Count ; i++){
            ArrayList  arr = (ArrayList)mainList[i];
            int s = 0;
            for(int j = 0; j< arr.Count; j++){
                s += (int)arr[j];
            }
            if(sum < s){
                sum = s;
            }
        }
            Console.Write("Maximum is : " + sum);
    }
    

    }

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: