Maximum Circular sum

i applied kadane’s algo on every rotation of array and printed the maximum one , can you please tell me my mistake , its not clearing 3 test cases out of 5;

import java.util.*;
public class Main {
public static void main(String args[]) {

Scanner s = new Scanner(System.in);
int t= s.nextInt();
for(int j =0;j<t;j++){

 int n =s.nextInt();
 int[] arr = new int[n];
 for(int i=0;i<n;i++){
	 arr[i] =s.nextInt();

 }
int tmax =0;

for(int l =1;l<n;l++){
	
	leftRotate(arr,l,n);
	 int max =0;
     int sum =0;
  for(int i=0;i<n;i++){

   sum = sum+ arr[i];
 
  if(sum<0){
	  sum=0;
  }
  if(max<sum){
	  max = sum;
  }
}
if(max>tmax){
   tmax =max;
  }
}

  System.out.println(tmax);
}
}

public static void leftRotate(int arr[], int d, int n) 
{ 
    for (int i = 0; i < d; i++) 
        leftRotatebyOne(arr, n); 
} 

public static void leftRotatebyOne(int arr[], int n) 
{ 
    int i, temp; 
    temp = arr[0]; 
    for (i = 0; i < n - 1; i++) 
        arr[i] = arr[i + 1]; 
    arr[i] = temp; 
} 

}

apply kadane’s algo on original array may be greater sum .

For maximum circular sub array sum you need to consider the maximum of these 2 cases:

The maxm sum subarray is obtained in non circular fashion as in normal Kadane’s algorithm.Apply normal Kadane on the array and obtain this.
The maxm circular sub array sum is obtained in a circular fashion.
To compute the 2nd case:
As you know that Kadane algo gives the maxm subarry sum…so if you invert the sign of each element of the array and then apply Kadane, the maxm subarray sum now obtained will actually be the minimum subarray sum for the original array.

Consider array elements as: 1 2 -1 -3 4 6
1.maximum subarray sum in non circular fashion is: 4+6=10
2. on inverting the signs, the array becomes: -1 -2 1 3 -4 -6
Now applying Kadane, maxm subarry sum is: 1+3=4
So minimum subarray sum for the original array is: -4
3. Now cumulative sum of the original array is: 1+2-1-3+4+6= 9
If you subtract the minimum sub array sum from cumulative sum you get: 9-(-4)=13 which is actually the
maximum subarray sum in circular fashion ie. 4+6+1+2=13 .
So now the answer will be max(10,13)=13.

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.