Hari Kiran Vusirikala
Hari Kiran Vusirikala

Reputation: 113

My Java code is getting TimeLimit exceeded whereas the same logic in C/C++/Python/Python3 are getting accepted for larger testcases

Constraints:

0<T<100
0<N<10000

Input:

2
6
abcdef 
3 
abc 

Output:

bdfeca 
bca

I tried to get this Problem fixed so much time and fed up with the wrong that I was getting,

I know C, C++ are faster, as they use only Compiler, compared to Java, Python, as they use Interpreter also,

But my code is getting finely done for Python2 and Python3,

Is their any other Logic that decrease the Time Complexity than this,

I think I have done it in O(n), Time Complexity.

My code in Java:(Where my code is getting Time Limit exceeded)

import java.util.*;
class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine()), i;
        while ( t --> 0 )
        {
            int n = Integer.parseInt(sc.nextLine());
            String a = sc.nextLine();
            for(i=1;i<n;i+=2)
                System.out.print(a.charAt(i));
            i=n-1;
            if(n%2==0)
                i--;
            for(;i>=0;i-=2)
                System.out.print(a.charAt(i));
            System.out.println();
        }
    }
}

My code in C:

#include <stdio.h>
int main()
{
    int t, n, i;
    char a[10000];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %s",&n,a);
        for(i=1;i<n;i+=2)
            printf("%c",a[i]);
        i=n-1;
        if(!(n&1))
            i--;
        for(;i>=0;i-=2)
            printf("%c",a[i]);
        printf("\n");
    }
    return 0;
}

My code in Python3:

from __future__ import print_function
t = int(input())
for i in range(t):
    n=int(input())
    a=input()
    for i in range(1,n,2):
        print(a[i],end='')
    i=n-1
    if(n%2==0):
        i=i-1
    for q in range(i,-1,-2):
        print(a[q],end='')
    print("\n")

Limits:
Time Limit: 0.15 sec(s) for each input file. Memory Limit: 256 MB Source Limit: 1024 KB

Upvotes: 1

Views: 94

Answers (1)

Hari Kiran Vusirikala
Hari Kiran Vusirikala

Reputation: 113

I got the mistake where my Java code is getting slowed down,

i.e., at System.out.println(a.charAt(i)),

So to minimize it we use StringBuilders and update it and then finally print at final

By this we can say that public char charAt(int index) method takes some what more time compared to public String substring(int beginIndex, int endingIndex)(one of the Constructors)

So, My final Java Code Satisfying all conditions(with less time compared to before) is:

import java.util.*;
class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine()), i;
        while(t-->0)
        {
            int n = Integer.parseInt(sc.nextLine());
            StringBuilder str = new StringBuilder("");
            String a = sc.nextLine();
            for(i=1;i<n;i+=2)
                str.append(a.substring(i,i+1));
            i=n-1;
            if(n%2==0)
                i--;
            for(;i>=0;i-=2)
                str.append(a.substring(i,i+1));
            System.out.println(str);
        }
    }
}

Upvotes: 1

Related Questions