package code.asgard.arrays;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Sagar
 */
public class LargestSubArray {

//    public static int largestSubArraySum(int inputArr[], int sum) {
//        int size = inputArr.length;
//
//        int star = 0;
//        int end = 0;
//        int max_so_far = inputArr[0];
//        int curr_max = inputArr[0];
//
//        for (int i = 1; i < size; i++)
//        {
//            curr_max = Math.max(inputArr[i], curr_max+inputArr[i]);
//            max_so_far = Math.max(max_so_far, curr_max);
//        }
//        return max_so_far;
//    }

    public static int[] largestSubArraySum(int inputArr[], int value) {
        int start = 0;
        int end = 0;
        int sum;
        for (int i = 0; i < inputArr.length; i++) {
            sum = inputArr[i];
            for (int j = i + 1; j < inputArr.length; j++) {
                sum += inputArr[j];
                if(sum == value && j - i > end - start) {
                    start = i;
                    end = j;
                }
            }
        }
        if(start == 0 && end == 0 && value != inputArr[0]) {
            System.out.println("Not able to find the sub array");
            return new int[0];
        }
        int[] outputArr = new int[end - start + 1];
        System.arraycopy(inputArr, start, outputArr, 0, end - start + 1);
        return outputArr;
    }

    public static void main(String[] args) {
        System.out.println("Please enter how many array elements you want to add:");
        Scanner scanner = new Scanner(System.in);
        int arr_size = scanner.nextInt();
        int[] input = new int[arr_size];
        for (int i = 0; i < arr_size; i++) {
            System.out.println("Please enter your number:");
            input[i] = scanner.nextInt();
        }
        System.out.println("Input array: " + Arrays.toString(input));
        System.out.println("Largest sum: " + Arrays.toString(largestSubArraySum(input, 10)));
    }
}
