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

LeetCode 1848 | Minimum Distance to the Target Element | Java | Easy | Array | CodeByTushu

Video information, playback and saved actions.

LeetCode 1848 | Minimum Distance to the Target Element | Java | Easy | Array | CodeByTushu

Description: πŸš€ Welcome Back to CodeByTushu!

In today’s video, we solve LeetCode 1848 β€” Minimum Distance to the Target Element using Array and Linear Search in Java.

We are given an integer array nums, an integer target that is guaranteed to appear in nums, and a starting index start. Our objective is to find a valid index i where nums[i] equals target such that the absolute distance abs(i - start) is minimized. Since the array length is at most 1000, we can directly scan through the array to check each element matching the target value.

We maintain a running minimum distance initialized to the maximum possible integer value. Whenever nums[i] matches target, we calculate the absolute difference between i and start, updating our minimum distance. As an immediate optimization, if the minimum distance ever reaches zero, meaning the target resides exactly at start, we terminate early and return zero. This approach guarantees an optimal O(N) runtime with O(1) auxiliary space.

πŸ”₯ Problem Highlights

βœ… Direct linear traversal to identify matching target values
βœ… Absolute distance minimization using abs(i - start)
βœ… Early exit optimization when distance equals zero
βœ… Constant auxiliary space utilization
βœ… Clean and intuitive edge-case handling
βœ… Optimal O(N) time and O(1) auxiliary space complexity

⏱️ TIMESTAMPS

00:00 Intro & Daily Challenge Kickoff (Streak 98 Days)
01:25 Problem Understanding & Constraints
02:58 Linear Traversal & Zero Distance Early Exit
04:21 Concise Single-Pass Algorithm & Optimal Flow
05:31 Clean Java Solution Walkthrough Line by Line
06:32 Test Cases Dry Run & Benchmark Verification (0 ms, Beats 100%)
07:41 Time Complexity Analysis (O(N) Linear Time)
08:31 Space Complexity Analysis (O(1) Constant Space - 0 Bytes)
09:03 Day 13 Wrap-up & Channel Resources

πŸ“‚ Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/April/13-April/1848-Minimum-Distance-to-the-Target-Element

β˜• Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/13-April/1848-Minimum-Distance-to-the-Target-Element/Solution.java

🐍 Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/13-April/1848-Minimum-Distance-to-the-Target-Element/solution.py

⚑ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/13-April/1848-Minimum-Distance-to-the-Target-Element/solution.cpp

🌐 JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/13-April/1848-Minimum-Distance-to-the-Target-Element/solution.js

πŸ”— LeetCode Source Code

πŸ“Œ Admin type the link

🌐 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 Traversal
Linear Search
Distance Optimization
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 condition allows us to terminate our search immediately with an optimal answer?

1️⃣ When nums[i] is greater than target
2️⃣ When abs(i - start) == 0 (target is at the start index)
3️⃣ When we reach the middle index of the array
4️⃣ When target appears more than once

#LeetCode1848 #LeetCode #MinimumDistanceToTheTargetElement #Java #JavaDSA #Array #LinearSearch #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu

πŸ‘‡ Comment the answer below! πŸ”₯Β Close description
Β Β Β Β Β Β 
Videos related to "LeetCode 1848 | Minimum Distance to the Target Element | Java | Easy | Array | CodeByTushu"