I have tried code on GeeksforGeeks which is giving TLE error while submitting.
Here is the question:
Geek is given an array of nums of length n and two integers x and y. Geek is interested to find the total number of pairs (i,j) such that x <= a[i]*a[j] <= y (1<=i<j<=n).
Help the geek to find total number of such pairs.
Example 1:
Input: nums[] = {5,3,7,9,7,9,7,7},
x = 7, y = 19
Output: 1
Explanation: There is only one pair which
satisfies the given conditions. The pair is
(1,2).
Example 2:
Input: nums[] = {3,5,5,2,6},
x = 8, y = 13
Output: 3
Explanation: Pairs which satisfiy the given
conditions are (2,4), (3,4), (4,5).
Constraints:
1<=n<=10^4
1<=nums[i]<=10^4
1<=x<=y<=10^8
Here is my code:
class Solution:
def TotalPairs(self, nums, x, y):
count = 0
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if x <= nums[i]*nums[j] <= y:
count += 1
return count
#{
# Driver Code Starts
if __name__ == '__main__':
T=int(input())
for i in range(T):
n, x, y = map(int, input().split())
nums = list(map(int, input().split()))
obj = Solution()
ans = obj.TotalPairs(nums, x, y)
print(ans)
# } Driver Code Ends
Output after submission:
Runtime Error:
Runtime ErrorTime Limit Exceeded
Your program took more time than expected.
Expected Time Limit 7.60sec
Hint : Please optimize your code and submit again.