Sort by

recency

|

1698 Discussions

|

  • + 0 comments

    Here is my one line Python solution!

    def saveThePrisoner(n, m, s):
        return (m + s - 1) % n if (m + s - 1) % n != 0 else n
    
  • + 0 comments

    My TypeScript solution:

    function saveThePrisoner(n: number, m: number, s: number): number {
        const lastPrisioner: number = (s - 1 + m) % n;
        
        return lastPrisioner == 0 ? n : lastPrisioner;
    }
    
  • + 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

    java 8 but there is error time error

    public static int saveThePrisoner(int n, int m, int s) {
    
        // Write your code here
    
        int i = 0;
        while (s < n) {
            i++;
            if (i == m) {
                return s;
            }
            s = (s + 1) % n;
    
        }
        return s;
    
    }
    
  • + 0 comments

    C# return (m+s-1) > n ?(((m+s-1)%n) == 0 ? n:(m+s-1)%n ):(m+s-1);