//just playing for fun public static SubArrayResult maxsubArrayWithIndexMethod2(int[] array) { SubArrayResult result = new SubArrayResult(); int maxsum = Int32.MinValue; int runningsum = 0; StringBuilder sbIndexrunning = new StringBuilder(); StringBuilder maxIndexstring = new StringBuilder(); for (int i = 0; i < array.Length; i++) { runningsum = runningsum + array[i]; sbIndexrunning.Append(i).Append(','); if (runningsum > maxsum) { maxsum = runningsum; maxIndexstring = sbIndexrunning; } if (runningsum < 0) { runningsum = 0; sbIndexrunning.Clear(); } } if (maxIndexstring.Length > 0) { result.Sum = maxsum; string[] maxvalues = maxIndexstring.ToString().TrimEnd(',').Split(','); result.StartIndex = Convert.ToInt32(maxvalues[0]); result.EndIndex = Convert.ToInt32(maxvalues[maxvalues.Length - 1]); } return(result); }
//Kadane’s algorithm for 1D array public static SubArrayResult maxsubArrayWithIndex(int[] array) { int maxsum = Int32.MinValue; int runningsum = 0; List <int> runningindexlist = new List <int>(); List <int> maxindexlist = null; SubArrayResult result = null; for (int i = 0; i < array.Length; i++) { runningsum = runningsum + array[i]; runningindexlist.Add(i); if (runningsum > maxsum) { maxsum = runningsum; maxindexlist = new List <int>(runningindexlist); } //reset sum to 0 when the sum value is negative if (runningsum < 0) { runningsum = 0; runningindexlist.Clear(); } } if (maxindexlist.Count > 0) { result = new SubArrayResult(); result.Sum = maxsum; result.StartIndex = maxindexlist[0]; result.EndIndex = maxindexlist[maxindexlist.Count - 1]; } return(result); }
public static MatrixSubArrayResult MaxSubMatrixWithIndex(int[,] matrix) { int rowcount = matrix.GetLength(0); int colcount = matrix.GetLength(1); int[] partialsum = new int[colcount]; //size of colum int maxsum = Int32.MinValue; MatrixSubArrayResult result = new MatrixSubArrayResult(); //test List <String> test = new List <string>(); // 1 2 -1 //-3 -1 -4 // 1 -5 2 // get all possible rows.. //Now in this 3 x 3 matrix, the row can be grouped as [1],[1,2],[1,2,3],[2],[2,3],[3]. for (int rowstart = 0; rowstart < rowcount; rowstart++) { ClearArray(partialsum); for (int rowend = rowstart; rowend < rowcount; rowend++) { //here we will get all possible row grouping //For the given row range ( 1 to 1 or 1 -3) we sum the col value //p[0] will be the sum of first col //p[1] will be the sume of secon col //p[columcount -1] //Important note: here we don't clear the array in this iteration //becoze row of 1 result will be added to row of 2 and then three.. //we clear it only after the master loop value changes for (int i = 0; i < colcount; i++) { //This is an important piece..what we do is //for [1 t0 1] row p[0] = 1 p[1] = 2 p[2] = -1 partialsum[i] = partialsum[i] + matrix[rowend, i]; } //StringBuilder sb = new StringBuilder(); ////testing //for (int j = 0; j < partialsum.Length; j++) //{ // sb.Append(partialsum[j]).Append(','); //} //test.Add(sb.ToString()); //here we can play with co-ordinates SubArrayResult infores = maxsubArrayWithIndex(partialsum); if (infores != null && infores.Sum > maxsum) { //maxsum = sum; maxsum = infores.Sum; result.Sum = infores.Sum; result.RowStart = rowstart; result.RowEnd = rowend; result.ColStart = infores.StartIndex; result.ColEnd = infores.EndIndex; } } } return(result); }