Skip to content

Commit

Permalink
Merge pull request #56 from sanskar28/master
Browse files Browse the repository at this point in the history
Added Miscellaneous problems in C++
  • Loading branch information
championballer authored Dec 24, 2019
2 parents 175ac6b + 3be8bf7 commit 49cb42c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Miscellaneous/FreindCircleQueries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node* parent = nullptr;
int count = 1; // # decendents + current
};
Node* getRoot(Node* node) {
while(node->parent) node = node->parent;
return node;
}
int merge(Node* a, Node* b) {
Node* r1 = getRoot(a);
Node* r2 = getRoot(b);
if(r1 == r2) return r1->count;
// Add small tree to root of big tree
Node *big, *small;
if(r1->count > r2->count) { big = r1; small = r2; } else { big = r2; small = r1; }
small->parent = big;
big->count += small->count;
return big->count;
}
int main() {
int n;
scanf("%d", &n);
int largest = 1, a, b;
unordered_map<int, Node> nodes;
for(int i = 0; i < n; ++i) {
scanf("%d %d", &a, &b);
largest = max(largest, merge(&nodes[a], &nodes[b]));
printf("%d\n", largest);
}
return 0;
}
81 changes: 81 additions & 0 deletions Miscellaneous/MaximumXOR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include<bits/stdc++.h>
using namespace std;
struct trie{
trie *child[2];
bool is_end;
};
trie *new_node()
{
trie *temp=new trie;
temp->child[0]=NULL;
temp->child[1]=NULL;
temp->is_end=false;
return temp;
}
void insert(trie *root,int x)
{
trie *temp=root;
int index;
for(int i=31;i>=0;i--)
{
if(x&(1<<i))
index=1;
else
index=0;
if(temp->child[index]==NULL)
temp->child[index]=new_node();

temp=temp->child[index];
}
temp->is_end=true;
}
void search_result(trie *root,int x)
{ int cnt=0,index,index_;
trie *temp=root;
for(int i=31;i>=0;i--)
{
if(x&(1<<i))
index=1;
else
index=0;

index_=(index+1)%2;

if(temp->child[index_]!=NULL && temp->is_end==false)
{
cnt=cnt+pow(2,i) ;
temp=temp->child[index_];
}
else {
temp=temp->child[index];
}
}
cout<<cnt<<endl;
}
void max_xor(int vec[],int n,int q[],int t)
{
trie *root=new_node();

for(int i=0;i<n;i++){
//cout<<"insert: "<<q[i]<<endl;;
insert(root,vec[i]);}

for(int i=0;i<t;i++)
{ //cout<<"q["<<i<<"]: "<<q[i]<<"=";
search_result(root,q[i]);
}
}
int main()
{
int n,x,t;
cin>>n;
int vec[n];
for(int i=0;i<n;i++)
cin>>vec[i];

cin>>t;int q[t];
for(int i=0;i<t;i++)
cin>>q[i];
max_xor(vec,n,q,t);
return 0;
}
14 changes: 14 additions & 0 deletions Miscellaneous/flippingbits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin >> t;
while (t--){
unsigned int n;
cin >> n;
cout << ~n << endl;
}
return 0;
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ in C++, Java and Python.
||[Crossword Puzzle](https://www.hackerrank.com/challenges/crossword-puzzle/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=recursion-backtracking)|[Done](/Recursion%20and%20Backtracking/Davis'_staircase.py)||[Done](/Recursion%20and%20Backtracking/crossword_puzzle.py)|
||[Recursive Digit Sum](https://www.hackerrank.com/challenges/recursive-digit-sum/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=recursion-backtracking)|[Done](/Recursion%20and%20Backtracking/Recursive_Digit_Sum.cpp)||[Done](/Recursion%20and%20Backtracking/Recursive_Digit_Sum)|
|Miscellaneous | ||| |
||[Flipping Bits](https://www.hackerrank.com/challenges/flipping-bits/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)||[Done](/Miscellaneous/flippingBits.java)|[Done](/Miscellaneous/FlippingBits.py)|
||[Flipping Bits](https://www.hackerrank.com/challenges/flipping-bits/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)|[Done](/Miscellaneous/flippingbits.cpp)|[Done](/Miscellaneous/flippingBits.java)|[Done](/Miscellaneous/FlippingBits.py)|
||[Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)|[Done](/Miscellaneous/Primality.cpp)|[Done](/Miscellaneous/Primality.java)|[Done](/Miscellaneous/Primality.py)|
||[Friend Circle Queries](https://www.hackerrank.com/challenges/friend-circle-queries/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)|||[Done](/Miscellaneous/FriendCircleQueries.py)|
||[Maximum Xor](https://www.hackerrank.com/challenges/maximum-xor?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)|||[Done](/Miscellaneous/MaximumXor.py)|
||[Friend Circle Queries](https://www.hackerrank.com/challenges/friend-circle-queries/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)|[Done](/Miscellaneous/FreindCircleQueries.cpp)||[Done](/Miscellaneous/FriendCircleQueries.py)|
||[Maximum Xor](https://www.hackerrank.com/challenges/maximum-xor?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=miscellaneous)|[Done](/Miscellaneous/MaximumXOR.cpp)||[Done](/Miscellaneous/MaximumXor.py)|

## Communication channel

Expand Down

0 comments on commit 49cb42c

Please sign in to comment.