Skip to content

Latest commit

 

History

History
121 lines (102 loc) · 2.34 KB

_1551. Minimum Operations to Make Array Equal.md

File metadata and controls

121 lines (102 loc) · 2.34 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 13, 2024

Last updated : July 04, 2024


Related Topics : Math

Acceptance Rate : 82.28 %


sum = (2 * n + 2) * n // 2
mean = (2 * n + 2) / 2
margineToMove = sum(2n+1 - ((2 * n + 2) / 2)) 
              = sum(2n+1 - (n+1)) 
              = sum(n) 
              = (1 + n) * n // 2
number of steps = margineToMove / 2 
                = (1 + n) * n // 2 // 2 
                = (n^2 + 1) // 4 
                = n^2 // 4
                = n ** 2 // 4
        

Solutions

C

#define minOperations(n) n * n / 4
int minOperations(int n) {
    return n * n / 4;
}

Java

class Solution {
    public int minOperations(int n) {
        return n * n / 4;
    }
}

JavaScript

/**
 * @param {number} n
 * @return {number}
 */
var minOperations = function(n) {
    return n * n / 4;
};

Kotlin

class Solution {
    fun minOperations(n: Int): Int {
        return n * n / 4;
    }
}

Python

class Solution:
    def minOperations(self, n: int) -> int:
        return n ** 2 // 4

Ruby

# @param {Integer} n
# @return {Integer}
def min_operations(n)
    return n * n / 4;
end

Rust

impl Solution {
    pub fn min_operations(n: i32) -> i32 {
        return n * n / 4;
    }
}