Time limit exceed

i can’t debug it. can you help me in finding out what is wrong with my code?

Hey @montymehta ya sure, please share your code using ide.codingblocks.com
will help you in debugging it.

hi, @Encoder can you share your email.

#include using namespace std; int main() { int n; cin>>n; int*arr=new int[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } int p0,p1; p0=p1=0; while( p1<n && p0<n){ while(p0<n && arr[p0]!=0){ p0++; } p1=p0; while(p1<n && arr[p1]==0){ p1++; } if(p1<n && p0<n && p1>p0){ int temp=arr[p0]; arr[p0] =arr[p1]; arr[p1] = temp; } } for(int i=0;i<n;i++){ cout<<arr[i]<<" "; } return 0; }

You can share this code using ide.codingblocks.com

please find the link.

I got your code, it’s working fine. Why you are getting TLE is because of the time complexity of your program, you are using two nested loops , resulting time complexity in O(N^2) , your brute force is working fine. Now try to optimise your code.
Hint: you just have to maintain count of a single number 0, and have to swap other numbers.
If you face any other difficulty ask me here only.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.