HELP me with this question

You are given a list of N integers and a value K. Print 1 if K exists in the given list of N integers, otherwise print −1.
help me with this i have write this code but it is saying it wrong

#include
using namespace std;

int main()

{
int n,k,i;
int a[5];

cin>>n>>k;
for(i=0;i<5;i++)
{
cin>>a[i];

}
while(1)
{
if(a[i]==k)
cout<<"\n"<<“1”;
else
cout<<"\n"<<"-1";
break;
}
return 0;

}

You are declaring the size of array to be 5 .You should declare the array after taking input of size of array as a[n]

Update your variable " i " in while loop

#include
using namespace std;

int main() {
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";
    }else{
        cout<<"-1";
    }
}
return 0;

}

I wrote this is code and is saying wrong, I don’t understand where am I going wrong

1 Like

Consider this input

5 3
1 2 3 4 5

Expected Output

1

Your output

-1-11-1-1

is there anything wrong with my logic ?

1 Like

A simple break statement would help. Also, you might want to use a boolean variable for handling -1 case.

1 Like

Hii Anushka
your logic is wrong because if(arr[i]==k) it will print (1) for single index and print(-1) for left indexes .means 1 and -1 both print till n length for k exist and if k not exist print only (-1) till n length.
see below My Code
Hope it will works.

#include<bits/stdc++.h>
using namespace std;
bool present(int arr[],int n,int k)
{
    for(int i=0;i<n;i++)
    {
        if(arr[i]==k)
            return true;
    }
    return false;
}
int main()
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
        cin>>arr[i];
    if(present(arr,n,k))
        cout<<"1"<<endl;
    else
        cout<<"-1"<<endl;
}
2 Likes

No your code is perfect

@anon27268501, there are a few things you’d want to do in order to fix your code. First off, you would need to include iostream. Next, the size of your array is independent of the value of N. You are using a value of 5 everywhere. If you change that 5 to N, you should be good. Next, in the second loop which you are using to check if any of the values is equal to K, you’d need to initialize the value of i back to 0 and you’d need to increment the value of i after every iteration of the loop. A good idea would be to use a for loop just like the one you used to take the input. Now, coming to the body of the loop, you want to check this condition for all the values inside the array. In your code, the loop would exit in its very first iteration since the break would get executed no matter the result of the condition in the body (a[i] == k). You want the break to be executed ONLY when you encounter a value that is equal to K (hey look I found a value that is equal to K at index 5 in the array, so no need to look at positions 6, 7, 8, etc.). Lastly, I’d recommend indenting your code - this will save you tons of hours and headaches in the future. (Apologies if there were formatting issues while pasting your code in the forum)

@anushka5653, you kind of got the gist of it except there is one problem in your code’s logic - you are printing the output for every single value present in the array. You should tell the program to quit once you find a value present in the array that is equal to K. In other words, you’ll start checking the first element. If that is equal to K, print 1 and quit. If not, move on to the next element. So if the last element is not equal to K, we know that there is no element present in the array that is equal to K.

Here's an approach I'd use in C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);

    for (auto &e : a) cin >> e;

    bool present = false;
    for (auto e : a) present |= (e == k);

    cout << (present ? 1 : -1) << endl;
}

Don’t worry if that didn’t make any sense.

Here's a simpler approach in C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, k, i;
    cin >> n >> k;

    vector<int> a(n);
    for (i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    bool found = false;
    for (i = 0; i < n; i++)
    {
        if (a[i] == k)
        {
            found = true;
            break;
        }
    }

    if (found)
    {
        cout << 1 << endl;
    }
    else
    {
        cout << -1 << endl;
    }
}
2 Likes

Thank you I understood my mistake. Thank you everyone!!

1 Like