Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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 preceeding the index is equals to sum of numbers
- // succeeding the index.
- public class JavaFiddle
- {
- public static void main(String[] args)
- {
- JavaFiddle jf = new JavaFiddle();
- int[] numbers = {1, 2, 10, 4, 1, 5, 6, 5, 1, 10, 6, 3, 2, 6, 1, 9, 3, 7, 5, 8, 2, 10, 4, 5, 1, 0, 1, 3, 5, 7, 9, 3, 2};
- jf.index(numbers);
- }
- private void index(int[] array){
- int leftsum;
- int rightsum;
- for(int i = 1; i < array.length; i++){
- leftsum = 0;
- rightsum = 0;
- for(int l = 0; l < i; l++){
- leftsum += array[l];
- }
- for(int r = array.length-1; r > i; r--){
- rightsum += array[r];
- }
- if(leftsum == rightsum){
- System.out.println("The index: " + i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement