Skip to main content

Posts

Showing posts from October, 2026

Second Largest Element - DSA

Java · DSA · Arrays ◐ Find the second largest distinct element in an array in a single pass , with no sorting and no extra array. Watch largest and secondLargest update step by step on your own input. 01 Problem 02 Code 03 How it works 04 Tracer 05 Approaches 06 Revision 07 Quiz 01 The problem Given an array of N integers, print the second largest distinct value. Duplicates of the largest don't count. If there is no second distinct value, print -1 . For [10, 20, 20, 15, 5] , the largest is 20. The next distinct value below it is 15 . For [7, 7, 7] there is no second value, so the answer is -1 . Type an array to see what the program prints. Negative numbers work too. Array (spaces or commas) Run 02 The code Keep two variables while walking the array once: largest and secondLargest . A boolean, isSLargestFound , records whether a r...

Sum of Min/Max Frequencies of the Arrays - Brute Force & Optimized Approaches

Java · DSA · Hashing ◐ Add the lowest and highest frequency in an array. Solve it two ways, brute force and hashing , then watch both run step by step on your own input. 01 Problem 02 Brute force 03 Hashing 04 Tracer 05 Comparison 06 Revision 07 Quiz 01 The problem Given an array of N non-negative integers, find how often each value occurs. Print the minimum frequency + maximum frequency . For [1, 2, 2, 3, 3, 3, 3] , the frequencies are 1 → 1, 2 → 2 and 3 → 4. The lowest is 1 and the highest is 4, so the answer is 1 + 4 = 5 . Type an array to run both programs and see every frequency. Array (spaces or commas) Run 02 Approach 1: Brute force Walk the array with i . The first time a value appears, mark it in visited and count it with an inner j loop. Then update minFreq and maxFreq with that count. Copy import java.util. Sca...