-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #425 from anjalijaiswal08/my-branch3
#413 solved
- Loading branch information
Showing
2 changed files
with
135 additions
and
0 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,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; | ||
} |
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,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; | ||
} |