Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions python/1497.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 1497. Check If Array Pairs Are Divisible by k

[LeetCode problem 1497](https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/)

- **Difficulty**: Medium
- **Tags**: array, hash-table, counting

## Description

Given an array of integers `arr` of even length `n` and an integer `k`,
determine whether it's possible to divide the array into exactly `n / 2`
pairs such that the sum of each pair is divisible by `k`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "solution.hpp"

namespace leetcode::p1497 {

bool solve(const std::vector<int>& arr, int k) {
std::vector<int> remainderCounts(static_cast<std::size_t>(k), 0);

for (int value : arr) {
int remainder = ((value % k) + k) % k;
++remainderCounts[static_cast<std::size_t>(remainder)];
}

if (remainderCounts[0] % 2 != 0) {
return false;
}

for (int remainder = 1; remainder <= k / 2; ++remainder) {
if (remainderCounts[static_cast<std::size_t>(remainder)] !=
remainderCounts[static_cast<std::size_t>(k - remainder)]) {
return false;
}
}

return true;
}

} // namespace leetcode::p1497
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vector>

namespace leetcode::p1497 {

// optimized: O(n + k) time, O(k) space — count remainders mod k, then check
// that each remainder's count matches its complement's (k - r) count.
bool solve(const std::vector<int>& arr, int k);

} // namespace leetcode::p1497
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

TEST_CASE("Problem 1497", "[p1497]") {
CHECK(leetcode::p1497::solve({1, 2, 3, 4, 5, 10, 6, 7, 8, 9}, 5));
CHECK(leetcode::p1497::solve({1, 2, 3, 4, 5, 6}, 7));
CHECK_FALSE(leetcode::p1497::solve({1, 2, 3, 4, 5, 6}, 10));
CHECK(leetcode::p1497::solve({-1, 1, -2, 2, -3, 3, -4, 4}, 3));
}
Loading