From 307f93425a245700e528403c3fbe312adbb4ed08 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Tue, 1 Oct 2024 00:23:49 -0700 Subject: [PATCH] 1497-check-if-array-pairs-are-divisible-by-k --- .../driver.cpp | 12 +++ .../soln.cpp | 77 +++++++++++++++++++ .../soln.hpp | 22 ++++++ .../soln.py | 49 ++++++++++++ template/soln.cpp | 1 + template/soln.hpp | 15 ++++ 6 files changed, 176 insertions(+) create mode 100644 1497-check-if-array-pairs-are-divisible-by-k/driver.cpp create mode 100644 1497-check-if-array-pairs-are-divisible-by-k/soln.cpp create mode 100644 1497-check-if-array-pairs-are-divisible-by-k/soln.hpp create mode 100644 1497-check-if-array-pairs-are-divisible-by-k/soln.py diff --git a/1497-check-if-array-pairs-are-divisible-by-k/driver.cpp b/1497-check-if-array-pairs-are-divisible-by-k/driver.cpp new file mode 100644 index 0000000..7d71f49 --- /dev/null +++ b/1497-check-if-array-pairs-are-divisible-by-k/driver.cpp @@ -0,0 +1,12 @@ +#include "soln.hpp" + +int main() +{ + Solution soln; + vector arr{1, 2, 3, 4, 5, 6}; + int k = 10; + bool answer = false; + cout << "Checked pairings correctly? " << endl; + cout << soln.test(arr, k, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/1497-check-if-array-pairs-are-divisible-by-k/soln.cpp b/1497-check-if-array-pairs-are-divisible-by-k/soln.cpp new file mode 100644 index 0000000..d05b633 --- /dev/null +++ b/1497-check-if-array-pairs-are-divisible-by-k/soln.cpp @@ -0,0 +1,77 @@ +#include "soln.hpp" + +bool Solution::DEBUG = true; + +bool Solution::canArrange(vector &arr, int k) +{ + // vector result = vector(); + // for (int num = 0; num < arr.size(); num++) + // { + // bool found_pair = false; + // for (int existing = 0; existing < result.size(); existing++) + // { + // if ((arr[num] + result[existing]) % k == 0) + // { + // found_pair = true; + // dbg("pop", result[existing]); + // result.erase(find(result.begin(), result.end(), result[existing])); + // break; + // } + // } + // if (!found_pair) + // { + // result.emplace_back(arr[num]); + // dbg("insert", arr[num]); + // } + // } + // return !(result.size() > 0); + + vector rems; + int zeros = 0; + for (int i = 0; i < arr.size(); i++) + { + if (arr[i] % k == 0) + { + zeros++; + } + else if (arr[i] % k < 0) + { + rems.emplace_back((arr[i] % k) + k); + } + else + { + rems.emplace_back(arr[i] % k); + } + } + if (zeros % 2) + { + return false; + } + unordered_map result; + for (int i = 0; i < rems.size(); i++) + { + if (result.find(k - rems[i]) != result.end() && result[k - rems[i]] > 0) + { + result[k - rems[i]]--; + dbg("pop", k - rems[i]); + } + else + { + result[rems[i]] = result[rems[i]] + 1; + dbg("insert", rems[i]); + } + } + for (auto it = result.begin(); it != result.end(); it++) + { + if (it->second > 0) + { + return false; + } + } + return true; +} + +bool Solution::test(vector &arr, int k, bool answer) +{ + return canArrange(arr, k) == answer; +} \ No newline at end of file diff --git a/1497-check-if-array-pairs-are-divisible-by-k/soln.hpp b/1497-check-if-array-pairs-are-divisible-by-k/soln.hpp new file mode 100644 index 0000000..c2abc4f --- /dev/null +++ b/1497-check-if-array-pairs-are-divisible-by-k/soln.hpp @@ -0,0 +1,22 @@ +#include +using namespace std; +class Solution +{ +public: + static bool DEBUG; + + template + void dbg(Args &&...args) + { + if (DEBUG) + { + const char *sep = " "; + const char *label = "[DEBUG]"; + cout << label; + (((cout << sep << args), sep = sep), ...); + cout << endl; + } + } + bool canArrange(vector &arr, int k); + bool test(vector &arr, int k, bool answer); +}; \ No newline at end of file diff --git a/1497-check-if-array-pairs-are-divisible-by-k/soln.py b/1497-check-if-array-pairs-are-divisible-by-k/soln.py new file mode 100644 index 0000000..99a2c20 --- /dev/null +++ b/1497-check-if-array-pairs-are-divisible-by-k/soln.py @@ -0,0 +1,49 @@ +class Solution: + def canArrange(self, arr: list[int], k: int) -> bool: + # DEBUG = False + # dbg_print = lambda *x: print(f"[DEBUG] {x}") if DEBUG else print("", end="") + + # # brute force, TLE + # result = [] + # for num in arr: + # found_pair = False + # for existing in result: + # if (num+existing) % k == 0: + # found_pair = True + # result.remove(existing) + # break + # if not found_pair: + # result.append(num) + + # return not len(result) > 0 + + # idea: sum of remainders will be divisible too + rems = [x % k for x in arr] + # dbg_print("rems", rems) + zeros = 0 + for rem in rems: + if rem == 0: + zeros += 1 + if zeros % 2: + return False + rems[:] = [x for x in rems if x] + # dbg_print("rems without zeros", rems) + + result = {} + for rem in rems: + if k - rem in result.keys() and result[k - rem] > 0: + result[k - rem] -= 1 + # dbg_print("pop", k-rem) + else: + result[rem] = result.get(rem, 0) + 1 + # dbg_print("insert", rem) + for x in result.keys(): + if result[x]: + return False + + return True + + +if __name__ == "__main__": + soln = Solution() + print(soln.canArrange([1, 2, 3, 4, 5, 10, 6, 7, 8, 9], 5)) diff --git a/template/soln.cpp b/template/soln.cpp index 05f0770..0f20cdf 100644 --- a/template/soln.cpp +++ b/template/soln.cpp @@ -1,2 +1,3 @@ #include "soln.hpp" +bool Solution::DEBUG = true; diff --git a/template/soln.hpp b/template/soln.hpp index 52671ed..57ff256 100644 --- a/template/soln.hpp +++ b/template/soln.hpp @@ -3,6 +3,21 @@ using namespace std; class Solution { public: + static bool DEBUG; + template + void dbg(Args &&...args) + { + if (DEBUG) + { + const char *sep = " "; + const char *label = "[DEBUG]"; + cout << label; + (((cout << sep << args), sep = sep), ...); + cout << endl; + } + } + + bool test(, bool answer); }; \ No newline at end of file