- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find sum of differences between max and min elements from randomly selected k balls from n balls in Python

Suppose we have n balls which are numbered by an array nums, whose size is n and nums[i] represents the number of ball i. Now we have another value k. In each turn we pick k balls from n different balls and find the difference of maximum and minimum values of k balls and store the difference in a table. Then put these k balls again into that pot and pick again until we have selected all possible selections. Finally find the sum of all differences from the table. If the answer is too large, then return result mod 10^9+7.

So, if the input is like n = 4 k = 3 nums = [5, 7, 9, 11], then the output will be 20 because combinations are −

- [5,7,9], difference 9-5 = 4
- [5,7,11], difference 11-5 = 6
- [5,9,11], difference 11-5 = 6
- [7,9,11], difference 11-7 = 4

so 4+6+6+4 = 20.

To solve this, we will follow these steps −

- m := 10^9 + 7
- inv := a new list with elements [0, 1]
- for i in range 2 to n, do
- insert (m - floor of (m / i) * inv[m mod i] mod m) at the end of inv

- comb_count := 1
- res := 0
- for pick in range k - 1 to n - 1, do
- res := res +(nums[pick] - nums[n - 1 - pick]) * comb_count mod m
- res := res mod m
- comb_count := comb_count *(pick + 1) mod m * inv[pick + 2 - k] mod m

- return res

## Example

Let us see the following implementation to get better understanding −

def solve(n, k, nums): m = 10**9 + 7 inv = [0, 1] for i in range(2, n + 1): inv.append(m - m // i * inv[m % i] % m) comb_count = 1 res = 0 for pick in range(k - 1, n): res += (nums[pick] - nums[n - 1 - pick]) * comb_count % m res %= m comb_count = comb_count * (pick + 1) % m * inv[pick + 2 - k] % m return res n = 4 k = 3 nums = [5, 7, 9, 11] print(solve(n, k, nums))

## Input

4, 3, [5, 7, 9, 11]

## Output

20

- Related Articles
- Program to find length of longest sublist where difference between min and max smaller than k in Python
- A box contains 2 white balls,1 black ball, 2 red balls, and a green ball. 2 Balls are taken randomly from the box. What is the probability that 2 balls are white?
- Program to find max number of K-sum pairs in Python
- Program to find maximum sum of popped k elements from a list of stacks in Python
- C program to find sum, max and min with Variadic functions
- Program to find common fraction between min and max using given constraint in Python
- Program to find minimum limit of balls in a bag in Python
- Python program to randomly create N Lists of K size
- Python – Test if elements of list are in Min/Max range from other list
- Program to find maximum number of balls in a box using Python
- Program to find three unique elements from list whose sum is closest to k Python
- Program to find sum of odd elements from list in Python
- Program to count non-empty subsets where sum of min and max element of set is less than k in Python
- Python – Next N elements from K value
- Program to maximize the minimum force between balls in a bucket using Python