Check If Array Pairs Are Divisible by k

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
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.
LeetCode Problem - 1497
class Solution {
public boolean canArrange(int[] arr, int k) {
// Array to keep track of the frequency of remainders when elements are divided by k
int[] freq = new int[k];
// Loop through the array to calculate remainders and update their frequencies
for(int num : arr){
// Calculate remainder ensuring it's non-negative
int remainder = (num % k + k) % k;
freq[remainder]++; // Increment the frequency of the remainder
}
// If the frequency of remainder 0 is odd, it's not possible to pair the elements
if(freq[0] % 2 != 0) return false;
// Loop through the remainders from 1 to k/2 (to check both sides of the remainder spectrum)
for(int rem = 1; rem <= k / 2; rem++){
int counterHalf = k - rem; // Find the counterpart remainder that would sum to a multiple of k
// If the frequency of a remainder is not equal to the frequency of its counterpart
// it means pairs cannot be formed
if(freq[rem] != freq[counterHalf]){
return false;
}
}
// If all checks passed, return true
return true;
}
}




