Skip to content

Commit

Permalink
add khushal87 new essential codes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Mar 7, 2021
1 parent baee519 commit 02095e4
Show file tree
Hide file tree
Showing 21 changed files with 532 additions and 6 deletions.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions Array/SubArray/max_subarray_with_non_negative_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> findSubarray(int a[], int n)
{
// code here
vector<int> ans, temp;
int sum = -1, mx = INT_MIN;
for (int i = 0; i < n; i++)
{
if (a[i] >= 0)
{
temp.push_back(a[i]);
sum += a[i];
}
else
{
if (sum > mx)
{
ans = temp;
mx = sum;
}
temp.clear();
sum = 0;
}
}
if (sum > mx)
{
ans = temp;
mx = sum;
}
if (ans.size() > 0)
{
return ans;
}
else
return {-1};
}
File renamed without changes.
56 changes: 56 additions & 0 deletions Array/SubArray/subarray_with_given_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> subarraySum(int arr[], int n, int s)
{

// Your code here
int l = 0, sum = arr[0];
for (int i = 1; i <= n; i++)
{
while (sum > s)
{
sum -= arr[l];
l++;
}
if (sum == s)
{
vector<int> ans = {l + 1, i};
return ans;
}
if (i < n)
sum += arr[i];
}
return {-1};
}

vector<int> findSubarray(int a[], int n)
{
// code here
vector<int> ans, temp;
int sum = 0, mx = INT_MIN;
for (int i = 0; i < n; i++)
{
if (a[i] >= 0)
{
temp.push_back(a[i]);
sum += a[i];
}
if (sum >= mx)
{
ans = temp;
mx = sum;
}
if (a[i] < 0)
{
temp = {};
sum = 0;
}
}
if (ans.size() > 0)
{
return ans;
}
else
return {-1};
}
23 changes: 23 additions & 0 deletions Array/element_with_left_side_smaller_right_side_greater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

int findElement(vector<int> &arr)
{
int n = arr.size();
vector<int> leftMax(n);
leftMax[0] = INT_MIN;
for (int i = 1; i < n; i++)
{
leftMax[i] = max(leftMax[i - 1], arr[i]);
}
int rightMin = INT_MAX;
for (int i = n - 1; i >= 0; i--)
{
if (leftMax[i] < arr[i] && rightMin > arr[i])
{
return i;
}
rightMin = min(rightMin, arr[i]);
}
return -1;
}
23 changes: 23 additions & 0 deletions Array/two_sum_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

int count(vector<int> &arr, int sum)
{
int n = arr.size();
map<int, int> mp;
for (auto j : arr)
{
mp[j]++;
}
int cnt = 0;
for (int i = 0; i < n; i++)
{
cnt += mp[sum - arr[i]];
// if pair(arr[i],arr[i])
if (sum - arr[i] == arr[i])
{
cnt--;
}
}
return cnt / 2;
}
32 changes: 32 additions & 0 deletions BST/sorted_array_to_bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <bits/stdc++.h>
using namespace std;

struct Tree
{
int data;
Tree *left;
Tree *right;
};

Tree *newNode(int data)
{
Tree *temp;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}

Tree *sortedArrayToBST(vector<int> arr, int start, int end)
{
if (start > end)
return nullptr;
else
{
int mid = start + (end - start) / 2;
Tree *root = newNode(arr[mid]);
root->left = sortedArrayToBST(arr, start, mid - 1);
root->right = sortedArrayToBST(arr, mid + 1, end);
return root;
}
}
30 changes: 30 additions & 0 deletions Binary Search/pair_whose_sum_closest_to_x.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> printClosest(vector<int> arr1, vector<int> arr2, int x)
{
int n = arr1.size(), m = arr2.size();
int i = 0, j = m - 1;
int mn = INT_MAX;
vector<int> ans;
int res_l, res_r;
while (i < n && j >= 0)
{
if ((arr1[i] + arr2[j] - x) < mn)
{
res_l = i, res_r = j;
mn = min(mn, arr1[i] + arr2[j] - x);
}
if ((arr1[i] + arr2[j]) > x)
{
j--;
}
else
{
i++;
}
}
ans.push_back(arr1[res_l]);
ans.push_back(arr2[res_r]);
return ans;
}
46 changes: 46 additions & 0 deletions Graph/is_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <bits/stdc++.h>
using namespace std;

// a graph is a tree if there is no cycle and the graph is fully connected
bool cyclePresentOrNot(vector<vector<int>> &adj, vector<bool> &vis, int src, int parent)
{
vis[src] = true;
for (auto j : adj[src])
{
if (!vis[j])
{
if (cyclePresentOrNot(adj, vis, j, src))
{
return true;
}
}
else if (j != parent)
{
return true;
}
}
return false;
}

bool checkIfTree(vector<vector<int>> &adj)
{
int sz = adj.size();
vector<bool> vis(sz, false);
if (cyclePresentOrNot(adj, vis, 0, -1))
{
return false;
}
int flag = 0;
for (auto j : vis)
{
if (!j)
{
flag = 1;
break;
}
}
if (flag == 1)
return false;
else
return true;
}
2 changes: 1 addition & 1 deletion Graph/undirected_graph_has_cycle_or_not.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bool dfs(vector<vector<int>> &adj, vector<bool> &vis, int i, int parent)
{
if (vis[j] == false)
{
if (dfs(adj, vis, j, parent) == true)
if (dfs(adj, vis, j, i) == true)
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions Grid/rotten_orange.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

bool isVaid(int i, int j, int r, int c)
bool isValid(int i, int j, int r, int c)
{
if (i >= 0 && i < r && j >= 0 && j < c)
return true;
Expand Down Expand Up @@ -50,7 +50,7 @@ int orangesRotting(vector<vector<int>> &arr)
{
int x_dir = tmp.first + x[i];
int y_dir = tmp.second + y[i];
if (isVaid(x_dir, y_dir, r, c) && arr[x_dir][y_dir] == 1)
if (isValid(x_dir, y_dir, r, c) && arr[x_dir][y_dir] == 1)
{
arr[x_dir][y_dir] = 2;
qu.push({x_dir, y_dir});
Expand Down
35 changes: 35 additions & 0 deletions Heap/rearrange_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;

bool rearrangeString(string str)
{
//code here
priority_queue<pair<int, char>> pq;
unordered_map<char, int> mp;
for (auto j : str)
{
mp[j]++;
}
for (auto j : mp)
{
pq.push({j.second, j.first});
}
string ans = "";
pair<int, char> prev = make_pair(-1, '#');
while (!pq.empty())
{
pair<int, char> tmp = pq.top();
ans += tmp.second;
tmp.first -= 1;
pq.pop();
if (prev.first > 0)
{
pq.push(prev);
}
prev = tmp;
}
if (ans.length() == str.length())
return true;
else
return false;
}
3 changes: 2 additions & 1 deletion Stack/previous_greater_element.c++
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

void previousGreaterElement(int arr[], int n)
vector<int> previousGreaterElement(int arr[], int n)
{
vector<int> v;
stack<int> st;
Expand Down Expand Up @@ -32,6 +32,7 @@ void previousGreaterElement(int arr[], int n)
}
st.push(arr[i]);
}
return v;
}

int main()
Expand Down
Loading

0 comments on commit 02095e4

Please sign in to comment.