Optimizing Binary Array for Minimum Sum and Lexicographical Order
Last Updated :
04 Dec, 2023
Given a binary array X[] of length N (N >= 3). You are allowed to select any three consecutive integers of the array and replace them with 1, 0, and 0 respectively and you can apply the given operation at any number of times including zero, the task is to return the minimum possible sum of all elements of X[] such that X[] must be lexicographically smallest after applying the given operation.
Examples:
Input: N=3, X[] = {0, 1, 1}
Output: 2
Explanation: We can’t apply operations here, e.g. If we apply operation at first three consecutive indices (Which is only possible case here), then X[] = {1, 0, 0}.It can be seen that {1, 0, 0} is not lexographically smallest than initial X[] = {0, 1, 1}. Therefore, it will not be appropriate to apply any operation and remain X[] as it is. So the minimum sum will be 2.
Input: N = 6, X[] = {0, 0, 1, 0, 1, 1}
Output: 1
Explanation: Operation 1(at three consecutive indices (3, 4, 5)): updated X[] = {0, 0, 1, 1, 0, 0}, Operation 2(at three consecutive indices (2, 3, 4)): updated X[] = {0, 0, 1, 0, 0, 0}
Now, at last X[] = {0, 0, 1, 0, 0, 0} smallest possible flexographically smallest X[], which gives the sum of X[] as 1. Therefore, output is equal to 1.
Approach: To solve the problem follow the below steps:
- Find the index of the first occurrence of element 1 in X[] and store it into a variable let’s say Ind.
- If there is no 1 present in X[], then output 0.
- Else convert all the ones before and after Ind into zero.
- Put 1 at index Ind.
- After that calculate the sum by iterating over X[] and output the minimum possible sum.
Implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int sum( int X[], int N)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
if (X[i] == 1)
sum++;
}
return sum;
}
void min_sum( int X[], int N)
{
int index = -1;
for ( int i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break ;
}
}
if (index == -1 || index == (N - 1)
|| index == (N - 2)) {
cout << sum(X, N) << endl;
}
else {
for ( int i = 0; i < index; i++) {
X[i] = 0;
}
X[index] = 1;
for ( int i = index + 1; i < N; i++) {
X[i] = 0;
}
cout << sum(X, N) << endl;
}
}
int main()
{
int N = 6;
int X[] = { 0, 0, 1, 0, 1, 1 };
min_sum(X, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 6 ;
int [] X = { 0 , 0 , 1 , 0 , 1 , 1 };
min_sum(X, N);
}
public static void min_sum( int [] X, int N)
{
int index = - 1 ;
for ( int i = 0 ; i < N; i++) {
if (X[i] == 1 ) {
index = i;
break ;
}
}
if (index == - 1 || index == (N - 1 )
|| index == (N - 2 )) {
System.out.println(sum(X, N));
}
else {
for ( int i = 0 ; i < index; i++) {
X[i] = 0 ;
}
X[index] = 1 ;
for ( int i = index + 1 ; i < N; i++) {
X[i] = 0 ;
}
System.out.println(sum(X, N));
}
}
public static int sum( int [] X, int N)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
if (X[i] == 1 )
sum++;
}
return sum;
}
}
|
Python3
def sum (X, N):
sum_val = 0
for i in range (N):
if X[i] = = 1 :
sum_val + = 1
return sum_val
def min_sum(X, N):
index = - 1
for i in range (N):
if X[i] = = 1 :
index = i
break
if index = = - 1 or index = = (N - 1 ) or index = = (N - 2 ):
print ( sum (X, N))
else :
for i in range (index):
X[i] = 0
X[index] = 1
for i in range (index + 1 , N):
X[i] = 0
print ( sum (X, N))
if __name__ = = "__main__" :
N = 6
X = [ 0 , 0 , 1 , 0 , 1 , 1 ]
min_sum(X, N)
|
C#
using System;
public class GFG {
static int Sum( int [] X, int N)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
if (X[i] == 1)
sum++;
}
return sum;
}
static void MinSum( int [] X, int N)
{
int index = -1;
for ( int i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break ;
}
}
if (index == -1 || index == (N - 1)
|| index == (N - 2)) {
Console.WriteLine(Sum(X, N));
}
else {
for ( int i = 0; i < index; i++) {
X[i] = 0;
}
X[index] = 1;
for ( int i = index + 1; i < N; i++) {
X[i] = 0;
}
Console.WriteLine(Sum(X, N));
}
}
static void Main()
{
int N = 6;
int [] X = { 0, 0, 1, 0, 1, 1 };
MinSum(X, N);
}
}
|
Javascript
function sum(X, N) {
let sum = 0;
for (let i = 0; i < N; i++) {
if (X[i] == 1)
sum++;
}
return sum;
}
function min_sum(X, N) {
let index = -1;
for (let i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break ;
}
}
if (index == -1 || index == (N - 1) || index == (N - 2)) {
console.log(sum(X, N));
}
else {
for (let i = 0; i < index; i++) {
X[i] = 0;
}
X[index] = 1;
for (let i = index + 1; i < N; i++) {
X[i] = 0;
}
console.log(sum(X, N));
}
}
let N = 6;
let X = [0, 0, 1, 0, 1, 1];
min_sum(X, N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...