Minimize steps to make Array elements 0 by reducing same A[i] – X from Subarray
Last Updated :
11 Jul, 2022
Given an array A[] of size N, the task is to find the minimum number of operations required to make all the elements of the array zero. In one step, the following operations are done on a subarray of the given array:
- Any integer X is chosen
- If an element is greater than X (A[i] > X), the array element is reduced by the value A[i] – X
- (A[i] – X) must be the same for all the elements which are reduced.
Examples:
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
Approach: The task can be solved using the following observations:
- 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
Follow the steps below to solve the above problem:
- Declare a set to store the count of unique elements.
- Iterate over the elements of the array using a loop:
- If an array element say A[i], is not equal to zero insert it in the set.
- the size of the set denotes the number of unique elements.
- Return the size of the set as the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSteps( int A[], int N)
{
unordered_set< int > s;
for ( int i = 0; i < N; ++i) {
if (A[i] != 0) {
s.insert(A[i]);
}
}
return s.size();
}
int main()
{
int A[] = { 4, 5, 8, 3, 15, 5, 4,
6, 8, 10, 45 };
int N = 11;
cout << minSteps(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int minSteps( int A[], int N)
{
HashSet<Integer> s = new HashSet<>();
for ( int i = 0 ; i < N; ++i) {
if (A[i] != 0 ) {
s.add(A[i]);
}
}
return s.size();
}
public static void main(String[] args)
{
int A[] = { 4 , 5 , 8 , 3 , 15 , 5 , 4 , 6 , 8 , 10 , 45 };
int N = 11 ;
System.out.print(minSteps(A, N));
}
}
|
Python3
def minSteps(A, N):
s = set ()
for i in range (N):
if (A[i] ! = 0 ):
s.add(A[i])
return len (s)
if __name__ = = '__main__' :
A = [ 4 , 5 , 8 , 3 , 15 , 5 , 4 , 6 , 8 , 10 , 45 ]
N = 11
print (minSteps(A, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int minSteps( int [] A, int N)
{
Dictionary< int , int > s = new Dictionary< int , int >();
for ( int i = 0; i < N; ++i) {
if (A[i] != 0) {
if (s.ContainsKey(A[i])) {
s[A[i]] = 1;
}
else {
s.Add(A[i], 1);
}
}
}
return s.Count;
}
public static void Main()
{
int [] A = { 4, 5, 8, 3, 15, 5, 4, 6, 8, 10, 45 };
int N = 11;
Console.Write(minSteps(A, N));
}
}
|
Javascript
<script>
const minSteps = (A, N) => {
let s = new Set();
for (let i = 0; i < N; ++i) {
if (A[i] != 0) {
s.add(A[i]);
}
}
return s.size;
}
let A = [4, 5, 8, 3, 15, 5, 4,
6, 8, 10, 45];
let N = 11;
document.write(minSteps(A, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...