给定一个由N 个正数组成的数组arr[] ,任务是找到使数组的所有元素小于或等于0所需的最小操作次数。在每个操作中,必须从数组中选取最小的正元素并从该数字中减去数组的所有元素。
例子:
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
朴素的方法:解决问题的最简单方法是遍历数组,同时数组的所有元素不小于或等于0 ,找到最小的非零正元素,然后从整个数组中减去该元素。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:通过观察答案将是数组中非零不同元素的数量,可以进一步优化上述方法。请按照以下步骤解决问题:
- 初始化一个哈希映射说m存储数组中存在的唯一元素。
- 使用变量i在范围[0, N-1] 中迭代并将m[arr[i]]标记为1 。
- 打印m.size()的值作为答案。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to find minimum number of
// non-zero elements that has to be subtracted
// such that all the elements are less than or 0
int distinct(vector arr)
{
// Hash map to mark elements true
// that are present in the array
map m;
// Traverse the array
for (int i = 0; i < arr.size(); i++) {
// Mark arr[i] true
m[arr[i]] = true;
}
// Finally, return the size of hasmap
return m.size();
}
// Driver Code
int main()
{
// Given Input
vector arr = { 1, 2, 4, 2, 2, 5, 6 };
// Function Call
int ans = distinct(arr);
cout << ans << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.HashMap;
class GFG{
// Function to find minimum number of
// non-zero elements that has to be subtracted
// such that all the elements are less than or 0
public static int distinct(int[] arr)
{
// Hash map to mark elements true
// that are present in the array
HashMap m = new HashMap();
// Traverse the array
for(int i = 0; i < arr.length; i++)
{
// Mark arr[i] true
m.put(arr[i], true);
}
// Finally, return the size of hasmap
return m.size();
}
// Driver Code
public static void main(String args[])
{
// Given Input
int[] arr = { 1, 2, 4, 2, 2, 5, 6 };
// Function Call
int ans = distinct(arr);
System.out.println(ans);
}
}
// This code is contributed by gfgking
Python3
# Python 3 Program for the above approach
# Function to find minimum number of
# non-zero elements that has to be subtracted
# such that all the elements are less than or 0
def distinct(arr):
# Hash map to mark elements true
# that are present in the array
m = {}
# Traverse the array
for i in range(len(arr)):
# Mark arr[i] true
m[arr[i]] = True
# Finally, return the size of hasmap
return len(m)
# Driver Code
if __name__ == "__main__":
# Given Input
arr = [1, 2, 4, 2, 2, 5, 6]
# Function Call
ans = distinct(arr)
print(ans)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find minimum number of
// non-zero elements that has to be subtracted
// such that all the elements are less than or 0
static int distinct(List arr)
{
// Hash map to mark elements true
// that are present in the array
Dictionary m = new Dictionary();
// Traverse the array
for(int i = 0; i < arr.Count; i++)
{
// Mark arr[i] true
if (m.ContainsKey(arr[i]))
m[arr[i]] = true;
else
m.Add(arr[i],true);
}
// Finally, return the size of hasmap
return m.Count;
}
// Driver Code
public static void Main()
{
// Given Input
List arr = new List(){ 1, 2, 4, 2, 2, 5, 6 };
// Function Call
int ans = distinct(arr);
Console.Write(ans);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。