Minimum number of steps required to place all 1s at a single index
Last Updated :
22 Dec, 2021
Given a binary array A[] of size N, where all 1s can be moved to its adjacent position, the task is to print an array res[] of size N, where res[i] contains the minimum number of steps required to move all the 1s at the ith index.
Examples:
Input: A[] = {1, 0, 1, 0}
Output: {2, 2, 2, 4}
Explanation:
For i = 0, moving all 1s to 0th index requires 2 steps, i.e abs(0 – 0) + abs(0 – 2) = 2.
For i = 1, moving all 1s to 1st index requires 2 steps, i.e abs(1 – 0) + abs(1 – 2) = 2.
For i = 2, moving all 1s to 2nd index requires 2 steps, i.e abs(2 – 0) + abs(2 – 2) = 2.
For i = 3, moving all 1’s to 3rd index requires 4 steps, i.e abs(3 – 0) + abs(3 – 2) = 4.
Hence, res[] is {2, 2, 2, 4}
Input: A[] = {0, 0, 1, 0, 0, 1}
Output: {7, 5, 3, 3, 3, 3}
Explanation:
For i = 0, moving all 1s to 0th index requires 7 steps, i.e abs(2 – 0) + abs(5 – 0) = 7.
For i = 1, moving all 1s to 1st index requires 5 steps, i.e abs(2 – 1) + abs(5 – 1) = 5.
For i = 2, moving all 1s to 2nd index requires 3 steps, i.e abs(2 – 2) + abs(5 – 2) = 3.
For i = 3, moving all 1s to 3rd index will take 3 steps, i.e abs(2 – 2) + abs(5 – 3) = 3.
For i = 4, moving all 1s to 4th index will take 3 steps, i.e abs(2 – 4) + abs(5 – 4) = 3.
For i = 5, moving all 1s to 5th index will take 3 steps, i.e abs(2 – 5) + abs(5 – 5) = 3.
Hence, res[] is {7, 5, 3, 3, 3, 3}
Naive Approach: Follow the steps to solve the problem :
- Traverse the array.
- For every ith index, count the number of steps required to move all 1s to the ith index.
- Iterate over the range [0, N – 1], using a variable, say i.
- Initialize steps = 0.
- Iterate over the range [0, N – 1], using a variable j.
- If A[j] is equal to 1, add abs(i – j) to steps.
- Print the minimum number of steps.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Prefix Sum. Follow the steps below to solve this problem:
- Traverse the array.
- Initialize an array, say left[], and initialize count = A[0].
- Iterate over the range [1, N – 1], and update left[i] = left[i – 1] + count, where count is the number of 1s on the left side of i.
- Initialize an array, say right[], and initialize count = A[N – 1].
- Iterate over the range [N – 2, 0], and update right[i] = right[i + 1] + count, where count is the number of 1s on the right side of i.
- Calculate and update the final answer in res[], where res[i] is the sum of the steps from left and right sides, i.e res[i] = right[i] + left[i]
- Print the array res[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minsteps(vector< int >& A)
{
int n = A.size();
vector< int > left(n, 0), right(n, 0), res(n, 0);
int count = A[0];
for ( int i = 1; i < n; i++) {
left[i] = left[i - 1] + count;
count += A[i];
}
count = A[n - 1];
for ( int i = n - 2; i >= 0; i--) {
right[i] = right[i + 1] + count;
count += A[i];
}
for ( int i = 0; i < n; i++) {
res[i] = left[i] + right[i];
cout << res[i] << " " ;
}
cout << "\n" ;
}
int main()
{
vector< int > A = { 1, 0, 1, 0 };
minsteps(A);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static void minsteps( int [] A)
{
int n = A.length;
int [] left = new int [n];
Arrays.fill(left, 0 );
int [] right = new int [n];
Arrays.fill(right, 0 );
int [] res = new int [n];
Arrays.fill(res, 0 );
int count = A[ 0 ];
for ( int i = 1 ; i < n; i++) {
left[i] = left[i - 1 ] + count;
count += A[i];
}
count = A[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--) {
right[i] = right[i + 1 ] + count;
count += A[i];
}
for ( int i = 0 ; i < n; i++) {
res[i] = left[i] + right[i];
System.out.print(res[i] + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int [] A = { 1 , 0 , 1 , 0 };
minsteps(A);
}
}
|
Python3
def minsteps(A):
n = len (A)
left, right, res = [ 0 ] * n, [ 0 ] * n, [ 0 ] * n
count = A[ 0 ]
for i in range ( 1 , n):
left[i] = left[i - 1 ] + count
count + = A[i]
count = A[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
right[i] = right[i + 1 ] + count
count + = A[i]
for i in range (n):
res[i] = left[i] + right[i]
print (res[i], end = " " )
print ()
if __name__ = = '__main__' :
A = [ 1 , 0 , 1 , 0 ]
minsteps(A)
|
C#
using System;
class GFG
{
static void minsteps( int [] A)
{
int n = A.Length;
int [] left = new int [n];
for ( int i = 1; i < n; i++) {
left[i] = 0;
}
int [] right = new int [n];
for ( int i = 1; i < n; i++) {
right[i] = 0;
}
int [] res = new int [n];
for ( int i = 1; i < n; i++) {
res[i] = 0;
}
int count = A[0];
for ( int i = 1; i < n; i++) {
left[i] = left[i - 1] + count;
count += A[i];
}
count = A[n - 1];
for ( int i = n - 2; i >= 0; i--) {
right[i] = right[i + 1] + count;
count += A[i];
}
for ( int i = 0; i < n; i++) {
res[i] = left[i] + right[i];
Console.Write(res[i] + " " );
}
Console.WriteLine();
}
public static void Main(String[] args)
{
int [] A = { 1, 0, 1, 0 };
minsteps(A);
}
}
|
Javascript
<script>
function minsteps(A)
{
let n = A.length;
let left = Array.from({length: n}, (_, i) => 0);
let right = Array.from({length: n}, (_, i) => 0);
let res = Array.from({length: n}, (_, i) => 0);
let count = A[0];
for (let i = 1; i < n; i++) {
left[i] = left[i - 1] + count;
count += A[i];
}
count = A[n - 1];
for (let i = n - 2; i >= 0; i--) {
right[i] = right[i + 1] + count;
count += A[i];
}
for (let i = 0; i < n; i++) {
res[i] = left[i] + right[i];
document.write(res[i] + " " );
}
document.write( "<br/>" );
}
let A = [ 1, 0, 1, 0 ];
minsteps(A);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...