Open In App

Minimize steps to make Array elements 0 by reducing same A[i] – X from Subarray

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
  
#include <bits/stdc++.h>
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<int> 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<Integer> 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<int, int> s = new Dictionary<int, int>();
  
    // 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




<script>
    // JavaScript program for the above approach
 
    // Function to find the
    // minimum number of steps required
    // to reduce all array elements to zero
    const minSteps = (A, N) => {
        // To store all distinct array elements
        let s = new Set();
 
        // Loop to iterate over the array
        for (let 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
 
    // Given array
    let A = [4, 5, 8, 3, 15, 5, 4,
        6, 8, 10, 45];
    let N = 11;
 
    // Function Call
    document.write(minSteps(A, N));
 
// This code is contributed by rakeshsahni
 
</script>


 
 

Output

8

 

Time Complexity: O(N)
Auxiliary Space: O(N)

Related Topic: Subarrays, Subsequences, and Subsets in Array



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads