Suppose we have a sequence nums of size n. We have to find the maximum size of subsequence of nums in which every pair (p, q) is a nice pair? A pait is said to be nice pair if and only if it holds at least one of these conditions: 1. The parity of the number of distinct prime divisors of p is equal to that of b. For example, the value 18 has two distinct prime divisors: 2 and 3. 2. The parity of the sum of all positive divisors of p is same as q.
So, if the input is like nums = [2,3,6,8], then the output will be 3
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(nums): n = len(nums) cnt = [] total = [] result = [] for i in nums: count = 0 tot = 0 prime = [] for j in nums: if all(j % k for k in range(2, j)) == True: prime.append(j) for j in prime: if i % j == 0: count += 1 if count % 2: cnt.append('odd') else: cnt.append('even') for j in range(1,i+1): if i % j == 0: tot += j if tot % 2: total.append('odd') else: total.append('even') for i in range(n-1): for j in range(i+1, n): if cnt[i] == cnt[j] or total[i] == total[j]: result.append(nums[i]) if j == n-1: result.append(nums[j]) result = list(set(result)) return len(result) nums = [2,3,6,8] print(solve(nums))
15, 3, 8
3