We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Test cases 9 onwards are giving timeout errors. Anybody?
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;classFuncData{Stringindex;longgcd,sum,max,func;publicFuncData(Stringindex,longgcd,longsum,longmax,longfunc){this.index=index;this.gcd=gcd;this.sum=sum;this.max=max;this.func=func;}publiclonggetFunc(){returnthis.func;}}publicclassSolution{publicstaticlongGCD(int[]a){longresult=Math.abs(a[0]);for(inti=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);}}returnresult;}//this method assumes a>b. If that is not the case, then we'd have to swap the numbers.publicstaticlongGCD(longa,longb){if(a==0){returnb;}elseif(b==0){returna;}if(a==b){returna;}if(a<b){returnGCD(a,b%a);}else{returnGCD(a%b,b);}}publicstaticlongsum(int[]a){intsum=0;for(inti=0;i<a.length;i++){sum=sum+a[i];}returnsum;}publicstaticlongmax(int[]a){intmax=a[0];for(inti=1;i<a.length;i++){if(a[i]>max){max=a[i];}}returnmax;}publicstaticlongmin(int[]a){intmin=a[0];for(inti=1;i<a.length;i++){if(a[i]<min){min=a[i];}}returnmin;}publicstaticlongmaxFuncValue(List<FuncData>funcData){longmaxValue=Integer.MIN_VALUE;for(FuncDatafd:funcData){if(fd.getFunc()>maxValue){maxValue=fd.getFunc();}}returnmaxValue;}publicstaticlongmaximumValue(int[]a){List<FuncData>funcData=BuildData(a);returnmaxFuncValue(funcData);}publicstaticint[]getArrayFromIndexes(inti,intj,int[]a){int[]array=newint[j-i+1];for(intk=i,l=0;k<=j;k++,l++){array[l]=a[k-1];}returnarray;}publicstaticList<FuncData>BuildData(int[]a){List<FuncData>list=newArrayList<FuncData>();for(inti=1;i<=a.length;i++){for(intj=i;j<=a.length;j++){int[]temp=getArrayFromIndexes(i,j,a);list.add(newFuncData(i+","+j,GCD(temp),sum(temp),max(temp),GCD(temp)*(sum(temp)-max(temp))));}}returnlist;}publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn=in.nextInt();int[]a=newint[n];for(inta_i=0;a_i<n;a_i++){a[a_i]=in.nextInt();}longresult=maximumValue(a);System.out.println(result);in.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Strange Function
You are viewing a single comment's thread. Return to all comments →
Test cases 9 onwards are giving timeout errors. Anybody?