
- 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 a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
Suppose we have a list of numbers called nums, we have to find a pair (i, j) where i < j, and nums[i] + nums[j] + (i - j) is maximized.
So, if the input is like nums = [6, 6, 2, 2, 2, 8], then the output will be 11, as if we pick the two 6 then its score is 6 + 6 + 0 - 1 = 11.
To solve this, we will follow these steps:
large := nums[0]
maxi := 0
for i in range 1 to size of nums, do
large := large - 1
maxi := maximum of large + nums[i] and maxi
large := maximum of large and nums[i]
return maxi
Let us see the following implementation to get better understanding
Example
class Solution: def solve(self, nums): large = nums[0] maxi = 0 for i in range(1, len(nums)): large -= 1 maxi = max(large + nums[i], maxi) large = max(large, nums[i]) return maxi ob = Solution() nums = [6, 6, 2, 2, 2, 8] print(ob.solve(nums))
Input
[6, 6, 2, 2, 2, 8]
Output
11
- Related Articles
- Program to find a triplet nums[i] < nums[k] < nums[j] from a list nums in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Construct a Turing machine for L = {aibjck | i*j = k; i, j, k ≥ 1}
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++

Advertisements