GuptaJi's blog

By GuptaJi, history, 5 years ago, In English

import java.util.*; public class Main { public static int max(int[] arr, int start , int end) { int max = Integer.MIN_VALUE;

for( int i = start ; i<= end ; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }

    }



   return max; 
}


public static void main(String[] args)
{
    Scanner scn = new Scanner(System.in);

    int t = scn.nextInt();

    for( int j = 1; j<= t ; j++ )
    {
        int n = scn.nextInt() ;
        int[] arr = new int[n];

        for(int i= 0; i < n  ; i++)
        {

            arr[i] = scn.nextInt();

        }


        int start = 0 ;
        int end ;
        long sum = 0 ;

        for( int i = 1 ; i < n ; i++)
        {
            long x = arr[start]*arr[i] ;
            if(x < 0)
            {
                end = i-1 ;


                sum = sum + max(arr,start ,end) ;

                start = i ;
            }



        }

        end = n-1 ; 
        sum = sum +  max(arr,start ,end) ;

    System.out.println(sum) ;
    }
}

}

  • Vote: I like it
  • -5
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

long x = arr[start]*arr[i] check this string, probably this value can be very big.

UPD. Check this test

1

2

1000000000 1000000000

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    thnx a lot....resolved.. i m just a beginer..so just fed up of not getting reason of getting error....