File tree 1 file changed +56
-0
lines changed
Course 2 - Data Structures in JAVA/Lecture 4 - Recursion I
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments