Created
March 20, 2020 16:25
-
-
Save abhi2694/8978d996d562e6203fd7a46c726228d9 to your computer and use it in GitHub Desktop.
This file contains 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
//Ques : Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed | |
// C++ program to find max value of i*arr[i] | |
#include <iostream> | |
using namespace std; | |
// Returns max possible value of i*arr[i] | |
int maxSum(int arr[], int n) | |
{ | |
// Find array sum and i*arr[i] with no rotation | |
int arrSum = 0; // Stores sum of arr[i] | |
int currVal = 0; // Stores sum of i*arr[i] | |
for (int i=0; i<n; i++) | |
{ | |
arrSum = arrSum + arr[i]; | |
currVal = currVal+(i*arr[i]); | |
} | |
// Initialize result as 0 rotation sum | |
int maxVal = currVal; | |
// Try all rotations one by one and find | |
// the maximum rotation sum. | |
for (int j=1; j<n; j++) | |
{ | |
currVal = currVal + arrSum-n*arr[n-j]; | |
if (currVal > maxVal) | |
maxVal = currVal; | |
} | |
// Return result | |
return maxVal; | |
} | |
// Driver program | |
int main(void) | |
{ | |
int arr[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
int n = sizeof(arr)/sizeof(arr[0]); | |
cout << "\nMax sum is " << maxSum(arr, n); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment