Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Last active August 24, 2020 20:06
Show Gist options
  • Save cs-fedy/827b6be52ef8931d42168cd98cf0773c to your computer and use it in GitHub Desktop.
Save cs-fedy/827b6be52ef8931d42168cd98cf0773c to your computer and use it in GitHub Desktop.
Find maximum value of Sum with only rotations on given array allowed in C.
int get_max(int x, int y) {
if (x > y)
return x;
return y;
}
int get_sum(int * arr, int arr_size) {
int sum = 0, index;
for(index = 0; index < arr_size; index++) {
sum += index * arr[index];
}
return sum;
}
int max_sum(int * arr, int arr_size) {
// Time complexity of this solution is O(n2).
int index, j, temp, max = 0;
for (index = 0; index < arr_size; index++) {
temp = arr[0];
for (j = 1; j < arr_size; j++) {
arr[j - 1] = arr[j];
}
arr[arr_size - 1] = temp;
max = get_max(get_sum(arr, arr_size), max);
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment