
- 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 number of operations needed to make pairs from first and last side are with same sum in Python
Suppose we have a list of numbers called nums. The length of this list is even. Now consider an operation where we select any number in nums and update it with a value in range [1 and maximum of nums]. We have to find the minimum number of such operations required such that, for every i, nums[i] + nums[n-1-i] equals to the same number.
So, if the input is like nums = [8,6,2,5,9,2], then the output will be 2, because if we change first 2 at nums[2] to 5, and 9 at nums[4] to 4, then the elements will be [8,6,5,5,4,2], then the nums[i] + nums[n-1-i] for each i will be (8+2) = (6+4) = (5+5) = 10.
To solve this, we will follow these steps −
- N := size of nums
- mx := maximum of nums
- events := a new list
- idx := 0
- while idx < floor of N / 2, do
- a := nums[idx]
- b := nums[N - idx - 1]
- insert a pair (minimum of (a + 1), (b + 1), 1) at the end of events
- insert a pair (a + b, 1) at the end of events
- insert a pair (a + b + 1, -1) at the end of events
- insert a pair (maximum of (a + mx) and (b + mx + 1), -1) at the end of events
- idx := idx + 1
- sort the list events
- current := 0
- mx_same := 0
- for each pair (event, delta) in events, do
- current := current + delta
- mx_same := maximum of current and mx_same
- return N - mx_same
Example
Let us see the following implementation to get better understanding −
def solve(nums): N = len(nums) mx = max(nums) events = [] idx = 0 while idx < N // 2: a = nums[idx] b = nums[N - idx - 1] events.append((min(a + 1, b + 1), 1)) events.append((a + b, 1)) events.append((a + b + 1, -1)) events.append((max(a + mx, b + mx) + 1, -1)) idx += 1 events.sort() current = 0 mx_same = 0 for event, delta in events: current += delta mx_same = max(current, mx_same) return N - mx_same nums = [8,6,2,5,9,2] print(solve(nums))
Input
[6, 8, 5, 2, 3]
Output
2
- Related Articles
- Program to find number of quadruples for which product of first and last pairs are same in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ program to find minimum how many operations needed to make number 0
- Program to find a sublist where first and last values are same in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Program to find number of restricted paths from first to last node in Python
- Program to find equal sum arrays with minimum number of operations in Python
- Program to find number of coins needed to make the changes in Python
- C++ program to count number of steps needed to make sum and the product different from zero
- Program to find minimum number of operations to make string sorted in Python
- Program to find max number of K-sum pairs in Python
- Program to find number of operations needed to convert list into non-increasing list in Python
- Program to find number of coins needed to make the changes with given set of coins in Python
- Program to find minimum number of operations required to make one number to another in Python

Advertisements