Array Pair Sum Divisibility
Function Name: isPairSumDivisible
Description
In this challenge, you are given an array of integers and a positive integer 'k'. Your task is to write a function that checks whether it is possible to divide the array into pairs such that the sum of each pair is divisible by 'k'.
The function should return a boolean value: true if such a division is possible, or false if it is not. Each element of the array can only be used in one pair.
Requirements
- The function 'isPairSumDivisible' must take two parameters: an array of integers and a positive integer 'k'.
- The function must return a boolean value.
- An empty array should return true since there are no elements that could violate the divisibility condition.
- If the array contains an odd number of elements, the function should automatically return false, as pairs cannot be formed.
- Optimize the function for time complexity.
Examples
If isPairSumDivisible([1, 2, 3, 4, 5, 6], 3), the function should return true. (Because pairs (1, 2), (3, 6), and (4, 5) all sum up to a multiple of 3)If isPairSumDivisible([1, 2, 3, 4, 5], 3), the function should return false. (Because there's no way to pair all elements such that their sums are multiples of 3)If isPairSumDivisible([], 10), the function should return true. (Empty array has no elements to pair, thus it's trivially divisible)
Links
Circular Queue Implementation
Thu Nov 28 2024
Implement a Circular Queue using an array. The Circular Queue should support the following operations: enqueue, dequeue, peek, and isEmpty. The enqueue operation adds an item to the back of the queue, the dequeue operation removes an item from the front of the queue, and the peek operation retrieves the item at the front of the queue without removing it. The isEmpty operation should return a boolean indicating whether the queue is empty. The Circular Queue should also have a fixed size specified at the time of creation and should handle the wrap-around when the end of the array is reached.
Count Primes
Sat Nov 30 2024
Write a function that takes a positive integer n and returns the number of prime numbers less than n.The function should implement an efficient algorithm for counting primes (e.g., Sieve of Eratosthenes).Do not use any built-in functions for detecting prime numbers.Optimize the function to handle large numbers as inputs.