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
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the sum
int sum(int X[], int N)
{
int sum = 0;
for (int i = 0; i < N; i++) {
if (X[i] == 1)
sum++;
}
return sum;
}
// Function to output the minimum possible sum
// such that X[] is lexicographically smallest
void min_sum(int X[], int N)
{
// Variable to store the first index
// of the first integer = 1
int index = -1;
// Loop to find the first index
// of integer 1
for (int i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break;
}
}
// If operations can't be applied, the
// sum is equal to the number of
// integers equal to 1
if (index == -1 || index == (N - 1)
|| index == (N - 2)) {
cout << sum(X, N) << endl;
}
// If operations can be applied, then
// this block will execute
else {
// Making all zeroes before the index
for (int i = 0; i < index; i++) {
X[i] = 0;
}
// Placing '1' at the index
X[index] = 1;
// Making all zeroes after the index
for (int i = index + 1; i < N; i++) {
X[i] = 0;
}
// Function call to calculate the
// sum of the newly formed array
cout << sum(X, N) << endl;
}
}
// Driver Function
int main()
{
// Inputs
int N = 6;
int X[] = { 0, 0, 1, 0, 1, 1 };
// Function call
min_sum(X, N);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
// Driver Function
public static void main(String[] args)
throws java.lang.Exception
{
// Inputs
int N = 6;
int[] X = { 0, 0, 1, 0, 1, 1 };
// Function call
min_sum(X, N);
}
// Function to output the minimum possible sum
// Such that X[] is lexographically smallest
public static void min_sum(int[] X, int N)
{
// Variable to store first index of
// first integer = 1
int index = -1;
// Loop which finds first index
// of integer 1
for (int i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break;
}
}
// If operations can't be applied
// Then sum is equal to number of
// integers equal to 1
if (index == -1 || index == (N - 1)
|| index == (N - 2)) {
System.out.println(sum(X, N));
}
// If operations can be applied then
// else loop will execute
else {
// Making all zeroes before index
for (int i = 0; i < index; i++) {
X[i] = 0;
}
// Placing '1' at index
X[index] = 1;
// Making all zeroes after index
for (int i = index + 1; i < N; i++) {
X[i] = 0;
}
// Function call to calculate sum
// of newly formed string
System.out.println(sum(X, N));
}
}
// Function call to calculate sum
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
# Python code to implement the approach
# Function to calculate the sum
def sum(X, N):
sum_val = 0
for i in range(N):
if X[i] == 1:
sum_val += 1
return sum_val
# Function to output the minimum possible sum
# such that X[] is lexicographically smallest
def min_sum(X, N):
# Variable to store the first index
# of the first integer = 1
index = -1
# Loop to find the first index
# of integer 1
for i in range(N):
if X[i] == 1:
index = i
break
# If operations can't be applied, the
# sum is equal to the number of
# integers equal to 1
if index == -1 or index == (N - 1) or index == (N - 2):
print(sum(X, N))
# If operations can be applied, then
# this block will execute
else:
# Making all zeroes before the index
for i in range(index):
X[i] = 0
# Placing '1' at the index
X[index] = 1
# Making all zeroes after the index
for i in range(index + 1, N):
X[i] = 0
# Function call to calculate the
# sum of the newly formed array
print(sum(X, N))
# Driver Code
if __name__ == "__main__":
# Inputs
N = 6
X = [0, 0, 1, 0, 1, 1]
# Function call
min_sum(X, N)
# This code is contributed by Susobhan Akhuli
C#
// C# program for the above approach
using System;
public class GFG {
// Function to calculate the sum
static int Sum(int[] X, int N)
{
int sum = 0;
for (int i = 0; i < N; i++) {
if (X[i] == 1)
sum++;
}
return sum;
}
// Function to output the minimum possible sum
// such that X[] is lexicographically smallest
static void MinSum(int[] X, int N)
{
// Variable to store the first index
// of the first integer = 1
int index = -1;
// Loop to find the first index
// of integer 1
for (int i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break;
}
}
// If operations can't be applied, the
// sum is equal to the number of
// integers equal to 1
if (index == -1 || index == (N - 1)
|| index == (N - 2)) {
Console.WriteLine(Sum(X, N));
}
// If operations can be applied, then
// this block will execute
else {
// Making all zeroes before the index
for (int i = 0; i < index; i++) {
X[i] = 0;
}
// Placing '1' at the index
X[index] = 1;
// Making all zeroes after the index
for (int i = index + 1; i < N; i++) {
X[i] = 0;
}
// Function call to calculate the
// sum of the newly formed array
Console.WriteLine(Sum(X, N));
}
}
// Driver Function
static void Main()
{
// Inputs
int N = 6;
int[] X = { 0, 0, 1, 0, 1, 1 };
// Function call
MinSum(X, N);
}
}
// This code is contributed by Susobhan Akhuli
JavaScript
// Javascript code to implement the approach
// Function to calculate the sum
function sum(X, N) {
let sum = 0;
for (let i = 0; i < N; i++) {
if (X[i] == 1)
sum++;
}
return sum;
}
// Function to output the minimum possible sum
// such that X[] is lexicographically smallest
function min_sum(X, N) {
// Variable to store the first index
// of the first integer = 1
let index = -1;
// Loop to find the first index
// of integer 1
for (let i = 0; i < N; i++) {
if (X[i] == 1) {
index = i;
break;
}
}
// If operations can't be applied, the
// sum is equal to the number of
// integers equal to 1
if (index == -1 || index == (N - 1) || index == (N - 2)) {
console.log(sum(X, N));
}
// If operations can be applied, then
// this block will execute
else {
// Making all zeroes before the index
for (let i = 0; i < index; i++) {
X[i] = 0;
}
// Placing '1' at the index
X[index] = 1;
// Making all zeroes after the index
for (let i = index + 1; i < N; i++) {
X[i] = 0;
}
// Function call to calculate the
// sum of the newly formed array
console.log(sum(X, N));
}
}
// Driver Function
// Inputs
let N = 6;
let X = [0, 0, 1, 0, 1, 1];
// Function call
min_sum(X, N);
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum length subarray of 1s in a Binary Array Given binary array. The task is to find the length of subarray with minimum number of 1s.Note: It is guaranteed that there is atleast one 1 present in the array.Examples : Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1} Output : 3 Minimum length subarray of 1s is {1, 1}.Input : arr[] = {0, 0, 1
8 min read
Construct lexicographically smallest Binary array of size N with A 0s and X inversion count Given three numbers N, A, and X, the task is to construct the lexicographically smallest binary array of size N, containing A 0s and having an inversion count of X. Examples: Input: N=5, A=2, X=1Output: 0 1 0 1 1Explanation: The number of inversions in this array is 1(2nd and 3rd index). Input: N=5,
9 min read
Maximum and minimum sum of Bitwise XOR of pairs from an array Given an array arr[] of size N, the task is to find the maximum and minimum sum of Bitwise XOR of all pairs from an array by splitting the array into N / 2 pairs. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6 10Explanation:Bitwise XOR of the all possible pair splits are as follows:(1, 2), (3, 4) â
11 min read
Lexicographically smallest permutation of [1, N] based on given Binary string Given a binary string S of size (N - 1), the task is to find the lexicographically smallest permutation P of the first N natural numbers such that for every index i, if S[i] equals '0' then P[i + 1] must be greater than P[i] and if S[i] equals '1' then P[i + 1] must be less than P[i]. Examples: Inpu
6 min read
Minimum sum by choosing minimum of pairs from array Given an array A[] of n-elements. We need to select two adjacent elements and delete the larger of them and store smaller of them to another array say B[]. We need to perform this operation till array A[] contains only single element. Finally, we have to construct the array B[] in such a way that to
4 min read