We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is my code in java, It is passing all test cases but test case 10 and 11,which shows Time Limit exceeded. Can Someone please suggest me a way to optimize it?
public class Solution {
public static void main(String[] args)throws IOException {
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
String s[]=obj.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int Q=Integer.parseInt(s[1]);
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=i;
for(int t=0;t<Q;t++)
{
String s1[]=obj.readLine().split(" ");
int p,q,r;
p=Integer.parseInt(s1[0]);
switch(p)
{
case 1:
q=Integer.parseInt(s1[1]);
r=Integer.parseInt(s1[2]);
for(int i=0;i<(r-q)/2+1;i++)
{
int temp=a[r-i];
a[r-i]=a[i+q];
a[i+q]=temp;
}
break;
case 2:
q=Integer.parseInt(s1[1]);
for(int i=1;i<=n/2+1;i++)
{
if(a[i]==q)
{
System.out.println("element "+q+" is at position "+i);
break;
}
if(a[n-i+1]==q)
{
System.out.println("element "+q+" is at position "+(n-i+1));
break;
}
}
break;
case 3:
q=Integer.parseInt(s1[1]);
System.out.println("element at position "+q+" is "+a[q]);
break;
}
}
// System.out.println(Arrays.toString(a));
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The crazy helix
You are viewing a single comment's thread. Return to all comments →
public class Solution {
}