From 32ce0996d24a9046d156cfd0d40e5293cb9123d8 Mon Sep 17 00:00:00 2001 From: Mrudu09 <142366898+Mrudu09@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:31:27 +0530 Subject: [PATCH] Create mrudula.cpp --- .../mrudula.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Move all the negative elements to one side of the array/mrudula.cpp diff --git a/Move all the negative elements to one side of the array/mrudula.cpp b/Move all the negative elements to one side of the array/mrudula.cpp new file mode 100644 index 0000000..4d30201 --- /dev/null +++ b/Move all the negative elements to one side of the array/mrudula.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +vector moveNegativesToLeft(vector& arr) { + vector result; + + for (int i = 0; i < arr.size(); i++) { + if (arr[i] < 0) { + result.insert(result.begin(), arr[i]); + } + } + + for (int i = 0; i < arr.size(); i++) { + if (arr[i] >= 0) { + result.push_back(arr[i]); + } + } + + return result; +} + +int main() { + vector inputArray = {1, -2, 3, -4, 5, -6}; + vector rearrangedArray = moveNegativesToLeft(inputArray); + + cout << "Rearranged Array: "; + for (int i = 0; i < rearrangedArray.size(); i++) { + cout << rearrangedArray[i] << " "; + } + cout << endl; + + return 0; +}