# 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.

1. 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.
2. 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!=-1.
3. 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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)) `

Output:

```153
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.