## Similar Problems

Similar Problems not available

# Successful Pairs Of Spells And Potions - Leetcode Solution

## Companies:

LeetCode: Successful Pairs Of Spells And Potions Leetcode Solution

Difficulty: Medium

Topics: sorting two-pointers array binary-search

Problem Statement:

You are given two lists of integers, spells and potions, representing the power of spells and potions respectively. You want to find the number of pairs, such that the sum of the power of a spell and a potion is divisible by an integer, k.

Function Signature: int successfulPairs(vector<int>& spells, vector<int>& potions, int k);

Input:

- two integer arrays, spells and potions, of length n (1 ≤ n ≤ 105)
- an integer k (1 ≤ k ≤ 109)

Output:

- the number of pairs such that the sum of the power of a spell and a potion is divisible by k.

Solution:

To solve this problem, we need to find pairs of spells and potions such that the sum of their power is divisible by k. We can do this using the following algorithm:

- Create a hash table to keep track of the frequency of remainders when dividing each element of the spells array by k.
- Loop through the potions array and for each element in the array, calculate the remainder of the element when divided by k.
- Check if k - remainder exists in the hash table. If it does, increment the count of successful pairs by the product of the frequency of remainder and the frequency of k - remainder.
- If the remainder is zero, increment the count of successful pairs by the frequency of remainder.
- Return the count of successful pairs.

Code:

int successfulPairs(vector<int>& spells, vector<int>& potions, int k) { unordered_map<int, int> freq; int count = 0; for(int i=0; i<spells.size(); i++) { int remainder = spells[i] % k; freq[remainder]++; } for(int i=0; i<potions.size(); i++) { int remainder = potions[i] % k; int required = (k - remainder) % k; if(freq.find(required) != freq.end()) { count += freq[required]; } if(required == remainder && freq.find(remainder) != freq.end()) { count += freq[remainder] - 1; } } return count/2; }

Explanation:

We first create a hash table to store the frequency of remainders when dividing elements of the spells array by k. We then loop through the potions array and for each element, we calculate the remainder when divided by k. We also calculate the required remainder such that the sum of the remainder and required remainder is divisible by k.

If the required remainder exists in the hash table, we increment the count of successful pairs by the product of the frequency of remainder and the frequency of required remainder. If the remainder is zero, we increment the count of successful pairs by the frequency of remainder. We also check if the remainder and required remainder are the same, in which case we increment the count by (frequency - 1) to avoid counting the same element twice.

Finally, we return the count of successful pairs divided by 2 since we have counted each pair twice in our algorithm.

Time Complexity: The time complexity of this algorithm is O(n) since we loop through both the spells and potions arrays once.

Space Complexity: The space complexity of this algorithm is O(n) since we use a hash table to store the frequency of remainders.

## Successful Pairs Of Spells And Potions Solution Code

`1`