InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Maximum Difference

Given an array of integers A and an integer B. Find and return the maximum value of ** s1 - s2 **

where s1 = sum of any subset of size B, s2 = sum of elements of A - sum of elemets of s1

Note ** x ** denotes the absolute value of x.

Input Format

The arguments given are the integer array A and integer B.

Output Format

Return the maximum value of | s1 - s2 |.


1 <= B < length of the array <= 100000
1 <= A[i] <= 10^5 

For Example

Input 1:
    A = [1, 2, 3, 4, 5]
    B = 2
Output 1:

Input 2:
    A = [5, 17, 100, 11]
    B = 3
Output 2:
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Maximum Difference on Interview Code Editor
Sign Up
to access hints and editorial solutions for Maximum Difference


Click here to start solving coding interview questions