Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/main/java/com/company/minoperation/MinOperationV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.company.minoperation;


/**
* TimeComplexity O(N)
*/
public class MinOperationV1 {
public int minOperations(int n) {

// [1, 3, 5, 7] leftValue: 3, rightValue: 5
//
// [1, 3, 5, 7, 9] leftValue: 3, righValue: 7
int ret = 0;
int mIdx = n/2; // 2


int x; //왼쪽으로 이동할 예정 -- // 2
int y; // 오른쪽으로 이동할 예정 ++// 2
if (n % 2 == 1) {
x = mIdx - 1;
y = mIdx + 1;
} else {
x = mIdx - 1;
y = mIdx;
}

int leftValue = getValue(x); //
int rightValue = getValue(y); //

while(true) {

if(x >= 0) {
// Move left
leftValue = getValue(x--); // leftValue = 1 x = 0 -> x = -1
}

if (y < n) {
rightValue = getValue(y++); // rightValue = 5, y = 2 -> y = 3
}

while (true) {
leftValue++; // 4
rightValue--; // 6
ret++; // ret = 1
if(leftValue == rightValue) {
break;
}
}
if(x < 0) {
break;
}
}
return ret;
}

private int getValue(int idx) {
return (2 * idx) + 1;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/company/minoperation/MinOperationV2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.company.minoperation;

/**
* Time complexity O(1)
*/
public class MinOperationV2 {

// [1 (2), 3,(2) 5] answer =2
//[1(4), 3(2), 5, (2) 7,(4) 9] = answer=6
//[1(6), 3(4), 5(2), 7, 9, 11, 13] 2 * 1 + 2* 2, ..... + 2 * (n/2)

// O(n)
public int minOperations(int n) {
if (n == 1) {
return 0;
}
if (n % 2 != 0) {
return sumOdds(n/2);
} else {
return sumEvens(n/2);
}
}

private int sumOdds(int n) {
return n * (n + 1);
}

private int sumEvens(int n) {
return n * (n-1 ) + n;
}

/*
홀수: 2 6 12 20 = 시그마 (2*n) -> 2 * 시그마(n) = (n-1) * (n)
2 4 6 8 -> a(n) = 2*n
짝수: 1 4 9 16 -> 시그마 (2*n + 1) -> 1 + n * (n-1) + (n-1) = n * (n-1) + n
3 5 7 9 -> a(n) = 2*n + 1
*/
}
18 changes: 18 additions & 0 deletions src/main/java/com/company/minoperation/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Minimum Operations to Make Array Equal

You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e. 0 <= i < n).

In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e. perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations.

Given an integer n, the length of the array. Return the minimum number of operations needed to make all the elements of arr equal.

```
Input: n = 3
Output: 2
Explanation: arr = [1, 3, 5]
First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
```

Source:
leetcode.com