C++ Solution with Very Detailed Explanation
For each ith index, we have two options - either swap or do not swap them. We have to calculate the moves for both of them until the last index.\n1. We have ini
For each ith index, we have two options - either swap or do not swap them. We have to calculate the moves for both of them until the last index.\n1. We have initialized two arrays with MAX value, which is N as we are going to calculate minimum.\n2. For index 0, if we swap then swap[0] = 1 and if we do not swap then noswap[0] =0\n3. For index 1 and all other indices, we check two conditions and determine the values of noswap[i] and swap[i]:\n4. In a normal scenario(First If Condition, either swap them both or don\'t), where the given two numbers in both the arrays are sorted i.e. A[i-1]<A[i] and B[i-1]<B[i]. Our goal is to keep them sorted. However we calculate the noswap[1] and swap[1] like this:\n\ta. If we have swapped A[0] and B[0] and we are also swapping A[1] and B[1] then swap[1] will be count of previous swaps plus 1. i.e. swap[i] = swap[i-1]+1;\n\tb. Similarly, if we did not swap A[0] and B[0] and we are also not swapping A[1] and B[1] so moves remain same as previous noswap[i] = noswap[i-1];\n\tc. The key point here is either swap A[0] and A[1] elements with B[0] and B[1] or do not swap them both. We cannot choose either or here because when you think about it, two elements are sorted in A and two elements are sorted in B but we don\'t know if swapping one of them alone will hold this property true. That\'s why we have to swap them both or don\'t.\n5. Second if condition, swap either one of them i.e 0th or 1st when the condition satisfies.\n\t  a. For example, if we swap A[0] and B[0] then we have to check if B[0] < A[1] and similarly A[0]<B[1] because thats how its gonna be after swapping and they have to be strictly increasing.\n\t  b. If we swap A[1] with B[1] then we cannot swap A[0] with B[0](we have already considered that scenario in the first If condition). Now we try this scenario of swapping only one of them either A[0] or A[1]. So if we swap A[1] and B[1], then swap[1] = min(swap[1],noswap[0]+1), noswap[0]+1 is because ony A[1] is swapped so +1 with noswap[0] as A[0] is not swapped.\n\t  c. Similarly, if we do not swap A[1] and B[1] then noswap[1] = min(noswap[1],swap[0]) we are doing +1 because current element is not swapped. \n6. Finally, the minimum of swap[n-1] and noswap[n-1] is the answer.\nHope it is clear !\n\n```\nclass Solution {\npublic:\n    int minSwap(vector<int>& A, vector<int>& B) \n    {\n        int n = A.size();\n        vector<int> swap(n,n);\n        vector<int> noswap(n,n);\n        swap[0] = 1;\n        noswap[0] = 0;\n        for(int i=1;i<n;i++)\n        {\n           if(A[i-1]<A[i] and B[i-1]<B[i])\n           {\n               swap[i] = swap[i-1]+1;\n               noswap[i] = noswap[i-1];\n           }\n           if(A[i-1]<B[i] and B[i-1]<A[i])\n           {\n               swap[i] = min(noswap[i-1]+1,swap[i]);\n               noswap[i] = min(swap[i-1],noswap[i]);\n           }\n        }\n        return min(swap[n-1],noswap[n-1]);\n    }\n};\n\n\n```
[]