Zeta | Senior SDE | R1 | PS/DS | Pass
Anonymous User
547

Q1: Given an unsorted array of unique elements, find the max length of increasing consecutive sequence which can be built.
Eg. arr = [5,2,1,0,3,6,7,12,13] return maxlength = 4, [ as 0,1,2,3 is the longest consecutive sequence ]

Q2: Given an array of integer numbers sorted in ascending order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1]
Eg. nums = [1, 2, 2, 3, 3, 5, 6,7], target = 2
Ans: [1, 2]

int lowerBound(vector<int> &arr, int target) {
	int n = arr.size();
	int result = -1;
	int l = 0, r = n - 1;
	while(l <= r) {
		int mid = (l + (r - l) / 2);
		if (arr[mid] == target) {
			r = mid - 1;
			result = mid;
		} else if (arr[mid] > target) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	return result;
}
Comments (1)