@@ -11,7 +11,8 @@ public static int max(int a, int b)
11
11
else
12
12
return b ;
13
13
}
14
- // function implementing Kadane's Algorithm (array contains at least one positive number)
14
+
15
+ // Function implementing Kadane's Algorithm (array contains at least one positive number)
15
16
public static int kadane ( int [ ] input , int size )
16
17
{
17
18
int current_max = 0 ;
@@ -22,41 +23,42 @@ public static int kadane(int[] input, int size)
22
23
current_max = max ( 0 , current_max + input [ i ] ) ;
23
24
max_so_far = max ( max_so_far , current_max ) ;
24
25
}
25
- return max_so_far ; // maximum subarray sum
26
+ return max_so_far ; // Maximum subarray sum
26
27
}
27
28
28
29
static void Main ( String [ ] args )
29
30
{
30
31
int size , max_subarray_sum ;
31
32
32
- int [ ] input = { - 2 , 1 , - 6 , 4 , - 1 , 2 , 1 , - 5 , 4 } ; //input array
33
+ int [ ] input = { - 2 , 1 , - 6 , 4 , - 1 , 2 , 1 , - 5 , 4 } ; // Input array
33
34
34
- size = 9 ; //size of array
35
+ size = 9 ; // Size of array
35
36
36
- int flag = 0 ; //flag variable to check if all the numbers in array are negative or not
37
+ int flag = 0 ; // Flag variable to check if all the numbers in array are negative or not
37
38
38
- int largest_in_negative = input [ 0 ] ; //smallest_negative variable will store the maximum subarray sum if all the numbers are negative in array
39
+ int largest_in_negative = input [ 0 ] ; // Smallest_negative variable will store the maximum subarray sum if all the numbers are negative in array
39
40
40
- for ( int i = 0 ; i < size ; i ++ ) // scanning each element in array
41
+ for ( int i = 0 ; i < size ; i ++ ) // Scanning each element in array
41
42
{
42
- if ( input [ i ] >= 0 ) // if any element is positive, kadane's algo can be applied
43
+ if ( input [ i ] >= 0 ) // If any element is positive, kadane's algo can be applied
43
44
{
44
45
flag = 1 ;
45
46
break ;
46
47
}
47
- else if ( input [ i ] > largest_in_negative ) // if all the elements are negative, find the largest in them
48
+ else if ( input [ i ] > largest_in_negative ) // If all the elements are negative, find the largest in them
48
49
largest_in_negative = input [ i ] ;
49
50
}
50
51
51
- if ( flag == 1 ) // kadane 's algo applicable
52
+ if ( flag == 1 ) // Kadane 's algo applicable
52
53
max_subarray_sum = kadane ( input , size ) ;
53
54
else
54
- max_subarray_sum = largest_in_negative ; // kadane 's algo not applicable,
55
- //hence the max_subarray_sum will be the largest number in array itself
56
- Console . WriteLine ( "Maximum Subarray Sum is " + Convert . ToString ( max_subarray_sum ) ) ;
55
+ max_subarray_sum = largest_in_negative ; // Kadane 's algo not applicable,
56
+ // hence the max_subarray_sum will be the largest number in array itself
57
+ Console . WriteLine ( "Maximum Subarray Sum is " + Convert . ToString ( max_subarray_sum ) ) ;
57
58
Console . ReadLine ( ) ;
58
59
}
59
60
}
60
61
}
61
62
62
- //output : Maximum Subarray Sum is 6
63
+ // Output
64
+ // Maximum Subarray Sum is 6
0 commit comments