Skip to content
Related Articles

Related Articles

Improve Article
Minimum sum of array elements based on given Criteria
  • Last Updated : 14 Aug, 2019

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[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))
Output:
153

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :