• + 5 comments

    My code passes two testcases and I believe the logic is right.But for other testcases it gives "terminated due to timeout error". Can you tell what's going wrong ?

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        static int saveThePrisoner(int n, int m, int s){
            int returner=0;
            for(int x=s;x<=n;x++)
                {
                m--;
                if(m>0 && x==n)
                    {
                    x=1;
                }
                if(m==0)
                    {
                    returner=x;
                    break;
                }
            }
            return returner;
        }
        
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int t = in.nextInt();
            for(int a0 = 0; a0 < t; a0++){
                int n = in.nextInt();
                int m = in.nextInt();
                int s = in.nextInt();
                int result = saveThePrisoner(n, m, s);
                System.out.println(result);
            }
        }
    }
    
    • + 4 comments

      The for loop in the saveThePrisoner function is probably taking too long to execute. Notice that my code does not have such a for loop (other than the for loop that has to run all the testcases)

      HackerRank solutions.

      • + 0 comments

        Ohk ,thats probably right. Thanks

      • + 0 comments

        Dude you are a genius! I don't know how you figured out the minus and plus concept

      • + 0 comments

        i have same issue. but i know what is actual problem. if i don't shutdown my system wil crashed.

    • + 0 comments

      i ran my code using cpp. here in the first if of for loop x should assigned to 0 as x increments after each iteration so that for x==n, x will be 1

    • + 0 comments

      java 8 O(1) : return ((s+m-1)%n >0)?(s+m-1)%n:(s+m-1)%n +n ;

    • + 0 comments

      it is an circular array method