May 18, 2017

Srikaanth Kumar

Find Out Middle Index Where Sum Of Both Ends are Equal Java Program


Program: 

Find out middle index where sum of both ends are equal ?

Description:

You are given an array of numbers. Find out the array index or position
where sum of numbers preceding the index is equals to sum of numbers
succeeding the index.

Code:


package com.java2novice.algos;

public class FindMiddleIndex {

    public static int findMiddleIndex(int[] numbers) throws Exception {

        int endIndex = numbers.length - 1;
        int startIndex = 0;
        int sumLeft = 0;
        int sumRight = 0;
        while (true) {
            if (sumLeft > sumRight) {
                sumRight += numbers[endIndex--];
            } else {
                sumLeft += numbers[startIndex++];
            }
            if (startIndex > endIndex) {
                if (sumLeft == sumRight) {
                    break;
                } else {
                    throw new Exception(
                            "Please pass proper array to match the requirement");
                }
            }
        }
        return endIndex;
    }

    public static void main(String a[]) {
        int[] num = { 2, 4, 4, 5, 4, 1 };
        try {
            System.out.println("Starting from index 0, adding numbers till index "
                            + findMiddleIndex(num) + " and");
            System.out.println("adding rest of the numbers can be equal");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Output:

Starting from index 0, adding numbers till index 2 and
adding rest of the numbers can be equal



Subscribe to get more Posts :