Minimum sum of array elements based on given Criteria
Last Updated :
24 Apr, 2023
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.
- 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 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:
- Iterate through an array using linear search for identifying all the -1’s.
- Wherever there is -1, generate 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 break out of the loop, otherwise increment 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:
C++
#include <bits/stdc++.h>
using namespace std;
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) {
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];
}
cout << sum << endl;
}
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;
}
|
Java
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);
}
}
|
Python3
def Bit_Even_Arrays(arr):
for k, v in enumerate (arr):
z = 0
if v = = - 1 :
y = k - 1
z = arr[y]
while True :
S = bin (z)[ 2 :][ 1 :: 2 ]
if (z % 2 = = 0 ) and ( len ( set (S)) = = 1 ) and ( '0' in set (S)):
break
else :
z + = 1
arr[k] = z
return ( sum (arr))
if __name__ = = '__main__' :
arr = [ 1 , 5 , - 1 , 25 , - 1 , 7 , 35 , - 1 ]
print (Bit_Even_Arrays(arr))
|
C#
using System;
using System.Linq;
class Program
{
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 = arr.Sum();
Console.WriteLine(sum);
}
static void Main( string [] args)
{
int [] arr = { 1, 5, -1, 25, -1, 7, 35, -1 };
int N = arr.Length;
Bit_Even_Array(arr, N);
}
}
|
Javascript
function Bit_Even_Array(arr, N) {
for (let i = 0; i < N; i++) {
if (arr[i] === -1) {
let prev_index = i - 1;
let prev_element = arr[i - 1];
let current_element = prev_element;
while ( true ) {
if (current_element % 2 !== 0) {
current_element++;
continue ;
}
let temp = current_element;
let position = 1;
let flag = 0;
while (temp > 0) {
let number_at_this_bit = temp & 1;
if (number_at_this_bit === 1 && position % 2 !== 0) {
flag = 1;
break ;
let prevElement = arr[i - 1];
let currentElement = prevElement;
while ( true ) {
if (currentElement % 2 !== 0) {
currentElement++;
continue ;
}
let temp = currentElement;
let position = 1;
let flag = 0;
while (temp) {
let numberAtThisBit = temp & 1;
if (numberAtThisBit === 1 && position % 2 !== 0) {
flag = 1;
break ;
}
position++;
temp >>= 1;
}
if (flag === 0) {
arr[i] = currentElement;
break ;
} else {
currentElement++;
}
}
}
}
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);
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...