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
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




