File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Codechef Problems/DSA Learning Series Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* 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.
2
+
3
+ Input:
4
+ First-line will contain two numbers N and K.
5
+ Next line contains N space-separated numbers.
6
+ Output:
7
+ Print the answer in a new line.
8
+
9
+ Constraints
10
+ 1≤N,K,Ai≤105
11
+ Sample Input 1:
12
+ 4 2
13
+ 1 2 3 4
14
+ Sample Output 1:
15
+ 1
16
+ Sample Input 2:
17
+ 4 4
18
+ 1 2 6 9
19
+ Sample Output 2:
20
+ -1
21
+ EXPLANATION:
22
+ In the first example, as 2 is present in the list.
23
+ In the second example, 4 is not present in the list. */
24
+
25
+ #include <stdio.h>
26
+
27
+ int main (void ) {
28
+
29
+ int n ,k ;
30
+ scanf ("%d %d" ,& n ,& k );
31
+ int a [n ];
32
+ int c = 0 ;
33
+ for (int i = 1 ; i <=n ; i ++ )
34
+ {
35
+ scanf ("%d " ,& a [i ]);
36
+ if (a [i ]== k )
37
+ {
38
+ c = 1 ;
39
+ }
40
+ }
41
+
42
+ if (c == 1 )
43
+ {
44
+ printf ("1" );
45
+ }
46
+ else
47
+ {
48
+ printf ("-1" );
49
+ }
50
+ return 0 ;
51
+ }
You can’t perform that action at this time.
0 commit comments