通过从子数组中减少相同的 A[i] – X 来最小化使数组元素为 0 的步骤
给定一个大小为N的数组A[] ,任务是找到使数组的所有元素为零所需的最小操作数。在一个步骤中,对给定数组的子数组执行以下操作:
- 选择任何整数X
- 如果一个元素大于X ( A [i] > X ),则数组元素减少值A[i] – X
- ( A[i] – X ) 对于所有被归约的元素必须相同。
例子:
Input: A[] = {4, 3, 4}, N = 3
Output: 2
Explanation: Following operations are performed on the array:
For the first operation, choose the entire array as the subarray, take X = 3. Array becomes A[] = {3, 3, 3}.
For the second operation, choose the entire array as the subarray, take X = 0. Array becomes A[] = {0, 0, 0}.
Thus, 2 steps are required to make all elements of A equal to zero.
Input: A[] = {4, 5, 8, 3, 15, 5, 4, 6, 8, 10, 45}, N = 11
Output: 8
方法:可以使用以下观察来解决该任务:
- To satisfy the last condition, X should be such a value that the array elements which will be reduced are all same.
- To minimize the operations, the total array should be selected each time and X should be chosen in the following manner:
- For the first iteration, X is the 2nd distinct highest number. For the second iteration, X is the 3rd distinct highest number and so on
- Hence the minimum total operations will be the count of distinct elements present in the array
按照以下步骤解决上述问题:
- 声明一个集合来存储唯一元素的计数。
- 使用循环遍历数组的元素:
- 如果一个数组元素说A [i], 不等于 0 将它插入集合中。
- 集合的大小表示唯一元素的数量。
- 返回集合的大小作为最终答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the
// minimum number of steps required
// to reduce all array elements to zero
int minSteps(int A[], int N)
{
// To store all distinct array elements
unordered_set s;
// Loop to iterate over the array
for (int i = 0; i < N; ++i) {
// If an array element is
// greater than zero
if (A[i] != 0) {
// Insert the element
// in the set s
s.insert(A[i]);
}
}
// Return the size of the set s
return s.size();
}
// Driver Code
int main()
{
// Given array
int A[] = { 4, 5, 8, 3, 15, 5, 4,
6, 8, 10, 45 };
int N = 11;
// Function Call
cout << minSteps(A, N);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find the
// minimum number of steps required
// to reduce all array elements to zero
public static int minSteps(int A[], int N)
{
// To store all distinct array elements
HashSet s = new HashSet<>();
// Loop to iterate over the array
for (int i = 0; i < N; ++i) {
// If an array element is
// greater than zero
if (A[i] != 0) {
// Insert the element
// in the set s
s.add(A[i]);
}
}
// Return the size of the set s
return s.size();
}
// Driver Code
public static void main(String[] args)
{
// Given array
int A[] = { 4, 5, 8, 3, 15, 5, 4, 6, 8, 10, 45 };
int N = 11;
// Function Call
System.out.print(minSteps(A, N));
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find the
# minimum number of steps required
# to reduce all array elements to zero
def minSteps(A, N):
# To store all distinct array elements
s = set()
# Loop to iterate over the array
for i in range(N):
# If an array element is
# greater than zero
if (A[i] != 0):
# Insert the element
# in the set s
s.add(A[i])
# Return the size of the set s
return len(s)
# Driver Code
if __name__ == '__main__':
# Given array
A = [ 4, 5, 8, 3, 15, 5, 4, 6, 8, 10, 45 ]
N = 11
# Function Call
print(minSteps(A, N))
# This code is contributed by hrithikgarg03188.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the
// minimum number of steps required
// to reduce all array elements to zero
static int minSteps(int[] A, int N)
{
// To store all distinct array elements
Dictionary s = new Dictionary();
// Loop to iterate over the array
for (int i = 0; i < N; ++i) {
// If an array element is
// greater than zero
if (A[i] != 0) {
// Insert the element
// in the set s
if (s.ContainsKey(A[i])) {
s[A[i]] = 1;
}
else {
s.Add(A[i], 1);
}
}
}
// Return the size of the set s
return s.Count;
}
// Driver Code
public static void Main()
{
// Given array
int[] A = { 4, 5, 8, 3, 15, 5, 4, 6, 8, 10, 45 };
int N = 11;
// Function Call
Console.Write(minSteps(A, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
8
时间复杂度: O(N)
辅助空间: O(N)