Join Regular Classroom : Visit ClassroomTech

Tata Cliq Overall Interview Questions + Coding Solutions – codewindow.in

Hot Topics

Tata Cliq Solution

Technical Round

def first_occurrence(arr):
    left = 0
    right = len(arr) - 1
    result = -1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == 1:
            result = mid
            right = mid - 1
        else:
            left = mid + 1
    return result
This algorithm starts by setting the left and right pointers to the first and last elements of the array, respectively. It then repeatedly divides the search interval in half by updating the left and right pointers. If the element at the middle index is equal to 1, it updates the result variable to the current index and moves the right pointer to the left, otherwise it moves the left pointer to the right. If the search interval becomes empty, the algorithm returns the result variable, which will be -1 if no 1’s were found or the index of the first occurrence of 1 if one was found.
This algorithm has a time complexity of O(log n), which makes it efficient for large arrays.
Note that this algorithm assumes that the array is sorted, otherwise this method will not work.
#include <iostream>
#include <list>
#include <unordered_set>
using namespace std;

void removeDuplicates(list<int>& myList) {
    unordered_set<int> set;
    for (auto it = myList.begin(); it != myList.end();) {
        if (set.find(*it) != set.end()) {
            it = myList.erase(it);
        }
        else {
            set.insert(*it);
            ++it;
        }
    }
}

int main() {
    list<int> myList = {1, 2, 3, 3, 4, 5, 5, 6};
    removeDuplicates(myList);
    for (auto i : myList) {
        cout << i << " ";
    }
    return 0;
}
/*
OUTPUT
1 2 3 4 5 6 
*/

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories