Hi

Since the size of N <= 15 you can also use bitmasks and save space

class Solution {
public:
int countArrangement(int N) {
if(N <= 1)return N;
int ans = 0 , mask = 0;
countArrangementUtil(mask , ans , 1 ,N);
return ans;
}
void countArrangementUtil(const int mask , int& ans , const int idx ,const int N){
if(idx == N + 1){
ans++ ; return;
}
for(int i = 1; i <= N ; ++i){
int k = (mask & (1 << (i-1)));
if(k == 0 && (idx%i == 0 || i%idx == 0)){
countArrangementUtil(mask | (1 << (i-1)) , ans , idx + 1 , N);
}
}
}
};