mediumArraysPattern: Hashing

Unique Ingredient Combinations Solution

Problem Statement

Given a 2D array of integers `existingPotions` where each sub-array represents the ingredients used in an existing potion, and an array of integers `newPotion` representing the ingredients of a new potion, determine whether the new potion's ingredients are unique. The ingredients are represented by numeric codes, and the same code can be used in multiple potions, but the combination of codes must be unique.

Examples

Example 1:
Input:{"existingPotions":[[1,2,3],[4,5,6],[7,8,9]],"newPotion":[1,2,3]}
Output:false
Explanation: The new potion's ingredients are the same as an existing potion
Example 2:
Input:{"existingPotions":[[1,2,3],[4,5,6],[7,8,9]],"newPotion":[1,2,10]}
Output:true
Explanation: The new potion's ingredients are distinct from all existing potions

Constraints

  • The length of the input array is between 1 and 1000.
  • The elements in the input array are integers ranging from 1 to 10.
  • The input array is sorted in ascending order.
Time: O(n) Space: O(n)
Use a set to store unique ingredients and compare the new potion's ingredients with the set in O(n) time complexity

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.