LeetCode 2967: Minimum Cost to Make Array Equalindromic Solution

Master LeetCode problem 2967 (Minimum Cost to Make Array Equalindromic), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2967. Minimum Cost to Make Array Equalindromic

Problem Explanation

Explanation:

To make the array equalindromic, we need to find the minimum cost to change all elements of the array to a palindromic number. We can achieve this by finding the median of the array and changing all elements to the median value.

  1. Find the median of the array.
  2. Calculate the total cost by taking the absolute difference between each element and the median and summing them up.

Solution Code

class Solution {
    public int minCost(int[] nums) {
        Arrays.sort(nums);
        int median = nums[nums.length / 2];
        int cost = 0;
        
        for (int num : nums) {
            cost += Math.abs(num - median);
        }
        
        return cost;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2967 (Minimum Cost to Make Array Equalindromic)?

This page provides optimized solutions for LeetCode problem 2967 (Minimum Cost to Make Array Equalindromic) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2967 (Minimum Cost to Make Array Equalindromic)?

The time complexity for LeetCode 2967 (Minimum Cost to Make Array Equalindromic) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2967 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2967 in Java, C++, or Python.

Back to LeetCode Solutions