# Minimum sum of array elements based on given Criteria

Given an array ** A[] ** of size **N ** with entries as integers, some of the entries are -1. The task is to replace -1’s with numbers satisfying below criteria.

- The binary representation of the number to be replaced with should have only 0’s in its odd positions and the number has to be even.
- The array entries A[i] with which -1’s are replaced with are in such a way that
**A[i]>=A[i-1]**and also for the given array**A[0]!=-1**.

Find the minimum sum of array entries possible after the above operations are done.

**Examples:**

Input :A[] = {1, 5, -1, 25, -1, 7, 35, -1}

Output :153

Index 2: Replacing -1 with 8 as its binary representation is 1000 which has 0

in its odd places and 8 is even and 8 >=5

Index 4: Replacing -1 with 32 as its binary representation is 100000 which has 0

in its odd places and 32 is even and 32>=25

Index 7: Replacing -1 with 40 as its binary representation is 101000 which has 0

in its odd places and 40 is even and 40>=35

Input :A[] = {4, 8, 12, -1, 3, 0, 15, -1, 34, -1}

Output :142

**Approach:**

- Iterate through an array using linear search for identifying all the -1’s.
- Wherever there is -1, generating another while loop starting from the number that is at an index 1 less than the current index.
- Check the odd positions of the binary representations of all the terms in progression and if it contains only zeros as expected then breaking out of the loop, otherwise incrementing the iterator by 1 until we reach the required number.
- When the required number is met, the corresponding element at the given index is replaced with the new number found that is satisfying all the conditions.
- Calculate the sum of array entries after all -1’s are replaced.

Below is the implementation of the above approach:

`# Find the minimum sum of array ` `# entries following given criteria. ` `def` `Bit_Even_Arrays(arr): ` ` ` ` ` `# Iterating through the ` ` ` `# array to find -1's ` ` ` `for` `k, v ` `in` `enumerate` `(arr): ` ` ` `z ` `=` `0` ` ` `if` `v ` `=` `=` `-` `1` `: ` ` ` ` ` `# Starting from the entry ` ` ` `# with index 1 less than -1 ` ` ` `# as A[i]>= A[i-1] ` ` ` `y ` `=` `k ` `-` `1` ` ` `z ` `=` `arr[y] ` ` ` ` ` `# Initiating the infinite series ` ` ` `# that satisfies given criteria ` ` ` `# and breaking out of the loop ` ` ` `# once it satisfies ` ` ` `while` `True` `: ` ` ` `S ` `=` `bin` `(z)[` `2` `:][` `1` `::` `2` `] ` ` ` `if` `(z ` `%` `2` `=` `=` `0` `) \ ` ` ` `&(` `len` `(` `set` `(S))` `=` `=` `1` `)\ ` ` ` `& (` `'0'` `in` `set` `(S)): ` ` ` `break` ` ` `else` `: ` ` ` ` ` `# incrementing the entry ` ` ` `# until the required ` ` ` `# entry is met ` ` ` `z ` `+` `=` `1` ` ` `arr[k]` `=` `z ` ` ` `return` `(` `sum` `(arr)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `1` `, ` `5` `, ` `-` `1` `, ` `25` `, ` `-` `1` `, ` `7` `, ` `35` `, ` `-` `1` `] ` ` ` `print` `(Bit_Even_Arrays(arr)) ` |

*chevron_right*

*filter_none*

**Output:**

153

## Recommended Posts:

- Fill an array based on frequency where elements are in range from 0 to n-1
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Find minimum value to assign all array elements so that array product becomes greater
- Find the first, second and third minimum elements in an array
- Minimum value among AND of elements of every subset of an array
- Sum of all minimum occurring elements in an Array
- Remove minimum elements from array so that max <= 2 * min
- Remove minimum elements from the array such that 2*min becomes more than max
- Minimum sum obtained from groups of four elements from the given array
- Minimum gcd operations to make all array elements one
- Minimum array element changes to make its elements 1 to N
- Minimum possible sum of array elements after performing the given operation
- Minimum operation to make all elements equal in array
- Minimum steps to make all the elements of the array divisible by 4
- Minimum no. of operations required to make all Array Elements Zero

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.