Open In App

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 the 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 are in such a way that A[i]>=A[i-1] and also for the given array A[0]!=-1.

Examples:

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

Output : 153 

Explanation:-

  • 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 

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

Output : 142

Approach:

Below is the implementation of the above approach: 




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate sum of  array
// according to the given conditions
void Bit_Even_Array(int arr[], int N)
{
 
    for (int i = 0; i < N; i++) {
 
        if (arr[i] == -1) {
 
            int prev_index = i - 1; // since i!=0 ,
            // hence no edge case for element present at
            // index=1
 
            int prev_element
                = arr[i - 1]; // index of previous element
 
            int current_element
                = prev_element; // element at previous index
 
            while (true) {
 
                if (current_element % 2
                    != 0) { // if element is odd
                    current_element++;
                    continue;
                }
 
                int temp = current_element;
 
                int position = 1;
                int flag = 0;
                while (temp) {
 
                    int number_at_this_bit
                        = temp & 1; // checking the bits
 
                    // To satisfy the condition 1 should not
                    // be present at even position
                    if (number_at_this_bit == 1
                        && position % 2 != 0) {
                        flag = 1;
                        break;
                    }
 
                    position++;
 
                    temp >>= 1;
                }
 
                // if condition is met we replace arr[i] =
                // current_element
                if (flag == 0) {
                    arr[i] = current_element;
                    break;
                }
                else {
                    current_element++;
                }
            }
        }
    }
 
    // calculating total sum of the array
    int sum = 0;
 
    for (int i = 0; i < N; i++) {
        sum += arr[i];
    }
 
    cout << sum << endl;
}
 
// Driver code
int main()
{
 
    int arr[] = { 1, 5, -1, 25, -1, 7, 35, -1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    Bit_Even_Array(arr, N);
 
    return 0;
}




import java.util.*;
 
public class Gfg {
    public static void main(String[] args) {
        int[] arr = { 1, 5, -1, 25, -1, 7, 35, -1 };
        int N = arr.length;
 
        Bit_Even_Array(arr, N);
    }
 
    public static void Bit_Even_Array(int arr[], int N) {
        for (int i = 0; i < N; i++) {
            if (arr[i] == -1) {
                int prev_index = i - 1;
                int prev_element = arr[i - 1];
                int current_element = prev_element;
                while (true) {
                    if (current_element % 2 != 0) {
                        current_element++;
                        continue;
                    }
                    int temp = current_element;
                    int position = 1;
                    int flag = 0;
                    while (temp > 0) {
                        int number_at_this_bit = temp & 1;
                        if (number_at_this_bit == 1 && position % 2 != 0) {
                            flag = 1;
                            break;
                        }
                        position++;
                        temp >>= 1;
                    }
                    if (flag == 0) {
                        arr[i] = current_element;
                        break;
                    } else {
                        current_element++;
                    }
                }
            }
        }
        int sum = 0;
        for (int i = 0; i < N; i++) {
            sum += arr[i];
        }
        System.out.println(sum);
    }
}




# 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) and (len(set(S)) == 1) and ('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))




using System;
using System.Linq;
 
class Program
{
    // Function to generate sum of  array
    // according to the given conditions
    static void Bit_Even_Array(int[] arr, int N)
    {
        for (int i = 0; i < N; i++)
        {
            // Check if the current element is -1
            if (arr[i] == -1)
            {
                int prev_index = i - 1; // since i!=0 ,
                // hence no edge case for element present at
                // index=1
 
                int prev_element = arr[i - 1]; // index of previous element
 
                int current_element = prev_element; // element at previous index
 
                while (true)
                {
                    if (current_element % 2 != 0) // if element is odd
                    {
                        current_element++;
                        continue;
                    }
 
                    int temp = current_element;
                    int position = 1;
                    int flag = 0;
                    while (temp > 0)
                    {
                        int number_at_this_bit = temp & 1; // checking the bits
 
                        // To satisfy the condition 1 should not
                        // be present at even position
                        if (number_at_this_bit == 1 && position % 2 != 0)
                        {
                            flag = 1;
                            break;
                        }
 
                        position++;
                        temp >>= 1;
                    }
 
                    // if condition is met we replace arr[i] =
                    // current_element
                    if (flag == 0)
                    {
                        arr[i] = current_element;
                        break;
                    }
                    else
                    {
                        current_element++;
                    }
                }
            }
        }
 
        // calculating total sum of the array
        int sum = arr.Sum();
 
        Console.WriteLine(sum);
    }
 
    // Driver code
    static void Main(string[] args)
    {
        int[] arr = { 1, 5, -1, 25, -1, 7, 35, -1 };
        int N = arr.Length;
 
        Bit_Even_Array(arr, N);
    }
}




function Bit_Even_Array(arr, N) {
    for (let i = 0; i < N; i++) {
        // Check if the current element is -1
        if (arr[i] === -1) {
            let prev_index = i - 1; // since i!=0
            let prev_element = arr[i - 1]; // index of previous element
            let current_element = prev_element; // element at previous index
 
            while (true) {
                if (current_element % 2 !== 0) { // if element is odd
                    current_element++;
                    continue;
                }
 
                let temp = current_element;
                let position = 1;
                let flag = 0;
                while (temp > 0) {
                    let number_at_this_bit = temp & 1; // checking the bits
 
                    // To satisfy the condition 1 should not
                    // be present at even position
                    if (number_at_this_bit === 1 && position % 2 !== 0) {
                        flag = 1;
                        break;
            let prevElement = arr[i - 1]; // index of previous element
 
            let currentElement = prevElement; // element at previous index
 
            while (true) {
 
                if (currentElement % 2 !== 0) { // if element is odd
                    currentElement++;
                    continue;
                }
 
                let temp = currentElement;
 
                let position = 1;
                let flag = 0;
                while (temp) {
 
                    let numberAtThisBit = temp & 1; // checking the bits
 
                    // To satisfy the condition 1 should not
                    // be present at even position
                    if (numberAtThisBit === 1 && position % 2 !== 0) {
                        flag = 1;
                        break;
                    }
 
                    position++;
 
                    temp >>= 1;
                }
 
                // if condition is met we replace arr[i] =
                // current_element
                if (flag === 0) {
                    arr[i] = currentElement;
                    break;
                } else {
                    currentElement++;
                }
            }
        }
    }
 
    // calculating total sum of the array
    let sum = 0;
 
    for (let i = 0; i < N; i++) {
        sum += arr[i];
    }
 
    console.log(sum);
}
 
let arr = [1, 5, -1, 25, -1, 7, 35, -1];
let N = arr.length;
 
bitEvenArray(arr, N);

Output:
153

Time complexity:-O(NlogN). This is because for each element in the array we are traversing the digit in binary format which is of logN order.Hence (NlogN)

Auxiliary Space:-O(N).To store the array 


Article Tags :