Search In range

PHOTO EMBED

Thu Jul 18 2024 08:04:26 GMT+0000 (Coordinated Universal Time)

Saved by @Mohanish

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;
    }
}
content_copyCOPY