Skip to content
Related Articles
Open in App
Not now

Related Articles

Cost required to make all array elements equal to 1

Improve Article
Save Article
Like Article
  • Last Updated : 22 Apr, 2021
Improve Article
Save Article
Like Article

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:

  1. Converting arr[1] to 1 modifies arr[] to {1, 1, 1, 0, 1, 0}. Cost = 1.
  2. Converting arr[3] to 1 modifies arr[] to {1, 1, 1, 1, 1, 0}. Cost = 3.
  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. 

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

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the cost required
// to make all array elements equal to 1
int findCost(int A[], int N)
{
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If current element is 0
        if (A[i] == 0) {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 1, 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findCost(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
 
class GFG{
 
// Function to calculate the cost required
// to make all array elements equal to 1
static int findCost(int[] A, int N)
{
     
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If current element is 0
        if (A[i] == 0)
        {
             
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 0, 1, 0, 1, 0 };
    int N = arr.length;
 
    System.out.println(findCost(arr, N));
}
}
 
// This code is contributed by ukasp


Python3




# Python3 program for the above approach
 
# Function to calculate the cost required
# to make all array elements equal to 1
def findCost(A, N):
 
    # Stores the total cost
    totalCost = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # If current element is 0
        if (A[i] == 0):
 
            # Convert 0 to 1
            A[i] = 1
 
            # Add the cost
            totalCost += i
 
    # Return the total cost
    return totalCost
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 1, 0, 1, 0, 1, 0 ]
    N = len(arr)
     
    print(findCost(arr, N))
     
# This code is contributed by Shivam Singh


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to calculate the cost required
// to make all array elements equal to 1
static int findCost(int []A, int N)
{
     
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If current element is 0
        if (A[i] == 0)
        {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 0, 1, 0, 1, 0 };
    int N = arr.Length;
     
    Console.Write(findCost(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript




<script>
 
 
// Javascript program for the above approach
 
// Function to calculate the cost required
// to make all array elements equal to 1
function findCost(A, N)
{
    // Stores the total cost
    var totalCost = 0;
 
    var i;
    // Traverse the array arr[]
    for (i = 0; i < N; i++) {
 
        // If current element is 0
        if (A[i] == 0) {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
    var arr = [1, 0, 1, 0, 1, 0]
    var N = arr.length
    document.write(findCost(arr, N));
 
</script>


Output: 

9

 

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


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!