public class SearchInRange {
public static void main(String[] args) {
//arr=[18,12,-7,3,14,28]
//search for 3 in the range of index[1,4]
int[] nums={1,2,3,4,5,6,7,78,89};
int target=5;
System.out.println(search(nums,target,2,7));
}
static int search(int[] arr, int target,int start,int end) {
if (arr.length == 0) {
return -1;
}
//check for a element at every index if it is =target
for (int index = start; index < end; index++) {
int element = arr[index];
if (element == target) {
return index;
}
}
return -1;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter