Intro to Tutorial Challenges

  • + 0 comments

    The two-pointer approach reduces the number of iterations compared to a simple linear search.

        public static int introTutorial(int V, List<Integer> arr) {
        // Write your code here
            for (int i = 0, j = arr.size() - 1; i < j; i++, j--) {
                if (Objects.equals(arr.get(i), V)) {
                    return i;
                } 
                
                if (Objects.equals(arr.get(j), V)) {
                    return j;
                } 
            }
            
            return -1;
        }