Turbo sort problem

#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
int array[t];
if(t<=1010101010*10)
{
for(int i=0;i<t;i++)
{
cin>>array[i];
}
sort(array,array+t);
for(int n=0;n<t;n++)
{if(array[n]!=array[n+1])
{cout<<array[n];}}
}
return 0;
}




this is my code…ideone accepts it but codechef gives"wrong answer"…please help

Hello,

You should replace this line:

{cout<<array[n];}} 

with this other line:

 {cout<<array[n] << endl;}} 

to avoid the WA veredict. Nontheless, your code will give almost surely TLE…

You should instead attempt to use faster I/O methods :slight_smile:

Good luck,

Bruno

1 Like

According to your code You are printing the sorted numbers without new line and u are removing adjacent duplicates

Here is the corrected code

  #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
     {
         int t;
     cin>>t;
     int array[t];
     if(t<=10*10*10*10*10*10)
      {
          for(int i=0;i<t;i++)
           { 
              cin>>array[i];
           }
          sort(array,array+t);
           for(int n=0;n<t;n++)
           {
           {cout<<array[n]<<endl;}}   
      }
      return 0;
     }

According to Ur code As u are using if(a[n] != a[n+1]) statement for input’s containing duplicate elements the answer will be wrong

Try Using faster I/O method’s

2 Likes

This is your corrected code…the errors are explained in the code itself…!!!

Hope it helps…:slight_smile:

1 Like

When i compile it shows runtime error?

#include<iostream>
using namespace std;

int main()
{
	int t, temp;
	int N[100000];

	cin>>t;
	if(t<=1000000)
	{
		for(int k=0;k<t;k++)
		{
			cin>>N[k];
		}

	
		for(int j=0;j<t-1;j++)
		{
			for(int i=0;i<t-j-1;i++)
			{
				if(N[i]>N[i+1])
				{
					temp=N[i];
					N[i]=N[i+1];
					N[i+1]=temp;
				}
			}
		}

		for(int x=0;x<t;x++)
		{
			cout<<N[x]<<endl;
		}
	}
	else

		return 0;
}

@vjozic …the RE is due the low array size…the array N that u have declared is of size 10^5 where as there can be 10^6 numbers that can be given as input…also rethink on ur algo as bubble sort is bound to give a TLE…use the sort fxn of algorithm header file…or implement merge sort…hope this helps…:slight_smile: