Skip to content

Commit

Permalink
add khushal87 codes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Feb 7, 2021
1 parent 8549663 commit 23af0b2
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Binary Search/number_of_smaller_or_equal_elements_than_target.cpp
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;
}
39 changes: 39 additions & 0 deletions Binary Search/single_element_in_sorted_array.cpp
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.
47 changes: 47 additions & 0 deletions Dynamic Programming/count_of_subset_sum_of_given_sum.cpp
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 added Dynamic Programming/equal_sum_partition
Binary file not shown.
57 changes: 57 additions & 0 deletions Dynamic Programming/equal_sum_partition.cpp
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 added Dynamic Programming/knapsack
Binary file not shown.
40 changes: 40 additions & 0 deletions Dynamic Programming/knapsack.cpp
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 added Dynamic Programming/subset_sum
Binary file not shown.
47 changes: 47 additions & 0 deletions Dynamic Programming/subset_sum.cpp
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;
}
2 changes: 1 addition & 1 deletion Graph/directed_graph_has_cycle_or_npt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ int main()
adj[a].push_back(b);
adj[b].push_back(a);
}
detectCycle(adj, n);
cout << detectCycle(adj, n);
}
25 changes: 25 additions & 0 deletions Heap/rearranging_characters.cpp
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);
}
32 changes: 32 additions & 0 deletions Tree/tree_and_xor.cpp
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);
}
}

0 comments on commit 23af0b2

Please sign in to comment.