- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of pairs where elements square is within the given range in Python

Suppose we have two list of numbers nums1 and nums2. And also have two numbers lower and upper. We have to find the number of pairs (i, j) such that lower ≤ nums1[i]^2 + nums2[j]^2 ≤ upper.

So, if the input is like nums1 = [5, 3, 2] nums2 = [8, 12, 6] lower = 10 upper = 50, then the output will be 2 because the pairs are like (1, 2) and (2, 2)

- 10 <= 3^2 + 6^2 << 50 = 10 <= 45 << 50
- 10 <= 2^2 + 6^2 << 50 = 10 <= 40 << 50

To solve this, we will follow these steps −

- replace each element by its square in nums1
- replace each element by its square in nums2
- n := size of nums1
- m := size of nums2
- if n > m , then
- swap nums1 and nums2
- swap n and m

- nums2 := sort the list nums2
- res := 0
- for each e1 in nums1, do
- st := left most position to insert (lower - e1) into nums2 so that elements are sorted
- en := right most position to insert (upper - e1) into nums2 so that elements are sorted
- count := en - st
- res := res + count

- return res

## Example

Let us see the following implementation to get better understanding −

from bisect import bisect_left, bisect_right def solve(nums1, nums2, lower, upper): nums1 = [i * i for i in nums1] nums2 = [i * i for i in nums2] n, m = len(nums1), len(nums2) if n > m: nums1, nums2 = nums2, nums1 n, m = m, n nums2 = sorted(nums2) res = 0 for e1 in nums1: st = bisect_left(nums2, lower - e1) en = bisect_right(nums2, upper - e1) count = en - st res += count return res nums1 = [5, 3, 2] nums2 = [8, 12, 6] lower = 10 upper = 50 print(solve(nums1, nums2, lower, upper))

## Input

[5, 3, 2], [8, 12, 6], 10, 50

## Output

2

- Related Questions & Answers
- Python Program to replace list elements within a range with a given number
- Python - Find the number of prime numbers within a given range of numbers
- C++ program to find number in given range where each digit is distinct
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Find elements within range in numpy in Python
- Program to find number of ways where square of number is equal to product of two numbers in Python
- PHP program to find the sum of odd numbers within a given range
- Python Program for Number of elements with odd factors in the given range
- Java program to find the square root of a given number
- Program to find number of good pairs in Python
- Program to find out the number of special numbers in a given range in Python
- How to find Kaprekar numbers within a given range using Python?
- C++ program to find the side of the Octagon inscribed within the square
- Program to find bitwise AND of range of numbers in given range in Python
- C++ Program to find pairs of sequences where sequence holds minimum and maximum elements

Advertisements