Skip to content
Programmingoneonone
Programmingoneonone
  • 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

Leetcode Minimum Moves to Equal Array Elements II problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Minimum Moves to Equal Array Elements II problem solution we have given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.

In one move, you can increment or decrement an element of the array by 1.

Leetcode Minimum Moves to Equal Array Elements II problem solution

Leetcode Minimum Moves to Equal Array Elements II problem solution in Python.

count = 0
    nums.sort()
    middle_element = nums[len(nums)//2]        
    for num in nums:
        count += abs(middle_element - num)     
    return count

Minimum Moves to Equal Array Elements II problem solution in Java.

public class Solution {
    public int minMoves2(int[] nums) {
        if (nums == null) return 0;
        
        Arrays.sort(nums);
        int k = (nums.length + 1) >> 1;
        int sum = 0;
        
        for (int i = 0; i< (nums.length >> 1); i++) {
            sum += nums[k+i] - nums[i];
        }
        return sum;      
    }
}

Problem solution in C++.

class Solution {
public:    
    int partition(vector<int> &nums, int l, int r) {
        int len = r - l + 1;
        int index = (rand() % len) + l;
        swap(nums[index], nums[r]);
        int pivot = nums[r];        
        int less = l;
        for (int i = l; i < r; i++) {
            if (nums[i] <= pivot) {
                swap(nums[i], nums[less++]);
            }
        }        
        swap(nums[less], nums[r]);        
        return less;
    }    
    int selectK(vector<int> &nums, int k, int l , int r) {
        if (l >= r) return l;        
        int pivot_index = partition(nums, l, r);        
        if (pivot_index - l + 1 == k) {
            return pivot_index;
        } else if (pivot_index - l + 1 < k) {
            return selectK(nums, k - (pivot_index - l + 1), pivot_index + 1, r);
        } else {
            return selectK(nums, k, l, pivot_index - 1);
        }
    }    
    int minMoves2(vector<int>& nums) {
        if (nums.empty()) return 0;        
        int median = nums.size() / 2;        
        int middle = nums[selectK(nums, median + 1, 0, nums.size() - 1)];
        int count = 0;
        for (int n : nums) {
            if (n > middle) {
                count += n - middle;
            } else {
                count += middle - n;
            }
        }        
        return count;
    }
};

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 *

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 just 7 dollars. Ask all your career-related questions 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