Equilibrium Indices Java Program | ISC Computer Science Sample Paper 2026

Write a program to declare a single-dimensional array A[] of size L, where L is an integer greater than or equal to 3 and less than or equal to 50. Allow the user to input integers into this array. Display an appropriate error message for an invalid input.

Perform the following tasks on the array:
a) Display the array.
b) Find and print all equilibrium indices in increasing order. An index i (0-based) is called an equilibrium index if the sum of elements on its left equals the sum of elements on its right.
c) If no equilibrium index exists, display an appropriate message.

Test your program with the following data and some random data:

Example 1
INPUT:
L = 7
Array Elements: 2, 3, -1, 8, 4, -2, 2
OUTPUT:
Array: 2, 3, -1, 8, 4, -2, 2
Equilibrium Indices: 3

Example 2
INPUT:
L = 5
Array Elements: 0, -7, 3, -2, 6
OUTPUT:
Array: 0, -7, 3, -2, 6
Equilibrium Indices: 0

Example 3
INPUT:
L = 6
Array Elements: 1, 2, 3, 4, 5, 6
OUTPUT:
Array: 1, 2, 3, 4, 5, 6
Equilibrium Indices: NIL

Example 4
INPUT:
L = 7
Array Elements: -7, 1, 5, 2, -4, 3, 0
OUTPUT:
Array: -7, 1, 5, 2, -4, 3, 0
Equilibrium Indices: 3, 6

Example 5
INPUT:
L = 2
OUTPUT:
INVALID INPUT

import java.util.Scanner;
class Equilibrium{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("L = ");
        int L = in.nextInt();
        if(L < 3 || L > 50){
            System.out.println("INVALID INPUT");
            return;
        }
        int A[] = new int[L];
        System.out.println("Enter array elements:");
        for(int i = 0; i < A.length; i++)
            A[i] = in.nextInt();
        System.out.print("Array Elements: ");
        for(int i = 0; i < A.length; i++)
            System.out.print(A[i] + " ");
        System.out.println();
        boolean found = false;
        System.out.print("Equilibrium Indices: ");
        for(int i = 0; i < A.length; i++){
            int left = 0;
            int right = 0;
            for(int j = 0; j < i; j++)
                left += A[j];
            for(int j = i + 1; j < A.length; j++)
                right += A[j];
            if(left == right){
                found = true;
                System.out.print(i + " ");
            }
        }
        if(!found)
            System.out.println("NIL");
    }
}