Sort by

recency

|

1696 Discussions

|

  • + 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);

  • + 0 comments
    function saveThePrisoner(n, m, s) {
        let pos = s + (m % n) - 1;
        return (pos == 0 || pos > n) ? Math.abs(pos - n) : pos;
    }
    
  • + 0 comments
    function saveThePrisoner(prisoners, candies, start) {
        // Write your code here
        let lastPrisoner = (start + candies - 1) % prisoners; 
        return lastPrisoner ? lastPrisoner : prisoners; 
    }