mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:44:07 +00:00
1497-check-if-array-pairs-are-divisible-by-k
This commit is contained in:
parent
44dd48d78b
commit
307f93425a
12
1497-check-if-array-pairs-are-divisible-by-k/driver.cpp
Normal file
12
1497-check-if-array-pairs-are-divisible-by-k/driver.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Solution soln;
|
||||||
|
vector<int> 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;
|
||||||
|
}
|
||||||
77
1497-check-if-array-pairs-are-divisible-by-k/soln.cpp
Normal file
77
1497-check-if-array-pairs-are-divisible-by-k/soln.cpp
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
bool Solution::DEBUG = true;
|
||||||
|
|
||||||
|
bool Solution::canArrange(vector<int> &arr, int k)
|
||||||
|
{
|
||||||
|
// vector<int> result = vector<int>();
|
||||||
|
// 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<int> 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<int, int> 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<int> &arr, int k, bool answer)
|
||||||
|
{
|
||||||
|
return canArrange(arr, k) == answer;
|
||||||
|
}
|
||||||
22
1497-check-if-array-pairs-are-divisible-by-k/soln.hpp
Normal file
22
1497-check-if-array-pairs-are-divisible-by-k/soln.hpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static bool DEBUG;
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
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<int> &arr, int k);
|
||||||
|
bool test(vector<int> &arr, int k, bool answer);
|
||||||
|
};
|
||||||
49
1497-check-if-array-pairs-are-divisible-by-k/soln.py
Normal file
49
1497-check-if-array-pairs-are-divisible-by-k/soln.py
Normal file
@ -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))
|
||||||
@ -1,2 +1,3 @@
|
|||||||
#include "soln.hpp"
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
bool Solution::DEBUG = true;
|
||||||
|
|||||||
@ -3,6 +3,21 @@ using namespace std;
|
|||||||
class Solution
|
class Solution
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static bool DEBUG;
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
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);
|
bool test(, bool answer);
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user