Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 1385 | Find the Distance Value Between Two Arrays | Java | Easy | Array | CodeByTushu

Video information, playback and saved actions.

LeetCode 1385 | Find the Distance Value Between Two Arrays | Java | Easy | Array | CodeByTushu

Description: πŸš€ Welcome Back to CodeByTushu!

In today’s video, we solve LeetCode 1385 β€” Find the Distance Value Between Two Arrays using Array & Two Pointers in Java.

In this problem, we are given two integer arrays arr1 and arr2 along with an integer threshold d. We are asked to compute the distance value between the two arrays, which represents the count of elements in arr1 for which no element in arr2 has an absolute difference less than or equal to d. Geometrically and algebraically, an element a from arr1 is valid if and only if no value in arr2 falls inside the closed interval [a - d, a + d].

We implement an optimal linear scan paired with early-exit pruning. For each element in arr1, we check elements of arr2, immediately breaking the inner loop upon finding the first violating element where |a - b| is less than or equal to d. This optimization drastically reduces actual operations in practice, achieving high performance with O(N * M) worst-case time complexity, near-linear average time, and strictly O(1) constant auxiliary space.

πŸ”₯ Problem Highlights

βœ… Interval exclusion principle: no element in arr2 can lie within [a - d, a + d]
βœ… Early-exit pruning breaks the inner comparison loop on the first threshold violation
βœ… Direct in-place evaluation with zero extra memory allocations
βœ… Simple and cache-friendly comparison pattern
βœ… Handles negative values and edge thresholds seamlessly
βœ… O(N * M) worst-case time complexity and O(1) constant auxiliary space

⏱️ TIMESTAMPS

00:00 Intro & Day 103 Streak Celebration
00:32 Problem Understanding & Constraints Analysis
01:21 Key Breakthrough: The Interval Exclusion Principle
02:04 Step-by-Step Algorithm & Early Exit Pruning
02:48 Optimal Java 17 Solution Walkthrough Line by Line
03:37 Test Cases Walkthrough & Day 103 Streak Celebration
04:10 Time Complexity Analysis (O(N * M) Worst, O(1) Pruned)
04:44 Space Complexity Analysis (O(1) Constant Auxiliary Space)
05:04 Summary & Outro!

πŸ“‚ Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/05-November/1385-Find-the-Distance-Value-Between-Two-Arrays

β˜• Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/05-November/1385-Find-the-Distance-Value-Between-Two-Arrays/Solution.java

🐍 Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/05-November/1385-Find-the-Distance-Value-Between-Two-Arrays/solution.py

⚑ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/05-November/1385-Find-the-Distance-Value-Between-Two-Arrays/solution.cpp

🌐 JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/05-November/1385-Find-the-Distance-Value-Between-Two-Arrays/solution.js

πŸ”— LeetCode Source Code

πŸ“Œ https://codebytushu.com/leetcode/problem/find-the-distance-value-between-two-arrays

🌐 All LeetCode Solutions

πŸ“Œ https://codebytushu.com/Leetcode/

πŸ“± Join CodeByTushu on WhatsApp

Get coding updates, LeetCode solutions, daily questions and more.

https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40

❀️ Donate

πŸ“Œ https://codebytushu.com/Leetcode/donate.php

πŸ”₯ Perfect For

LeetCode Easy Problems
Java DSA Preparation
Array Manipulation
Two Pointers Technique
Binary Search Applications
Time Complexity Optimization
Coding Interview Preparation
Problem Solving

🌐 Follow CodeByTushu

▢️ YouTube: https://www.youtube.com/@codebytushu

πŸ“Έ Instagram: https://instagram.com/codebytushu/

πŸ’Ό LinkedIn: https://www.linkedin.com/company/codebytushu/

🌍 Website: https://codebytushu.com

πŸ™ GitHub: https://github.com/CodeByTushu

❀️ Don’t Forget To

πŸ‘ Like | πŸ’¬ Comment | πŸ”” Subscribe

πŸ’¬ Question of the Day

What is the condition on element b in arr2 that causes an element a in arr1 to be disqualified from the distance count?

1️⃣ b lies within the closed interval [a - d, a + d]
2️⃣ b is strictly greater than a + d
3️⃣ b is strictly less than a - d
4️⃣ b equals a

#LeetCode1385 #LeetCode #FindTheDistanceValueBetweenTwoArrays #Java #JavaDSA #Array #TwoPointers #BinarySearch #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu

πŸ‘‡ Comment the answer below! πŸ”₯Β Close description
Β Β Β Β Β Β 
Videos related to "LeetCode 1385 | Find the Distance Value Between Two Arrays | Java | Easy | Array | CodeByTushu"

Related videos are temporarily unavailable.

Try again, or come back later.