All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0901_1000.s0996_number_of_squareful_arrays.Solution Maven / Gradle / Ivy

package g0901_1000.s0996_number_of_squareful_arrays;

// #Hard #Array #Dynamic_Programming #Math #Bit_Manipulation #Backtracking #Bitmask
// #2022_03_31_Time_2_ms_(72.32%)_Space_41.9_MB_(32.14%)

import java.util.HashSet;
import java.util.Set;

/**
 * 996 - Number of Squareful Arrays\.
 *
 * Hard
 *
 * An array is **squareful** if the sum of every pair of adjacent elements is a **perfect square**.
 *
 * Given an integer array nums, return _the number of permutations of_ `nums` _that are **squareful**_.
 *
 * Two permutations `perm1` and `perm2` are different if there is some index `i` such that `perm1[i] != perm2[i]`.
 *
 * **Example 1:**
 *
 * **Input:** nums = [1,17,8]
 *
 * **Output:** 2
 *
 * **Explanation:** [1,8,17] and [17,8,1] are the valid permutations. 
 *
 * **Example 2:**
 *
 * **Input:** nums = [2,2,2]
 *
 * **Output:** 1 
 *
 * **Constraints:**
 *
 * *   `1 <= nums.length <= 12`
 * *   0 <= nums[i] <= 109
**/
public class Solution {
    int count;

    public int numSquarefulPerms(int[] nums) {
        int n = nums.length;
        if (n < 2) {
            return count;
        }
        backtrack(nums, n, 0);
        return count;
    }

    private void backtrack(int[] nums, int n, int start) {
        if (start == n) {
            count++;
        }
        Set set = new HashSet<>();
        for (int i = start; i < n; i++) {
            if (set.contains(nums[i])) {
                continue;
            }
            swap(nums, start, i);
            if (start == 0 || isPerfectSq(nums[start], nums[start - 1])) {
                backtrack(nums, n, start + 1);
            }
            swap(nums, start, i);
            set.add(nums[i]);
        }
    }

    private void swap(int[] array, int a, int b) {
        int temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }

    private boolean isPerfectSq(int a, int b) {
        int x = a + b;
        double sqrt = Math.sqrt(x);
        return sqrt - (int) sqrt == 0;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy