simple c++ code using normal factorial  function with comments
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

// Function to compute factorial iteratively and prevent overflow
long long fact(int n) {
    long long result = 1;
    for (long long i = 2; i <= n; ++i) {
        result = result * i;
        if (result >= 1e16) return 1e16; // Cap at 1e16
    }
    return result;
}

class Solution {
public:
    vector<int> permute(int N, long long K) {
        vector<int> ans;
        vector<int> vals;
        
        // Fill vals with numbers from 1 to N
        for (int i = 1; i <= N; i++) {
            vals.push_back(i);
        }
        
        int want = 1; // Determines if we pick even or odd numbers
        
        for (int t = 0; t < N; t++) {
            long long ways = fact((N - t - 1) / 2);
            long long z = fact((N - t) / 2);
            
            // Check for overflow using logarithmic method
            if (ways > 0 && z > 0) {
                double logWays = log10(ways) + log10(z);
                if (logWays > 16) {
                    ways = 1e16;
                } else {
                    ways *= z;
                }
            }
            
            int i = 0;
            while (i < vals.size()) {
                if (vals[i] % 2 != want && (t > 0 || N % 2 == 1)) {
                    i++;
                    continue; // Skip numbers that don't match criteria
                }
                if (K <= ways) {
                    break; // Found the correct index
                }
                K -= ways; // Reduce K by skipping permutations
                i++;
            }
            
            if (i == vals.size()) return {}; // If no valid permutation found, return empty
            
            ans.push_back(vals[i]); // Add chosen element to answer
            vals.erase(vals.begin() + i); // Remove it from list
            
            want = ans.back() % 2 == 0 ? 1 : 0; // Flip want (even to odd or vice versa)
        }
        
        return ans;
    }
};

```
['C++']