Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Shuffle an Array problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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