Minimize cost required to make all array elements greater than or equal to zero
Last Updated :
15 Nov, 2021
Given an array arr[] consisting of N integers and an integer X, the task is to find the minimum cost required to make all array elements greater than or equal to 0 by performing the following operations any number of times:
- Increase any array element by 1. Cost = 1.
- Increase all array elements by 1. Cost = X.
Examples:
Input: arr[] = {-1, -3, 3, 4, 5}, X = 2
Output: 4
Explanation:
Increment arr[0] by 1. The array arr[] modifies to {0, -3, 3, 4, 5}. Cost = 1.
Increment arr[1] by 1 thrice. The array arr[] modifies to {0, 0, 3, 4, 5}. Therefore, Cost = 4.
Hence, the total cost required is 4.
Input: arr[] = {-3, -2, -1, -5, 7}, X = 2
Output: 8
Approach: The idea is to use Greedy Approach to solve the problem. Follow the steps below to solve the problem:
- Sort the array arr[] in ascending order.
- Initialize an auxiliary vector, say list, to store the negative array elements.
- Initialize a variable, cost = 0, to store the cost required to make the current array element 0 and another variable, min_cost = INT_MAX, to store the final minimum cost to make all array elements >= 0.
- Traverse the array arr[] and try to convert all the array elements in the list >= 0 by applying the suitable operations and update min_cost accordingly.
- Print the value of min_cost as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// cost to make all array elements
// greater than or equal to 0
void minCost(int arr[], int N, int X)
{
// Sort the array in
// ascending order
sort(arr, arr + N);
int sum = 0;
// Stores the cost to make
// current array element >= 0
int cost = 0;
// Stores the cost to make
// all array elements >= 0
int min_cost = INT_MAX;
// Traverse the array and insert all the
// elements which are < 0
for (int i = 0; i < N; i++) {
// If current array element
// is negative
if (arr[i] < 0) {
// Cost to make all array
// elements >= 0
cost = abs(arr[i]) * X
+ (sum - abs(arr[i]) * i);
sum += abs(arr[i]);
// Update curr if ans is minimum
min_cost = min(min_cost, cost);
}
}
// Print the minimum cost
cout << min_cost;
}
// Driver Code
int main()
{
// Given array
int arr[] = { -1, -3, -2, 4, -1 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Given value of X
int X = 2;
// Function call to find minimum
// cost to make all array elements >= 0
minCost(arr, N, X);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
public class GFG
{
// Function to find the minimum
// cost to make all array elements
// greater than or equal to 0
static void minCost(int arr[], int N, int X)
{
// Sort the array in
// ascending order
Arrays.sort(arr) ;
int sum = 0;
// Stores the cost to make
// current array element >= 0
int cost = 0;
int INT_MAX = Integer.MAX_VALUE;
// Stores the cost to make
// all array elements >= 0
int min_cost = INT_MAX;
// Traverse the array and insert all the
// elements which are < 0
for (int i = 0; i < N; i++) {
// If current array element
// is negative
if (arr[i] < 0) {
// Cost to make all array
// elements >= 0
cost = Math.abs(arr[i]) * X
+ (sum - Math.abs(arr[i]) * i);
sum += Math.abs(arr[i]);
// Update curr if ans is minimum
min_cost = Math.min(min_cost, cost);
}
}
// Print the minimum cost
System.out.print(min_cost);
}
// Driver Code
public static void main (String[] args)
{
// Given array
int arr[] = { -1, -3, -2, 4, -1 };
// Size of the array
int N = arr.length;
// Given value of X
int X = 2;
// Function call to find minimum
// cost to make all array elements >= 0
minCost(arr, N, X);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
import sys
# Function to find the minimum
# cost to make all array of elements
# greater than or equal to 0
def mincost(arr, N, X):
# sort the array in
# ascending order
arr.sort()
sum = 0
# stores the count to make
# current array element >=0
cost = 0
# stores the cost to make
# all array elements >=0
min_cost = sys.maxsize
# Traverse the array and insert all the
# elements which are <=0
for i in range(0, N):
# if current array element
# is negative
if (arr[i] < 0):
# cost to make all array
# elements >=0
cost = abs(arr[i]) * x + (sum - abs(arr[i]) * i)
sum += abs(arr[i])
# update curr if ans is minimum
min_cost = min(min_cost,cost)
# return minimum cost
return min_cost
# Driver code
arr = [-1, -3, -2, 4, -1]
# size of the array
N = len(arr)
# Given value of x
x = 2
# Function call to find minimum
# cost to make all array elements >=0
print(mincost(arr, N, x))
# This code is contributed by Virusbuddah
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum
// cost to make all array elements
// greater than or equal to 0
static void minCost(int[] arr, int N, int X)
{
// Sort the array in
// ascending order
Array.Sort(arr) ;
int sum = 0;
// Stores the cost to make
// current array element >= 0
int cost = 0;
//int INT_MAX = Int32.MaxValue;
// Stores the cost to make
// all array elements >= 0
int min_cost = Int32.MaxValue;
// Traverse the array and insert all the
// elements which are < 0
for(int i = 0; i < N; i++)
{
// If current array element
// is negative
if (arr[i] < 0)
{
// Cost to make all array
// elements >= 0
cost = Math.Abs(arr[i]) * X +
(sum - Math.Abs(arr[i]) * i);
sum += Math.Abs(arr[i]);
// Update curr if ans is minimum
min_cost = Math.Min(min_cost, cost);
}
}
// Print the minimum cost
Console.Write(min_cost);
}
// Driver Code
static public void Main ()
{
// Given array
int[] arr = { -1, -3, -2, 4, -1 };
// Size of the array
int N = arr.Length;
// Given value of X
int X = 2;
// Function call to find minimum
// cost to make all array elements >= 0
minCost(arr, N, X);
}
}
// This code is contributed by susmitakundugoaldanga
JavaScript
<script>
// javascript program for the above approach
// Function to find the minimum
// cost to make all array elements
// greater than or equal to 0
function minCost(arr , N , X)
{
// Sort the array in
// ascending order
arr.sort() ;
var sum = 0;
// Stores the cost to make
// current array element >= 0
var cost = 0;
var INT_MAX = Number.MAX_VALUE;
// Stores the cost to make
// all array elements >= 0
var min_cost = INT_MAX;
// Traverse the array and insert all the
// elements which are < 0
for (i = 0; i < N; i++) {
// If current array element
// is negative
if (arr[i] < 0) {
// Cost to make all array
// elements >= 0
cost = Math.abs(arr[i]) * X
+ (sum - Math.abs(arr[i]) * i);
sum += Math.abs(arr[i]);
// Update curr if ans is minimum
min_cost = Math.min(min_cost, cost);
}
}
// Print the minimum cost
document.write(min_cost);
}
// Driver Code
//Given array
var arr = [ -1, -3, -2, 4, -1 ];
// Size of the array
var N = arr.length;
// Given value of X
var X = 2;
// Function call to find minimum
// cost to make all array elements >= 0
minCost(arr, N, X);
// This code is contributed by 29AjayKumar
</script>
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Similar Reads
Minimum operations required to make all elements of Array less than equal to 0 Given an array arr[] consisting of N positive numbers, the task is to find the minimum number of operations required to make all elements of the array less than or equal to 0. In each operation, one has to pick the minimum positive element from the array and subtract all the elements of the array fr
5 min read
Minimum operations required to make every element greater than or equal to K Given an array of length N. The task is to convert it into a sequence in which all elements are greater than or equal to K.The only operation allowed is taking two smallest elements of the sequence and replace them by their LCM. Find the minimum number of operations required. If it is impossible to
7 min read
Make all Array elements equal to zero in atmost m operations Given an integer array A[] of size N and integer k. For a fixed value p, choose an index i (1 ⤠i ⤠n) and assign A[i] = max(0, A[i] â p), this counts as one operation, and the task is to find the smallest value of p such that all the elements of the array A[] become 0 in at most k operations. Examp
10 min read
Minimize count of flips required to make sum of the given array equal to 0 Given an array arr[] consisting of N integers, the task is to minimize the count of elements required to be multiplied by -1 such that the sum of array elements is 0. If it is not possible to make the sum 0, print "-1". Examples: Input: arr[] = {2, 3, 1, 4}Output: 2Explanation: Multiply arr[0] by -1
15+ min read
Make all array elements equal with minimum cost Given an array of size n, the task is to make the value of all elements equal with minimum cost. The cost of changing a value from x to y is abs(x - y).Examples : Input: arr[] = [1, 100, 101]Output: 100Explanation: We can change all its values to 100 with minimum cost,|1 - 100| + |100 - 100| + |101
15 min read