Created
November 20, 2021 09:14
-
-
Save Shivani13121007/0230c881f4f3d2bd74f2d18e9fb4ff9b 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
import java.util.*; | |
public class Main { | |
public static int maximise(int[]arr) { | |
//write your code here | |
Arrays.sort(arr); | |
int ans = 0; | |
for(int i=0; i < arr.length;i++) { | |
ans += arr[i]*i; | |
} | |
return ans; | |
} | |
public static void main(String[]args) { | |
//input work | |
Scanner scn = new Scanner(System.in); | |
int n = scn.nextInt(); | |
int[]arr = new int[n]; | |
for(int i = 0 ; i < n; i++) { | |
arr[i] = scn.nextInt(); | |
} | |
int ans = maximise(arr); | |
System.out.println(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
import java.util.*; | |
public class Main { | |
public static int maximise(int[]arr) { | |
//write your code here | |
return 0; | |
} | |
public static void main(String[]args) { | |
//input work | |
Scanner scn = new Scanner(System.in); | |
int n = scn.nextInt(); | |
int[]arr = new int[n]; | |
for(int i = 0 ; i < n; i++) { | |
arr[i] = scn.nextInt(); | |
} | |
int ans = maximise(arr); | |
System.out.println(ans); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment