Sort by

recency

|

1708 Discussions

|

  • + 0 comments

    C++ Solution:

    int saveThePrisoner(int n, int m, int s) {
        int warn = 0;
        warn = (m+(s-1)) % n; 
        if(warn == 0) warn = n;
        return warn;
    }
    
  • + 0 comments
    public static int saveThePrisoner(int n, int m, int s) {
        return (((((m % n) + (s - 1)) % n) + (n - 1)) % n) + 1;
    }
    
  • + 0 comments
    fn saveThePrisoner(n: i32, m: i32, s: i32) -> i32 {
        let result = (s + m - 1) % n;
        if result  == 0 {
            n
        } else {
            result
        }
    }
    
  • + 0 comments

    Here solution in c++, you can watch the explanation here : https://youtu.be/xoV2P0Pv9Fg

    Solution 1 :

    int saveThePrisoner(int n, int m, int s) {
        int c = (m % n) + s - 1 ;
        if ( c > n) c -= n;
        if ( c == 0) return n;
        return c;
    }
    

    Solution 2 :

    int saveThePrisoner(int n, int m, int s) {
        return (m - 1  + s - 1 ) % n + 1;
    }
    
  • + 0 comments

    Perl:

    sub saveThePrisoner {
        my ($n, $m, $s) = @_;
        
        return ($s + $m - 1) % $n == 0 ? $n : ($s + $m - 1) % $n;
    }