Open In App

Optimizing Binary Array for Minimum Sum and Lexicographical Order

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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);


Output

1







Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads