-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmaximum-sum-array-in-rotations.cpp
More file actions
46 lines (37 loc) · 953 Bytes
/
maximum-sum-array-in-rotations.cpp
File metadata and controls
46 lines (37 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// A Naive C++ program to find maximum sum rotation
#include<bits/stdc++.h>
using namespace std;
// Returns maximum value of i*arr[i]
int maxSum(int arr[], int n)
{
// Initialize result
int res = INT_MIN;
// Consider rotation beginning with i
// for all possible values of i.
for (int i=0; i<n; i++)
{
// Initialize sum of current rotation
int curr_sum = 0;
// Compute sum of all values. We don't
// acutally rotation the array, but compute
// sum by finding ndexes when arr[i] is
// first element
for (int j=0; j<n; j++)
{
int index = (i+j)%n;
curr_sum += j*arr[index];
}
// Update result if required
res = max(res, curr_sum);
}
return res;
}
// Driver code
int main()
{
int arr[] = {8, 3, 1, 2};
int n = sizeo
f(arr)/sizeof(arr[0]);
cout << maxSum(arr, n) << endl;
return 0;
}