-
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
21 changed files
with
532 additions
and
6 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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,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.
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,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
23
Array/element_with_left_side_smaller_right_side_greater.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 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; | ||
} |
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 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; | ||
} |
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; | ||
|
||
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; | ||
} | ||
} |
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,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; | ||
} |
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,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; | ||
} |
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
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
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,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; | ||
} |
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
Oops, something went wrong.