-
Find the maximal of maximum sum of contiguous subarray by deleting no more than one elemnent?
over 8 years ago
-
over 8 years ago
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); }
}
-
1 Answer(s)