Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions jiyunpar/binary_search/1920.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
vector<int> seq;
vector<int> target;

int binarysearch(int i)
{
int start, end, mid;
start = 0;
end = n -1;
while (start <= end)
{
mid = (start + end) / 2;
if (seq[mid] < target[i])
start = mid + 1;
else if (seq[mid] > target[i])
end = mid - 1;
else
return (1);
}
return (0);
}

int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
seq.reserve(100000);
target.reserve(100000);
cin >> n;
for (int i = 0; i < n; ++i)
{
int val;
cin >> val;
seq.push_back(val);
}
cin >> m;
for (int i = 0; i < m; ++i)
{
int val;
cin >> val;
target.push_back(val);
}
sort(seq.begin(), seq.end());
for (int i = 0; i < m; ++i)
{
if (binarysearch(i))
cout << 1 << '\n';
else
cout << 0 << '\n';
}
return (0);
}
41 changes: 41 additions & 0 deletions jiyunpar/binary_search/1920_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
vector<int> seq;
vector<int> target;

// using STL binary_search
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
seq.reserve(100000);
target.reserve(100000);
cin >> n;
for (int i = 0; i < n; ++i)
{
int val;
cin >> val;
seq.push_back(val);
}
cin >> m;
for (int i = 0; i < m; ++i)
{
int val;
cin >> val;
target.push_back(val);
}
sort(seq.begin(), seq.end());
for (int i = 0; i < m; ++i)
{
if (binary_search(seq.begin(), seq.end(), target[i]))
cout << 1 << '\n';
else
cout << 0 << '\n';
}
return (0);
}