From 6581c105e55c5023cc3f04030cc5ad3d5f9ad7ee Mon Sep 17 00:00:00 2001 From: "Amisha.Sahu" <115696152+Amisha2093@users.noreply.github.com> Date: Fri, 21 Oct 2022 15:45:47 +0530 Subject: [PATCH 1/3] Left Rotate an Array by d places Naive Solution --- LeftRotationDplace(Naive).cpp | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 LeftRotationDplace(Naive).cpp diff --git a/LeftRotationDplace(Naive).cpp b/LeftRotationDplace(Naive).cpp new file mode 100644 index 0000000..4605ec1 --- /dev/null +++ b/LeftRotationDplace(Naive).cpp @@ -0,0 +1,39 @@ +// Left Rotate an Array by D places +// example: +// Input: arr[] = {1, 2, 3, 4, 5, 6, 7} +// d = 2 +// Output: arr[] = {3, 4, 5, 6, 7, 1, 2} + +#include +using namespace std; +#define vi vector + +// naive solution +// Time Complexity: O(n*d) +// Space Complexity: O(1) + +signed main(){ + int n,d; + cin>>n>>d; + vi v(n);/ + for(int i=0;i>v[i]; + + // Naive Approach + while(d--) + { + for(int i = 0; i < n-1; i++) + { + swap(v[i],v[i+1]); + } + } + + for(int i=0;i Date: Fri, 21 Oct 2022 15:48:34 +0530 Subject: [PATCH 2/3] Left Rotate an Array by d places better solution --- LeftRotationDplace(Better).cpp | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 LeftRotationDplace(Better).cpp diff --git a/LeftRotationDplace(Better).cpp b/LeftRotationDplace(Better).cpp new file mode 100644 index 0000000..e611383 --- /dev/null +++ b/LeftRotationDplace(Better).cpp @@ -0,0 +1,36 @@ +// Left Rotate an Array by d places +// example: +// Input: arr[] = {1, 2, 3, 4, 5, 6, 7} +// d = 2 +// Output: arr[] = {3, 4, 5, 6, 7, 1, 2} + +#include +using namespace std; +#define vi vector + +// Efficient Solution +// Time Complexity: O(n) +// Space Complexity: O(d) + +signed main(){ + int n,d; + cin>>n>>d; + vi v(n); + for(int i=0;i>v[i]; + + // Efficient Approach + int temp[d]; + for(int i=0;i Date: Fri, 21 Oct 2022 15:50:44 +0530 Subject: [PATCH 3/3] Left Rotate an Array by D places Optimal Solution --- LeftRotationDplace(Efficient).cpp | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 LeftRotationDplace(Efficient).cpp diff --git a/LeftRotationDplace(Efficient).cpp b/LeftRotationDplace(Efficient).cpp new file mode 100644 index 0000000..9419c05 --- /dev/null +++ b/LeftRotationDplace(Efficient).cpp @@ -0,0 +1,35 @@ +// Left Rotate an Array by D places +// example: +// Input: arr[] = {1, 2, 3, 4, 5, 6, 7} +// d = 2 +// Output: arr[] = {3, 4, 5, 6, 7, 1, 2} + +#include +using namespace std; +#define vi vector + +// Optimal Solution +// Time Complexity: O(n) +// Space Complexity: O(1) + +signed main(){ + int n,d; + cin>>n>>d; + vi v(n); + for(int i=0;i>v[i]; + // More Efficient Approach + // Reversal Approach + // 1. Reverse the first d elements + // 2. Reverse the last n-d elements + // 3. Reverse the whole array + + reverse(v.begin(),v.begin()+d); + reverse(v.begin()+d,v.end()); + reverse(v.begin(),v.end()); + + for(int i=0;i