class Solution { public List<Integer> powerfulIntegers(int x, int y, int bound) { //this is a question of maths more //make hashset for avoiding duplicates HashSet<Integer> set = new HashSet<>(); //i should be individually smaller than bound for(int i = 1 ; i < bound ; i *= x){ //i + j should also be smaller than bound for(int j = 1 ; i+j <= bound ; j *= y ){ set.add(i + j); //else infinite loop therefore break after 1 iteration if(y == 1) break ; } if(x == 1) break ; } return new ArrayList<>(set); //will make a arraylist of set } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter