31void print(
const std::vector<int32_t> &array) {
32 for (int32_t i : array) {
33 std::cout << i <<
" ";
49std::vector<int32_t>
get_union(
const std::vector<int32_t> &first,
50 const std::vector<int32_t> &second) {
51 std::vector<int32_t> res;
54 size_t f_length = first.size();
55 size_t s_length = second.size();
58 while (f_index < f_length && s_index < s_length) {
59 if (first[f_index] < second[s_index]) {
60 next = first[f_index];
62 }
else if (first[f_index] > second[s_index]) {
63 next = second[s_index];
66 next = first[f_index];
70 if ((res.size() == 0) || (next != res.back())) {
74 while (f_index < f_length) {
75 next = first[f_index];
76 if ((res.size() == 0) || (next != res.back())) {
81 while (s_index < s_length) {
82 next = second[s_index];
83 if ((res.size() == 0) || (next != res.back())) {
105 std::cout <<
"TEST CASE 1\n";
106 std::cout <<
"Intialized a = {} b = {}\n";
107 std::cout <<
"Expected result: {}\n";
108 std::vector<int32_t> a = {};
109 std::vector<int32_t> b = {};
110 std::vector<int32_t>
result = get_union(a, b);
113 std::cout <<
"TEST PASSED!\n\n";
120 std::cout <<
"TEST CASE 2\n";
121 std::cout <<
"Intialized a = {} b = {2, 3}\n";
122 std::cout <<
"Expected result: {2, 3}\n";
123 std::vector<int32_t> a = {};
124 std::vector<int32_t> b = {2, 3};
125 std::vector<int32_t>
result = get_union(a, b);
128 std::cout <<
"TEST PASSED!\n\n";
135 std::cout <<
"TEST CASE 3\n";
136 std::cout <<
"Intialized a = {4, 6} b = {2, 3}\n";
137 std::cout <<
"Expected result: {2, 3, 4, 6}\n";
138 std::vector<int32_t> a = {4, 6};
139 std::vector<int32_t> b = {2, 3};
140 std::vector<int32_t>
result = get_union(a, b);
141 std::vector<int32_t> expected = {2, 3, 4, 6};
142 assert(result == expected);
144 std::cout <<
"TEST PASSED!\n\n";
151 std::cout <<
"TEST CASE 4\n";
152 std::cout <<
"Intialized a = {4, 6, 6, 7} b = {2, 3, 4}\n";
153 std::cout <<
"Expected result: {2, 3, 4, 6, 7}\n";
154 std::vector<int32_t> a = {4, 6, 6, 7};
155 std::vector<int32_t> b = {2, 3, 4};
156 std::vector<int32_t>
result = get_union(a, b);
157 std::vector<int32_t> expected = {2, 3, 4, 6, 7};
158 assert(result == expected);
160 std::cout <<
"TEST PASSED!\n\n";
167 std::cout <<
"TEST CASE 5\n";
168 std::cout <<
"Intialized a = {1, 4, 6, 7, 9} b = {2, 3, 5}\n";
169 std::cout <<
"Expected result: {1, 2, 3, 4, 5, 6, 7, 9}\n";
170 std::vector<int32_t> a = {1, 4, 6, 7, 9};
171 std::vector<int32_t> b = {2, 3, 5};
172 std::vector<int32_t>
result = get_union(a, b);
173 std::vector<int32_t> expected = {1, 2, 3, 4, 5, 6, 7, 9};
174 assert(result == expected);
176 std::cout <<
"TEST PASSED!\n\n";
184 std::cout <<
"TEST CASE 6\n";
185 std::cout <<
"Intialized a = {1, 3, 3, 2, 5, 9, 4, 3, 2} ";
186 std::cout <<
"b = {11, 3, 7, 8, 6}\n";
187 std::cout <<
"Expected result: {1, 2, 3, 4, 5, 6, 7, 8, 9, 11}\n";
188 std::vector<int32_t> a = {1, 3, 3, 2, 5, 9, 4, 3, 2};
189 std::vector<int32_t> b = {11, 3, 7, 8, 6};
190 std::sort(a.begin(), a.end());
191 std::sort(b.begin(), b.end());
192 std::vector<int32_t>
result = get_union(a, b);
193 std::vector<int32_t> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
194 assert(result == expected);
196 std::cout <<
"TEST PASSED!\n\n";
uint64_t result(uint64_t n)
std::vector< int32_t > get_union(const std::vector< int32_t > &first, const std::vector< int32_t > &second)
Gets the union of two sorted arrays, and returns them in a vector.
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
Testcases to check Union of Two Arrays.
void test1()
A Test to check an simple case.
void test4()
A Test to check a very large input.
void test3()
A Test to check an invalid shift value.
void test6()
A Test to check correct functionality with an array sorted using std::sort.
void test2()
A Test to check an empty vector.
void test5()
A Test to check a shift of zero.
static void test()
Function to test the correctness of get_union() function.