September 5, 2025

πŸ”Ή Finding the First Equilibrium Point in an Array

When solving coding problems, one frequently encountered challenge is finding the equilibrium point in an array. This is a classical problem in algorithm design that tests the ability to balance sums efficiently without resorting to brute force. Let’s break it down in detail.


πŸ“Œ Problem Statement

Given an array A of n non-negative integers, the task is to find the first equilibrium point. An equilibrium point is an index such that the sum of elements before it is equal to the sum of elements after it.

  • Use 1-based indexing.

  • If no equilibrium point exists, return -1.

Constraints:

  • 1 <= n <= 10^5

  • 0 <= A[i] <= 10^9


πŸ”Ž Examples

Example 1

Input:

n = 5
A[] = {1, 3, 5, 2, 2}

Process:

  • Index 1 → Left sum = 0, Right sum = 12 ❌

  • Index 2 → Left sum = 1, Right sum = 9 ❌

  • Index 3 → Left sum = 4, Right sum = 4 ✅

Output:

3

Example 2

Input:

n = 1
A[] = {1}

Since there’s only one element, it automatically becomes the equilibrium point.

Output:

1

⚡ Why Brute Force Fails

A naΓ―ve approach would be:

  • For each index i, compute sum of elements before and after it.

  • Compare the two.

This requires O(n²) time in the worst case, which is inefficient for large inputs (n ≤ 100,000). Hence, we need a more optimized approach.


✅ Optimized Approaches

1. Total Sum & Left Sum Method (Efficient Approach)

We can achieve O(n) time and O(1) space using the following idea:

  1. Compute the total sum of the array.

  2. Traverse through the array while maintaining a left sum.

  3. At each index i, check:

    leftSum == totalSum - leftSum - A[i]
    

    If true, this is the equilibrium point.

  4. Update leftSum += A[i] and continue.


2. Prefix Sum Approach

Another approach uses prefix sums to quickly compute left and right sums:

  1. Create an array prefix[] where prefix[i] stores the sum of elements from 0 to i.

  2. For each index i, left sum = prefix[i-1], right sum = prefix[n-1] - prefix[i].

  3. If left sum == right sum → equilibrium found.

Complexity:

  • Time Complexity: O(n) (one pass to compute prefix, one pass to check).

  • Space Complexity: O(n) (extra array for prefix sums).

This approach is less space-efficient than the total sum method, but conceptually easier to understand.


πŸ§‘‍πŸ’» Implementation (Java - Total Sum Method)

class Solution {
    public static int equilibriumPoint(long arr[], int n) {
        long totalSum = 0, leftSum = 0;

        // Step 1: Find total sum
        for (int i = 0; i < n; i++) {
            totalSum += arr[i];
        }

        // Step 2: Traverse and check equilibrium condition
        for (int i = 0; i < n; i++) {
            totalSum -= arr[i]; // totalSum now acts as rightSum

            if (leftSum == totalSum) {
                return i + 1; // 1-based index
            }

            leftSum += arr[i];
        }

        return -1; // No equilibrium point found
    }
}

πŸ§‘‍πŸ’» Implementation (Python - Prefix Sum Method)

def equilibriumPoint(arr, n):
    prefix = [0] * n
    prefix[0] = arr[0]

    # Build prefix sum array
    for i in range(1, n):
        prefix[i] = prefix[i-1] + arr[i]

    total_sum = prefix[-1]

    for i in range(n):
        left_sum = prefix[i-1] if i > 0 else 0
        right_sum = total_sum - prefix[i]
        
        if left_sum == right_sum:
            return i + 1  # 1-based index

    return -1

πŸ•’ Complexity Analysis

Total Sum Method

  • Time Complexity: O(n)

  • Space Complexity: O(1)

Prefix Sum Method

  • Time Complexity: O(n)

  • Space Complexity: O(n)

Both are efficient, but the Total Sum Method is preferred due to lower space usage.


🎯 Key Observations

  1. The equilibrium point is essentially a balance point.

  2. Total sum helps avoid recomputation of right sums.

  3. Prefix sums provide an intuitive way to check but need extra space.

  4. Works efficiently under given constraints.


πŸ“Œ Applications

  • Load balancing: Splitting workloads evenly.

  • Financial analysis: Checking balance points in profit/loss arrays.

  • Partitioning problems: Finding equal subarray sums.


πŸš€ Conclusion

The equilibrium point problem highlights the power of prefix sums and optimized linear scanning. While prefix sums provide clarity, the total sum + left sum approach is the most space-efficient. By reducing unnecessary recalculations, we turn an O(n²) problem into an O(n) one, making it highly useful in interviews and real-world applications.


September 1, 2025

Find the Second Largest Distinct Element in an Array

When working with arrays, one common interview question is finding the second largest distinct element. Unlike just sorting the array, we want an efficient solution with O(N) time complexity and O(1) space complexity.

Let’s carefully break down the problem.


Problem Statement

You are given an array Arr of size N. You need to print the second largest distinct element in the array.

  • If the second largest element doesn’t exist (all elements are the same), return -1.


Example 1

Input:

N = 6
Arr[] = {12, 35, 1, 10, 34, 1}

Output:

34

Explanation:

  • Largest element = 35

  • Second largest element = 34


Example 2

Input:

N = 3
Arr[] = {10, 5, 10}

Output:

5

Explanation:

  • Largest element = 10

  • Second largest distinct element = 5


Constraints

  • 2 ≤ N ≤ 10^5

  • 1 ≤ Arr[i] ≤ 10^5


Naive Approach (Sorting)

The simplest approach would be:

  1. Sort the array in descending order.

  2. Pick the first element as the largest.

  3. Find the next distinct element as the second largest.

But sorting takes O(N log N), which is not optimal when N can go up to 10^5.


Optimal Approach (Single Pass - O(N))

We can solve this in just one pass:

  1. Maintain two variables:

    • first → largest element

    • second → second largest element

  2. Traverse the array:

    • If arr[i] > first: update second = first and first = arr[i].

    • Else if arr[i] > second and arr[i] != first: update second = arr[i].

  3. At the end:

    • If second is still -1, return -1.


Implementation (Java)

class Solution {
    int print2largest(int arr[], int n) {
        int first = -1, second = -1;
        
        for (int i = 0; i < n; i++) {
            if (arr[i] > first) {
                second = first;
                first = arr[i];
            } else if (arr[i] > second && arr[i] != first) {
                second = arr[i];
            }
        }
        
        return second;
    }
}

Dry Run Example

Input: Arr[] = {12, 35, 1, 10, 34, 1}

  • Start: first = -1, second = -1

  • 12: first = 12, second = -1

  • 35: first = 35, second = 12

  • 1: no update

  • 10: second = 12 → stays

  • 34: second = 34 (since 34 < 35 but > 12)

  • 1: no update

Answer = 34


Key Takeaways

  • Time Complexity: O(N) → Single traversal.

  • Space Complexity: O(1) → Only two variables used.

  • Works for large arrays efficiently.


Common Mistakes

  1. Forgetting to check for distinctness (e.g., {10, 10, 10} should return -1).

  2. Not updating the second properly when a new first is found.

  3. Returning the same value as largest when all elements are equal.


Final Words

This is a classic array problem that helps you think about tracking multiple values in one pass without sorting. It’s a great stepping stone to more advanced interview questions where optimization matters.

πŸ‘‰ If you’ve understood this, try extending the idea to find the third largest distinct element in an array!


Would you like me to also write the Python version of this solution for your blog readers who might prefer Python over Java?