Skip to content

Commit

Permalink
add khushal87 new algorithms and problems
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Mar 1, 2021
1 parent 3feedc7 commit 14c625a
Show file tree
Hide file tree
Showing 26 changed files with 831 additions and 84 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"conio.h": "c"
}
}
47 changes: 47 additions & 0 deletions Array/maximum_occured_integer_in_n_ranges.cpp
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 added Array/maximum_occured_integer_in_n_ranges.exe
Binary file not shown.
49 changes: 49 additions & 0 deletions Array/min_value_such_that_xor_is_minimized.cpp
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;
}
}
38 changes: 38 additions & 0 deletions Dynamic Programming/minimum_falling_path_sum.cpp
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;
}
67 changes: 0 additions & 67 deletions Graph/Grid/number_of_connected_components.cpp

This file was deleted.

File renamed without changes.
40 changes: 40 additions & 0 deletions Grid/number_of_connected_components.cpp
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;
}
71 changes: 71 additions & 0 deletions Grid/shortest_distance_between_two_cells.cpp
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;
}
Loading

0 comments on commit 14c625a

Please sign in to comment.