-
Notifications
You must be signed in to change notification settings - Fork 30
/
Rotate Function.java
40 lines (28 loc) · 1.01 KB
/
Rotate Function.java
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
/*
Solution by Rahul Surana
***********************************************************
You are given an integer array nums of length n.
Assume arrk to be an array obtained by rotating nums by k positions clock-wise.
We define the rotation function F on nums as follow:
F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].
Return the maximum value of F(0), F(1), ..., F(n-1).
The test cases are generated so that the answer fits in a 32-bit integer.
***********************************************************
*/
class Solution {
public int maxRotateFunction(int[] nums) {
int s = 0;
int fval = 0;
for(int i = 0; i < nums.length; i++){
s+=nums[i];
fval += (i*nums[i]);
}
int ans = fval;
for(int i = 0; i < nums.length; i++){
fval += (s - (nums.length * nums[nums.length-1-i]));
// System.out.println(i + " " + fval);
ans = Math.max(ans,fval);
}
return ans;
}
}