Created
November 20, 2021 09:11
-
-
Save Shivani13121007/8db19f54823eeb1ba706f7201e5e15c6 to your computer and use it in GitHub Desktop.
Maximize Sum Of Arr[i]*i Of An Array
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
#include<bits/stdc++.h> | |
using namespace std; | |
int maximise(vector<int>& arr) | |
{ | |
sort(arr.begin(),arr.end()); | |
int ans = 0; | |
for(int i=0;i<arr.size();i++) | |
{ | |
ans += arr[i]*i; | |
} | |
return ans; | |
} | |
int main() | |
{ | |
int n; | |
cin>>n; | |
vector<int> arr(n,0); | |
for(int i=0;i<n;i++) | |
{ | |
cin>>arr[i]; | |
} | |
int ans = maximise(arr); | |
cout<<ans; | |
} |
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
#include<bits/stdc++.h> | |
using namespace std; | |
int maximise(vector<int>& arr) | |
{ | |
/*Write your code here*/ | |
} | |
int main() | |
{ | |
int n; | |
cin>>n; | |
vector<int> arr(n,0); | |
for(int i=0;i<n;i++) | |
{ | |
cin>>arr[i]; | |
} | |
int ans = maximise(arr); | |
cout<<ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment