FINDMELI DSA learning problem can any one help me

Is anyone help me on this question In C,C++ language

You are given a list of NN integers and a value KK. Print 11 if KK exists in the given list of NN integers, otherwise print −1−1.

Input:

  • First-line will contain two numbers NN and KK.
  • Next line contains NN space-separated numbers.

Output:

Print the answer in a new line.

You can check this code

Why give a wrong answer in this code :neutral_face:
Question : https://www.codechef.com/YTPP001/problems/FINDMELI

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int n,k;
	cin>>n>>k;
	int a[n];
	
	for(int i=0;i<n;i++)
	{
	    cin>>a[i];
	}
	
	for(int i=0;i<n;i++)
	{
	    if(a[i]==k)
	    {
	        cout<<"1"<<endl;
	        break;
	    }
	    else if(a[i]!=k && a[i]==a[n-1])
	    {
	        cout<<"-1"<<endl;
	    }
	}
	return 0;
}

remove else if argument and condition for n&k&ai to be >=1(given in the question in constraint, cant be 0)

1 Like

// Anyone can Tell what is wrong with this code.

#include
using namespace std;
int main()
{
int N,K,a[10],i,count=0;
cin>>N>>K;
for(i=0;i<N;i++)
{
cin>>a[i];
}
for(i=0;i<N;i++)
{
if(a[i]==K)
{
count++;
}
else
{
continue;
}
}
if(count==1)
{
cout<<“1”;
}
else
{
cout<<"-1";
}
return 0;
}

Change this to

if(count >= 1)
1 Like