Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Hashing/2Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 < index2. Please note that your returned answers (both index1 and index2 ) are not zero-based.
Put both these numbers in order in an array and return the array from your function ( Looking at the function signature will make things clearer ). Note that, if no pair exists, return empty list.

If multiple solutions exist, output the one where index2 is minimum. If there are multiple solutions with the minimum index2, choose the one with minimum index1 out of them.*/

vector<int> Solution::twoSum(const vector<int> &A, int B) {
map<int,int> Hash;
vector<int> ans;
int n = A.size();
for(int i=0;i<n;i++){
if(Hash.find(B-A[i])!=Hash.end()){
ans.push_back(Hash[B-A[i]]);
ans.push_back(i+1);
return ans;
}
if(Hash.find(A[i])==Hash.end()) Hash[A[i]]=i+1;
}
return ans;
}
51 changes: 51 additions & 0 deletions Hashing/4Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
Example :
Given array S = {1 0 -1 0 -2 2}, and target = 0
A solution set is:

(-2, -1, 1, 2)
(-2, 0, 0, 2)
(-1, 0, 0, 1)
Also make sure that the solution set is lexicographically sorted.
Solution[i] < Solution[j] iff Solution[i][0] < Solution[j][0] OR (Solution[i][0] == Solution[j][0] AND ... Solution[i][k] < Solution[j][k])*/

vector<vector<int> > Solution::fourSum(vector<int> &A, int B) {
int n = A.size();
sort(A.begin(),A.end());
int i,j;
unordered_map<int, pair<int,int>> hash;
vector<vector<int>> Ans;
vector<int>temp;


for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
hash[A[i]+A[j]] = {i,j};
}
}

for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
int sum = A[i]+A[j];
if(hash.find(B-sum)!=hash.end()){
pair<int,int>p = hash[B-sum];
if(p.first != i && p.first != j && p.second != i && p.second != j){
temp.push_back(A[i]);
temp.push_back(A[j]);
temp.push_back(A[p.first]);
temp.push_back(A[p.second]);
sort(temp.begin(),temp.end());
Ans.push_back(temp);
temp.clear();
}
}
}
}
sort(Ans.begin(), Ans.end());
Ans.erase(unique(Ans.begin(), Ans.end()), Ans.end());
return Ans;
}
48 changes: 48 additions & 0 deletions Hashing/fraction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part is repeating, enclose the repeating part in parentheses.

Example :

Given numerator = 1, denominator = 2, return "0.5"
Given numerator = 2, denominator = 1, return "2"
Given numerator = 2, denominator = 3, return "0.(6)"*/


string Solution::fractionToDecimal(int A, int B) {
int64_t n = A, d = B;

if(n==0) return "0";

string result = "";

if(n<0 ^ d<0) result += '-';

n = abs(n);
d = abs(d);

result += to_string((n/d));

if(n%d == 0) return result;

result += '.';

unordered_map<int, int> Hash;

for(int64_t r=n%d; r; r %= d){

if(Hash.find(r)!=Hash.end()){
result.insert(Hash[r],1,'(');
result += ')';
break;
}

Hash[r] = result.size();

r *= 10;

result += (char)('0'+(r/d));

}
return result;
}
22 changes: 22 additions & 0 deletions Tree Data Structure/Inorder_Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*Given a binary tree, return the inorder traversal of its nodes’ values*/

vector<int> Solution::inorderTraversal(TreeNode* A) {
vector<int> Answer;
stack<TreeNode*> s;
if(!A) return Answer;

while(A!=NULL || !s.empty()){

while(A!=NULL){
s.push(A);
A = A->left;
}

A = s.top();
s.pop();
Answer.push_back(A->val);

A = A->right;
}
return Answer;
}
21 changes: 21 additions & 0 deletions Tree Data Structure/Postorder_Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*Given a binary tree, return the postorder traversal of its nodes’ values.*/


vector<int> Solution::postorderTraversal(TreeNode* A) {
vector<int> Answer;
stack<TreeNode*> stac;
if(!A)return Answer;
stac.push(A);

while(!stac.empty()){
struct TreeNode * nod = stac.top();
Answer.push_back(nod -> val);
stac.pop();
if(nod -> left) stac.push(nod->left);
if(nod -> right) stac.push(nod->right);

}
reverse(Answer.begin(),Answer.end());
return Answer;

}
22 changes: 22 additions & 0 deletions Tree Data Structure/Preorder_Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*Given a binary tree, return the preorder traversal of its nodes’ values.*/

vector<int> Solution::preorderTraversal(TreeNode* A){
vector<int> Answer;
stack<TreeNode*> stac;
if(!A) return Answer;

stac.push(A);
while(stac.empty()==false){
struct TreeNode* TreeNod = stac.top();
Answer.push_back(TreeNod -> val);
stac.pop();

if(TreeNod -> right)
stac.push(TreeNod -> right);
if(TreeNod -> left)
stac.push(TreeNod -> left);
}
return Answer;


}