1

Hello I've been getting this error in Kattis, 'Run time error' while all my test cases are correct in my own machine. Tested everything but as soon as i run this in kattis i get a run time error. Can you guys help me figure this out? Ive been debugging for hours but i am struggling.

https://open.kattis.com/problems/throwns?editsubmit=9372235 :Link of the problem

import java.util.*;
  import java.io.*;
   public class GOT{
    public static void main(String[] args)throws IOException{
        
        BufferedReader bi = new BufferedReader(new I 
       nputStreamReader(System.in));
        int[] parseLine1 = new int[2];
        String[] strLine1;

        strLine1 = bi.readLine().split(" ");
        
        //Parsing of 1st line of inputs i.e. N and K
        for (int i = 0; i < strLine1.length; i++) {
            parseLine1[i] = Integer.parseInt(strLine1[i]);
        }
        
        
        //init of Kids array
        int[] nKids = new int[parseLine1[0]];
        String[] commands = new String[parseLine1[1]];
        
        int i;
        for(i = 0; i < nKids.length; i++){
            nKids[i] = i;
        }
        
        //parsing of 2nd line which are the commands
        String strLine2;
        String[] nCommands;
        Scanner sc = new Scanner(System.in);
        
        strLine2 = sc.nextLine();
        nCommands = strLine2.split(" ");
        
        int holder=0;
        ArrayList<Integer> tracker = new ArrayList<Integer>();
        int exit;
        int throwns;
        int undoCtr=0;
        
        for(i = 0; i<nCommands.length; i++){
            if(nCommands[i].equals("undo")){
                nCommands[i] = nCommands[i].replaceAll("undo","101");
            }
        }       
        
        exit = nCommands.length;
        i = 0;
        while(exit != 0){
            //System.out.println(Integer.parseInt(nCommands[i]));
            if(Integer.parseInt(nCommands[i]) > 0){
                for(int k = 0; k< Integer.parseInt(nCommands[i]); k++){
                    holder++;
                    if(holder==nKids.length){
                        holder = 0;
                    }
                }
            }if(Integer.parseInt(nCommands[i]) < 0){
                for(int k = Integer.parseInt(nCommands[i]); k<0 ; k++){
                    holder--;
                    if(holder==0){
                        holder = nKids.length;
                    }
                }
            }else if(Integer.parseInt(nCommands[i]) == 101){
                i++;
                
                undoCtr = Integer.parseInt(nCommands[i]);
                while(undoCtr!=0){
                    tracker.remove(tracker.size()-1);
                    undoCtr--;
                }
                exit--;
                
            }
            tracker.add(holder);
            
            exit--;
            i++;
        }
        
        
            System.out.println(tracker.get(0));
        
      }`
      }`
Machavity
  • 30,841
  • 27
  • 92
  • 100

1 Answers1

0

Your approach is too complex for a 2.8 difficulty problem. If you find yourself writing more than 25 lines of code, it's usually time to take a step back and re-think your approach.

Here's the algorithm that worked for me:

  • Make a list of "final" throw commands, initially empty.
  • Loop over the input tokens and analyze each command:
    • If a command is a number, append to the command list.
    • If a command is an undo n, pop the command list n times.
  • Now sum the commands in the list and print the mod of this sum, taking care to keep the mod positive.

Here's the spoiler:

public class Throwns {
    public static void main(String[] args) {
        var sc = new java.util.Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine().split(" ")[0]);
        var line = sc.nextLine().split(" ");
        var commands = new java.util.ArrayList<Integer>();
        int sum = 0;

        for (int i = 0; i < line.length; i++) {
            if (line[i].matches("^-?\\d+$")) {
                commands.add(Integer.parseInt(line[i]));
                continue;
            }

            for (int j = Integer.parseInt(line[++i]); j > 0; j--) {
                commands.remove(commands.size() - 1);
            }
        }

        for (int c : commands) {
            sum += c;
        }

        System.out.println(Math.floorMod(sum, n));
    }
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106