Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Example
2.1.1.
First Example Input
2.1.2.
Output
2.1.3.
Explanation
2.1.4.
Second Example Input
2.1.5.
Output
3.
Approach 1
4.
Approach 2
4.1.
Algorithm
4.2.
Implementation in C++
4.2.1.
Output
4.3.
Implementation in Java
4.3.1.
Output
4.4.
Time Complexity 
4.5.
Auxiliary Space Complexity 
5.
Frequently Asked Questions
5.1.
How are the subsets present in the given set counted or generated?
5.2.
The array's subset continuous? 
5.3.
What is the purpose of Kadane's algorithm?
5.4.
Can a Subarray sum be achieved?  
5.5.
What does an array subsequence mean?
6.
Conclusion
Last Updated: Mar 27, 2024

Count subsets having distinct even numbers

Introduction

If you want a successful career in the tech field, you need to have a strong knowledge of Data Structure and algorithms to help you get. We’ll solve a question frequently asked in the technical interview based on the concept of the HashSet.

Must Recommended Topic, Hash Function in Data Structure.

Problem Statement

A combination or array of numbers has been given to us. The maximum number of subsets with different even numbers that we can have is to be listed.

Example

First Example Input

Output

7

Explanation

Subsets are [4, 2, 6], [4, 2], [2, 6], [4, 6], [4], [2], [6].

 

Second Example Input

Output

127

Approach 1

There are numerous approaches to discovering what we want. The brute force method would find all possible subsets and then identify those that satisfy our criterion.

Approach 2

The technique above is equivalent to hitting your head against a wall. Therefore, I have figured out the simplest strategies that can help you get at the answer in the shortest amount of time, saving you the shame of seeming foolish.

Every time we come across an even number, we have two options.

  1. It may decide to either join the subgroup or not.
     
  2. It may decide to exclude itself from the subset.

Therefore, the only work left for us to complete is figuring out whether or not the number is unique.

Algorithm

  1. Maintain a HashSet to hold all of the even numbers we have come across.
     
  2. Next, perform a loop.
     
  3. Verify that a number is even.
     
  4. If the number is even, the HashSet is added.
     
  5. We then determine the number of entries in the HashSet by using self-ordering, which makes sure that only unique elements enter.
     
  6. This number represents the amount of distinct even numbers that exist.
     
  7. The number of subgroups may then be determined using this count.
     
  8. Subset count=1 and number=2

Implementation in C++

#include <iostream>


int main() {
    // Given array 
     int givenArray[] = {4, 2, 1, 9, 2, 6, 5, 3}; 
     //calculating the size of the array
    int n = sizeof(givenArray)/sizeof(givenArray[0]); 
    
    cout << "Number of Sets are: " 
         << allEvenSets(givenArray, n); 
}
int allEvenSets(int givenArray[],int n)
{
    unordered_set<int>store; 
    //loop
    for(int i=0;i<n;i++)
    {
        if(givenArray[i]%2==0)
            store.insert(givenArray[i]);
    }
    int p=store.size();
    return (pow(2,p)-1);
}

Output

Number of Sets are: 7

Implementation in Java

public class Main
{
    public static int evenSub(int givenarray[],int n)
    {
        
        HashSet<Integer>store=new HashSet<Integer>();
        //loop
        for(int i=0;i<givenarray.length;i++)
        {
            if(givenarray[i]%2==0)
                store.add(givenarray[i]);
        }
        // Calculating power by inbuilt function pow
        int powerr=store.size();
        return (int)(Math.pow(2,powerr)-1);
    }
    public static void main(String[] args)  
    { 
        //given array
    int givenarray[] = {2, 1, 9, 2, 6, 5, 3}; 
    //calculating length
    int n = givenarray.length; 
    System.out.println("Number of Sets are:"+ evenSub(givenarray, n)); 
    }
}

Output

Number of Sets are: 3

Time Complexity 

O(N) Where, ‘N’ stands for the length of the array.

Reason: As we traverse the array, it is O(n). Before including an element in the subset, we evaluate each one. So the time complexity will be O(N)

Auxiliary Space Complexity 

O(N), Where ‘N’ stands for the array's length.

Reason: Because we might end up storing the full array in the worst scenario, it is O(n).

You can also read about the Longest Consecutive Sequence.

Frequently Asked Questions

How are the subsets present in the given set counted or generated?

The straightforward approach is to create all possible subsets of a given set that are 2n, where n denotes the set's size, and count the number of subsets that contain element X.

The array's subset continuous? 

A subsequence need not be contiguous. However, a subarray or substring will always be contiguous. Therefore, subsequences don't need to occupy adjacent locations inside the parent sequences. Contiguous subsequence and subarray, however, are equivalent. 

What is the purpose of Kadane's algorithm?

There are numerous uses for Kadane's algorithm, some of which are listed below: calculating the largest subarray sum for the given integer array. used as an algorithm for image processing. Problems like "Station Travel in Order" and "Hotels Along the Coast" can be solved using it.

Can a Subarray sum be achieved?  

The aim is to determine the sum of each distinct sub-array sum. The sub-array sum is the total of all components in a specific sub-array. Note that no other sub-array will have the same sum value as the unique sub-array sum.

What does an array subsequence mean?

An ordered subset of an array's items with the same sequential ordering as the main array is referred to as a subsequence.

Conclusion

This article has gone through problems related to the subset and array

If you identify any errors or would like to add more information to the discussion above, kindly comment.

Check out our articles in Library. You may also CheckOut our course on the array.

Recommended Problems: 

Attempt our Online Mock Test Series on Coding Ninjas Studio now! 

Ninja, have a great time learning.

Live masterclass