Sort by

recency

|

1752 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/6rYwcW6BYH4

    int beautifulDays(int i, int j, int k) {
        int ans = 0;
        for(int el = i; el <= j; el++){
            string s = to_string(el);
            reverse(s.begin(), s.end());
            if(abs(stoi(s) -el ) % k == 0) ans++;
        }
        return ans;
    }
    
  • + 0 comments

    Perl (super short):

    sub beautifulDays {
        my ($i, $j, $k) = @_;
        
        return scalar(grep { ($_ - reverse($_)) % $k == 0 } ($i..$j));
    }
    
  • + 0 comments
    public static int beautifulDays(int i, int j, int k) {
            int count = 0;
            
            for (int day = i; day <= j; day++) {
                if (Math.abs(day - reverse(day)) % k == 0) count++;
            }
    
            return count;
        }
    
    static int reverse(int n) {
            StringBuilder sb = new StringBuilder(String.valueOf(n));
            
            return Integer.parseInt(sb.reverse().toString());
        }
    
  • + 0 comments

    another approach !

        if (i < 1 || j < 1 || k < 1) return -1;
    
        int count = 0;
    
        for (int n = i; n <= j; n++) {
            String wordInt = String.valueOf(n);
            int y = wordInt.length() - 1;
            String reverse = "";
    
            while (y >= 0) {
                reverse = reverse + wordInt.charAt(y);
                y--;
            }
    
            int num = Integer.parseInt(reverse);
            int difference = Math.abs(num - n);
    
            if (difference % k == 0) {
                count++;
            }
        }
        return count;
    
  • + 0 comments

    public static int beautifulDays(int i, int j, int k) { if (i < 1 || j < 1 || k < 1) return -1;

    int count = 0;
    
    for (int n = i; n <= j; n++) {
        String wordInt = String.valueOf(n);
        int x = 0;
        int y = wordInt.length() - 1;
    
        while (x < y) {
    
            char chFront = wordInt.charAt(x);
            char chLast = wordInt.charAt(y);
    
    
            wordInt = wordInt.substring(0, x) + chLast + wordInt.substring(x + 1, y) + chFront + wordInt.substring(y + 1);
    
            x++;
            y--;
        }
    
        int num = Integer.parseInt(wordInt);
        int difference = Math.abs(num - n);
    
        if (difference % k == 0) {
            count++;
        }
    }
    return count;
    }