1855. Maximum Distance Between a Pair of Values LeetCode Solution
In this guide, you will get 1855. Maximum Distance Between a Pair of Values LeetCode Solution with the best time and space complexity. The solution to Maximum Distance Between a Pair of Values problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Distance Between a Pair of Values solution in C++
- Maximum Distance Between a Pair of Values solution in Java
- Maximum Distance Between a Pair of Values solution in Python
- Additional Resources

Problem Statement of Maximum Distance Between a Pair of Values
You are given two non-increasing 0-indexed integer arrays nums1 and nums2.
A pair of indices (i, j), where 0 <= i < nums1.length and 0 <= j < nums2.length, is valid if both i <= j and nums1[i] = arr[i] for every 1 <= i < arr.length.
Example 1:
Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
Output: 2
Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).
The maximum distance is 2 with pair (2,4).
Example 2:
Input: nums1 = [2,2,2], nums2 = [10,10,1]
Output: 1
Explanation: The valid pairs are (0,0), (0,1), and (1,1).
The maximum distance is 1 with pair (0,1).
Example 3:
Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
Output: 2
Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).
The maximum distance is 2 with pair (2,4).
Constraints:
1 <= nums1.length, nums2.length <= 105
1 <= nums1[i], nums2[j] <= 105
Both nums1 and nums2 are non-increasing.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1855. Maximum Distance Between a Pair of Values LeetCode Solution in C++
class Solution {
public:
int maxDistance(vector<int>& nums1, vector<int>& nums2) {
int ans = 0;
int i = 0;
int j = 0;
while (i < nums1.size() && j < nums2.size())
if (nums1[i] > nums2[j])
++i;
else
ans = max(ans, j++ - i);
return ans;
}
};
/* code provided by PROGIEZ */
1855. Maximum Distance Between a Pair of Values LeetCode Solution in Java
class Solution {
public int maxDistance(int[] nums1, int[] nums2) {
int ans = 0;
int i = 0;
int j = 0;
while (i < nums1.length && j < nums2.length)
if (nums1[i] > nums2[j])
++i;
else
ans = Math.max(ans, j++ - i);
return ans;
}
}
// code provided by PROGIEZ
1855. Maximum Distance Between a Pair of Values LeetCode Solution in Python
class Solution:
def maxDistance(self, nums1: list[int], nums2: list[int]) -> int:
ans = 0
i = 0
j = 0
while i < len(nums1) and j < len(nums2):
if nums1[i] > nums2[j]:
i += 1
else:
ans = max(ans, j - i)
j += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.