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.
Constraints:
2 <= arr.length <= 500
-103 <= arr[i] <= 103
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to see if, within a group of numbers, any number has its double also present in the group. The brute force approach is all about checking every possible pairing of numbers to see if the condition is met.
Here's how the algorithm would work step-by-step:
def check_if_n_and_double_exist(number_list):
list_length = len(number_list)
for first_number_index in range(list_length):
first_number = number_list[first_number_index]
# Iterate through list, comparing to first_number
for second_number_index in range(list_length):
# Skip checking if number is same as first
if first_number_index == second_number_index:
continue
second_number = number_list[second_number_index]
# Check if second number is double the first
if second_number == 2 * first_number:
return True
# If no double is found, return False
return False
The most efficient way to solve this problem is to check for a number's double quickly. We can do this by first storing all the numbers we see and then checking for each number if its double is already stored.
Here's how the algorithm would work step-by-step:
def check_if_double_exists(arr):
seen_numbers = set()
for number in arr:
# Before adding, check if double is already present
if number * 2 in seen_numbers:
return True
# Before adding, check if half exists for even numbers
if number % 2 == 0 and number // 2 in seen_numbers:
return True
seen_numbers.add(number)
# If no double is found, return False
return False
Case | How to Handle |
---|---|
Null or empty input array | Return false immediately as no double can exist. |
Array with only one element | Return false because a number and its double require at least two elements. |
Array containing only zeros | Handle the case where zero's double (zero) exists by checking for its presence and handling index issues carefully. |
Array with large positive and negative numbers that could lead to integer overflow when doubled. | Consider using a language with automatic overflow handling or using a data type that can hold larger values to prevent overflow during multiplication. |
Array with duplicate elements other than zero. | The hash set or map approach inherently handles duplicates by overwriting entries. |
Array with no number and its double present | Return false after iterating through the entire array if no double is found. |
Large input array causing memory constraints. | Optimize space usage by considering in-place modifications (if allowed) or alternative data structures. |
Array contains only negative numbers | The algorithm should correctly identify doubled negative numbers. |