- 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 out the maximum value of a 'valid' array in Python

Suppose, we have an array of n integers 'nums'. Each value in 'nums' represent its 'power'. The array will be evaluated 'valid' if the length of the array is greater than two and the first and last value of the array is equal. We have to make the array valid by deleting elements from the array so that the rest can satisfy the condition. As output, we return the maximum possible power value of the array by adding all the power values of the array.

So, if the input is like nums = [3, 4, 5, 3, 4], then the output will be 16.

If we remove the first value 3 from the array nums, then it becomes [4, 5, 3, 4]. This is a valid array and the sum of the powers are 4 + 5 + 3 + 4 = 16. This is the maximum possible sum for any valid array from the given input.

To solve this, we will follow these steps −

table := a new map

prefix := a new list initialized with value 0

negative := a new list initialized with value 0

for each index i and value j in nums, do

if j is not present in table, then

table[j] := a new pair (i, 0)

otherwise,

table[j, -1] := i

add a new element to prefix that is equal to the last element of prefix + j

duplicate the last element of negative

if j < 0 , then

last element of negative := last element of negative + j

ans := negative infinity

for each pair (i,j) in all values of table, do

if j is not same as 0, then

sm1 := prefix[j+1] - prefix[i]

if j > i+1, then

sm2 := negative[j] - negative[i+1]

otherwise,

sm2 := 0

ans := maximum of (ans, sm1 - sm2)

return ans

## Example

Let us see the following implementation to get better understanding −

def solve(nums): table = {} prefix = [0] negative = [0] for i, j in enumerate(nums): if j not in table: table[j] = [i, 0] else: table[j][-1] = i prefix += prefix[-1] + j, negative += negative[-1], if j < 0: negative[-1] += j ans = float('-inf') for i,j in table.values(): if j != 0: sm1 = prefix[j+1] - prefix[i] sm2 = negative[j] - negative[i+1] if j > i+1 else 0 ans = max(ans, sm1 - sm2) return ans print(solve([3, 4, 5, 3, 4]))

## Input

[3, 4, 5, 3, 4]

## Output

16

- Related Questions & Answers
- C++ program to find out the maximum value of i
- Program to find out the cells containing maximum value in a matrix in Python
- Program to find out the value of a given equation in Python
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find out the value of a power of 2 in Python
- Program to find out the sum of the maximum subarray after a operation in Python
- Program to Find Out the Maximum Points From Removals in Python
- Program to find out the maximum points collectable in a game in Python
- Program to find the maximum score from all possible valid paths in Python
- Program to find maximum erasure value in Python
- Python Program to find out the number of sets greater than a given value
- Python program to find the second maximum value in Dictionary
- Program to find maximum value at a given index in a bounded array in Python
- C++ program to find out the maximum sum of a minimally connected graph
- Program to find maximum possible value of smallest group in Python