Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

624. Maximum Distance in Arrays

Medium45.7% Acceptance
ArrayGreedy
Asked by:
Y
Yahoo
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

You are given m arrays, where each array is sorted in ascending order.

You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a - b|.

Return the maximum distance.

Example 1:

Input: arrays = [[1,2,3],[4,5],[1,2,3]]
Output: 4
Explanation: One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

Example 2:

Input: arrays = [[1],[1]]
Output: 0

Constraints:

  • m == arrays.length
  • 2 <= m <= 105
  • 1 <= arrays[i].length <= 500
  • -104 <= arrays[i][j] <= 104
  • arrays[i] is sorted in ascending order.
  • There will be at most 105 integers in all the arrays.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #624 Maximum Distance in Arrays, each array is already sorted in ascending order, and the goal is to pick one element from two different arrays such that their absolute difference is maximized. A key observation is that the largest distance will likely involve the minimum value from one array and the maximum value from another.

An efficient greedy strategy iterates through the arrays while maintaining the global min and max values seen so far from previous arrays. For each new array, compute candidate distances using its first and last elements against the tracked global extremes. This ensures that elements always come from different arrays. After evaluating, update the running minimum and maximum accordingly.

This approach avoids comparing all pairs of arrays and works in linear time relative to the number of arrays while using constant extra space.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy tracking of global min and maxO(n)O(1)

Video Solution Available

Ashish Pratap Singh

View all video solutions

Solutions (12)

Using Minimum and Maximum Tracking

One simple yet effective way to tackle this problem is to track the minimum and maximum values as we iterate over the arrays, since each array is sorted in ascending order. We initialize the maximum distance as zero. For each array except the first one, calculate the possible distance using the current array's first element with the global maximum, and the current array's last element with the global minimum. Update the global minimum and maximum as you traverse each array. This approach ensures all distance calculations only consider meaningful pairs from different arrays and efficiently computes the result in a single pass.

Time Complexity: O(m) where m is the number of arrays. Space Complexity: O(1) as we are only using constant extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <math.h>
3
4int maxDistance(int** arrays, int arraysSize, int*


Explanation

This solution iterates over the given arrays while keeping track of the global minimum and maximum values seen so far. It utilizes these for calculating potential maximum distances between the current array's element boundaries and previous minima/maxima without redundant calculations.

Brute Force Comparison

A naive brute force approach would involve iterating over each pair of arrays and comparing all possible combinations of elements from both arrays. While this method ensures completeness of coverage for all potential maximum distance computations, it results in a less efficient O(m2 * n) complexity where n is the average number of elements in one array. It serves as a solid baseline comparison for the more efficient approach.

Time Complexity: O(m2 * n) where m is the number of arrays and n is the average number of elements in each array. Space Complexity: O(1), constant space aside from an output variable.

CC++JavaPythonC#JavaScript


Video Solutions

Watch expert explanations and walkthroughs

LeetCode was HARD until I Learned these 15 Patterns

Ashish Pratap Singh
13:001,002,131 views

Asked By Companies

1 companies
Y
Yahoo

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
Jump Game IIMedium
Jump GameMedium
Best Time to Buy and Sell Stock IIMedium
More similar problems

Related Topics

ArrayGreedy

Problem Stats

Acceptance Rate45.7%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Maximum Distance in Arrays asked in FAANG interviews?

Yes, variations of this problem appear in technical interviews at large tech companies. It tests understanding of greedy reasoning, array traversal, and optimizing comparisons across multiple datasets.

What data structure is best for Maximum Distance in Arrays?

No special data structure is required. Simple variables to store the running minimum and maximum values are enough, making the solution both space-efficient and easy to implement.

What is the optimal approach for Maximum Distance in Arrays?

The optimal approach uses a greedy strategy. Track the global minimum and maximum values from previously processed arrays and compare them with the current array's extremes to compute the maximum distance.

Why does the greedy approach work for Maximum Distance in Arrays?

Because each array is already sorted, the smallest and largest elements represent the extreme candidates for maximizing distance. Comparing these with previously seen global extremes guarantees the largest possible difference across different arrays.

arraysColSize
)
{
5
int
maxDist
=
0
;
6
int
minVal
=
arrays
[
0
]
[
0
]
;
7
int
maxVal
=
arrays
[
0
]
[
arraysColSize
[
0
]
-
1
]
;
8
9
for
(
int
i
=
1
;
i
<
arraysSize
;
i
++
)
{
10
int
first
=
arrays
[
i
]
[
0
]
;
11
int
last
=
arrays
[
i
]
[
arraysColSize
[
i
]
-
1
]
;
12
13
// Calculate distances and update maxDist
14
maxDist
=
fmax
(
maxDist
,
fabs
(
last
-
minVal
)
)
;
15
maxDist
=
fmax
(
maxDist
,
fabs
(
maxVal
-
first
)
)
;
16
17
// Update global min and max
18
minVal
=
fmin
(
minVal
,
first
)
;
19
maxVal
=
fmax
(
maxVal
,
last
)
;
20
}
21
return
maxDist
;
22
}
23
1
#
include
<stdlib.h>
2
#
include
<stdio.h>
3
4
int
max
(
int
a
,
int
b
)
{
return
a
>
b
?
a
:
b
;
}
5
int
abs
(
int
a
)
{
return
a
>
0
?
a
:
-
a
;
}
6
7
int
maxDistance
(
int
*
*
arrays
,
int
arraysSize
,
int
*
arraysColSize
)
{
8
int
maxDist
=
0
;
9
for
(
int
i
=
0
;
i
<
arraysSize
;
i
++
)
{
10
for
(
int
j
=
i
+
1
;
j
<
arraysSize
;
j
++
)
{
11
int
dist1
=
abs
(
arrays
[
i
]
[
0
]
-
arrays
[
j
]
[
arraysColSize
[
j
]
-
1
]
)
;
12
int
dist2
=
abs
(
arrays
[
j
]
[
0
]
-
arrays
[
i
]
[
arraysColSize
[
i
]
-
1
]
)
;
13
maxDist
=
max
(
maxDist
,
max
(
dist1
,
dist2
)
)
;
14
}
15
}
16
return
maxDist
;
17
}
18

Explanation

The brute force solution systematically checks distance calculations across every possible pair of separate arrays, albeit with lesser theoretical efficiency due to the intrinsic nested loop structure causing significant execution time overhead.