Skip to main content

Command Palette

Search for a command to run...

Minimum Number of Moves to Seat Everyone

Published
1 min read
Minimum Number of Moves to Seat Everyone
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.

There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the i<sup>th</sup> seat. You are also given the array students of length n, where students[j] is the position of the j<sup>th</sup> student.

You may perform the following move any number of times:

  • Increase or decrease the position of the i<sup>th</sup> student by 1 (i.e., moving the i<sup>th</sup> student from position x to x + 1 or x - 1)

Return theminimum number of movesrequired to move each student to a seat such that no two students are in the same seat.

Note that there may be multiple seats or students in the same position at the beginning.

LeetCode Problem - 2037

class Solution {
    public int minMovesToSeat(int[] seats, int[] students) {
        // Sort both arrays to facilitate minimal adjustments calculation
        Arrays.sort(seats);
        Arrays.sort(students);

        int count = 0; // Initialize the total count of moves

        // Calculate the sum of absolute differences between corresponding elements
        for(int i = 0; i < seats.length; i++){
            count += Math.abs(seats[i] - students[i]);
        }

        return count; // Return the total moves required
    }
}

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.