Subarray Sum

You are given an array having N integers. You have to select at most K positions in the array and re…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

I tried this code … But it throws wrong answer in the 2nd point of testing. What should I do.
Here is my code(Java)…

import java.util.*;

class Kadane
{
public static void main (String[] args)
{
try {
int k;
Scanner in = new Scanner(System.in);
double num = in.nextInt();

        if(num>=0 & num <= 5000) {
            for (k = 0; k < num; k++) {

                int boo = in.nextInt();
                int[] a = new int[boo];
                int j = 0;
                int l = in.nextInt();

                try {
                    for (j = 0; j < boo; j++) {
                        a[j] = in.nextInt();

                    }
                } catch (Exception e) {

                }


                int i;
                for (i = 0; i < l; i++) {
                    int key = getNegMin(a);
                    if (key != 111111111) {
                        a[key] = 0;
                    }
                }


                int m = (int)k + 1;
                System.out.println("Case " + m + ": " + (int)maxSubArraySum(a));
            }
        }
    }catch (Exception e){

    }
}

public static int getNegMin(int[] arr){
    int i;
    int max = 0;
    int key = 111111111;

    for (i=0;i<arr.length;i++){
        if(arr[i]<max){
            max = arr[i];
            key = i;
        }
    }


    return key;


}

static int maxSubArraySum(int a[])
{

    int size = a.length;
    int ans = Integer.MIN_VALUE, max_ending = 0;

    for (int i = 0; i < size; i++)
    {
        max_ending = max_ending + a[i];
        if (ans < max_ending)
            ans = max_ending;
        if (max_ending < 0)
            max_ending = 0;
    }
    return ans;
}

}