-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Binary Search/number_of_smaller_or_equal_elements_than_target.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int binarySearch(vector<int> arr, int n, int el) | ||
{ | ||
int start = 0; | ||
int end = n - 1; | ||
int res = 0; | ||
while (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (arr[mid] <= el) | ||
{ | ||
res = mid + 1; | ||
start = mid + 1; | ||
} | ||
else if (arr[mid] > el) | ||
{ | ||
end = mid - 1; | ||
} | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(vector<int> &nums) | ||
{ | ||
int n = nums.size(); | ||
int start = 0; | ||
int end = n - 1; | ||
|
||
while (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (mid > 0 && mid < n - 1) | ||
{ | ||
if (nums[mid] != nums[mid + 1] || nums[mid] != nums[mid - 1]) | ||
{ | ||
return nums[mid]; | ||
} | ||
else if (nums[mid] == nums[mid - 1]) | ||
{ | ||
end = mid - 1; | ||
} | ||
else if (nums[mid] == nums[mid + 1]) | ||
{ | ||
start = mid + 1; | ||
} | ||
} | ||
else if (mid == 0) | ||
{ | ||
if (nums[mid] != nums[mid + 1]) | ||
return mid; | ||
} | ||
else if (mid == n - 1) | ||
{ | ||
if (nums[mid] != nums[mid - 1]) | ||
return mid; | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int p[], int n, int cap) | ||
{ | ||
int t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = 1; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else | ||
{ | ||
if (p[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i - 1][j - p[i - 1]] + t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
} | ||
int cap; | ||
cin >> cap; | ||
cout << solve(p, n, cap) << endl; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool solve(int p[], int n, int cap) | ||
{ | ||
bool t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = true; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = false; | ||
} | ||
else | ||
{ | ||
if (p[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i - 1][j - p[i - 1]] || t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
int sum = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
sum += p[i]; | ||
} | ||
if (sum & 1) | ||
{ | ||
cout << "Not possible" << endl; | ||
} | ||
else | ||
{ | ||
cout << solve(p, n, sum / 2) << endl; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int p[], int w[], int n, int cap) | ||
{ | ||
int t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (i == 0 || j == 0) | ||
{ | ||
t[i][j] = 0; | ||
} | ||
else if (w[i - 1] <= j) | ||
{ | ||
t[i][j] = max(p[i - 1] + t[i - 1][j - w[i - 1]], t[i - 1][j]); | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n], w[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i] >> w[i]; | ||
} | ||
int cap; | ||
cin >> cap; | ||
cout << solve(p, w, n, cap) << endl; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool solve(int p[], int n, int cap) | ||
{ | ||
bool t[n + 1][cap + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
for (int j = 0; j <= cap; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
t[i][j] = true; | ||
} | ||
else if (i == 0 && j > 0) | ||
{ | ||
t[i][j] = false; | ||
} | ||
else | ||
{ | ||
if (p[i - 1] <= j) | ||
{ | ||
t[i][j] = t[i - 1][j - p[i - 1]] || t[i - 1][j]; | ||
} | ||
else | ||
{ | ||
t[i][j] = t[i - 1][j]; | ||
} | ||
} | ||
} | ||
} | ||
return t[n][cap]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
int p[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> p[i]; | ||
} | ||
int cap; | ||
cin >> cap; | ||
cout << solve(p, n, cap) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,5 +49,5 @@ int main() | |
adj[a].push_back(b); | ||
adj[b].push_back(a); | ||
} | ||
detectCycle(adj, n); | ||
cout << detectCycle(adj, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
string solve(string s) | ||
{ | ||
map<char, int> mp; | ||
for (auto j : s) | ||
{ | ||
mp[j]++; | ||
} | ||
priority_queue<pair<int, char>> pq; | ||
|
||
for (auto j : mp) | ||
{ | ||
pq.push({j.second, j.first}); | ||
} | ||
string result = ""; | ||
} | ||
|
||
int main() | ||
{ | ||
string s; | ||
cin >> s; | ||
cout << solve(s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(vector<int> &v, vector<vector<int>> &adj, int n) | ||
{ | ||
} | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<int> v; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int x; | ||
cin >> x; | ||
v.push_back(x); | ||
} | ||
vector<vector<int>> adj(n); | ||
for (int i = 0; i < n - 1; i++) | ||
{ | ||
int u, v; | ||
cin >> u >> v; | ||
adj[u].push_back(v); | ||
} | ||
int ans = solve(v, adj, n); | ||
} | ||
} |