Created
November 20, 2021 09:16
-
-
Save Shivani13121007/7b4e3b4c84d739dfd7366f6628337538 to your computer and use it in GitHub Desktop.
Max Sum In The Configuration
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 | |
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); | |
} | |
} | |
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