30void print(
const std::vector<int32_t> &array) {
31 for (int32_t i : array) {
32 std::cout << i <<
" ";
47std::vector<int32_t>
shift_right(
const std::vector<int32_t> &array,
49 if (array.size() <= shift) {
52 std::vector<int32_t> res(array.size());
53 for (
size_t i = shift; i < array.size(); i++) {
54 res[i] = array[i - shift];
56 for (
size_t i = 0; i < shift; i++) {
58 array[array.size() - shift + i];
77 std::cout <<
"TEST CASE 1\n";
78 std::cout <<
"Initialized arr = {1, 2, 3, 4, 5}\n";
79 std::cout <<
"Expected result: {4, 5, 1, 2, 3}\n";
80 std::vector<int32_t> arr = {1, 2, 3, 4, 5};
81 std::vector<int32_t> res = shift_right(arr, 2);
82 std::vector<int32_t> expected = {4, 5, 1, 2, 3};
83 assert(res == expected);
85 std::cout <<
"TEST PASSED!\n\n";
92 std::cout <<
"TEST CASE 2\n";
93 std::cout <<
"Initialized arr = {}\n";
94 std::cout <<
"Expected result: {}\n";
95 std::vector<int32_t> arr = {};
96 std::vector<int32_t> res = shift_right(arr, 2);
97 std::vector<int32_t> expected = {};
98 assert(res == expected);
100 std::cout <<
"TEST PASSED!\n\n";
107 std::cout <<
"TEST CASE 3\n";
108 std::cout <<
"Initialized arr = {1, 2, 3, 4, 5}\n";
109 std::cout <<
"Expected result: {}\n";
110 std::vector<int32_t> arr = {1, 2, 3, 4, 5};
111 std::vector<int32_t> res = shift_right(arr, 7);
112 std::vector<int32_t> expected = {};
113 assert(res == expected);
115 std::cout <<
"TEST PASSED!\n\n";
122 std::cout <<
"TEST CASE 4\n";
123 std::cout <<
"Initialized arr = {2, 4, ..., 420}\n";
124 std::cout <<
"Expected result: {420, 2, 4, ..., 418}\n";
125 std::vector<int32_t> arr;
126 for (
int i = 1; i <= 210; i++) {
127 arr.push_back(i * 2);
130 std::vector<int32_t> res = shift_right(arr, 1);
131 std::vector<int32_t> expected;
132 expected.push_back(420);
133 for (
int i = 0; i < 209; i++) {
134 expected.push_back(arr[i]);
136 assert(res == expected);
138 std::cout <<
"TEST PASSED!\n\n";
145 std::cout <<
"TEST CASE 5\n";
146 std::cout <<
"Initialized arr = {1, 2, 3, 4, 5}\n";
147 std::cout <<
"Expected result: {1, 2, 3, 4, 5}\n";
148 std::vector<int32_t> arr = {1, 2, 3, 4, 5};
149 std::vector<int32_t> res = shift_right(arr, 0);
152 std::cout <<
"TEST PASSED!\n\n";
static void test()
Function to test the correctness of shift_right() function.
std::vector< int32_t > shift_right(const std::vector< int32_t > &array, size_t shift)
Shifts the given vector to the right by the shift amount and returns a new vector with the result....
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 test2()
A Test to check an empty vector.
void test5()
A Test to check a shift of zero.