mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-05 05:40:28 -04:00
add some array playing
This commit is contained in:
parent
5732cfcc61
commit
5c55b81641
7 changed files with 263 additions and 0 deletions
arrays_and_strings
27
arrays_and_strings/array-check-if-exist.py
Normal file
27
arrays_and_strings/array-check-if-exist.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
"""
|
||||
Given an array arr of integers, check if there exist two indices i and j such that :
|
||||
|
||||
i != j
|
||||
0 <= i, j < arr.length
|
||||
arr[i] == 2 * arr[j]
|
||||
"""
|
||||
|
||||
|
||||
def check_if_exist(arr: list[int]) -> bool:
|
||||
|
||||
aux_dict = {}
|
||||
|
||||
for i, num in enumerate(arr):
|
||||
aux_dict[2*num] = i
|
||||
|
||||
for j, num in enumerate(arr):
|
||||
if num in aux_dict.keys() and j != aux_dict[num]:
|
||||
return (j, aux_dict[num])
|
||||
|
||||
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
arr = [-2, 0, 10, -19, 4, 6, -8]
|
||||
print(check_if_exist(arr))
|
Loading…
Add table
Add a link
Reference in a new issue