forked from AdityaSubrahmanyaBhat/hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear.java
33 lines (32 loc) · 823 Bytes
/
linear.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.company;
public class linearWithRecursion {
public static void main(String[] args) {
int[] arr={3,2,1,18,9};
int target=18;
System.out.println(findElement(arr,0,target));
System.out.println(findElementRev(arr,arr.length-1,target));
}
static int findElement(int[] arr,int s,int target)
{
if(s==arr.length)
{
return -1;
}
else if(arr[s]==target)
{
return s;
}
return findElement(arr,s+1,target);
}
static int findElementRev(int[] arr,int s,int target)
{
if(s==-1)
{
return -1;
}
else if(arr[s]==target)
{
return s;
}
return findElement(arr,s-1,target);
}