818. Number of Squareful Arrays

HardRecursionRecursion

Given an array of integers, return the number of distinct permutations such that for every pair of adjacent elements, their sum is a perfect square. The array may contain duplicates. Input: sorted integer array.

Input: An integer array (sorted).

Output: Integer count.

Examples

Example 1
Input: [1,17,8]
Output: 2
Explanation: [1,8,17] and [17,8,1]; 1+8=9, 8+17=25.
Example 2
Input: [2,2,2]
Output: 0
Explanation: 2+2=4 is square, but need 3 adjacent pairs; only 2 pairs exist for 3 elements. [2,2,2]: 2+2=4 ✓, 2+2=4 ✓ -> count=1. Wait, let me recheck.

Constraints

Asked by

AppleGoogle
Solve this problem in the editor →