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
66 changes: 66 additions & 0 deletions TwoPoniters cpp/merge2SortedLists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//Ques Link https://www.interviewbit.com/problems/merge-two-sorted-lists-ii
/* Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note: You have to modify the array A to contain the merge of A and B. Do not output anything in your code.
TIP: C users, please malloc the result into a new array and return the result.
If the number of elements initialized in A and B are m and n respectively, the resulting size of array A after your code is executed should be m + n */

//main function has been included to ease readability and for better understanding

#include <bits/stdc++.h>
using namespace std;
void Merge2SortedArrays(vector<int> &A, vector<int> &B)
{

vector<int> arr; //use of additional memory allowed
int i = 0, j = 0; //2 pointers to at head of A and B resp.
while (i < A.size() && j < B.size())
{
if (A[i] > B[j])
{
arr.push_back(B[j]);
j++;
}
else
{
arr.push_back(A[i]);
i++;
}
}
if (i < A.size()) //if elements of A are left, keep pushing them in arr till end
{
while (i != A.size())
{
arr.push_back(A[i]);
i++;
}
}
if (j < B.size())
{
while (j != B.size()) //if elements of B are left, keep pushing them in arr till end
{
arr.push_back(B[j]);
j++;
}
}

A = arr; //copy vector arr to A to modify A
}

int main()
{
int size1, size2;
cin >> size1 >> size2;
vector<int> A(size1), B(size2);

for (auto &x : A)
cin >> x;

for (auto &xx : B)
cin >> xx;

Merge2SortedArrays(A, B);
for (auto &x : A)
cout << x << " "; //array elemets after merge
return 0;
}
54 changes: 54 additions & 0 deletions TwoPoniters cpp/remove-duplicates-from-sorted-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//Ques Link: https://www.interviewbit.com/problems/remove-duplicates-from-sorted-array/

/* Remove duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appears only once and return the new length.

Note that even though we want you to return the new length, make sure to change the original array as well in place

Do not allocate extra space for another array, you must do this in place with constant memory.

Example:
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2]. */

//main function has been included to ease readability and for better understanding

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

int removeDuplicates(vector<int> &A)
{
if (A.size() == 1)
return 1;
int i = 1, j = 1;
while (i < A.size() && j < A.size())
{
if (A[i] != A[j])
{
A[j] = A[i]; //allocate element only if it is unique i.e non duplicate
}
if (A[j - 1] != A[j]) //array is sorted so we compare adjacent indeces
{
j++;
}
i++;
}
return j;
}
int main()
{
int size;
cin >> size;
vector<int> A(size);

for (auto &x : A)
cin >> x;

int newlength = removeDuplicates(A);

cout << "New length after removal of duplicates is " << newlength << endl;

for (int i = 0; i < newlength; i++)
cout << A[i] << " ";
return 0;
}
51 changes: 51 additions & 0 deletions TwoPoniters cpp/remove-element-from-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Ques Link : https: //www.interviewbit.com/problems/remove-element-from-array
/* Given an array and a value, remove all the instances of that value in the array.
Also return the number of elements left in the array after the operation.
It does not matter what is left beyond the expected length.

Example:
If array A is [4, 1, 1, 2, 1, 3]
and value elem is 1,
then new length is 3, and A is now [4, 2, 3]
Try to do it in less than linear additional space complexity. */

//main function has been included to ease readability and for better understanding

#include <bits/stdc++.h>
using namespace std;
int removeElement(vector<int> &A, int B)
{

int i = 0, j = 0, duplicates = 0;

while (i < A.size() && j < A.size())
{
if (A[i] != B)
{
A[j++] = A[i];
}
else
duplicates++; //whenever A[i] == B
i++;
}
return A.size() - duplicates;
}

int main()
{
int size;
cin >> size;
vector<int> A(size);

for (auto &x : A)
cin >> x;
int element;
cin >> element;
int newlength = removeElement(A, element);
cout << "Newlength after removal of element is " << newlength << '\n';

for (int i = 0; i < newlength; i++)
cout << A[i] << " ";

return 0;
}