Given an array nums of n integers, return reverse of the array.
Input : nums = [1, 2, 3, 4, 5]
Output : [5, 4, 3, 2, 1]
Input : nums = [1, 3, 3, 3, 5]
Output : [5, 3, 3, 3, 1]
Input : nums = [1, 2, 1]
The concept of reversing an array involves swapping elements from both ends, moving towards the center. Recursion facilitates this by initially swapping the first and last elements, then progressively moving inward until the pointers converge at the center.
class Solution {
public:
vector<int> reverseArray(vector<int>& nums) {
// Call the helper function to reverse the array
reverse(nums, 0, nums.size() - 1);
// Return the reversed array
return nums;
}
// Helper method to reverse the array using recursion
private:
void reverse(vector<int>& nums, int left, int right) {
if (left >= right) {
return;
}
// Swap the elements
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
// Recursive call with updated pointers
reverse(nums, left + 1, right - 1);
}
};
int main() {
Solution solution;
vector<int> nums = {1, 2, 3, 4, 5};
vector<int> result = solution.reverseArray(nums);
for (int num : result) {
cout << num << " ";
}
return 0;
}
class Solution {
public int[] reverseArray(int[] nums) {
// Call the helper function to reverse the array
reverse(nums, 0, nums.length - 1);
// Return the reversed array
return nums;
}
private void reverse(int[] nums, int left, int right) {
// Base case: pointers have crossed, the array is reversed
if (left >= right) {
return;
}
// Swap the elements
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
// Recursive call with updated pointers
reverse(nums, left + 1, right - 1);
}
}
// Main method for testing the reverseArray function
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {1, 2, 3, 4, 5};
int[] result = solution.reverseArray(nums);
for (int num : result) {
System.out.print(num + " ");
}
}
}
class Solution:
def reverseArray(self, nums):
# Call the helper function to reverse the array
self.reverse(nums, 0, len(nums) - 1)
# Return the reversed array
return nums
def reverse(self, nums, left, right):
if left >= right:
return
# Swap the elements
nums[left], nums[right] = nums[right], nums[left]
# Recursive call with updated pointers
self.reverse(nums, left + 1, right - 1)
# Main method for testing the reverseArray function
if __name__ == "__main__":
solution = Solution()
nums = [1, 2, 3, 4, 5]
result = solution.reverseArray(nums)
print(result) # Print the reversed array
class Solution {
// Method to reverse the array
reverseArray(nums) {
// Call the helper function to reverse the array
this.reverse(nums, 0, nums.length - 1);
// Return the reversed array
return nums;
}
// Helper method to reverse the array using recursion
reverse(nums, left, right) {
if (left >= right) {
return;
}
// Swap the elements
[nums[left], nums[right]] = [nums[right], nums[left]];
// Recursive call with updated pointers
this.reverse(nums, left + 1, right - 1);
}
}
// Main method for testing the reverseArray function
const solution = new Solution();
const nums = [1, 2, 3, 4, 5];
const result = solution.reverseArray(nums);
console.log(result);
Time Complexity: O(N) The time complexity is O(N)
because we perform a constant-time swap operation for each element pair.
Space Complexity : O(N) The space complexity is O(N)
due to the recursion stack.