给定一个大小为N的数组arr[] ,任务是通过用任意整数、最少次数替换相等元素的子序列的所有元素来使所有数组元素等于0 。
例子:
Input: arr[] = {3, 7, 3}, N = 3
Output: 2
Explanation:
Selecting a subsequence { 7 } and replacing all its elements by 0 modifies arr[] to { 3, 3, 3 }.
Selecting the array { 3, 3, 3 } and replacing all its elements by 0 modifies arr[] to { 0, 0, 0 }
Input: arr[] = {1, 5, 1, 3, 2, 3, 1}, N = 7
Output: 4
方法:该问题可以使用贪心技术解决。这个想法是计算数组中存在的不等于 0 的不同元素并打印获得的计数。请按照以下步骤解决问题:
- 初始化 Set 以存储数组中存在的不同元素,该元素不等于0 。
- 遍历数组arr[]并将数组元素插入到 Set 中。
- 最后,打印 Set 的大小。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum count of operations
// required to convert all array elements to zero
// br replacing subsequence of equal elements to 0
void minOpsToTurnArrToZero(int arr[], int N)
{
// Store distinct elements
// present in the array
unordered_set st;
// Traverse the array
for (int i = 0; i < N; i++) {
// If arr[i] is already present in
// the Set or arr[i] is equal to 0
if (st.find(arr[i]) != st.end()
|| arr[i] == 0) {
continue;
}
// Otherwise, increment ans by
// 1 and insert current element
else {
st.insert(arr[i]);
}
}
cout << st.size() << endl;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 3, 7, 3 };
// Size of the given array
int N = sizeof(arr) / sizeof(arr[0]);
minOpsToTurnArrToZero(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find minimum count of operations
// required to convert all array elements to zero
// br replacing subsequence of equal elements to 0
static void minOpsToTurnArrToZero(int[] arr, int N)
{
// Store distinct elements
// present in the array
Set st = new HashSet();
// Traverse the array
for (int i = 0; i < N; i++) {
// If arr[i] is already present in
// the Set or arr[i] is equal to 0
if (st.contains(arr[i]) || arr[i] == 0) {
continue;
}
// Otherwise, increment ans by
// 1 and insert current element
else {
st.add(arr[i]);
}
}
System.out.println(st.size());
}
// Driver Code
public static void main(String args[])
{
// Given array
int arr[] = { 3, 7, 3 };
// Size of the given array
int N = arr.length;
minOpsToTurnArrToZero(arr, N);
}
}
// This code is contributed by 18bhupendrayadav18
Python3
# Python3 program for the above approach
# Function to find minimum count of
# operations required to convert all
# array elements to zero by replacing
# subsequence of equal elements to 0
def minOpsToTurnArrToZero(arr, N):
# Store distinct elements
# present in the array
st = dict()
# Traverse the array
for i in range(N):
# If arr[i] is already present in
# the Set or arr[i] is equal to 0
if (i in st.keys() or arr[i] == 0):
continue
# Otherwise, increment ans by
# 1 and insert current element
else:
st[arr[i]] = 1
print(len(st))
# Driver Code
# Given array
arr = [ 3, 7, 3 ]
# Size of the given array
N = len(arr)
minOpsToTurnArrToZero(arr, N)
# This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find minimum count of operations
// required to convert all array elements to zero
// br replacing subsequence of equal elements to 0
static void minOpsToTurnArrToZero(int[] arr, int N)
{
// Store distinct elements
// present in the array
HashSet st = new HashSet();
// Traverse the array
for (int i = 0; i < N; i++)
{
// If arr[i] is already present in
// the Set or arr[i] is equal to 0
if (st.Contains(arr[i]) || arr[i] == 0)
{
continue;
}
// Otherwise, increment ans by
// 1 and insert current element
else
{
st.Add(arr[i]);
}
}
Console.WriteLine(st.Count);
}
// Driver Code
public static void Main(String []args)
{
// Given array
int []arr = { 3, 7, 3 };
// Size of the given array
int N = arr.Length;
minOpsToTurnArrToZero(arr, N);
}
}
// This code is contributed by gauravrajput1
输出:
2
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live