Given an integer array nums, rotate the array to the left by one.
Note: There is no need to return anything, just modify the given array.
Input: nums = [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
Explanation: Initially, nums = [1, 2, 3, 4, 5]
Rotating once to left -> nums = [2, 3, 4, 5, 1]
Input: nums = [-1, 0, 3, 6]
Output: [0, 3, 6, -1]
Explanation: Initially, nums = [-1, 0, 3, 6]
Rotating once to left -> nums = [0, 3, 6, -1]
Input: nums = [7, 6, 5, 4]
To rotate an array by one position to the right, consider that the last element of the array will move to the first position, while all other elements shift one position to the right. The thought process involves first capturing the value of the first element in a temporary variable. Next, iterate through the array starting from the second element and shift each element to the position of its predecessor. Finally, place the initially captured value into the last position of the array. This approach ensures that the array is rotated by one position effectively.
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
void rotateArrayByOne(vector<int>& nums) {
// Store the first element in a temporary variable
int temp = nums[0];
// Shift elements to the left
for (int i = 1; i < nums.size(); ++i) {
nums[i - 1] = nums[i];
}
// Place the first element at the end
nums[nums.size() - 1] = temp;
}
};
int main() {
Solution solution;
vector<int> nums = {1, 2, 3, 4, 5};
solution.rotateArrayByOne(nums);
for (int num : nums) {
cout << num << " "; // Output the rotated array
}
return 0;
}
class Solution {
public void rotateArrayByOne(int[] nums) {
// Store the first element in a temporary variable
int temp = nums[0];
// Shift elements to the left
for (int i = 1; i < nums.length; i++) {
nums[i - 1] = nums[i];
}
// Place the first element at the end
nums[nums.length - 1] = temp;
}
// Main method for testing
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {1, 2, 3, 4, 5};
solution.rotateArrayByOne(nums);
// Output the rotated array
for (int num : nums) {
System.out.print(num + " ");
}
}
}
class Solution:
def rotateArrayByOne(self, nums):
# Store the first element in a temporary variable
temp = nums[0]
# Shift elements to the left
for i in range(1, len(nums)):
nums[i - 1] = nums[i]
# Place the first element at the end
nums[-1] = temp
# Main method for testing
if __name__ == "__main__":
solution = Solution()
nums = [1, 2, 3, 4, 5]
solution.rotateArrayByOne(nums)
print(nums) # Output the rotated array
class Solution {
rotateArrayByOne(nums) {
// Store the first element in a temporary variable
const temp = nums[0];
// Shift elements to the left
for (let i = 1; i < nums.length; i++) {
nums[i - 1] = nums[i];
}
// Place the first element at the end
nums[nums.length - 1] = temp;
}
}
// Main method for testing
const solution = new Solution();
const nums = [1, 2, 3, 4, 5];
solution.rotateArrayByOne(nums);
console.log(nums); // Output the rotated array
Time Complexity: O(N), where N is the number of elements in the array. Each element is visited once during the iteration.
Space Complexity: O(1). The space used does not depend on the size of the input array and remains constant.
Q: Can this logic handle nested arrays or complex objects?
A: Yes, the logic applies to arrays of any type. For complex objects, ensure that only references are moved and no deep copying is unintentionally triggered.
Q: How can you validate correctness across various test cases?
A: Validation involves testing: - Empty arrays: Output should remain empty. - Single-element arrays: Output should remain unchanged. - Arrays with duplicates: Check that all duplicates are correctly preserved and shifted. - Arrays with mixed values: Ensure the logic handles all types of elements consistently.
Q: How would the algorithm handle multidimensional arrays?
A: For multidimensional arrays (e.g., matrices), rotation involves more complex transformations: - Left Rotation: Shifting rows or columns depending on the axis of rotation. - Right Rotation: Similar logic but reversed.
Q: What is the difference between in-place rotation and using extra space?
A: - In-Place Rotation: Rearranges elements directly in the original array without using additional memory. It is more space-efficient but often requires more careful handling of indices. - Using Extra Space: Creates a temporary array to hold shifted elements, simplifying the process but increasing memory usage.