0

I have been solving competitive coding questions for a brief period of time now, and I have observed something. Sometimes, in any solution involving array manipulations or addition or multiplication, some of the test cases gave me TLE (time limit exceeded).

I used integer variables. When I change such variables to the long type, the execution time for that test case reduces, and sometimes, makes my solution excepted 100% instead of partial acceptance.

My question is, how, and why, is this the case? And in which scenarios would this (long over int) not hold true? The only disadvantage I can think of for not using long over int in such scenarios would be higher memory consumption.

UPDATE - Code Example: Here is a sample code I wrote to rotate a given array of numbers to the right by K places and display the output.

 public static void main(String [] args)
{
    Scanner sc = new Scanner(System.in);
    StringBuilder sb = new StringBuilder();
    int numTestCases = sc.nextInt();

    for(int i = 0; i< numTestCases;i++)
    {
        int arraySize = sc.nextInt();
        int offset = sc.nextInt();
        int [] array = new int[arraySize];

        if(offset>=arraySize)
            offset = offset%arraySize;

        int count = 0;
        while(count<arraySize)
        {
            array[offset] = sc.nextInt();
            offset = (offset+1)%arraySize;
            count++;
        }

        for(int j = 0; j< arraySize; j++) {
            sb.append(array[j]);
            sb.append(" ");
        }

        sb.append("\n");
    }

    System.out.println(sb.toString());
    sc.close();
}

One of the test cases gave a TLE for this code. However, when changed the array type to long, that test case passed.

The test case contained an array of 89384 integers (all less than 100 - or atleast within int range) and had to be rotated right by 930886 places.

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • 1
    This is way too theoretical. Please provide some actual, [valid benchmarks](https://stackoverflow.com/q/504103/1553851) to make the question more meaningful. – shmosel Sep 19 '17 at 19:07
  • What @shmosel said, plus some sample code would help too. – twm Sep 19 '17 at 19:09
  • Please let me know what additional details I need to provide. I will update my question. – SoulRayder Sep 19 '17 at 19:10
  • @twm : Updated with a code example. – SoulRayder Sep 19 '17 at 19:18
  • @shmosel : Updated with an example, with my code, as well as the test case in question. – SoulRayder Sep 19 '17 at 19:22
  • My guess is that the environment where that code is running and measured is not stable and controlled enough to provide accurate, reproducible times. I would focus on readability and maintainability first: your code will be just as efficient if you use clear, meaningful variable names that respect the Java naming conventions. – JB Nizet Sep 19 '17 at 19:26
  • Updated code for improved readability. I tried running the code a few times repeatedly, but still got same TLE result every time. It was only when I changed the array type to long that this test case passed. – SoulRayder Sep 19 '17 at 19:40

0 Answers0