Cost required to make all array elements equal to 1
Given a binary array arr[] of size N, the task is to find the total cost required to make all array elements equal to 1, where the cost of converting any 0 to 1 is equal to the count of 1s present before that 0.
Examples:
Input: arr[] = {1, 0, 1, 0, 1, 0}
Output: 9
Explanation:
Following operations are performed:
- Converting arr[1] to 1 modifies arr[] to {1, 1, 1, 0, 1, 0}. Cost = 1.
- Converting arr[3] to 1 modifies arr[] to {1, 1, 1, 1, 1, 0}. Cost = 3.
- Converting arr[5] to 1 modifies arr[] to {1, 1, 1, 1, 1, 5}. Cost = 5.
Therefore, the total cost is 1 + 3 + 5 = 9.
Input: arr[] = {1, 1, 1}
Output: 0
Naive Approach: The simplest approach to solve the given problem is to traverse the array arr[] and count the numbers of 1s present before every index containing 0 and print the sum of all the costs obtained.
Implementation:
C++
#include <iostream>
using namespace std;
int getTotalCost( int arr[], int N) {
int countones = 0;
int cost=0;
for ( int i = 0; i <N; i++) {
if (arr[i]==1)
{
countones++;
continue ;
}
if (arr[i] == 0) {
cost += countones;
countones++;
}
}
return cost;
}
int main() {
int arr[] = {1,0,1,0,1,0};
int N = sizeof (arr) / sizeof (arr[0]);
cout <<getTotalCost(arr,N)<<endl;
return 0;
}
|
Java
public class TotalCost {
public static int getTotalCost( int [] arr) {
int countOnes = 0 ;
int cost = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] == 1 ) {
countOnes++;
} else if (arr[i] == 0 ) {
cost += countOnes;
countOnes++;
}
}
return cost;
}
public static void main(String[] args) {
int [] arr = { 1 , 0 , 1 , 0 , 1 , 0 };
System.out.println(getTotalCost(arr));
}
}
|
Python3
def get_total_cost(arr):
count_ones = 0
cost = 0
for num in arr:
if num = = 1 :
count_ones + = 1
continue
if num = = 0 :
cost + = count_ones
count_ones + = 1
return cost
if __name__ = = "__main__" :
arr = [ 1 , 0 , 1 , 0 , 1 , 0 ]
print (get_total_cost(arr))
|
C#
using System;
class Program
{
static int GetTotalCost( int [] arr)
{
int countOnes = 0;
int cost = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (arr[i] == 1)
{
countOnes++;
continue ;
}
if (arr[i] == 0)
{
cost += countOnes;
countOnes++;
}
}
return cost;
}
static void Main( string [] args)
{
int [] arr = { 1, 0, 1, 0, 1, 0 };
Console.WriteLine( "Total Cost: " + GetTotalCost(arr));
}
}
|
Javascript
function getTotalCost(arr) {
let countOnes = 0;
let cost = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 1) {
countOnes++;
continue ;
}
if (arr[i] === 0) {
cost += countOnes;
countOnes++;
}
}
return cost;
}
function main() {
const arr = [1, 0, 1, 0, 1, 0];
const N = arr.length;
console.log(getTotalCost(arr));
}
main();
|
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1), as we are not using any extra array.
Efficient Approach: The above approach can be optimized by observing the fact that after converting every 0 to 1, the count of 1s present before every 0 is given by the index at which 0 occurs. Therefore, the task is to traverse the given array and print all the sum of all the indices having 0s in the array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCost( int A[], int N)
{
int totalCost = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 0) {
A[i] = 1;
totalCost += i;
}
}
return totalCost;
}
int main()
{
int arr[] = { 1, 0, 1, 0, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findCost(arr, N);
return 0;
}
|
Java
class GFG{
static int findCost( int [] A, int N)
{
int totalCost = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (A[i] == 0 )
{
A[i] = 1 ;
totalCost += i;
}
}
return totalCost;
}
public static void main(String[] args)
{
int [] arr = { 1 , 0 , 1 , 0 , 1 , 0 };
int N = arr.length;
System.out.println(findCost(arr, N));
}
}
|
Python3
def findCost(A, N):
totalCost = 0
for i in range (N):
if (A[i] = = 0 ):
A[i] = 1
totalCost + = i
return totalCost
if __name__ = = '__main__' :
arr = [ 1 , 0 , 1 , 0 , 1 , 0 ]
N = len (arr)
print (findCost(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findCost( int []A, int N)
{
int totalCost = 0;
for ( int i = 0; i < N; i++)
{
if (A[i] == 0)
{
A[i] = 1;
totalCost += i;
}
}
return totalCost;
}
public static void Main()
{
int []arr = { 1, 0, 1, 0, 1, 0 };
int N = arr.Length;
Console.Write(findCost(arr, N));
}
}
|
Javascript
<script>
function findCost(A, N)
{
var totalCost = 0;
var i;
for (i = 0; i < N; i++) {
if (A[i] == 0) {
A[i] = 1;
totalCost += i;
}
}
return totalCost;
}
var arr = [1, 0, 1, 0, 1, 0]
var N = arr.length
document.write(findCost(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...