Skip to content

Commit bf02a1b

Browse files
authored
Create CheckNumArray.java
1 parent ffb0fec commit bf02a1b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Given an array of length N and an integer x, you need to find if x is present in the array or not. Return true or false. Do this recursively.
3+
4+
Input Format :
5+
Line 1 : An Integer N i.e. size of array
6+
Line 2 : N integers which are elements of the array, separated by spaces
7+
Line 3 : Integer x
8+
9+
Output Format :
10+
'true' or 'false'
11+
12+
Constraints :
13+
1 <= N <= 10^3
14+
15+
Sample Input 1 :
16+
3
17+
9 8 10
18+
8
19+
Sample Output 1 :
20+
true
21+
22+
Sample Input 2 :
23+
3
24+
9 8 10
25+
2
26+
Sample Output 2 :
27+
false
28+
*/
29+
30+
public class CheckNumArray {
31+
32+
public static boolean checkNumber(int input[], int x) {
33+
if (input.length==1)
34+
{
35+
if (input[0]==x)
36+
{
37+
return true;
38+
}
39+
else
40+
{
41+
return false;
42+
}
43+
}
44+
else if (input[input.length-1]==x)
45+
{
46+
return true;
47+
}
48+
int[] smallCheck = new int[input.length-1];
49+
for (int i=0;i<input.length-1;i++)
50+
{
51+
smallCheck[i]=input[i];
52+
}
53+
boolean output=checkNumber(smallCheck,x);
54+
return output;
55+
}
56+
}

0 commit comments

Comments
 (0)