1231. Beautiful Arrangement — Count Permutations

HardDynamic ProgrammingBitmask DP

Given an array nums of distinct positive integers, a permutation of nums is beautiful if for every 1-indexed position i the value placed there is divisible by i or divides i. Return the number of beautiful permutations. (With nums = [1, 2, ..., n] this is the classic Beautiful Arrangement count.) The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — the number of beautiful permutations.

Examples

Example 1
Input: {"nums":[1,2]}
Output: 2
Explanation: Both orders are beautiful.
Example 2
Input: {"nums":[1,2,3]}
Output: 3
Explanation: Three beautiful permutations.
Example 3
Input: {"nums":[1]}
Output: 1
Explanation: Single element.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →