Easy C++| Detailed Explanation | 2 Pointers
Problem Definition with Constraints :\n\nBoth the arrays are Non-Increasing.\nWe need to define a solution to find the  Maximum distance j-i while maintaining t
**Problem Definition with Constraints** :\n\nBoth the arrays are **Non-Increasing**.\nWe need to define a solution to find the  **Maximum** distance `j-i` while maintaining the below 2 constraints.\n* i <= j\n* nums1[i] <= nums2[j]\n\n\n**Solution with Example** :\n \n```\nclass Solution {\npublic:\n    int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n      int i=0, j=0;\n      int dis=0;\n      while(true) {\n        \n\t\t//  No more elements are left to process.\n        if(i==nums1.size()||j==nums2.size()) break;\n         \n\t\t//  If the condition is not, then increment both.\n        if(nums2[j]<nums1[i] ) {\n          i++; \n          j++;\n        } else {\n\t\t  // Calculate the current distance and try incrementing j to maximise the distance. \n          dis = max(dis, j++ - i);\n        }\n        \n      }\n      return dis;\n    }\n};\n```\n\n Consider the following 2 arrays.\n\n```\n[70,60,50,40,30,20,10]\n[80,77,74,71,68,65,62]\n```\n\n1. `i=0, j=0`\n`80>70, dist is j-i = 0, max = 0`\nTry incrementing j to maximise the distance. \n\n2. `i=0, j=1`\n`77>70, dist is j-i = 1, max = 1`\nTry incrementing j to maximise the distance. \n\n3. ` i=0, j=2`\n`74>70, dist is j-i = 2, max = 2`\nTry incrementing j to maximise the distance. \n\n4. `i=0, j=3`\n`71>70, dist is j-i = 3, max = 3`\nTry incrementing j to maximise the distance. \n\n5. `i=0, j=4`\n`68<70, max = 3`\nIncrement i and j. \n**Why j ? Cause we are trying to maintain this maximum. Incrementing i alone will anyways decrease the maximum only (4-1 < 5-1) .**\n\n6. `i=1, j=5`\n `60<65, dist is j-i = 4, max = 4`\nTry incrementing j to maximise the distance. \n7. ` i=1, j=6`\n`60<62, dist is j-i = 5, max = 5`\n\nReturn the maximum value of `5`.\n\n**Complexity**:\n\nTime: `O(maximum(nums1.size() , nums2.size())) `\nSpace: `O(1)`\nPl upvote if this was useful :).
['Two Pointers', 'C']