Need help ! Wrong o/p for n >= 6.


  • 0
    V

    Please help me in finding a bug which gives wrong o/p for n >= 6

    class Solution {
    public:
        int countArrangement(int N) {
            vector<int> arrangement(N);
            int count = 0;
            for(int i=0; i<N; i++) {
                arrangement[i] = i+1;
            }
            countArrangementUtil(0, arrangement, count);
            return count;
        }
        
        void countArrangementUtil(int pos, vector<int> &arrangement, int &count) {
            if(pos >= arrangement.size()) {
                printArrangement("Arrangement", arrangement);
                count += 1;
                return;
            }
            countArrangementUtil(pos+1, arrangement, count);
            for(int i=pos+1; i<arrangement.size(); i++) {
                swap(arrangement[pos], arrangement[i]);
                if((arrangement[pos]%(pos+1) == 0 || (pos+1)%arrangement[pos] == 0) && (arrangement[i]%(i+1) == 0 || (i+1)%arrangement[i] == 0)) {
                    countArrangementUtil(pos+1, arrangement, count);
                }
                swap(arrangement[i], arrangement[pos]);
            }
        }
        
        void printArrangement(string str, vector<int> nums) {
            cout << str << endl;
            for(int i=0; i<nums.size(); i++) {
                cout << nums[i] << " ";
            }
            cout << endl;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.