You are given n rectangles represented by a 0-indexed 2D integer array rectangles, where rectangles[i] = [widthi, heighti] denotes the width and height of the ith rectangle.

Two rectangles i and j (i &lt; j) are considered interchangeable if they have the same width-to-height ratio. More formally, two rectangles are interchangeable if widthi/heighti == widthj/heightj (using decimal division, not integer division).

Return the number of pairs of interchangeable rectangles in rectangles.

&nbsp;
Example 1:

Input: rectangles = [[4,8],[3,6],[10,20],[15,30]]
Output: 6
Explanation: The following are the interchangeable pairs of rectangles by index (0-indexed):
- Rectangle 0 with rectangle 1: 4/8 == 3/6.
- Rectangle 0 with rectangle 2: 4/8 == 10/20.
- Rectangle 0 with rectangle 3: 4/8 == 15/30.
- Rectangle 1 with rectangle 2: 3/6 == 10/20.
- Rectangle 1 with rectangle 3: 3/6 == 15/30.
- Rectangle 2 with rectangle 3: 10/20 == 15/30.


Example 2:

Input: rectangles = [[4,5],[7,8]]
Output: 0
Explanation: There are no interchangeable pairs of rectangles.


&nbsp;
Constraints:


	n == rectangles.length
	1 &lt;= n &lt;= 105
	rectangles[i].length == 2
	1 &lt;= widthi, heighti &lt;= 105


class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        ratios = defaultdict(int)
        for x, y in rectangles:
            ratios[x/y] += 1
        res = 0
        for val in ratios.values():
            res += (val*(val-1)//2)
        return res
class Solution {
    
    public long interchangeableRectangles(int[][] rectangles) {
        Map <Double, Long> hash = new HashMap<>();
        
        for (int i = 0; i < rectangles.length; i++) {
            Double tmp = (double) (rectangles[i][0] / (double) rectangles[i][1]);
            
            hash.put(tmp, hash.getOrDefault(tmp, 0L) + 1);
        }
        
        long ans = 0;
        for (Map.Entry<Double,Long> entry : hash.entrySet()) {
            if (entry.getValue() > 1) {
                Long n = entry.getValue();
                ans += (n * (n - 1)) / 2;
            }
        }
        
        return ans;
    }
}
class Solution {
public:
    long long interchangeableRectangles(vector<vector<int>>& rectangles) {
        int n = rectangles.size();
        unordered_map<double,int> mp;
        for(int i = 0;i<n;i++){
            double ratio = rectangles[i][0]/(double)rectangles[i][1];
            mp[ratio]++;
        }
        long long count = 0;

        for(auto i: mp){
            long long x = i.second;
            x = (x * (x-1))/2.0;
            count += x;
        }
        return count;
    }
};
/**
 * @param {number[][]} rectangles
 * @return {number}
 */
var interchangeableRectangles = function(rectangles) {
    let map = new Map();
    for (let i = 0; i < rectangles.length; i++) {
        let [a, b] = rectangles[i];
        let val = a / b;
        if (!map.get(val)) map.set(val, [0, 0]);
        else {
            const [count, total] = map.get(val);
            map.set(val, [count + 1, total + count + 1]);
        }
    }
    
    return [...map.entries()].reduce((prev, [key, [count, total]]) => {
        if (count < 1) return prev;
        return prev + total;
    }, 0);
};
Number of Pairs of Interchangeable Rectangles