Lecture 2 Binary SearchTwo Pointers
Lecture 2 Binary SearchTwo Pointers
So,
Implementation
generally
marked by two
variables, start
and end.
Consider a sorted array containing 54.
You wanna find its position.
00000000000000001111111111111
Question Time
Question
Find the smallest integral n such that n²+n>1e18
int main(){
long long n = 1e9 ;
long long l = 0, r = n + 1;
while (l < r){
long long mid = (l + r) / 2; Code for
long long temp = mid * mid + mid;
int main(){
long long n = 1e9 ;
long long l = 0, r = n + 1;
while (l < r){
long long mid = (l + r + 1) / 2;
Code for
long long temp = mid * mid + mid; finding
if (temp <= n){
l = mid; last 0.
}
else{
r = mid - 1;
}
}
cout << l << endl;
return 0;
}
Implementing
Interesting, we can see that whenever, searching
anything in a monotonous collection, our binary search
problem can be either be broken down into largest value
satisfying or smallest value not satisfying
int main(){
int t;
cin >> t;
while(t--){
ll n, x;
cin >> n >> x;
ll height[n];
ll l = 0, r = 1e10;
while (l < r){
ll mid = (l + r) / 2;
ll count = 0;
int main(){
ll n, k;
cin >> n >> k;
ll times[n];
for (int i = 0; i < n; i++){
cin >> times[i];
}
ll l = 0, r = 1e18 + 1;
while (l < r){
ll mid = (l + r) / 2;
ll count = 0;
bool chk = 0;
}
cout << l << endl;
}
Binary Search available in
STL?
Direct Functions available in STL library of C++
so you don’t have to write the implementation.
Lower Bound:
Lower Bound of x returns the index of element
greater than or equal to x in a sorted array or
vector.
Upper Bound:
Upper Bound of x returns the index of element
Example
Consider the array A = [1,2,3,3,4,5,6,8]
Lower Bound of 3 = 2
Upper Bound of 3 = 5
Lower Bound of 9 = 8
Upper Bound of 8 = 8
Lower Bound of 0 = 0
Upper Bound of 0 = 0
Some more Questions
https://codeforces.com/problemset/
problem/474/B
https://codeforces.com/problemset/
problem/1703/F
Two
pointe
r
metho
Introduction
The Two Pointers Method is an important technique that is often used
in competitive programming. It is a broad concept which helps us
deal with a multitude of problems.
Constraints :
1 ≤ n , m ≤ 5 x 105
-109 ≤ A[i] , B[j] ≤ 109 ∀ 0 ≤ i ≤ n - 1 & 0 ≤ j ≤ m - 1
A = [ 10 , 15 , 231 ]
B = [ 12 , 17 , 211 , 420 ,
2911 ]
} C = [ 10 , 12 , 15 , 17 , 211 , 231 , 420
, 2911 ]
https://codeforces.com/edu/course/2/le
sson/9/1/practice/contest/307092/prob
lem/A
Approach - !
Naive Solution :
Better Solution :
The main observation is that since both arrays are already sorted, we
need not compare elements of the same array. So we only need
to compare the elements of A with elements of B.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int a1[n],a2[m];
vector<int>ans;
for (int i=0;i<n;i++)
cin>>a1[i];
for (int i=0;i<m;i++)
cin>>a2[i];
int first = 0,second = 0;
while (first < n && second < m)
{
if (a1[first] < a2[second]){
ans.push_back(a1[first]);
first++;
}
Solution - !
else{
ans.push_back(a2[second]);
second++;
}
}
if (first < n)
{
for (int i=first;i<n;i++)
ans.push_back(a1[i]);
}
else if (second < m)
{
for (int i=second;i<m;i++)
ans.push_back(a2[i]);
}
for (int i=0;i<n+m;i++)
cout<<ans[i]<<' ';
return 0;
}
Motivation Problem - !!
Given an array of 𝑛 integers 𝑎[𝑖]. Let's say that the segment of this array
𝑎[𝑙..𝑟] (1≤𝑙≤𝑟≤𝑛) is good if the sum of elements on this segment is at most
𝑠. Your task is to find the length of longest good segment.
Constraints :
1 ≤ n ≤ 105 , 1 ≤ s ≤ 1018
1 ≤ A[i] ≤ 109 ∀ 0 ≤ i ≤ n - 1
A=[2,6,4,3,6,8,9
] }
Ans = 4
https://codeforces.com/edu/course/2/le
sson/9/2/practice/contest/307093/prob
lem/A
Approach - !!
Naive Solution :
Better Solution :
•1≤n≤5000
•1≤x,ai≤10^9
Input:
4 10
1 2 5 7
Output:
0 1 3
https://cses.fi/problemset/task/1641/
Solution
@pag_iitr
facebook.com/sdspag
discord.gg/egUQEdt3