Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b95d156

Browse files
used brute force to find max and min sum in subarray of size k
1 parent 19c559c commit b95d156

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

‎Dynamic Programming/maxSumSubarrayWhoseSizeisK.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using namespace std;
55

66
//program to find maximum subarray sum whose size equals k
77

8-
int maxSum(int arr[],int size,int k,int i=0)
8+
int maxSum(int arr[],int size,int k)
99
{
1010
int Max = 0,curMax;
1111
for(int i = 0 ; i <= (size-k) ; i++)
@@ -25,9 +25,34 @@ int maxSum(int arr[],int size,int k,int i=0)
2525
return Max;
2626
}
2727

28+
int minSum(int arr[],int size,int k)
29+
{
30+
int Min = INT_MAX,curMin;
31+
for(int i = 0 ; i <= (size-k) ; i++)
32+
{
33+
34+
curMin = 0;
35+
for(int j = i ; j < i+k ; j++)
36+
{
37+
curMin += arr[j];
38+
39+
if(curMin < Min)
40+
Min = curMin;
41+
}
42+
43+
}
44+
45+
return Min;
46+
}
47+
2848

2949
int main()
3050
{
31-
int arr[] =
51+
int arr[] = {3,4,6,7,-10};
52+
int size = sizeof(arr)/sizeof(arr[0]);
53+
cout<<maxSum(arr,size,2);
54+
55+
cout<<endl<<minSum(arr,size,2);
56+
3257
return 0;
3358
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /