Content

Friday, January 18, 2019

787 - Maximum Sub-sequence Product

Problem Link

My Solution:


import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.Scanner;

class Main {
   
    public static void main (String[] args) {
      
        Scanner scr = new Scanner(System.in);
        BigInteger n, pin, curr_max, curr_min, prev_max = null, prev_min = null, ans = null;
        Boolean flag = true;
        pin = new BigInteger("-999999");
        while(scr.hasNext()) {
          
            n = scr.nextBigInteger();
          
            if(n.equals(pin)) {
              
                System.out.println(ans);
                flag = true;
              
            }
          
            else if(flag) {
              
                flag = false;
                curr_max = n;
                curr_min = n;
                prev_max = n;
                prev_min = n;
                ans = n;
              
            }
          
            else {
              
                BigInteger temp, temp1, temp2;
                temp = prev_max.multiply(n);
                temp1 = prev_min.multiply(n);
                temp2 = temp1.max(n);
                curr_max = temp2.max(temp);
                temp2 = temp1.min(n);
                curr_min = temp2.min(temp);
                ans = ans.max(curr_max);
                prev_max = curr_max;
                prev_min = curr_min;
              
            }
          
        }
      
    }
   
}

No comments:

Post a Comment