ignus deoria the great city...why i am getting wrong??

#include
using namespace std;

struct node
{

	int data;
	node *next;
};

node *head;
node *tail;
int count;

void push(int b,int n)
{

	node *x=new node;
	x->data=b;
	x->next=NULL;
	if(count==0)
	{
		head=x;
		tail=x;
	}
	else if(count>=n)
	{

			tail->next=x;
			tail=tail->next;
			head=head->next;
	}
	else
	{
			tail->next=x;
			tail=tail->next;
	}
}

bool search(int b)
{

	node *x;
	x=head;
	bool flag= false;
	if(x==NULL)
	{
		return flag;
	}
	while(x!=NULL)
	{
		if((x->data)==b)
		{
			flag=true;
			return flag;
		}
		x=x->next;
	}
	return flag;
}


void print()
{
	node *x;
	x=head;
	while(x!=NULL)
	{
		cout<<x->data<<" ";
		x=x->next;
	}
	cout<<"\n";
}


	
	
 	

//main function
int main()
{

	int t,m,n,a,b;
	cin>>t;
	while(t--)
	{
	
		head=NULL;
		tail=NULL;
		cin>>n>>m;
		count=0;
		while(m--)
		{
			cin>>a>>b;
			if(a==1)
			{
	
				push(b,n);
				++count;
			
			}
			if(a==2)
			{
				if(search(b))
				{
					cout<<"YES\n";
				}
				else
				{
					cout<<"NO\n";
				}
			}
		}
		
	}
	return 0;
}

You didn’t checked if he already remembers that friend before pushing.
Take the case


n=2 q=5
1 1
1 2
1 2
2 1
2 2

Ans should be YES for both but you code will give NO for first but YES for second. What you have to do is this, if he already remembers that friend you have to remove him and add him at the last. This question is same as LRU scheduling implementation.

I hope it helps

Check my code java implementation

3 Likes

Thanks to ashish, even I made the same mistake and was getting wrong answer… :slight_smile: Thanks!

1 Like

@ashish…reali thanks bro:)