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.09. Find Pair With Given Sum in an Array.html -- 1.8 meg
Find Pair With Given Sum in an Array - Coderust: Hacking the Coding Interview
We’re given an array of integers and a value. Determine if there are any two integers in the array whose sum is equal to the given value. Return true if the sum exists; otherwise, return false.
In this solution, we’ll use the following algorithm to find a pair that adds to the target (say val).
Scan the whole array once and store visited elements in a hash set.
During the scan, for every element e in the array, we check if val - e is present in the hash set. In other words, we check if val - e was already visited.
If val - e is found in the hash set, it means there is a pair (e, val - e) in the array whose sum is equal to the given val, and the function returns true.
If we exhaust all elements in the array and don’t find any such pair, the function returns false.
Let’s visually run this algorithm:
Created with Fabric.js 3.6.6Initial state5712843val = 10hashsetvalue
1 of 6
Created with Fabric.js 3.6.610 - 5 = 55 is NOT present in the hashset.Add 5 into the hashset.5712843val = 10hashsetvalue5
1 of 6
Created with Fabric.js 3.6.610 - 7 = 33 is NOT present in the hashset.Add 7 into the hashset.5712843val = 10hashsetvalue57
1 of 6
Created with Fabric.js 3.6.610 - 1 = 99 is NOT present in the hashset.Add 1 into the hashset.5712843val = 10hashsetvalue571
1 of 6
Created with Fabric.js 3.6.610 - 2 = 88 is NOT present in the hashset.Add 2 into the hashset.5712843val = 10hashsetvalue5712
1 of 6
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>
#include<unordered_set>
usingnamespace std;
bool FindSumOfTwo(vector<int>& nums,int val){
// Initializing a hash set
unordered_set<int> found_values;
// Traversing the nums list for each element 'ele'
for(int& ele : nums){
// If (val - ele) is present in the hash set, there is
// at least one value among those already traversed that,
Note: This solution works only for the sorted array.
To understand the algorithm for this solution, let’s look at the pseudo-code below:
// assume 0 is the first index in array // and n is the total number of elements in array left =0 right = n -1 while left is less than right sum = array[left]+ array[right] if sum == val returntrue if sum is less than val // sum is smaller than value // means pair can only exist to the // right of left element, so left element // should be moved one step next left = left +1 else // sum is greater than value // means pair can only exist to the // left of right element, so right element // should be moved one step previous right = right -1
Let’s see a visual representation of the algorithm:
Created with Fabric.js 3.6.6Initial state134571415val = 11
1 of 7
Created with Fabric.js 3.6.6We start from the left and right ends of the input array. Left is the first element of array and right is the last element of array.sum = left + right = 16left134571415rightval = 11sum = 16
1 of 7
Created with Fabric.js 3.6.6Sum of left and right elements is 16 which is larger than given value 11. So we'll move right one step backward.sum = left + right = 15left134571415rightval = 11sum = 15
1 of 7
Created with Fabric.js 3.6.6Sum of left and right elements is 15 which is still larger than given value 11. So we'll move right one step backward.sum = left + right = 8left134571415rightval = 11sum = 8
1 of 7
Created with Fabric.js 3.6.6Now sum of left and right elements is 8 which is smaller than given value 11. So we'll move left one step forward.sum = left + right = 10left134571415rightval = 11sum = 10
1 of 7
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>
#include<algorithm>
usingnamespace std;
bool FindSumOfTwo(vector<int>& nums,int val){
// Sort the array
sort(nums.begin(), nums.end());
// Initializing markers i and j
int i =0;
int j = nums.size()-1;
while(i < j){
// Adding two marker's elements
int sum = nums[i]+ nums[j];
// Return true if nums_sum is equal to the val
if(sum == val){
returntrue;
}
// Updating low-end marker 'i' if nums_sum is less than the target value
if(sum < val){
++i;
}
// Updating high-end marker 'j' if nums_sum is greater than the target value
else{
#
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool FindSumOfTwo(vector<int>& nums, int val) {
// Sort the array
sort(nums.begin(), nums.end());
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.