Given an array of integers ‘A’. You need to find two integers, ‘P’ and ‘Q’ in ‘A’, such that P = 2*Q. (i.e., P is double of Q).
For Example:If A = [2, 5, 7, 4, 9], then P = 2*Q can hold true for P = 4 and Q = 2. Which are at the 0-th index and 3rd index respectively.
The first line contains a single integer ‘T’, denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of integers in the array.
The second line of each test case contains ‘N’ space-separated integers that make ‘A’.
Output Format:
For each test case, print “True”, if there exist two integers in the array as described in the problem statement. Otherwise, print “False”.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
-10^9 <= A[i] <= 10^9
Where A[i] is the integer at index ‘i’.
Time limit: 1 sec
2
4
6 13 8 5
5
4 9 2 1 7
False
True
Test Case 1: In the given array, there are no two integers, ‘P’ and ‘Q’, such that P = 2*Q.
Test Case 2: ‘2’ is double of ‘1’. So for P = 2, Q = 1. P = 2*Q.
2
6
10 12 5 20 0 11
3
2 2 2
True
False
Think of iterating through the array for every integer in ‘A’.
The simple idea is that for every integer in ‘A’, traverse the complete array and check if there exists an integer that is double of it. If for any integer, it double exists, we will return true, otherwise false.
Below are the steps:
O(N^2), where ‘N’ is the number of integers in the array.
For every element in ‘A’, we are iterating through the complete array. Therefore the time complexity is O(N^2).
O(1)
No extra space is used. So the space complexity is O(1).