From 0a7064b45357ef379938ebf84f97537328382e68 Mon Sep 17 00:00:00 2001 From: Xtha-Sunil Date: Wed, 30 Oct 2024 23:20:04 +0545 Subject: [PATCH] Added solution for minimum right shifts to sort the array. --- C++/minimum-right-shifts-to-sort-the-array.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 C++/minimum-right-shifts-to-sort-the-array.cpp diff --git a/C++/minimum-right-shifts-to-sort-the-array.cpp b/C++/minimum-right-shifts-to-sort-the-array.cpp new file mode 100644 index 0000000..a3dfbd3 --- /dev/null +++ b/C++/minimum-right-shifts-to-sort-the-array.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int minimumRightShifts(vector& nums) { + for (int i = 0; i < nums.size(); ++i) { + if (is_sorted(nums.begin(), nums.end())) + return i; + int last_element = nums.back(); + nums.pop_back(); + nums.insert(nums.begin(), last_element); + } + return -1; + } +}; \ No newline at end of file