From 2ead8bb040010c6f244ae33a7cd2b40485ab6f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:18:17 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A1=20Port=20problem=201497=20(Check?= =?UTF-8?q?=20If=20Array=20Pairs=20Are=20Divisible=20by=20k)=20from=20Pyth?= =?UTF-8?q?on=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces python/1497.py with solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/. The original Python only checked mirrored pairs (arr[i] with arr[n-1-i]), which is a stricter condition than the actual problem (any valid pairing) — it happened to pass its own tests but isn't the real algorithm. Replaced with the standard remainder-counting approach: count arr[i] % k for each element, then require freq[0] to be even and freq[r] == freq[k - r] for every other remainder. --- python/1497.py | 31 ------------------- .../README.md | 12 +++++++ .../solution.cpp | 27 ++++++++++++++++ .../solution.hpp | 11 +++++++ .../test.cpp | 10 ++++++ 5 files changed, 60 insertions(+), 31 deletions(-) delete mode 100644 python/1497.py create mode 100644 solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/README.md create mode 100644 solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.cpp create mode 100644 solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.hpp create mode 100644 solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/test.cpp diff --git a/python/1497.py b/python/1497.py deleted file mode 100644 index f288942..0000000 --- a/python/1497.py +++ /dev/null @@ -1,31 +0,0 @@ -import unittest - -class Solution: - def canArrange(self, arr: list[int], k: int) -> bool: - length = len(arr) - - for i in range(length // 2): - j = length - i - 1 - if (arr[i] + arr[j]) % k != 0: - return False - return True - -class TestCanArrange(unittest.TestCase): - def setUp(self): - self.sol = Solution() - - def testCanArrange(self): - self.assertTrue(self.sol.canArrange([1, 2, 3, 4, 5, 10, 6, 7, 8, 9], 5), - "Erro: esperado True para a lista [1, 2, 3, 4, 5, 10, 6, 7, 8, 9] e k = 5") - - self.assertTrue(self.sol.canArrange([1, 2, 3, 4, 5, 6], 7), - "Erro: esperado True para a lista [1, 2, 3, 4, 5, 6] e k = 7") - - self.assertFalse(self.sol.canArrange([1, 2, 3, 4, 5, 6], 10), - "Erro: esperado False para a lista [1, 2, 3, 4, 5, 6] e k = 10") - - self.assertTrue(self.sol.canArrange([-1,1,-2,2,-3,3,-4,4], 3), - "Erro: esperado True para a lista [-1,1,-2,2,-3,3,-4,4] e k = 3") - -if __name__ == '__main__': - unittest.main() diff --git a/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/README.md b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/README.md new file mode 100644 index 0000000..f395cc3 --- /dev/null +++ b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/README.md @@ -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`. diff --git a/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.cpp b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.cpp new file mode 100644 index 0000000..35d4c01 --- /dev/null +++ b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.cpp @@ -0,0 +1,27 @@ +#include "solution.hpp" + +namespace leetcode::p1497 { + +bool solve(const std::vector& arr, int k) { + std::vector remainderCounts(static_cast(k), 0); + + for (int value : arr) { + int remainder = ((value % k) + k) % k; + ++remainderCounts[static_cast(remainder)]; + } + + if (remainderCounts[0] % 2 != 0) { + return false; + } + + for (int remainder = 1; remainder <= k / 2; ++remainder) { + if (remainderCounts[static_cast(remainder)] != + remainderCounts[static_cast(k - remainder)]) { + return false; + } + } + + return true; +} + +} // namespace leetcode::p1497 diff --git a/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.hpp b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.hpp new file mode 100644 index 0000000..5a299ad --- /dev/null +++ b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +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& arr, int k); + +} // namespace leetcode::p1497 diff --git a/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/test.cpp b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/test.cpp new file mode 100644 index 0000000..86a638b --- /dev/null +++ b/solutions/1401-1500/1497-check-if-array-pairs-are-divisible-by-k/test.cpp @@ -0,0 +1,10 @@ +#include + +#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)); +}