Related Articles

# Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements

• Last Updated : 09 Jun, 2021

Given an array arr[] of size N, the task is to make all array elements equal to 0 by replacing all elements of a subsequences of equal elements by any integer, minimum number of times.

Examples:

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

Approach: The problem can be solved using Greedy technique. The idea is to count the distinct elements present in the array which is not equal to 0 and print the count obtained. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## 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<``int``> 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<``int``> st = ``new` `HashSet<``int``>();` `    ``// 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`

## Javascript

 ``
Output:
`2`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up