# Valid Pairs

Posted: 2 Oct, 2020

Difficulty: Easy

#### You are given an array 'ARR' of 'N' integers and two integers 'K' and 'M'.

#### You need to return true if the given array can be divided into pairs such that the sum of every pair gives remainder 'M' when divided by 'K'. Otherwise, you need to return false.

#### For example:

```
If the given array is [2, 1, 5, 7] and K = 9 and M = 3. Then you need to return true because we can divide the array into two pairs, i.e (2, 1) and (5, 7) whose sums are 3 and 12, which when divided by 9 gives remainder 3, thus it is possible to divide the given array into pairs.
```

#### Note:

```
Every element of the array should contribute to only one pair, i.e if the array is [3, 0, 0] and K = 2 and M = 1, then you need to return false, as element 3 will make a pair with any one of the 0.
```

##### Input format :

```
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run.
Then the 'T' test cases follow.
The first line of each test case contains an integer 'N' representing the size of the given array.
The second line contains 'N' single space-separated integers representing the elements of the array 'ARR'.
The third line contains two single space-separated integers 'K' and 'M'.
```

##### Output Format :

```
For each test case, print a single line containing either "True" or "False".
```

##### Note:

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

##### Constraint :

```
1 <= T <= 10
1 <= N <= 10 ^ 5
1 <= ARR[ i ] <= 10 ^ 9
1 <= K <= 10 ^ 9
0 <= M < K
Where 'N' is the length of the array, 'ARR[ i ]' denotes the 'ith' element of array 'ARR' and 'K' and 'M' are the given integers.
Time Limit: 1 sec
```

Approach 1

**Approach: **

- Check the length of the array if it is odd then you can’t make pairs. Thus, return false.
- Create a boolean array ‘VISTED’ of length ‘N’ initialized to false, which will store true if the element of the given array at that particular index is paired up already or not.
- For each element of the array, try to pair up with each other element of the array.
- If both the elements are not paired up i.e(VISITED[i] and VISITED[j] are false), and the sum of both the elements of the array gives remainder ‘M’ when divided by ‘K’, then pair them up by making theirs ‘VISITED’ array true.
- Break the iteration as the element is paired up.

- Check whether all the elements are paired up or not by checking whether all the elements of the ‘VISITED’ array is true or not, if it is true that means all the elements are paired up, thus return true otherwise return false.

Approach 2

**Approach: **

- If the length of the given array ‘ARR’ is odd, return false as the odd length array cannot be divided into pairs.
- Use a hashmap to store the frequencies of all the remainders.
- Traverse the array and calculate ARR[i] % K and store its frequency into the map.
- Traverse all the keys of the map and check :
- If the current key of the map is half of the ‘M’ then there must be an even occurrence of those remainder so that it can form pairs. Thus, check its frequency if it is odd then return false.
- Else, for the current element ‘X’ check the frequency of (M - X), if frequencies of both elements are not equal then return false.
- If all the keys are traversed then return true.

SIMILAR PROBLEMS

# Game of 3

Posted: 11 Jul, 2021

Difficulty: Easy

# Lexicographic Permutation Rank

Posted: 13 Jul, 2021

Difficulty: Moderate

# Zero Pair Sum

Posted: 22 Jul, 2021

Difficulty: Moderate

# Implement a Queue

Posted: 27 Jul, 2021

Difficulty: Easy

# Remove K Corner Elements

Posted: 31 Jul, 2021

Difficulty: Easy