Q46 Powerful Integers - LeetCode

PHOTO EMBED

Mon Jan 30 2023 09:38:04 GMT+0000 (Coordinated Universal Time)

Saved by @Ayush_dabas07

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

https://leetcode.com/problems/powerful-integers/