Help me with this problem , I am constantly getting wrong answer

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.

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

Test Cases -
Sample Input 1:
4 2
1 2 3 4

my code:

N,K = map(int,input().split())
list1 = list(map(int,input().split()))[:N]
flag = -1
#checking for the value of K in list1
for i in list1:
if i == K:
flag = 1

question link
my solution

1 Like

I don’t know much about python so can’t point out the problem in your code but this got AC :point_down:

n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = -1
for i in arr:
    if i == k:
        ans = 1

I don’t know much about python as I code in c++ but I can tell u the logic

  1. store the input in an array
  2. now traverse the array or list (in python) from start to end
  3. if u find the number k then return 1
  4. else if u reach the end of array and couldn’t find the number return 0

You Could Just Use The In Operator.

N, K = map(int, input().split(" "))
if str(K) in input():