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.
publicstaticvoiddecentNumber(intn){// Start by finding the largest multiple of 3 less than or equal to nfor(inti=n-(n%3);i>=0;i-=3){intremaining=n-i;// Check if the remaining is a multiple of 5if(remaining%5==0){// Append '5' i timesStringBuilderresult=newStringBuilder();result.append("5".repeat(i));// Append '3' (n - i) timesresult.append("3".repeat(remaining));System.out.println(result);return;// Exit as we've found the solution}}// If no valid number is found, print -1System.out.println(-1);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and The Beast
You are viewing a single comment's thread. Return to all comments →
Java: