Last active
August 24, 2020 20:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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