Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Shuffle an Array problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Shuffle an Array problem solution you have 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:

  1. Solution(int[] nums) Initializes the object with the integer array nums.
  2. int[] reset() Resets the array to its original configuration and returns it.
  3. int[] shuffle() Returns a random shuffling of the array.
Leetcode Shuffle an Array problem solution

Leetcode Shuffle an Array problem solution in Python.

def __init__(self, nums):
        self.original_array = nums
        """
        :type nums: List[int]
        """
        

    def reset(self):
        return self.original_array
        """
        Resets the array to its original configuration and return it.
        :rtype: List[int]
        """
        

    def shuffle(self):
        arr = self.original_array[:]
        for x in range(len(self.original_array)):
            n = random.randint(0, x)
            arr[x],arr[n] = arr[n], arr[x]
        return arr

Shuffle an Array problem solution in Java.

public class Solution {
    public int[] ini;
    public Random rand;
    
    public Solution(int[] nums) {
        ini = nums;
        rand = new Random();
    }
    
    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return ini;
    }
    
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        boolean[] visited = new boolean[ini.length];
        int[] nums = new int[ini.length];
        int index = 0;
        while (index < nums.length) {
            int r = rand.nextInt(nums.length);
            if (visited[r] == false) {
                nums[index++] = ini[r];
                visited[r] = true;
            }
        }
        return nums;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<int> array;
    vector<int> original;
    Solution(vector<int>& nums) : original(nums), array(nums) {}
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return original;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        if(array.size() > 0){
            swap(array[rand() % array.size()],array[rand() % array.size()]);
        }
        return array;
    }
};

Problem solution in C.

typedef struct {
    int *original;
    int size;
} Solution;

Solution* solutionCreate(int* nums, int size) {
    Solution* obj=(Solution*)malloc(sizeof(Solution));
    obj->original=nums;
    obj->size=size;
    return obj;
}

/** Resets the array to its original configuration and return it. */
int* solutionReset(Solution* obj, int *returnSize) {
    *returnSize=obj->size;
    return obj->original;
}

/** Returns a random shuffling of the array. */
int* solutionShuffle(Solution* obj, int *returnSize) {
    *returnSize=obj->size;
    int *ret=(int*)calloc((*returnSize),sizeof(int));
    int *array=(int*)calloc((*returnSize),sizeof(int));
    int temp=0;
    int count=-1;
    for(int i=0;i<*returnSize;i++){
        temp=random()%((*returnSize)-i);
        for(int j=0;j<*returnSize;j++){
            if(array[j]==0){
                count++;
                if(count==temp){
                    array[j]=1;
                    count=-1;
                    ret[i]=obj->original[j];
                    break;
                }
            }
        }
    }
    return ret;
}

void solutionFree(Solution* obj) {
    free(obj->original);
    free(obj);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes