Skip to content

Commit a363e13

Browse files
committed
Ading Array Addition solution
1 parent 73c5c1e commit a363e13

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ Solutions to various challenges published at www.hackerrank.com
465465
* Binary Search Tree LCA
466466
* Shortest Path in Unweighted Graph with cycles
467467
* Weighted Path
468+
* PowerSet
469+
* Subset Sum
470+
* Two Sum
471+
* Array Addition
468472

469473
## Contributing
470474

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
4+
class ArrayAddition:
5+
def __call__(self, arr: List[int]) -> bool:
6+
max_num = max(arr)
7+
arr.remove(max_num)
8+
return self._test(arr, max_num)
9+
10+
@classmethod
11+
def _test(cls, arr: List[int], max_num: int) -> bool:
12+
for x in arr:
13+
# Found, return early
14+
if x == max_num:
15+
return True
16+
# clone current list
17+
c_arr = list(arr)
18+
# remove used number
19+
c_arr.remove(x)
20+
# recur test, for target - removed value
21+
if cls._test(c_arr, max_num - x):
22+
return True
23+
# Target not found
24+
return False
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from py_algorithms.challenges.coderbyte.array_addition import ArrayAddition
2+
3+
4+
class ArrayAdditionTest:
5+
def test_impl(self):
6+
inputs = {
7+
True: [4, 6, 23, 10, 1, 3],
8+
False: [4, 6, 123, 10, 1, 3]
9+
}
10+
f = ArrayAddition()
11+
for key in inputs:
12+
assert f(inputs[key]) == key

0 commit comments

Comments
 (0)