Code gives wrong answer for test case but works fine with my inputs

import java.util.*;
public class Main {
public static int kadane_algo(int[] arr) {
int max_current = arr[0];
int max_global = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max_current + arr[i] > arr[i]) {
max_current+=arr[i];
} else {
max_current = arr[i];
}

		if (max_current > max_global) {
			max_global = max_current;
		}
	}
	return max_global;
}

public static int min(int[] arr) {
	int min = 0;
	for (int i = 0; i < arr.length; i++) {
		if (arr[min] > arr[i]) {
			min = i;
		}
	}
	return arr[min];
}

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int T = sc.nextInt();
	if (1 <= T && 100 >= T) {
		for (int i = 0; i < T; i++) {
			int N = sc.nextInt();
			if (1 <= N && 1000 >= N) {
				int[] arr = new int[N];
				for (int j = 0; j < arr.length; j++) {
					arr[j] = sc.nextInt();
				}
				int max = kadane_algo(arr);
				int min = min(arr);
				int sum = 0;
				for (int j = 0; j < arr.length; j++) {
					sum += arr[j];
				}
				if (sum < min) {
					System.out.println(max);
				} else {
					if ((sum - min) > max) {
						System.out.println((sum - min));
					} else {
						System.out.println(max);
					}
				}
			}
		}
	}
	sc.close();
}

}

Hello @10vaibhavsinghnegi ,

Normal max subarray problem uses a simple Kadane’s algorithm.

Circular array uses a modified kadane Algorithm. In that you have to first calculate the max subarray sum in the given array.
Now calculate sum of all elements in array and then multiply every element by -1 and then find max subarray sum again and then add that to the sum of all elements.
Now you will have 2 values, one value from the normal maximum subarray sum and another from the addition of array sum and maximum subarray sum after we multiply every element by -1.
The answer will be maximum of these 2 elements.

You can use the below test case for reference:
1
5
12 -6 4 -9 10

Correct answer will be 22.
Your code will give output 20.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.