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++
// 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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Minimize X such that array can made 0 by reducing elements less than X Given an array A[] of size N. In each step reduce an element to 0 and decrement all the non-zero elements by 1 till all the elements become 0. Find the minimum possible value of X such that the array can be made 0 by deleting values less than or equal to X in each step. Examples: Input: N = 4, A = {
4 min read
Minimize subarray increments/decrements required to reduce all array elements to 0 Given an array arr[], select any subarray and apply any one of the below operations on each element of the subarray: Increment by oneDecrement by one The task is to print the minimum number of above-mentioned increment/decrement operations required to reduce all array elements to 0. Examples: Input:
5 min read
Minimize steps to make Array elements equal by using giving operations Given an arr[] of length N. Then the task is to find minimum steps to make all the elements of the given array equal. Two types of operations can be performed as given below: Choose a prefix of size K, where arr[K] = max element in that chosen sub-array and convert all elements equal to max. Choose
8 min read
Minimize flips on K-length subarrays required to make all array elements equal to 1 Given a binary array arr[] of size N and a positive integer K, the task is to find the minimum number of times any subarray of size K from the given array arr[] is required to be flipped to make all array elements equal to 1. If it is not possible to do so, then print "-1". Examples: Input: arr[] =
15+ min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Maximize the minimum array element by M subarray increments of size S Given an array arr[] of N integers and two integers S and M, the task is to maximize the minimum array element by incrementing any subarray of size S by 1, M number of times. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, S = 2, M = 3Output: 3Explanation:Below are the operations performed:Operation 1:
10 min read
Minimize Array elements to be reduced to make subsequences sum 1 to Array max possible Given an array a[] of positive integers. Subtract any positive integer from any number of elements of the array such that there exists every possible subsequence with sum 1 to s, where s denotes the sum of all the elements of the array. Find the minimum possible number of the array element to be sub
6 min read
Minimum steps required to reduce all array elements to 1 based on given steps Given an array arr[] of size N. The task is to find the minimum steps required to reduce all array elements to 1. In each step, perform the following given operation: Choose any starting index, say i, and jump to the (arr[i] + i)th index, reducing ith as well as (arr[i] + i)th index by 1, follow thi
8 min read
Minimize decrements to make each Array elements same or 0 Given an array arr[] consisting of N positive integers. In one operation any number of the array can be decremented by 1. The task is to find the minimum number of operations required to make all of the elements equal or 0. Examples: Input: arr[] = {4, 1, 6, 6}Output: 5Explanation: Remove 1 from arr
5 min read
Make all array elements equal by reducing array elements to half minimum number of times Given an array arr[] consisting of N integers, the task is to minimize the number of operations required to make all array elements equal by converting Ai to Ai / 2. in each operation Examples: Input: arr[] = {3, 1, 1, 3}Output: 2Explanation: Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}. Red
6 min read