Advertisement

Check If Array Pairs Are Divisible by k - LeetCode 1497 Solution

Check If Array Pairs Are Divisible by k - Complete Solution Guide

Check If Array Pairs Are Divisible by k is LeetCode problem 1497, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of integers arr of even length n and an integer k . We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k . Return true If you can find a way to do that or false otherwise . Example 1: Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10). Example 2: Input: arr = [1,2,3,4,5,6], k = 7 Output: true Explanation: Pairs are (1,6),(2,5) and(3,4). Example 3: Input: arr = [1,2,3,4,5

Detailed Explanation

The problem asks us to determine if an array of even length can be divided into pairs such that the sum of each pair is divisible by a given integer 'k'. The input is an array 'arr' of integers and an integer 'k'. The output is a boolean value: 'true' if it's possible to divide the array into pairs with sums divisible by 'k', and 'false' otherwise. The constraints include the even length of the array, the size of the array, the range of values in the array, and the range of the divisor 'k'.

Solution Approach

The solution uses a remainder-counting approach. First, calculate the remainder of each number in the array when divided by 'k'. Then, store these remainders' frequencies in an array. Finally, iterate through the remainders. For each remainder 'i', check if there exists a corresponding remainder 'k-i' such that their counts are equal. Handle the edge cases of remainder 0 and k/2 carefully (they must occur an even number of times). If all conditions are met, the array can be divided as required; otherwise, it cannot.

Step-by-Step Algorithm

  1. Step 1: Initialize an array `remainder_counts` of size 'k' with all elements set to 0. This array will store the count of each remainder when an element of 'arr' is divided by 'k'.
  2. Step 2: Iterate through the input array 'arr'. For each element 'num', calculate its remainder when divided by 'k' (num % k). If the remainder is negative, add 'k' to make it positive. Increment the corresponding count in the `remainder_counts` array.
  3. Step 3: Check if the count of elements with remainder 0 is even. If not, return false (since they can only pair with themselves).
  4. Step 4: Iterate from i = 1 to k/2 (inclusive). If 2 * i == k, it implies that remainder i is k/2. Check if `remainder_counts[i]` is even. If not, return false.
  5. Step 5: If 2 * i != k, check if the count of elements with remainder 'i' is equal to the count of elements with remainder 'k-i'. If they are not equal, return false (since they can't form valid pairs).
  6. Step 6: If all checks pass, return true (meaning the array can be arranged into pairs with sums divisible by k).

Key Insights

  • Insight 1: Two numbers a and b sum to a multiple of k if (a % k + b % k) % k == 0. This means a % k and b % k must sum to 0 or k.
  • Insight 2: We can use a hash table or an array (since k is limited) to store the counts of remainders when each element is divided by k. This helps us efficiently check for pairs that sum to a multiple of k.
  • Insight 3: Special cases to consider are when the remainder is 0 or k/2 (when k is even). In these cases, the count of elements with these remainders must be even for valid pairing.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(k)

Topics

This problem involves: Array, Hash Table, Counting.

Companies

Asked at: DevRev, Visa.