We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Intro to Tutorial Challenges
Intro to Tutorial Challenges
+ 0 comments My Java solutions, feel free to ask me any questions.
public static int introTutorial(int V, List<Integer> arr) { int left = 0; int right = arr.size() - 1; while (left <= right) { int pivot = (left + right) / 2; int pivotValue = arr.get(pivot); if (V == pivotValue) { return pivot; } if (V < pivotValue) { right = pivot - 1; } else { left = pivot + 1; } } return -1; }
public static int introTutorial(int V, List<Integer> arr) { return arr.indexOf(V); }
+ 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 Python:
def introTutorial(V, arr): for i in range(len(arr)): if (arr[i]==V): print(i) return i
+ 0 comments C solution
int introTutorial(int V, int size, int* arr) { int first = 0, last = size - 1, middle = 0, index = -1; while(last >= first) { middle = (last+first)/2; if (arr[middle] == V) { index = middle; break; } else if(arr[middle] > V) { last = middle - 1; } else if(arr[middle] < V) { first = middle + 1; } } return index; }
+ 0 comments Can someone try with O(log(n)) solution with CPP? It is easy
Load more conversations
Sort 372 Discussions, By:
Please Login in order to post a comment