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 } }