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




