Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

chore(CPlusPlus): Merge two sorted array - Special Manner #1278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 algorithms/CPlusPlus/Arrays/merge-sort-special.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//Merging two sorted arrays in C++ in special manners

//You're given two sorted arrays. You have to merge them in this manner that if there are some same elements in both of them the third merging array should have less number of same elements. For example:
//Arr1= 1 5 5 5 6 6 7 7
//Arr2= 2 5 5 6
//
//output= 1 2 5 5 6 7 7
//There are two 5's because in one array there are three 5's and in second array there are two 5's. So we should always take less number.

// Time Complexity: O(len_arr1 + len_arr2) (since we are iterating over all the elements of both arrays)
// Space Complexity: O(len_arr1 + len_arr2)
#include <bits/stdc++.h>

using namespace std;

vector<int> mergeSpecial(vector<int> &a, vector<int> &b)
{
vector<int> final; // the final merged array
int i = 0, j = 0;
int lenA = a.size(), lenB = b.size();
while (i < lenA && j < lenB)
{
if (a[i] == b[j]) // element is present in both and within the min limit
{
final.push_back(a[i]);
i++;
j++;
}
else if (a[i] < b[j])
{
if (final.size() == 0 || final[final.size() - 1] != a[i]) // this condition will be false if a has more number of an element than b hence dont add to final list
final.push_back(a[i]);
i++;
}
else
{
if (final.size() == 0 || final[final.size() - 1] != b[j])
final.push_back(b[j]);
j++;
}
}
int lastElement = final[final.size() - 1]; // checking we don't push the last element more than it's minimum frequency in both array
if (j < lenB) // swapping a and b if array b was big
{
i = j;
lenA = lenB;
a = b;
}
while (i < lenA)
{
if (a[i] != lastElement)
final.push_back(a[i]);
i++;
}
return final;
}

int main()
{
vector<int> a = {1, 5, 5, 5, 6, 6, 7, 7};
vector<int> b = {2, 5, 5, 6};
vector<int> output = mergeSpecial(a, b);
for (int i : output)
cout << i << " ";
return 0;
}
1 change: 1 addition & 0 deletions algorithms/CPlusPlus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [Balanced Parenthesis](Arrays/balanced-parenthesis.cpp)
- [Find special index](Arrays/specialindex2.cpp)
- [Largest and smallest number in an array](Arrays/Largest-smallest.cpp)
- [Merge two sorted arrays - Special Manner](Arrays/merge-sort-special.cpp)

## Dynamic-Programming

Expand Down