Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int sumDiff(int []arr, int M) {
- // code here
- // sort the array in ascending order
- Arrays.sort(arr);
- // initialize the sum variables
- int maxSum = 0;
- int minSum = 0;
- // declare the variable N as the length of the array
- int N = arr.length; // added this line
- // loop through the array from the end to get the maximum sum of N-M elements
- for (int i = N - 1; i >= N-M; i--) { // changed this line
- maxSum += arr[i];
- }
- // loop through the array from the start to get the minimum sum of M elements
- for (int i = 0; i < M; i++) {
- minSum += arr[i];
- }
- // return the difference between the sums
- return maxSum - minSum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement