To make sure your calendar, event reminders, and other features are always
correct, please tell us your time zone (and other details) using the
drop-down menus below:
Set Date/Time format:
In 12 Hour format the hours will be displayed as 1 through 12 with “a.m.” and “p.m.”
displayed after the time (ex. 1:00p.m.). In 24 hour format the hours will be displayed as 00 through 23 (ex. 13:00).
You can always change your time zone by going to your Account Settings.
Use the dropdown menu to view the events in another time zone. The primary time zone will be displayed in parentheses.
Use the dropdown menu to view the events in another time zone. The primary time zone will be displayed in parentheses.
Visiting Rany Milton(username: itsrandy)
Tag
Please wait...
Select a Color
Manage Applications
Check the items that you want displayed. Uncheck all to hide the section.
Calendars
Files
Addresses
To Dos
Discussions
Photos
Bookmarks
The “Switch Navigator” button will no longer be available after February 14, 2017.
Please learn more about how to use the new Navigator by clicking this link.
01.DS.02.05. Find Low-High Index of a Key in a Sorted Array.html -- 1.7 meg
Find Low/High Index of a Key in a Sorted Array - Coderust: Hacking the Coding Interview
We’re given a sorted array of integers, nums, and an integer value, target. Return the low and high index of the given target element. If the indexes are not found, return -1.
Note: The array can contain multiple duplicates with length in millions.
#include <iostream>
#include <vector>
using namespace std;
int FindLowIndex(vector<int>& nums, int target) {
return -2;
}
int FindHighIndex(vector<int>& nums, int target) {
Linearly scanning the sorted array for low and high indices is highly inefficient since our array size can be in the millions. Instead, we will use a slightly modified binary search to find the low and high indices of a given target.
We need to binary search twice: the first time to find the low index and a second time to find the high index.
Let’s look at the algorithm for finding the low index:
At every step, consider the array between low and high indices and calculate the mid index.
If the element at the mid index is less than the target, the value of low becomes mid + 1 (to move towards the start of range). The value of high remains the same.
If the element at mid is greater or equal to the target, the value of high becomes mid - 1. The value of low remains the same.
When the value of low is greater than high, low would point to the first occurrence of the target.
If the element at low does not match the target, return -1. It means that the target doesn’t exist in the array.
Similarly, we can find the high index by slightly modifying the above algorithm:
If the value at the mid index is less than or equal to the target, the value of low becomes mid + 1.
If the value at mid is greater than the target, the value of high becomes mid - 1.
Let’s find low and high indices of 555 in the given example below:
Created with Fabric.js 3.6.6low012345678910125555555520midhighkey: 5Let's start with finding low index on array 'arr'We will move low and high towards each otheruntil low crosses high.
1 of 12
Created with Fabric.js 3.6.6low012345678910125555555520midhighkey: 5mid = 5arr[5] >= 5 so high will move to mid-1
1 of 12
Created with Fabric.js 3.6.6mid = 2arr[2] >= 5 so high will move to mid-1 againlow012345678910125555555520midhighkey: 5
1 of 12
Created with Fabric.js 3.6.6low012345678910125555555520midhighkey: 5mid = 0arr[0] < 5 so low will move to mid+1
1 of 12
Created with Fabric.js 3.6.6mid =1arr[1] < 5 so low will move to mid+1 againlow012345678910125555555520midhighkey: 5
1 of 12
C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<vector>
usingnamespace std;
// Finding the low index of the target element
int FindLowIndex(vector<int>& nums,int target){
int low =0;
int high = nums.size()-1;
int mid = high /2;
while(low <= high){
int mid_elem = nums[mid];
// Target value is less than the middle value
if(mid_elem < target){
low = mid +1;
}
// Target value is greater than or equal to the middle value
else{
high = mid -1;
}
// Updating the mid value
mid = low +(high - low)/2;
}
if(low < nums.size()&& nums[low]== target){
return low;
}
#
#include <iostream>
#include <vector>
using namespace std;
// Finding the low index of the target element
int FindLowIndex(vector<int>& nums, int target) {
int low = 0;
int high = nums.size() - 1;
int mid = high / 2;
Since we are using binary search, the time complexity is logarithmic, O(logn)O(logn)O(logn). Even though we do the binary search twice, the asymptotic time complexity is still O(logn)O(logn)O(logn).
Attach this document to an event, task, or address
You can attach a link to this document to an event in your Calendar, a task in your To Do list or an Address. Check the boxes below for the data you want to
bring into the event’s or task’s description, and then click “Select text to copy” to have the next event or task you create or edit have the document text and link.