Skip to content

Commit 683a27c

Browse files
Merge branch 'kothariji:master' into master
2 parents 211d9ed + cba9ce2 commit 683a27c

15 files changed

+587
-0
lines changed

Arrays/(LEETCODE) permutation_2.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
void dfs(vector<int> v, vector<int> &nums, unordered_map<int, int> &mp, vector<vector<int>> &ans){
4+
if(v.size() == nums.size()){
5+
ans.push_back(v);
6+
return;
7+
}
8+
9+
for(auto i : mp){
10+
if(i.second > 0){
11+
v.push_back(i.first);
12+
mp[i.first]--;
13+
14+
dfs(v, nums, mp, ans);
15+
16+
mp[i.first]++;
17+
v.pop_back();
18+
}
19+
}
20+
}
21+
22+
vector<vector<int>> permuteUnique(vector<int>& nums) {
23+
vector<vector<int>> ans;
24+
unordered_map<int, int> mp;
25+
26+
for(int i = 0; i < nums.size(); i++) mp[nums[i]]++;
27+
28+
dfs({}, nums, mp, ans);
29+
30+
return ans;
31+
}
32+
};

Backtracking/Combination_sum_1.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
void dfs(int ind, int t, vector<int> v, vector<int> &ar, vector<vector<int>> &ans){
4+
if(t < 0) return;
5+
if(t == 0){
6+
ans.push_back(v);
7+
return;
8+
}
9+
if(ind < 0) return;
10+
11+
if(ar[ind] <= t){
12+
v.push_back(ar[ind]);
13+
dfs(ind, t - ar[ind], v, ar, ans);
14+
v.pop_back();
15+
}
16+
17+
dfs(ind - 1, t, v, ar, ans);
18+
19+
}
20+
21+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
22+
vector<vector<int>> ans;
23+
// sort(candidates.begin(), candidates.end());
24+
25+
dfs(candidates.size() - 1, target, {}, candidates, ans);
26+
27+
return ans;
28+
}
29+
};

Backtracking/Combination_sum_2.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
void dfs(int ind, int t, vector<int> v, vector<int> &ar, vector<vector<int>> &ans){
4+
if(t < 0) return;
5+
if(t == 0){
6+
ans.push_back(v);
7+
return;
8+
}
9+
if(ind >= ar.size()) return;
10+
11+
for(int i = ind; i < ar.size(); i++){
12+
if((i != ind && ar[i] == ar[i-1] ) || ar[i] > t) continue;
13+
v.push_back(ar[i]);
14+
dfs(i + 1, t - ar[i], v, ar, ans);
15+
v.pop_back();
16+
}
17+
18+
}
19+
20+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
21+
vector<vector<int>> ans;
22+
sort(candidates.begin(), candidates.end());
23+
24+
dfs(0, target, {}, candidates, ans);
25+
26+
return ans;
27+
}
28+
};
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
long long int t,n,v;
6+
cin>>t;
7+
while(t--)
8+
{
9+
cin>>n>>v;
10+
long long int max=0,min=0;
11+
max= (n*(n-1))/2;
12+
if(v==1)
13+
min=max;
14+
else
15+
{
16+
if(v>=n-1)
17+
min=n-1;
18+
else
19+
min = v+((n-v)*(n-v+1))/2 -1;
20+
}
21+
cout<<max<<" "<<min<<endl;
22+
}
23+
return 0;
24+
}

Codechef solutions/CHEF7UP.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
int main() {
6+
ios_base::sync_with_stdio(false);
7+
8+
int t = 1;
9+
cin >> t;
10+
while(t--) {
11+
int n;
12+
cin >> n;
13+
int x; cin >> x;
14+
vector<int> v(n);
15+
for(auto &i : v) cin >> i;
16+
17+
sort(v.begin(), v.end());
18+
19+
if (x > v.back()) {
20+
int ans = 0;
21+
for(int j = n-1;j>=0;j--) {
22+
if(abs(v[j] - x)%2 == 1) ans++;
23+
else break;
24+
}
25+
cout << ans << " " << (ans == n ? 1: -1) << '\n';
26+
}
27+
else if (x < v.front()) {
28+
int ans = 0;
29+
for(int j = 0;j<n;j++){
30+
if(abs(v[j] - x)%2 == 1) ans++;
31+
else break;
32+
}
33+
cout << ans << " " << (ans == n ? 1: -1) << '\n';
34+
}
35+
else{
36+
int i = -1, j = -1;
37+
for(int k = 0;k<n;k++) {
38+
if (v[k] < x) i = k;
39+
if (v[k] > x and j == -1)j = k;
40+
}
41+
int ans =0;
42+
43+
while(i >= 0 || j < n) {
44+
int a=0, b=0;
45+
if(i >= 0)a = abs(v[i] - x)%2;
46+
if(j < n) b =abs(v[j] - x)%2;
47+
if(a == 0 and b== 0)break;
48+
49+
if (a == 1) {
50+
i--;
51+
ans++;
52+
}
53+
if (b == 1) {
54+
j++;
55+
ans++;
56+
}
57+
}
58+
cout << ans << " " << (ans == n ? 1: -1) << '\n';
59+
60+
}
61+
62+
}
63+
64+
65+
}

