Array Subset

Array Subset

?? Problem:

You're given two arrays: a1[] and a2[], where both may contain duplicates, and the goal is to determine whether a2[] is a subset of a1[]. The catch? The arrays are large, and the elements are of type long.


?? Approach:

Efficiently solve this problem using a frequency map to count occurrences in a1[] and validate against a2[].


Code Implementation

import java.Util.HashMap;

class Compute {
public String isSubset(long[] a1, long[] a2, long n, long m)
{
// Create a frequency map for a1[]
HashMap<Long, Integer> freqMap = new HashMap<>();

for(long num : a1)
{
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}

// Check elements of a2[] in the frequency map
for(long num : a2)
{
if(!freqMap.containsKey(num) ||  freqMap.get(num == 0)
{
return "No";   // Element not found or count exhausted
}

freqMap.put(num, freqMap.get(num) - 1);  // Decrease the count
}
return "Yes";   // All Elements of a2[] are present in a1[]
}
}        

How It Works

1?? Frequency Map:

  • Create a map to count occurrences of each element in a1[].

2?? Validation:

  • Traverse a2[] and check if each element exists in the map with sufficient occurrences.

3?? Output:

  • If all elements of a2[] are found in a1[] with valid counts, return "Yes"; otherwise, return "No".



Example

Input:

a1[] = {11, 7, 1, 13, 21, 3, 7, 3}  
a2[] = {11, 3, 7, 1, 7}         

Output:

Yes

Explanation: a2[] is a subset of a1[] since all its elements exist in a1[] with valid counts.



Efficiency

  • Time Complexity: O(max(n,m))O(\max(n, m))O(max(n,m))
  • Space Complexity: O(n)O(n)O(n)

?? Explore the beauty of maps and efficient validations!

要查看或添加评论,请登录

Pathan Ismailkhan的更多文章

  • Intersection Point in Linked Lists

    Intersection Point in Linked Lists

    When working with linked lists, finding where two lists intersect can be tricky especially when efficiency is crucial!…

  • Is Linked List Length Even?

    Is Linked List Length Even?

    To solve the problem of determining if the length of a linked list is even or odd, let's consider an efficient approach…

  • Count Linked List Nodes

    Count Linked List Nodes

    ?? Problem: Today’s challenge is a fundamental one in data structures—finding the length of a Singly Linked List. ??…

  • Two Smallests in Every Subarray

    Two Smallests in Every Subarray

    ? Short Summary: In today's CODE OF THE DAY, we tackle how to find the maximum sum of the two smallest elements in…

  • Reorganize The Array

    Reorganize The Array

    ?? Summary: In today’s "Code of the Day," we explore an exciting problem: rearranging an array so that arr[i] = i. ??…

  • Max distance between same elements

    Max distance between same elements

    ?? Summary: In today’s "Code of the Day," we tackle a classic problem: finding the maximum distance between repeated…

    3 条评论
  • Largest Pair Sum

    Largest Pair Sum

    To solve the problem of finding the largest pair sum in an array of distinct integers, we can utilize a simple and…

  • Not a subset sum

    Not a subset sum

    ????? Problem: Given a sorted array of positive integers, find the smallest positive integer that cannot be represented…

  • 2491. Divide Players Into Teams of Equal Skill

    2491. Divide Players Into Teams of Equal Skill

    ????? Problem: You are given an even-length array of players' skills and the goal is to divide them into teams of 2…

  • Multiply two linked lists

    Multiply two linked lists

    GeeksforGeeks Multiplying Linked Lists for Massive Numbers: Learn with O(max(n, m)) Time Complexity! ???? ?????…

社区洞察

其他会员也浏览了