# Solution For Successful Pairs Of Spells And Potions

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& spells, vector& 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:

1. Create a hash table to keep track of the frequency of remainders when dividing each element of the spells array by k.
2. Loop through the potions array and for each element in the array, calculate the remainder of the element when divided by k.
3. 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.
4. If the remainder is zero, increment the count of successful pairs by the frequency of remainder.
5. Return the count of successful pairs.

Code:

int successfulPairs(vector& spells, vector& potions, int k) {
unordered_map 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.

## Step by Step Implementation For Successful Pairs Of Spells And Potions

```There are two types of potion: A and B. There are three types of spell: X, Y, and Z.

We can use any number of potions of either type, and any number of spells of any type.

The goal is to have all three spells active at the same time, and to have as many potions of each type as possible.

(A spell is "active" if we have used at least one potion of the correct type since the last time the spell was used.)

For example, suppose we have two potions of type A and one potion of type B. We can use the spell Y twice, the spell Z once, and then use the spell X. This will leave us with one potion of type A and two potions of type B.

Alternatively, we could use the spell Y three times, the spell X once, and then use the spell Z. This would leave us with three potions of type A and one potion of type B.

In either case, we would have all three spells active, and we would have more potions of type A than type B.

So, the answer for this input would be "2 3".

import java.util.*;

public class Solution {

public static void main(String[] args) {

int[] A = {2, 1};

int[] B = {1, 2};

int[] C = {3, 0};

System.out.println(solve(A, B, C));

}

public static String solve(int[] A, int[] B, int[] C) {

int a = A;

int b = B;

int c = C;

int x = A;

int y = B;

int z = C;

int count = 0;

while (a > 0 && b > 0 && c > 0) {

if (x > 0) {

a--;

x--;

count++;

}

if (y > 0) {

b--;

y--;

count++;

}

if (z > 0) {

c--;

z--;

count++;

}

}

return count + " " + (a + b + c);

}

}```
```def spellPairs(S, P):

# create an empty list to store all successful pairs

successful_pairs = []

# iterate over all elements in the list S

for i in range(len(S)):

# iterate over all elements in the list P

for j in range(len(P)):

# check if the sum of the two elements is equal to 10

if S[i] + P[j] == 10:

# if the condition is true, then append the pair to the list

successful_pairs.append((S[i], P[j]))

# return the list of successful pairs

return successful_pairs```
```var successfulPairsOfSpellsAndPotions = function(arr1, arr2) {
var result = [];
for (var i = 0; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
if (arr1[i] + arr2[j] === 0) {
result.push([arr1[i], arr2[j]]);
}
}
}
return result;
};```
```One possible solution is as follows:

int findSuccessfulPairs(vector spellCost, vector potionCost) {
// Sort both vectors in ascending order
sort(spellCost.begin(), spellCost.end());
sort(potionCost.begin(), potionCost.end());

// Initialize counters for both vectors
int i = 0, j = 0;
int pairs = 0;

// Iterate over both vectors simultaneously
while (i < spellCost.size() && j < potionCost.size()) {
// If the current spell cost is less than or equal to the current potion cost,
// then we have a successful pair
if (spellCost[i] <= potionCost[j]) {
pairs++;
i++;
j++;
}
// Otherwise, we increment the potion cost counter
else {
j++;
}
}

return pairs;
}```
```using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
public int SuccessfulPairs(int[] spellCost, int[] potionCost) {
// check for empty input
if (spellCost.Length == 0 || potionCost.Length == 0) {
return 0;
}

// sort both arrays in ascending order
Array.Sort(spellCost);
Array.Sort(potionCost);

// keep track of the number of successful pairs
int numSuccessfulPairs = 0;

// keep track of the indices of the elements in each array that have been used
int spellIndex = 0;
int potionIndex = 0;

// iterate through both arrays until we reach the end of one of them
while (spellIndex < spellCost.Length && potionIndex < potionCost.Length) {
// if the cost of the spell is less than or equal to the cost of the potion, we have a successful pair
if (spellCost[spellIndex] <= potionCost[potionIndex]) {
numSuccessfulPairs++;

// move on to the next element in both arrays
spellIndex++;
potionIndex++;
}
// if the cost of the spell is greater than the cost of the potion, move on to the next element in the potion array
else {
potionIndex++;
}
}

return numSuccessfulPairs;
}
}```

Scroll to Top

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]