Skip to content

Commit 8f4cc9b

Browse files
Merge pull request #248 from Kaustubh-git/search
Searching element in an array within given range
2 parents b912c54 + 60319b6 commit 8f4cc9b

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Searching/searchinrange.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Searching element in an array within given range.
2+
package com.company;
3+
4+
public class searchinrange {
5+
public static void main(String[] args) {
6+
int[] arr={12,1,4,45,84,65,41,23,78,98};
7+
int target=98;
8+
int ans=rser(arr,target,1,5); //passing values to the function
9+
if(ans==0){
10+
System.out.println("Array is empty");
11+
}
12+
if (ans==1){
13+
System.out.println("Target element present in an array within given range");
14+
}
15+
if (ans==2){
16+
System.out.println("Target element not there in an array within given range");
17+
}
18+
19+
}
20+
static int rser(int[] arr,int target,int start,int end ){ //declaring function
21+
if(arr.length==0){ // checking array size
22+
return 0;
23+
}
24+
for(int i=start;i<=end;i++){ // loop for array to check in range
25+
int elem=arr[i];
26+
if(elem==target){ // target element found
27+
return 1;
28+
}
29+
}
30+
return 2;// loop out target is not there
31+
}
32+
}

0 commit comments

Comments
 (0)