Skip to main content

Command Palette

Search for a command to run...

Shuffle the Array

Updated
1 min read
Shuffle the Array
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given the array nums consisting of 2n elements in the form [x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>].

Return the array in the form [x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>].

LeetCode Problem - 1470

class Solution {
    public int[] shuffle(int[] nums, int n) {
        // Initialize a new array to store the shuffled numbers.
        int[] result = new int[2*n];
        // Counter to keep track of the position to insert numbers into the new array.
        int count = 0;

        // First loop to insert every other element from the first half of the original array.
        for(int i=0; i<n; i++){
            result[count] = nums[i]; // Insert element from the first half.
            count = count+2; // Move to the next even position.
        }

        // Reset the counter for inserting elements from the second half.
        count = 1;

        // Second loop to insert every other element from the second half of the original array.
        for(int i=n; i<(2*n); i++){
            result[count] = nums[i]; // Insert element from the second half.
            count = count+2; // Move to the next odd position.
        }

        // Return the shuffled array.
        return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.