Given a distinct integers array **arr[]** of size **N** and an integer **K**, the task is to count the number of subsets of size **K** of array whose product of elements can be represented as **a ^{2} – b^{2}**.

**Examples:**

Input:arr[] = {1, 2, 3} K = 2

Output:2

Explaination:

All possible subsets of length 2 with their products are given below:

{1, 2} = 2

{2, 3} = 6

{1, 3} = 3

Since, only 3 can be expressed as (2^{2}– 1^{2}, therefore only one such subset exists.

Input:arr[] = {2, 5, 6} K = 2

Output:2

Explaination:

All possible contiguous sub-sequences with their products given below:

{2, 5} = 10

{2, 6} = 12

{5, 6} = 30

Since, only 12 can be expressed as (4^{2}– 2^{2}), only one such subset exists.

**Approach:**

- Generate all subsets of size
**K**. - Calculate the products of all subsets.
- A number can be represented as the difference of square of two numbers only if it is
**odd or divisible by 4**. - Hence, count all subsets with with product that satisfies this condition.

Below is the implementation of the above approach:

## Python3

`# Python3 implementation of the approach ` ` ` `import` `itertools ` ` ` `# Function to return the ` `# Count of required sub-sequences ` `def` `count_seq(arr, n, k): ` ` ` ` ` `# ans is Count variable ` ` ` `ans ` `=` `0` ` ` ` ` `for` `seq ` `in` `itertools.combinations(arr, k): ` ` ` ` ` `# product of seq ` ` ` `pro ` `=` `1` ` ` ` ` `for` `ele ` `in` `seq: ` ` ` `pro ` `*` `=` `ele ` ` ` ` ` `# checking form of a2-b2 ` ` ` `if` `((pro ` `%` `4` `) !` `=` `2` `): ` ` ` `ans ` `+` `=` `1` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[` `2` `, ` `5` `, ` `6` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `2` ` ` `print` `(count_seq(arr, n, k)) ` |

*chevron_right*

*filter_none*

**Output:**

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Count number less than N which are product of perfect squares
- Count elements in an Array that can be represented as difference of two perfect squares
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Number of perfect squares between two given numbers
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Count squares of size K inscribed in a square of size N
- Product of all sorted subsets of size K using elements whose index divide K completely
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Find two numbers such that difference of their squares equal to N
- Sum of squares of all Subsets of given Array
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Largest sub-array whose all elements are perfect squares
- Sort perfect squares in an array at their relative positions
- Check if the sum of perfect squares in an array is divisible by x
- Smallest and Largest N-digit perfect squares
- Print all perfect squares from the given range

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.