Java: Shuffle a given array of integers
60. Shuffle a given array of integers
Write a Java program to shuffle a given array of integers.
Example:
Input :
nums = { 1, 2, 3, 4, 5, 6 }
Output:
Shuffle Array: [4, 2, 6, 5, 1, 3]
Sample Solution:
Java Code:
Sample Output:
Original Array: [1, 2, 3, 4, 5, 6] Shuffle Array: [4, 2, 6, 5, 1, 3]
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to shuffle an array while ensuring no element remains in its original position.
- Write a Java program to randomly select a subset of k elements from an array of size n.
- Write a Java program to check if a given shuffled array is a valid permutation of another array.
- Write a Java program to shuffle an array using the Fisher-Yates algorithm.
Go to:
PREV : Find maximum product of two integers in an array.
NEXT : Rearrange array with every second element greater.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.