-
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.
add khushal87 new algorithms and problems
- Loading branch information
Showing
26 changed files
with
831 additions
and
84 deletions.
There are no files selected for viewing
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,5 @@ | ||
{ | ||
"files.associations": { | ||
"conio.h": "c" | ||
} | ||
} |
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(vector<vector<int>> &ranges) | ||
{ | ||
int maxi = -1; | ||
for (auto j : ranges) | ||
{ | ||
if (j[1] > maxi) | ||
{ | ||
maxi = j[1]; | ||
} | ||
} | ||
vector<int> arr(maxi + 1, 0); | ||
for (auto j : ranges) | ||
{ | ||
arr[j[0]] += 1; | ||
arr[j[1] + 1] -= 1; | ||
} | ||
vector<int> prefix(maxi + 1); | ||
int ans = arr[0], ind = 0; | ||
|
||
for (int i = 1; i <= maxi; i++) | ||
{ | ||
prefix[i] = prefix[i - 1] + arr[i]; | ||
if (prefix[i] > ans) | ||
{ | ||
ans = prefix[i]; | ||
ind = i; | ||
} | ||
} | ||
return ind; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<vector<int>> arr(n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int x, y; | ||
cin >> x >> y; | ||
arr.push_back({x, y}); | ||
} | ||
cout << solve(arr); | ||
} |
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,49 @@ | ||
// You are given an array A consisting of integer values. | ||
// Find an integer K such that the value of the following function is minimized : | ||
// \sum_{i = 1}^{i = N} (A[i] \ XOR \ K) ,XOR represents a bitwise XOR operation | ||
// If multiple such K exist, then print the minimum possible value of K. | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(vector<long long> &nums, int n) | ||
{ | ||
auto it = max_element(nums.begin(), nums.end()); | ||
int p = log2(*it) + 1; | ||
long long ans = 0; | ||
for (int i = 0; i < p; i++) | ||
{ | ||
int ctr = 0; | ||
for (int j = 0; j < n; j++) | ||
{ | ||
if (nums[j] && (1 << i)) | ||
{ | ||
ctr++; | ||
} | ||
} | ||
if (ctr > (n / 2)) | ||
{ | ||
ans += (1 << i); | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<long long> arr; | ||
long long x; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> x; | ||
arr.push_back(x); | ||
} | ||
cout << solve(arr, n) << 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int minFallingPathSum(vector<vector<int>> &grid) | ||
{ | ||
int n = grid.size(); | ||
int m = grid[0].size(); | ||
vector<vector<int>> dp(n, vector<int>(m, INT_MAX)); | ||
for (int i = 0; i < m; i++) | ||
{ | ||
dp[0][i] = grid[0][i]; | ||
} | ||
for (int i = 1; i < n; i++) | ||
{ | ||
for (int j = 0; j < m; j++) | ||
{ | ||
if (j == 0) | ||
{ | ||
dp[i][j] = min(dp[i - 1][j] + grid[i][j], dp[i - 1][j + 1] + grid[i][j]); | ||
} | ||
else if (j == n - 1) | ||
{ | ||
dp[i][j] = min(dp[i - 1][j] + grid[i][j], dp[i - 1][j - 1] + grid[i][j]); | ||
} | ||
else | ||
{ | ||
dp[i][j] = min(dp[i - 1][j - 1] + grid[i][j], dp[i - 1][j] + grid[i][j], dp[i - 1][j + 1] + grid[i][j]); | ||
} | ||
} | ||
} | ||
int ans = INT_MAX; | ||
for (auto j : dp[n - 1]) | ||
{ | ||
if (j < ans) | ||
ans = j; | ||
} | ||
return ans; | ||
} |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int numIslands(vector<vector<char>> &grid) | ||
{ | ||
int n = grid.size(), m = grid[0].size(); | ||
int cnt = 0; | ||
int offsetX[4] = {0, 1, 0, -1}; | ||
int offsetY[4] = {1, 0, -1, 0}; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
for (int j = 0; j < m; j++) | ||
{ | ||
if (grid[i][j] == '1') | ||
{ | ||
cnt++; | ||
grid[i][j] = '0'; | ||
queue<pair<int, int>> que; | ||
que.push({i, j}); | ||
while (!que.empty()) | ||
{ | ||
pair<int, int> p = que.front(); | ||
que.pop(); | ||
for (int k = 0; k < 4; k++) | ||
{ | ||
int r = p.first + offsetX[k]; | ||
int c = p.second + offsetY[k]; | ||
if (r >= 0 && r < n && c >= 0 && c < m && grid[r][c] == '1') | ||
{ | ||
grid[r][c] = '0'; | ||
que.push({r, c}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return cnt; | ||
} |
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,71 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct item | ||
{ | ||
int row, col, weight; | ||
}; | ||
|
||
int shortestDistance(vector<vector<char>> &grid) | ||
{ | ||
item src; | ||
int n = grid.size(); | ||
int m = grid[0].size(); | ||
vector<vector<bool>> vis(n, vector<bool>(m, false)); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
for (int j = 0; j < m; j++) | ||
{ | ||
if (grid[i][j] == '1') | ||
{ | ||
vis[i][j] = true; | ||
} | ||
else | ||
{ | ||
vis[i][j] = false; | ||
} | ||
if (grid[i][j] == 's') | ||
{ | ||
src.row = i, src.col = j, src.weight = 0; | ||
} | ||
} | ||
} | ||
queue<item> qu; | ||
qu.push(src); | ||
vis[src.row][src.col] = true; | ||
while (!qu.empty()) | ||
{ | ||
item temp = qu.front(); | ||
qu.pop(); | ||
if (grid[temp.row][temp.col] == 'd') | ||
return temp.weight; | ||
// up | ||
if (temp.row - 1 >= 0 && vis[temp.row - 1][temp.col] == false) | ||
{ | ||
qu.push({temp.row - 1, temp.col, temp.weight + 1}); | ||
vis[temp.row - 1][temp.col] = true; | ||
} | ||
|
||
//down | ||
if (temp.row + 1 <= n - 1 && vis[temp.row + 1][temp.col] == false) | ||
{ | ||
qu.push({temp.row + 1, temp.col, temp.weight + 1}); | ||
vis[temp.row + 1][temp.col] = true; | ||
} | ||
|
||
// left | ||
if (temp.col - 1 >= 0 && vis[temp.row][temp.col - 1] == false) | ||
{ | ||
qu.push({temp.row, temp.col - 1, temp.weight + 1}); | ||
vis[temp.row][temp.col - 1] = true; | ||
} | ||
|
||
// right | ||
if (temp.col + 1 <= m - 1 && vis[temp.row][temp.col + 1] == false) | ||
{ | ||
qu.push({temp.row, temp.col + 1, temp.weight + 1}); | ||
vis[temp.row][temp.col + 1] = true; | ||
} | ||
} | ||
return -1; | ||
} |
Oops, something went wrong.