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++
#include <bits/stdc++.h>
using namespace std;
void minCost( int arr[], int N, int X)
{
sort(arr, arr + N);
int sum = 0;
int cost = 0;
int min_cost = INT_MAX;
for ( int i = 0; i < N; i++) {
if (arr[i] < 0) {
cost = abs (arr[i]) * X
+ (sum - abs (arr[i]) * i);
sum += abs (arr[i]);
min_cost = min(min_cost, cost);
}
}
cout << min_cost;
}
int main()
{
int arr[] = { -1, -3, -2, 4, -1 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 2;
minCost(arr, N, X);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG
{
static void minCost( int arr[], int N, int X)
{
Arrays.sort(arr) ;
int sum = 0 ;
int cost = 0 ;
int INT_MAX = Integer.MAX_VALUE;
int min_cost = INT_MAX;
for ( int i = 0 ; i < N; i++) {
if (arr[i] < 0 ) {
cost = Math.abs(arr[i]) * X
+ (sum - Math.abs(arr[i]) * i);
sum += Math.abs(arr[i]);
min_cost = Math.min(min_cost, cost);
}
}
System.out.print(min_cost);
}
public static void main (String[] args)
{
int arr[] = { - 1 , - 3 , - 2 , 4 , - 1 };
int N = arr.length;
int X = 2 ;
minCost(arr, N, X);
}
}
|
Python3
import sys
def mincost(arr, N, X):
arr.sort()
sum = 0
cost = 0
min_cost = sys.maxsize
for i in range ( 0 , N):
if (arr[i] < 0 ):
cost = abs (arr[i]) * x + ( sum - abs (arr[i]) * i)
sum + = abs (arr[i])
min_cost = min (min_cost,cost)
return min_cost
arr = [ - 1 , - 3 , - 2 , 4 , - 1 ]
N = len (arr)
x = 2
print (mincost(arr, N, x))
|
C#
using System;
class GFG{
static void minCost( int [] arr, int N, int X)
{
Array.Sort(arr) ;
int sum = 0;
int cost = 0;
int min_cost = Int32.MaxValue;
for ( int i = 0; i < N; i++)
{
if (arr[i] < 0)
{
cost = Math.Abs(arr[i]) * X +
(sum - Math.Abs(arr[i]) * i);
sum += Math.Abs(arr[i]);
min_cost = Math.Min(min_cost, cost);
}
}
Console.Write(min_cost);
}
static public void Main ()
{
int [] arr = { -1, -3, -2, 4, -1 };
int N = arr.Length;
int X = 2;
minCost(arr, N, X);
}
}
|
Javascript
<script>
function minCost(arr , N , X)
{
arr.sort() ;
var sum = 0;
var cost = 0;
var INT_MAX = Number.MAX_VALUE;
var min_cost = INT_MAX;
for (i = 0; i < N; i++) {
if (arr[i] < 0) {
cost = Math.abs(arr[i]) * X
+ (sum - Math.abs(arr[i]) * i);
sum += Math.abs(arr[i]);
min_cost = Math.min(min_cost, cost);
}
}
document.write(min_cost);
}
var arr = [ -1, -3, -2, 4, -1 ];
var N = arr.length;
var X = 2;
minCost(arr, N, X);
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...