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

LeetCode 3741 | Minimum Distance Between Three Equal Elements II | Java | Medium | CodeByTushu

Video information, playback and saved actions.

LeetCode 3741 | Minimum Distance Between Three Equal Elements II | Java | Medium | CodeByTushu

Description: πŸš€ Welcome Back to CodeByTushu!

In today’s video, we solve LeetCode 3741 β€” Minimum Distance Between Three Equal Elements II using Arrays and Previous Index Tracking in Java.

We are given an integer array nums containing up to 100,000 elements, where every value is between 1 and n. We need to find three distinct indices i, j, and k such that all three elements are equal and their total pairwise distance is minimum. If no value appears at least three times, we return -1.

For sorted indices i less than j and j less than k, the distance formula simplifies to 2 * (k - i). This means the middle index does not affect the result. Therefore, we only need to check consecutive groups of three occurrences for each value.

Since every array value is between 1 and n, we can use two fixed-size integer arrays to store the two most recent positions of each value. Whenever a new occurrence appears, we calculate the distance using the previous two positions and update the minimum answer.

This approach avoids Hash Map overhead and achieves O(n) time complexity with O(n) auxiliary space.

πŸ”₯ Problem Highlights

βœ… Algebraic Distance Formula: Simplifies the total distance to 2 * (k - i)

βœ… Middle Index Elimination: Shows why the middle index does not affect the final distance

βœ… Consecutive Triplet Check: Evaluates consecutive occurrences to find the minimum index span

βœ… Previous Index Tracking: Stores the two most recent positions using fixed-size arrays

βœ… Memory Optimization: Avoids Hash Map overhead with direct array indexing

βœ… Optimal Linear Complexity: Achieves O(n) time and O(n) auxiliary space

⏱️ TIMESTAMPS

00:00 Intro & Daily Challenge Kickoff (Streak 98 Days)
01:52 Problem Understanding & Distance Definition
03:27 Algebraic Simplification & Scale Scaling (10^5)
05:18 Direct Addressing Arrays & Sliding Triplet State
07:08 Clean Java Solution Walkthrough Line by Line
08:36 Test Cases Dry Run & Benchmark Verification (1 ms, Beats 100%)
10:22 Time Complexity Analysis (O(N) Linear Time)
11:25 Space Complexity Analysis (O(N) Flat Arrays - 800 KB)
12:16 Day 11 Wrap-up & Channel Resources

πŸ“‚ Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/April/11-April/3741-Minimum-Distance-Between-Three-Equal-Elements-II

β˜• Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/11-April/3741-Minimum-Distance-Between-Three-Equal-Elements-II/Solution.java

🐍 Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/11-April/3741-Minimum-Distance-Between-Three-Equal-Elements-II/solution.py

⚑ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/11-April/3741-Minimum-Distance-Between-Three-Equal-Elements-II/solution.cpp

🌐 JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/11-April/3741-Minimum-Distance-Between-Three-Equal-Elements-II/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 Medium Problems
Java DSA Preparation
Array & Index Tracking Patterns
Mathematical Simplifications
State Tracking with Fixed Arrays
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

Why is it sufficient to store only the two most recent occurrences of each value instead of keeping a full list of previous indices?

1️⃣ Because consecutive triplets provide the minimum or equal index span compared to non-consecutive triplets

2️⃣ Because older occurrences are automatically deleted by the JVM garbage collector

3️⃣ Because the problem requires all three indices to be adjacent in the array

4️⃣ Because an array cannot hold more than three equal values

#LeetCode3741 #LeetCode #MinimumDistanceBetweenThreeEqualElementsII #Java #JavaDSA #Array #HashTable #Math #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu

πŸ‘‡ Comment the answer below! πŸ”₯Β Close description
Β Β Β Β Β Β 
Videos related to "LeetCode 3741 | Minimum Distance Between Three Equal Elements II | Java | Medium | CodeByTushu"