From 3e4c8ffa0db1d5ec387178169ca4a070116e0b01 Mon Sep 17 00:00:00 2001 From: "Shivendra.B" <93897277+Astordnomer@users.noreply.github.com> Date: Tue, 1 Oct 2024 05:08:08 +0000 Subject: [PATCH 1/3] Added C++ solution for Array Rotation using Reversal Algorithm --- .../Array Rotation/solution.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "Beginner Level \360\237\223\201/Array Rotation/solution.cpp" diff --git "a/Beginner Level \360\237\223\201/Array Rotation/solution.cpp" "b/Beginner Level \360\237\223\201/Array Rotation/solution.cpp" new file mode 100644 index 00000000..db570384 --- /dev/null +++ "b/Beginner Level \360\237\223\201/Array Rotation/solution.cpp" @@ -0,0 +1,53 @@ +#include +using namespace std; + +// Function to reverse an array from index start to end +void reverseArray(int arr[], int start, int end) { + while (start < end) { + swap(arr[start], arr[end]); + start++; + end--; + } +} + +// Function to rotate the array to the left by d positions using the reversal algorithm +void rotateArray(int arr[], int d, int n) { + // Step 1: Reverse the first 'd' elements + reverseArray(arr, 0, d-1); + + // Step 2: Reverse the remaining 'n-d' elements + reverseArray(arr, d, n-1); + + // Step 3: Reverse the entire array + reverseArray(arr, 0, n-1); +} + +// Utility function to print the array +void printArray(int arr[], int n) { + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + +int main() { + int n, d; + cout << "Enter the size of the array: "; + cin >> n; + + int arr[n]; + cout << "Enter array elements: "; + for (int i = 0; i < n; i++) + cin >> arr[i]; + + cout << "Enter the number of positions to rotate: "; + cin >> d; + + // Rotate the array by 'd' positions + rotateArray(arr, d, n); + + // Print the rotated array + cout << "Rotated array: "; + printArray(arr, n); + + return 0; +} From 721abcebe6ff31fecf3c8c2ff7ef73a8aade4400 Mon Sep 17 00:00:00 2001 From: "Shivendra.B" <93897277+Astordnomer@users.noreply.github.com> Date: Tue, 1 Oct 2024 05:40:08 +0000 Subject: [PATCH 2/3] Added C++ solution for Array Rotation and updated data.json with my details --- "Beginner Level \360\237\223\201/data.json" | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git "a/Beginner Level \360\237\223\201/data.json" "b/Beginner Level \360\237\223\201/data.json" index d7efd767..3e1c81e0 100644 --- "a/Beginner Level \360\237\223\201/data.json" +++ "b/Beginner Level \360\237\223\201/data.json" @@ -141,6 +141,9 @@ "name": "Ramanpreet Kaur", "githubUsername": "1998ramanpreet" }, - + { + "name": "Sivendra B", + "githubUsername": "Astordnomer" + }, ] From 8a47f00ed602fb9c8b715a8adb0a8f4dbadedc1a Mon Sep 17 00:00:00 2001 From: "Shivendra.B" <93897277+Astordnomer@users.noreply.github.com> Date: Tue, 1 Oct 2024 05:43:23 +0000 Subject: [PATCH 3/3] Added C++ solution for Array Rotation --- "Beginner Level \360\237\223\201/data.json" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Beginner Level \360\237\223\201/data.json" "b/Beginner Level \360\237\223\201/data.json" index 3e1c81e0..c4a8821b 100644 --- "a/Beginner Level \360\237\223\201/data.json" +++ "b/Beginner Level \360\237\223\201/data.json" @@ -141,7 +141,7 @@ "name": "Ramanpreet Kaur", "githubUsername": "1998ramanpreet" }, - { + { "name": "Sivendra B", "githubUsername": "Astordnomer" },