384. Shuffle an Array LeetCode Solution

In this guide, you will get 384. Shuffle an Array LeetCode Solution with the best time and space complexity. The solution to Shuffle an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Shuffle an Array solution in C++
  4. Shuffle an Array solution in Java
  5. Shuffle an Array solution in Python
  6. Additional Resources
384. Shuffle an ArrayLeetCode Solution image

Problem Statement of Shuffle an Array

Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
Implement the Solution class:

Solution(int[] nums) Initializes the object with the integer array nums.
int[] reset() Resets the array to its original configuration and returns it.
int[] shuffle() Returns a random shuffling of the array.

Example 1:

Input
[“Solution”, “shuffle”, “reset”, “shuffle”]
[[[1, 2, 3]], [], [], []]
Output
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]

Explanation
Solution solution = new Solution([1, 2, 3]);
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
// Any permutation of [1,2,3] must be equally likely to be returned.
// Example: return [3, 1, 2]
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]

Constraints:

See also  434. Number of Segments in a String LeetCode Solution

1 <= nums.length <= 50
-106 <= nums[i] <= 106
All the elements of nums are unique.
At most 104 calls in total will be made to reset and shuffle.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

384. Shuffle an Array LeetCode Solution in C++

class Solution {
 public:
  Solution(vector<int>& nums) : nums(std::move(nums)) {}

  vector<int> reset() {
    return nums;
  }

  vector<int> shuffle() {
    vector<int> arr(nums);
    for (int i = arr.size() - 1; i > 0; --i) {
      const int j = rand() % (i + 1);
      swap(arr[i], arr[j]);
    }
    return arr;
  }

 private:
  vector<int> nums;
};
/* code provided by PROGIEZ */

384. Shuffle an Array LeetCode Solution in Java

class Solution {
  public Solution(int[] nums) {
    this.nums = nums;
  }

  public int[] reset() {
    return nums;
  }

  public int[] shuffle() {
    int[] arr = nums.clone();
    for (int i = arr.length - 1; i > 0; --i) {
      final int j = rand.nextInt(i + 1);
      swap(arr, i, j);
    }
    return arr;
  }

  private int[] nums;
  private Random rand = new Random();

  private void swap(int[] arr, int i, int j) {
    final int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}
// code provided by PROGIEZ

384. Shuffle an Array LeetCode Solution in Python

class Solution:
  def __init__(self, nums: list[int]):
    self.nums = nums

  def reset(self) -> list[int]:
    return self.nums

  def shuffle(self) -> list[int]:
    arr = self.nums.copy()
    for i in range(len(arr) - 1, 0, -1):
      j = random.randint(0, i)
      arr[i], arr[j] = arr[j], arr[i]
    return arr
 # code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.