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.
Scanners=newScanner(System.in);intQ=s.nextInt();List<Integer>triplet=newArrayList<>();triplet.add(17);for(inti=0;i<Q;i++){intn=s.nextInt();inta,b,c=2;intlastNum=triplet.get(triplet.size()-1);intwaste=0;intanswer=0;intansind=0;longtime1=Calendar.getInstance().getTimeInMillis();if(n>lastNum){for(a=2;a<n/3;a++){b=a;c=(int)Math.ceil(Math.sqrt(a*a+b*b-1));for(b=Math.max(a,((lastNum)/2)-a);b<(n-1)/2&&(c<a+b)&&(a+b+c<=n);b++){waste++;c=(int)Math.ceil(Math.sqrt(a*a+b*b-1));// System.out.println("\t\t" + a + ", " + b + ", " + c);if(b==c)break;if(a+b+c<=lastNum)continue;if(a*a+b*b==c*c+1){triplet.add(a+b+c);// System.out.println("a=" + a + " b=" + b + " c=" + c + " sum=" + (a + b + c));}}}Collections.sort(triplet);}if(n>=triplet.get(triplet.size()-1)){ansind=triplet.size();}elseif(n>17){for(intj=0;j<triplet.size();j++){if(triplet.get(j)>n){ansind=j;break;}}}longtime2=Calendar.getInstance().getTimeInMillis();answer=ansind+(n-1)/2;System.out.println("waste="+waste);System.out.println(answer);System.out.println((time2-time1)/(1000));// triplet.stream().forEach(x -> System.out.print(x + ", "));}
Can someone suggest how I can optimize it further. (waste in my code refers to the number of loop iterations. its just a informatory parameter i am using.)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #223: Almost right-angled triangles I
You are viewing a single comment's thread. Return to all comments →
i have a code taht runs for first 10 test cases.
Can someone suggest how I can optimize it further. (waste in my code refers to the number of loop iterations. its just a informatory parameter i am using.)