Minimum operations required to make all elements of Array less than equal to 0
Last Updated :
20 Feb, 2023
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 from that number.
Examples:
Input: arr[] = {1, 2, 4, 2, 2, 5, 6}
Output: 5
Explanation: The explanation is mentioned in the diagram below:
The resulting array has met the criteria as all it’s elements are either less than or equal to 0
Input: arr[] = {1, 2, 3}
Output: 3
Naive Approach: The simplest approach to solve the problem is to Traverse the array while all the elements of the array are not less than or equal to 0 and find the minimum non-zero positive element and subtract that element from the whole array.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized further by observing that the answer will be the number of non-zero distinct elements in the array. Follow the steps below to solve the problem:
- Initialize a hash-map say m that stores the unique elements present in the array.
- Iterate in the range [0, N-1] using the variable i and mark m[arr[i]] as 1.
- Print the value of m.size() as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int distinct(vector< int > arr)
{
map< int , bool > m;
for ( int i = 0; i < arr.size(); i++) {
m[arr[i]] = true ;
}
return m.size();
}
int main()
{
vector< int > arr = { 1, 2, 4, 2, 2, 5, 6 };
int ans = distinct(arr);
cout << ans << endl;
return 0;
}
|
Java
import java.util.HashMap;
class GFG{
public static int distinct( int [] arr)
{
HashMap<Integer,
Boolean> m = new HashMap<Integer,
Boolean>();
for ( int i = 0 ; i < arr.length; i++)
{
m.put(arr[i], true );
}
return m.size();
}
public static void main(String args[])
{
int [] arr = { 1 , 2 , 4 , 2 , 2 , 5 , 6 };
int ans = distinct(arr);
System.out.println(ans);
}
}
|
Python3
def distinct(arr):
m = {}
for i in range ( len (arr)):
m[arr[i]] = True
return len (m)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 2 , 2 , 5 , 6 ]
ans = distinct(arr)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int distinct(List< int > arr)
{
Dictionary< int ,
bool > m = new Dictionary< int ,
bool >();
for ( int i = 0; i < arr.Count; i++)
{
if (m.ContainsKey(arr[i]))
m[arr[i]] = true ;
else
m.Add(arr[i], true );
}
return m.Count;
}
public static void Main()
{
List< int > arr = new List< int >(){ 1, 2, 4, 2, 2, 5, 6 };
int ans = distinct(arr);
Console.Write(ans);
}
}
|
Javascript
<script>
function distinct(arr) {
let m = new Map();
for (let i = 0; i < arr.length; i++) {
m.set(arr[i], true );
}
return m.size;
}
let arr = [1, 2, 4, 2, 2, 5, 6];
let ans = distinct(arr);
document.write(ans + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N) since map takes up space equal to the length of the array hence the space used by the algorithm is linear
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...