Skip to content

Instantly share code, notes, and snippets.

@Shivani13121007
Created November 20, 2021 09:16
Show Gist options
  • Save Shivani13121007/7b4e3b4c84d739dfd7366f6628337538 to your computer and use it in GitHub Desktop.
Save Shivani13121007/7b4e3b4c84d739dfd7366f6628337538 to your computer and use it in GitHub Desktop.
Max Sum In The Configuration
import java.util.*;
public class Main {
public static int maximise(int[]arr) {
//write your code here
int n = arr.length;
int sum = 0;
int S0 = 0;
for(int i=0; i < n;i++) {
sum += arr[i];
S0 += arr[i]*i;
}
int max = S0;
int Si = S0;
for(int i=0; i < n-1 ;i++) {
int temp = Si + sum - n*arr[n-i-1];
Si = temp;
if(temp > max) {
max = temp;
}
}
return max;
}
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);
}
}
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