From 101854689746c3daeafd960ef05fdd70de6c3d3d Mon Sep 17 00:00:00 2001 From: B PAVAN KUMAR Date: Mon, 1 Mar 2021 00:54:22 +0530 Subject: [PATCH] added left-rotation.cpp file (#84) * added left-rotation.cpp file * Update README.md Added a link for left-rotation.cpp --- arrays/README.md | 37 ++++++++++++++++--------------- arrays/c-or-cpp/left-rotation.cpp | 33 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 arrays/c-or-cpp/left-rotation.cpp diff --git a/arrays/README.md b/arrays/README.md index 8419274a..2bf2b625 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -1,18 +1,19 @@ -# Algorithms related to arrays - -### C or C++ - -1. [Counting Inversions](c-or-cpp/count-inversions.cpp) -2. [Dutch Flag Algo](c-or-cpp/dutch-flag-algo.cpp) - -### Python - -1. [Counting Inversions](python/count-inversions.py) - -### JavaScript - -1. [Counting Inversions](js/count-inversions.js) - -### Java - -1. [Counting Inversions](java/count-inversions.java) +# Algorithms related to arrays + +### C or C++ + +1. [Counting Inversions](c-or-cpp/count-inversions.cpp) +2. [Dutch Flag Algo](c-or-cpp/dutch-flag-algo.cpp) +3. [Left Rotation of Array](c-or-cpp/left-rotation.cpp) + +### Python + +1. [Counting Inversions](python/count-inversions.py) + +### JavaScript + +1. [Counting Inversions](js/count-inversions.js) + +### Java + +1. [Counting Inversions](java/count-inversions.java) diff --git a/arrays/c-or-cpp/left-rotation.cpp b/arrays/c-or-cpp/left-rotation.cpp new file mode 100644 index 00000000..0d6aab69 --- /dev/null +++ b/arrays/c-or-cpp/left-rotation.cpp @@ -0,0 +1,33 @@ +#include + +#define int long long int +#define pb push_back +using namespace std; + +signed main() +{ + ios_base::sync_with_stdio(0); + cin.tie(0); + int n,left_rotation_val; + cin>>n>>left_rotation_val; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin>>arr[i]; + } + + left_rotation_val = left_rotation_val % n; + //Doing this we can get the place from where the rotation starts. + // 5 4 + // 1 2 3 4 5 + // 1 2 3 4 5 -> 2 3 4 5 1(1 rotation) -> 3 4 5 1 2(2 rotation) -> 4 5 1 2 3(3 rotation) -> 5 1 2 3 4(4 rotation) + + for(int i = left_rotation_val; i