The Strange Function

  • + 0 comments

    Test cases 9 onwards are giving timeout errors. Anybody?

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
     class FuncData {
        String index;
        long gcd, sum, max, func;
        
        public FuncData(String index, long gcd, long sum, long max, long func){
            this.index = index;
            this.gcd = gcd;
            this.sum = sum;
            this.max = max;
            this.func = func;
        }
        
        public long getFunc(){
            return this.func;
        }
    
    }
    
    public class Solution {
        
        public static long GCD(int[] a){
            long result = Math.abs(a[0]);
            
            for(int i=1;i<a.length;i++){
                if(result>Math.abs(a[i])){
                    result = GCD(result,Math.abs(a[i]));
                }        
                else{
                    result = GCD(Math.abs(a[i]), result);
                }
            }
            
            return result;
        }
        
        //this method assumes a>b. If that is not the case, then we'd have to swap the numbers.
        public static long GCD(long a, long b){
            if(a == 0){
                return b;
            }
            else if(b==0){
                return a;
            }
            
            if(a==b){
                return a;
            }
            
            if(a<b){
                return GCD(a, b%a);
            }
            else{
                return GCD(a%b, b);
            }
            
        }       
        
        public static long sum(int[] a){
            int sum = 0;
            for(int i=0; i<a.length; i++){
                sum = sum + a[i];
            }
            
            return sum;
        }
        
        public static long max(int[] a){
            int max = a[0];
            
            for(int i=1;i<a.length;i++){
                if(a[i]>max){
                    max = a[i];
                }
            }
            return max;
        }
        
        public static long min(int[] a){
            int min = a[0];
            
            for(int i=1;i<a.length;i++){
                if(a[i]<min){
                    min = a[i];
                }
            }
            return min; 
        }
    
        public static long maxFuncValue(List<FuncData> funcData){
            long maxValue = Integer.MIN_VALUE;
            for(FuncData fd: funcData){
                if(fd.getFunc() > maxValue){
                    maxValue = fd.getFunc();
                }
            }
            return maxValue; 
        }
    
        public static long maximumValue(int[] a) {
    
            List<FuncData> funcData = BuildData(a);
            
            return maxFuncValue(funcData);
        }
        
        public static int[] getArrayFromIndexes(int i, int j, int[] a){
            int[] array = new int[j-i+1];
            for(int k=i, l=0; k<=j; k++, l++){
                array[l] = a[k-1];
            }
            return array;
        }
        
        public static List<FuncData> BuildData(int[] a){
            
            List<FuncData> list = new ArrayList<FuncData>();
            
            for(int i=1;i<=a.length;i++){
                for(int j=i;j<=a.length;j++){
                    int[] temp = getArrayFromIndexes(i,j,a);
                    list.add(new FuncData (i + "," + j,
                                          GCD(temp),
                                          sum(temp),
                                          max(temp),
                                          GCD(temp) * (sum(temp) - max(temp))));
                }
            }
            
            return list;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] a = new int[n];
            for(int a_i = 0; a_i < n; a_i++){
                a[a_i] = in.nextInt();
            }
            long result = maximumValue(a);
            System.out.println(result);
            in.close();
        }
    }