Codechef solutions/FINDMOD.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
int main() {
6+
ios_base::sync_with_stdio(false);
7+
8+
int t = 1;
9+
cin >> t;
10+
while(t--) {
11+
long long x= 1e18+1;
12+
13+
cout << "? " << x << endl;
14+
15+
long long y; cin >> y;
16+
17+
cout << "? " << x - y - 1 << endl;
18+
19+
cin >> y;
20+
21+
cout << "! " << y + 1 << endl;
22+
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#define INFINITE 0x3f3f3f3f
2+
class Solution {
3+
public:
4+
5+
int nthUglyNumber(int n) {
6+
int ugly_nums[10010];
7+
int power_2 = 1, power_3 = 1, power_5 = 1,
8+
next_mul_2, next_mul_3, next_mul_5;
9+
10+
for(int i=2;i <= n;i++)
11+
ugly_nums[i] = INFINITE;
12+
ugly_nums[1] = 1;
13+
14+
for(int i=2;i<=n;i++)
15+
{
16+
next_mul_2 = ugly_nums[power_2]*2;
17+
next_mul_3 = ugly_nums[power_3]*3;
18+
next_mul_5 = ugly_nums[power_5]*5;
19+
20+
ugly_nums[i] = min({next_mul_2, next_mul_3, next_mul_5});
21+
22+
// increment the power of chosen prime
23+
if(ugly_nums[i] == next_mul_2)
24+
power_2++;
25+
if(ugly_nums[i] == next_mul_3)
26+
power_3++;
27+
if(ugly_nums[i] == next_mul_5)
28+
power_5++;
29+
}
30+
return ugly_nums[n];
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
4+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
5+
int n = grid.size(), len = 1;
6+
if(grid[0][0] == 1 || grid[n-1][n-1] == 1) return -1;
7+
if(n==1) return 1;
8+
9+
vector<vector<int>> off = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}};
10+
queue<pair<int,int>> q;
11+
q.push({0,0});
12+
grid[0][0] = 1;
13+
14+
int sz, row, col, i, j;
15+
16+
while(!q.empty()){
17+
sz = q.size();
18+
for(i = 0; i < sz; i++){
19+
row = q.front().first, col = q.front().second;
20+
q.pop();
21+
22+
if(row == n-1 && col == n-1) return len;
23+
24+
for(j = 0; j < 8; j++){
25+
int x = row + off[j][0], y = col + off[j][1];
26+
if(x >= 0 && y >= 0 && x < n && y < n && grid[x][y] == 0){
27+
grid[x][y] = 1;
28+
q.push({x,y});
29+
}
30+
}
31+
32+
}
33+
len++;
34+
}
35+
36+
return -1;
37+
}
38+
};
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* C++ program to count all paths from a source to a
3+
* destination
4+
*/
5+
#include <vector>
6+
#include <cassert>
7+
#include <list>
8+
#include<iostream>
9+
using namespace std;
10+
11+
/*
12+
* A directed graph using adjacency list representation;
13+
* every vertex holds a list of all neighbouring vertices
14+
* that can be reached from it.
15+
*/
16+
class Graph {
17+
public:
18+
// Construct the graph given the number of vertices...
19+
Graph(int vertices);
20+
// Specify an edge between two vertices
21+
void add_edge(int src, int dst);
22+
// Call the recursive helper function to count all the
23+
// paths
24+
int count_paths(int src, int dst, int vertices);
25+
26+
private:
27+
int m_vertices;
28+
list<int>* m_neighbours;
29+
void path_counter(int src, int dst, int& path_count, vector<bool>visited);
30+
};
31+
32+
Graph::Graph(int vertices)
33+
{
34+
m_vertices = vertices; // unused!!
35+
/* An array of linked lists - each element corresponds
36+
to a vertex and will hold a list of neighbours...*/
37+
m_neighbours = new list<int>[vertices];
38+
}
39+
40+
void Graph::add_edge(int src, int dst)
41+
{
42+
m_neighbours[src].push_back(dst);
43+
}
44+
45+
int Graph::count_paths(int src, int dst, int vertices)
46+
{
47+
int path_count = 0;
48+
vector<bool>visited(vertices,false);
49+
path_counter(src, dst, path_count, visited);
50+
return path_count;
51+
}
52+
53+
/*
54+
* A recursive function that counts all paths from src to
55+
* dst. Keep track of the count in the parameter.
56+
*/
57+
void Graph::path_counter(int src, int dst, int& path_count, vector<bool> visited)
58+
{
59+
// If we've reached the destination, then increment
60+
// count...
61+
visited[src]=true;
62+
if (src == dst) {
63+
path_count++;
64+
}
65+
// ...otherwise recurse into all neighbours...
66+
else {
67+
for (auto neighbour : m_neighbours[src]) {
68+
if(!visited[neighbour])
69+
path_counter(neighbour, dst, path_count, visited);
70+
}
71+
}
72+
}
73+
74+
// Tests...
75+
int main()
76+
{
77+
// Create a graph given in the above diagram - see link
78+
Graph g(5);
79+
g.add_edge(0, 1);
80+
g.add_edge(0, 2);
81+
g.add_edge(0, 4);
82+
g.add_edge(1, 3);
83+
g.add_edge(1, 4);
84+
g.add_edge(2, 3);
85+
g.add_edge(2, 1);
86+
g.add_edge(3, 2);
87+
// Validate it...
88+
cout<<g.count_paths(0, 4,5);
89+
90+
return 0;
91+
}

0 commit comments

Comments
 (0)