- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- 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++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- 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}
- 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 unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++

Advertisements