less than 1 minute read

1346. Check If N and Its Double Exist

Difficulty: Easy

Related Topics: Array, Hash Table, Two Pointers, Binary Search, Sorting

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]

Example 1:

Input: arr = [10,2,5,3] Output: true Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]

Example 2:

Input: arr = [3,1,7,11] Output: false Explanation: There is no i and j that satisfy the conditions.

Code:

class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        for i in range(len(arr)):
            for j in range(len(arr)):
                if i != j:
                    if arr[i] == 2*arr[j]:
                        return True
        return False

Feedbacks:

Since I found out that the lenght of array is less than 500 which is relatively small than other typical problems, I just simply implemented two nested for loops to solve this problem.