C Sharp program to Find the contiguous sub array with Maximum sum

Given an array of N integers.Find the contiguous sub array with Maximum sum.

Contiguous sub array means sub-array with sequence index like (0,1,2..) or(1,2,3…)

INPUT
N:Size of the array
arr[N]: arr[0],arr[1]…. arr[N-1] number of integer elements

Output

Print the maximum sum of the contiguous sub-array in a separate line for each test case.

For Example:
Input array size is 5
array values { -1, 3, -4,5, 7 }

Output of Maximum sum of the contiguous sub-array is 12 of elements (5,7)

Method-1

Program  to Find the contiguous sub array with Maximum sum

C# Code

using System;
class SubarrayWithMaximumSum
    {
        static void Main(string[] args)
        {
            int maxvalue = Int32.MinValue;
            //array with size N
            int[] arr = new int[] { -1, 3, -4,5, 7 };
            int sum = 0;

            //logic to find the Subarray with Maximum sum
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = i; j < arr.Length; j++)
                {
                    sum = sum + arr[j];
                    if (maxvalue < sum)
                    {  
                        //maxvalue stores the maximum sum of array wiht sequence index
                        maxvalue = sum;
                    }
                    else
                    {   //if sequence fails we will start with new index value as starting index for sub array
                        sum = 0;
                        break;
                    }
                }
                sum = 0;
            }
            Console.WriteLine("Sub array with Maximum sum= {0}",maxvalue);
        }
    }

Output

Sub array with Maximum sum= 12.

Method-2

Algorithm

Initialize:
minvalue =0
maxvalue= minimum value of an integer

Loop for each element of the array
(a) sum = sum + arr[i];
(b) if (maxvalue < minvalue
maxvalue = maxvalue;
else
sum = 0;

class SubarrayWithMaximumSum
    {
        static void Main(string[] args)
        {
            int maxvalue = Int32.MinValue;
            //array with size N
            int[] arr = new int[] { -1, 3, -4, 5, 7 };
            int minvalue = 0;
            //int start_index=0, end_index=0,start=0,end=0;

            //logic to find the Subarray with Maximum sum
            for (int i = 0; i < arr.Length; i++)
            {
                minvalue = minvalue + arr[i];
                if (maxvalue < minvalue)
                    {
                        //maxvalue stores the maximum sum of array wiht sequence index
                        maxvalue = minvalue;
                        
                    }
                    else
                    {   //if sequence fails we will start with new index value as starting index for sub array
                        minvalue = 0;
                       
                        
                    }
                }
               
            
            Console.WriteLine("Sub array with Maximum sum= {0}", maxvalue);
             
        }
    }

Output

Sub array with Maximum sum= 12.

Related Posts