public class Solution {
    public static long power(long a, long b) { //my method to do Math.pow
        long powerResult = 1;
        while (b >= 1) {
            powerResult = powerResult * a;
            b--;
        }
        return powerResult;
    }

    public static long[] getNumbers(long N) {
        if(N <= 1)return new long[0];
        ArrayList<Long> longArrayList = new ArrayList<>();

        for (long j = 1; j < N; j++) {
            long numberLength = String.valueOf(j).toCharArray().length; //determine number length
            char[] charArray = String.valueOf(j).toCharArray(); //charArray consists of digits (for 370 it's 3 7 0)
            long sumA = 0; //initialize result of operation
            for (int i = 0; i < charArray.length; i++) { //iterate over digits of input number
                long sumB = 0; //initialize let's say half-result
                char cyfra = charArray[i]; //get current char from array
                String cyfraString = Character.toString(cyfra); //change this char to String for later purpose
                int cyfraInt = Integer.parseInt(cyfraString); // now change this String to int - I don't know better solution to do this :(
                sumB = sumB + power(cyfraInt, numberLength); // adding next results of power to each other
                sumA += sumB; // adding to sumA to get the real result of program
            }
            if (sumA == j) { //check if result is the same as input number
                longArrayList.add(j); //same = add to list
            }
        }
        long[] result = new long[longArrayList.size()]; //create standard array from arrayList
        for (int i = 0; i < longArrayList.size(); i++) {
            result[i] = longArrayList.get(i);
        }
        return result;
    }
for input number 1000 I get array -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407] for input number 1000000 I get array -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834] program takes 0 seconds for 1000000 input and takes memory 12156 - is it too much ? Help guys!