C++/Java || DP || With Detailed Approach ||  Beginner friendly
Approach\nThe approach uses dynamic programming with a 2D matrix, dp, to store the lengths of non-decreasing subsequences.\n\nThe algorithm iterates through the
# Approach\nThe approach uses dynamic programming with a 2D matrix, dp, to store the lengths of non-decreasing subsequences.\n\nThe algorithm iterates through the arrays, comparing elements at each position to determine if they can be included in a non-decreasing subsequence. If an element in nums2 is greater than or equal to the previous element, the length of the subsequence is incremented in dp[i][1]. Similarly, if an element in nums1 is greater than or equal to the previous element, the length of the subsequence is incremented in dp[i][0].\n\nAfter iterating through the arrays, the maximum length of the non-decreasing subsequences is obtained by comparing the values stored in dp[i][0] and dp[i][1]. The final result is the maximum among the initial value ans and the values in dp[i][0] and dp[i][1]. This maximum length is returned as the solution.\n\n# Java Code\n```\nclass Solution {\n    public int maxNonDecreasingLength(int[] nums1, int[] nums2) {\n        int n = nums1.length;\n        int[][] dp = new int[n][2];\n        dp[0][0] = 1;\n        dp[0][1] = 1;\n        int maxLen = 1;\n        for(int i=1;i<n;i++){\n            dp[i][0] = 1;\n            dp[i][1] = 1;\n            if (nums1[i] >= nums1[i - 1])\n                dp[i][0] = dp[i - 1][0] + 1;\n            if (nums1[i] >= nums2[i - 1])\n                dp[i][0] = Math.max(dp[i][0], dp[i - 1][1] + 1);\n        }\n\n        for (int i = 1; i < n; i++) {\n\n            if (nums2[i] >= nums2[i - 1])\n                dp[i][1] = dp[i - 1][1] + 1;\n            if (nums2[i] >= nums1[i - 1])\n                dp[i][1] = Math.max(dp[i][1], dp[i - 1][0] + 1);\n            \n            maxLen = Math.max(maxLen, Math.max(dp[i][0], dp[i][1]));\n        }\n        return maxLen;\n    }\n}\n```\n# C++ code\n```\nclass Solution {\npublic:\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int n = nums1.size();\n        vector<vector<int>> dp(n+1,vector<int>(2,1));\n        int ans = 1;\n        if(n==1) return ans;\n\n        for(int i=1;i<n;i++){\n            if (nums2[i] >= nums2[i - 1])\n                dp[i][1] = dp[i - 1][1] + 1;\n            if (nums2[i] >= nums1[i - 1])\n                dp[i][1] = max(dp[i][1], dp[i - 1][0] + 1); \n            if (nums1[i] >= nums1[i - 1])\n                dp[i][0] = dp[i - 1][0] + 1;\n            if (nums1[i] >= nums2[i - 1])\n                dp[i][0] = max(dp[i][0], dp[i - 1][1] + 1);\n        }\n        \n        for(int i=0;i<n;i++){\n            ans = max({ans,dp[i][0],dp[i][1]});\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C++', 'Java']