Intro to Tutorial Challenges

Sort by

recency

|

395 Discussions

|

  • + 0 comments

    Here are my c++ solutions, you can watch the explanation here : https://youtu.be/teDaBhua0bc

    solution 1:

    int introTutorial(int V, vector<int> arr) {
        for(int i = 0; i < arr.size(); i++) if(arr[i] == V) return i;
        return 0;
    }
    

    solution 2:

    int introTutorial(int V, vector<int> arr) {
        return distance(arr.begin(), find(arr.begin(), arr.end(), V));
    }
    
  • + 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;
        }
    
  • + 0 comments

    My C code 😁😎

    int introTutorial(int V, int arr_count, int* arr) {
        int index = 0;
        while(arr[index] != V){
            index++;
        }
        return index;
    }
    
  • + 0 comments

    Javascript solution:

    function introTutorial(V, arr) {
        // Write your code here
        for(let i=0; i<arr.length; i++){
            if(V == arr[i]) return i
        }
    }
    
  • + 0 comments

    My solution using Python.

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'introTutorial' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts following parameters:
    #  1. INTEGER V
    #  2. INTEGER_ARRAY arr
    #
    
    def search(arr, V, low, high):
        if low == high:
            if arr[low] == V:
                return low
            else:
                return -1
        
        third = (high - low + 1) // 3
        mid1 = low + third
        mid2 = high - third
        
        if arr[mid1] == V:
            return mid1
        if arr[mid2] == V:
            return mid2
        
        if V < arr[mid1]:
            return search(arr, V, low, mid1 - 1)
        elif V > arr[mid2]:
            return search(arr, V, mid2 + 1, high)
        else:
            return search(arr, V, mid1 + 1, mid2 - 1)
    
    def introTutorial(V, arr):
        # Write your code here
       return search(arr, V, 0, len(arr) - 1)
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        V = int(input().strip())
    
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
    
        result = introTutorial(V, arr)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()