Costs required to move all 1s to each index of a given Binary Array
Last Updated :
04 Jan, 2023
Given a binary array, in which, moving an element from index i to index j requires abs(i – j) cost. The task is to find the cost to move all 1s to each index of the given array.
Examples:
Input: arr[] = {0, 1, 0, 1}
Output: 4 2 2 2
Explanation:
Moving elements from index 1, index 3 to index 0 requires abs(0 – 1) + abs(0 – 3) = 4.
Moving elements from index 1, index 3 to index 1 requires abs(1 – 1) + abs(1 – 3) = 2.
Moving elements from index 1, index 3 to index 2 requires abs(2 – 1) + abs(2 – 3) = 2.
Moving elements from index 1, index 3 to index 3 requires abs(3 – 1) + abs(3 – 3) = 2.
Therefore, the required output is 4 2 2 2.
Input: arr[] = {1, 1, 1}
Output: 3 2 3
Naive Approach: The simplest approach to solve this problem is to traverse the given array and for each array element of the given array, print the cost to move all 1s of the given array to the current index.
Below are the implementations of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void costMove1s( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int cost = 0;
for ( int j = 0; j < N;
j++) {
if (arr[j] == 1) {
cost += abs (i - j);
}
}
cout<<cost<< " " ;
}
}
int main()
{
int arr[] = { 0, 1, 0, 1};
int N = sizeof (arr) / sizeof (arr[0]);
costMove1s(arr, N);
}
|
Java
import java.io.*;
class GFG{
static void costMove1s( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++)
{
int cost = 0 ;
for ( int j = 0 ; j < N; j++)
{
if (arr[j] == 1 )
{
cost += Math.abs(i - j);
}
}
System.out.print(cost + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 0 , 1 , 0 , 1 };
int N = arr.length;
costMove1s(arr, N);
}
}
|
Python3
def costMove1s(arr, N):
for i in range ( 0 , N):
cost = 0
for j in range ( 0 , N):
if (arr[j] = = 1 ):
cost = cost + abs (i - j)
print (cost, end = " " )
if __name__ = = "__main__" :
arr = [ 0 , 1 , 0 , 1 ]
N = len (arr)
costMove1s(arr, N)
|
C#
using System;
class GFG{
static void costMove1s( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
{
int cost = 0;
for ( int j = 0; j < N; j++)
{
if (arr[j] == 1)
{
cost += Math.Abs(i - j);
}
}
Console.Write(cost + " " );
}
}
public static void Main()
{
int [] arr = { 0, 1, 0, 1 };
int N = arr.Length;
costMove1s(arr, N);
}
}
|
Javascript
<script>
function costMove1s(arr, N)
{
for (let i = 0; i < N; i++) {
let cost = 0;
for (let j = 0; j < N;
j++) {
if (arr[j] == 1) {
cost += Math.abs(i - j);
}
}
document.write(cost + " " );
}
}
let arr = [ 0, 1, 0, 1];
let N = arr.length;
costMove1s(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to traverse the given array and find the cost to move all 1s from the left side and right side of each index of the given array using prefix sum technique. Follow the steps below to solve the problem:
- Initialize an array, say cost[] to store the cost to move all 1s at each index of the given array.
- Initialize two variables, say costLeft, costRight to store the cost to move all 1s from the left side and right side of each index respectively.
- Traverse the given array from left to right and increment the value of costLeft by the number of 1s on the left side and the value of result[i] by costLeft ..
- Traverse the given array from right to left and increment the value of costRight by the number of 1s on the right side and the value of result[i] by costRight .
- Finally, print cost[] array.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void costMove1s( int arr[], int N)
{
int cost[N] = { 0 };
int cntLeft = 0;
int costLeft = 0;
for ( int i = 0; i < N; i++) {
costLeft += cntLeft;
cost[i] += costLeft;
if (arr[i] == 1) {
cntLeft++;
}
}
int cntRight = 0;
int costRight = 0;
for ( int i = N - 1; i >= 0;
i--) {
costRight += cntRight;
cost[i] += costRight;
if (arr[i] == 1) {
cntRight++;
}
}
for ( int i = 0; i< N; i++) {
cout<<cost[i]<< " " ;
}
}
int main()
{
int arr[] = { 0, 1, 0, 1};
int N = sizeof (arr) / sizeof (arr[0]);
costMove1s(arr, N);
}
|
Java
import java.io.*;
class GFG{
static void costMove1s( int arr[], int N)
{
int cost[] = new int [N];
int cntLeft = 0 ;
int costLeft = 0 ;
for ( int i = 0 ; i < N; i++)
{
costLeft += cntLeft;
cost[i] += costLeft;
if (arr[i] == 1 )
{
cntLeft++;
}
}
int cntRight = 0 ;
int costRight = 0 ;
for ( int i = N - 1 ; i >= 0 ; i--)
{
costRight += cntRight;
cost[i] += costRight;
if (arr[i] == 1 )
{
cntRight++;
}
}
for ( int i = 0 ; i < N; i++)
{
System.out.print(cost[i] + " " );
}
}
public static void main (String[] args)
{
int arr[] = { 0 , 1 , 0 , 1 };
int N = arr.length;
costMove1s(arr, N);
}
}
|
Python3
def costMove1s(arr, N):
cost = [ 0 ] * N
cntLeft = 0
costLeft = 0
for i in range (N):
costLeft + = cntLeft
cost[i] + = costLeft
if (arr[i] = = 1 ):
cntLeft + = 1
cntRight = 0
costRight = 0
for i in range (N - 1 , - 1 , - 1 ):
costRight + = cntRight
cost[i] + = costRight
if (arr[i] = = 1 ):
cntRight + = 1
for i in range (N):
print (cost[i], end = " " )
if __name__ = = '__main__' :
arr = [ 0 , 1 , 0 , 1 ]
N = len (arr)
costMove1s(arr, N)
|
C#
using System;
class GFG{
static void costMove1s( int []arr, int N)
{
int []cost = new int [N];
int cntLeft = 0;
int costLeft = 0;
for ( int i = 0; i < N; i++)
{
costLeft += cntLeft;
cost[i] += costLeft;
if (arr[i] == 1)
{
cntLeft++;
}
}
int cntRight = 0;
int costRight = 0;
for ( int i = N - 1; i >= 0; i--)
{
costRight += cntRight;
cost[i] += costRight;
if (arr[i] == 1)
{
cntRight++;
}
}
for ( int i = 0; i < N; i++)
{
Console.Write(cost[i] + " " );
}
}
public static void Main (String[] args)
{
int []arr = { 0, 1, 0, 1 };
int N = arr.Length;
costMove1s(arr, N);
}
}
|
Javascript
<script>
function costMove1s(arr, N)
{
var cost = Array(N).fill(0);
var cntLeft = 0;
var costLeft = 0;
for ( var i = 0; i < N; i++)
{
costLeft += cntLeft;
cost[i] += costLeft;
if (arr[i] == 1)
{
cntLeft++;
}
}
var cntRight = 0;
var costRight = 0;
for ( var i = N - 1; i >= 0; i--)
{
costRight += cntRight;
cost[i] += costRight;
if (arr[i] == 1)
{
cntRight++;
}
}
for ( var i = 0; i< N; i++)
{
document.write(cost[i] + " " );
}
}
var arr = [ 0, 1, 0, 1 ];
var N = arr.length;
costMove1s(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...