Skip to content

Commit

Permalink
Merge pull request #425 from anjalijaiswal08/my-branch3
Browse files Browse the repository at this point in the history
#413 solved
  • Loading branch information
Srikant Singh authored Oct 23, 2018
2 parents 849c573 + 8da5d3f commit 1aacab9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Data Structures/LinkedLists/duplicateRemoval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include<bits/stdc++.h>
#include <map>
using namespace std;

unordered_set<int> dup;
std::map<int, int> map1;

typedef struct Node{
int data;
struct Node *next;
}node;

node * head = NULL;


void printlist(struct Node * head){
struct Node * temp = head;
while(temp!=NULL){
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}

struct Node* createNode(int data){
struct Node * temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}

struct Node * CreateList(int n){
struct Node * temp;
struct Node * p = NULL;
int data;
for(int i=0;i<n;i++){
cin>>data;
temp = createNode(data);
if(head == NULL){
head = temp;
}
else{
p = head;
while(p->next != NULL){
p = p->next;
}
p->next = temp;
}
}
return head;
}

void duplicate(node * head){
node * temp = head, * temp1 = NULL;
while(temp != NULL){
if(dup.find(temp->data) != dup.end()){
temp1 -> next = temp ->next;
}
else{
dup.insert(temp -> data);
temp1 = temp;
}
temp = temp1 -> next;
}
cout<<"After Removal Of duplicates : ";
printlist(head);
}

void duplicate1(node * head){
node * temp = head, * temp1 = NULL;
while(temp != NULL){
map1[temp->data]++;
if(map1[temp->data] == 1){
temp1 = temp;
}
else if(map1[temp -> data] > 1){
temp1 -> next = temp ->next;
}
temp = temp1->next;
}
cout<<"After Removal Of duplicates : ";
printlist(head);
}

int main(){
int t,n;
cin>>t;
CreateList(t);
cout<<"Real List : ";
printlist(head);
duplicate(head);
return 0;
}
42 changes: 42 additions & 0 deletions Tricky Problems/maxOfTwo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//Max of two num with overflow condition
//Refrence : Cracking Coding Interview by Gayle Lakmann Mcdowell

#include <bits/stdc++.h>
using namespace std;

//XOR of bit to convert 1 into 0 and 0 into 1
int flip(int m){
return (1 ^ m);
}

//Return 1 if positive, 0 if a is negative.
int sign(int m){
return ((m >> 31) & 0x1);
}

void max(int a,int b){
int c = a - b;
int sa = sign(a);
int sb = sign(b);
int sc = sign(c);

int sign_a = sa ^ sb;

int sign_c = flip(sa ^ sb);

int k = sign_a * sa + sign_c * sc;

int q = flip(k);

int res = a * q + b * k;

cout<<"Max Of "<<a<<" and "<<b<<"is: ";
cout<<res;
}

int main(){
int a,b;
cin>>a>>b;
max(a,b);
return 0;
}

0 comments on commit 1aacab9

Please sign in to comment.