Tuesday, 5 September 2017

java - Sum of array elements that is first continuously increasing then decreasing

Where i found: http://www.geeksforgeeks.org/sum-array-elements-first-continuously-increasing-decreasing/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class Toast {
    static int question_1(int[] array) {
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] > array[i+1])
                return sum*2 + array[i];
            else
                sum += array[i];
        }

        return sum;
    }

}

styled using hilite.me

No comments:

Post a Comment