1346. Check If N and Its Double Exist
Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).
More formally check if there exists two indices i and j such that :
- i != j
- 0 <= i, j < arr.length
- arr[i] == 2 * arr[j]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: bool checkIfExist(vector<int>& arr) { sort(begin(arr), end(arr)); for(int i = 0; i < arr.size(); i++) { if(arr[i] == 0 and i + 1 < arr.size() and arr[i + 1] == 0) return true; else if(arr[i] != 0) { auto it = lower_bound(begin(arr), end(arr), arr[i] * 2); if(it != end(arr) and *it == arr[i] * 2) return true; } } return false; } };
|