SHARECHAT Coding Question – Solved

7 Live
Task Calculate the number of pairs of investment (i, j) such that the return is a perfect square. Example 1 Assumptions Input N = 4 Output: 6 Approach (1, 1), (1, 4), (2, 2), (3, 3), (4, 1), and (4, 4) are the possible pairs. Square Investment Bob and John both have N dollars each and want to invest some money in Jack. They will invest amounts i, j (1 ≤ i, j ≤ N). Jack has promised them a return of i * j. Bob and John will be happy if the return is a perfect square. Function Description Complete the function solution(). The function takes the following parameter and returns the solution: - N: Represents the total amount. Input Format for Custom Testing Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code. - The first linecontains N, denoting the total amount. Output Format Return an integer representing the number of possible pairs.

Asked in: SHARECHAT

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


import math
from collections import defaultdict

def square_free_factorization(x):
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
This problem requires counting the number of pairs (i, j) with 1 ≤ i, j ≤ N such that the product i * j is a perfect square. The challenge is to efficiently identify these pairs without explicitly checking all N^2 pairs, which would be impractical for large N.

---

### Understanding the problem deeply

We are given an integer N, and for each pair (i, j) where i and j are integers between 1 and N inclusive, we want to check if the product i * j is a perfect square number. We need to count all such pairs.

A perfect square is a number whose prime factorization contains even powers of all primes. For example, 36 = 2^2 * 3^2 is a perfect square, but 18 = 2^1 * 3^2 is not because 2 has an odd power.

---

### Step 1: Naive approach

The simplest approach is to:
- Iterate over all pairs (i, j).
- Compute product = i * j.
- Check if product is a perfect square by computing its integer square root and squaring it back to verify equality.
- Count the pairs where this holds true.

**Drawbacks:**
- This brute force approach requires O(N^2) operations, which is inefficient for large N (e.g., 10^5 or higher).
- We need a more mathematical insight to reduce complexity.

---

### Step 2: Mathematical insight on perfect squares and factorization

Since the problem revolves around the product i * j being a perfect square, let's look at the prime factorization properties.

- Each number i can be expressed uniquely as a product of primes raised to certain powers: i = p1^a1 * p2^a2 * ... * pk^ak.
- For i * j to be a perfect square, the combined powers of each prime in i and j must be even.
- If i's factorization is known, j must "complement" i such that when their exponents are summed, the resulting exponent for every prime is even.

---

### Step 3: Represent numbers by their "square-free" part

A key insight is to focus on the "square-free" part of a number. The square-free part of an integer is the product of all primes in its factorization that have an odd exponent.

For example:
- 12 = 2^2 * 3^1 → square-free part = 3 (since exponent of 3 is odd)
- 18 = 2^1 * 3^2 → square-free part = 2 (since exponent of 2 is odd)

Two numbers i and j will multiply to a perfect square if and only if their square-free parts are the same.

Why?
Because if the square-free parts of i and j are the same, multiplying them results in exponents being even for all primes.

---

### Step 4: Group numbers by their square-free parts

- For all integers from 1 to N, compute their square-free part.
- Count how many numbers share the same square-free part.
- If a group contains M numbers, any pair (i, j) from this group will have i * j as a perfect square.

The count of pairs in that group will be M^2 because pairs include (i, j) and (j, i), as well as (i, i).

---

### Step 5: Calculating the total count

- Sum over all groups: M^2 where M is the size of the group.

This sum gives the total number of pairs (i, j) such that i * j is a perfect square.

---

### Step 6: Efficiently computing square-free parts

To implement this:

- Use a sieve-like approach to find the smallest prime factor (SPF) for every number from 1 to N.
- Using SPF, factorize each number in O(log N) time.
- For each prime factorization, remove the even powers by dividing out squared primes until only odd exponents remain.
- The remaining product of primes raised to odd exponents is the square-free part.

This preprocessing allows grouping numbers by their square-free parts efficiently.

---

### Step 7: Handling large inputs

- The sieve for SPF calculation runs in O(N log log N), feasible for large N.
- Grouping and summing M^2 over groups can be done in O(N).
- The approach avoids any direct O(N^2) computation.

---

### Step 8: Final thoughts and edge cases

- Square-free part of 1 is 1 (no prime factors), so it forms its own group.
- All perfect squares have square-free part 1, so all perfect squares form one group.
- If N=1, the answer is 1 since (1,1) gives product 1 which is a perfect square.
- Confirm that counting pairs includes (i,i).

---

### Summary of approach:

1. Precompute smallest prime factors for all numbers up to N.
2. For each number, compute its square-free part by removing squares of primes from its factorization.
3. Count how many numbers share the same square-free part.
4. The number of valid pairs is the sum of squares of these counts.
5. Return the total count.

By transforming the problem from a brute force check of all pairs to grouping numbers by a mathematical property (square-free part), the problem becomes tractable and efficient.
```


Related Questions