Skip to content

Commit

Permalink
Solution to "Merge Two 2D Arrays by Summing Values" problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoval committed Mar 3, 2025
1 parent 718752e commit 10753a9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.dkoval.leetcode.challenge;

import java.util.ArrayList;

/**
* <a href="https://leetcode.com/problems/partition-array-according-to-given-pivot/">Partition Array According to Given Pivot</a>
* <p>
* You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:
* <ul>
* <li>Every element less than pivot appears before every element greater than pivot.</li>
* <li>Every element equal to pivot appears in between the elements less than and greater than pivot.</li>
* <li>The relative order of the elements less than pivot and the elements greater than pivot is maintained.</li>
* </ul>
* More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element.
* If i < j and both elements are smaller (or larger) than pivot, then pi < pj.
* <p>
* Return nums after the rearrangement.
* <p>
* Constraints:
* <ul>
* <li>1 <= nums.length <= 10^5</li>
* <li>-106 <= nums[i] <= 10^6</li>
* <li>pivot equals to an element of nums</li>
* </ul>
*/
public interface PartitionArrayAccordingToGivenPivot {

int[] pivotArray(int[] nums, int pivot);

// O(N) time | O(N) space
class PartitionArrayAccordingToGivenPivotRev1 implements PartitionArrayAccordingToGivenPivot {

@Override
public int[] pivotArray(int[] nums, int pivot) {
final var n = nums.length;

final var ans = new int[n];
var i = 0;

final var greater = new ArrayList<Integer>();
var same = 0;
for (var x : nums) {
if (x < pivot) {
ans[i++] = x;
} else if (x > pivot) {
greater.add(x);
} else {
same++;
}
}

while (same-- > 0) {
ans[i++] = pivot;
}

for (var x : greater) {
ans[i++] = x;
}

return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.dkoval.leetcode.challenge

import com.github.dkoval.leetcode.challenge.PartitionArrayAccordingToGivenPivot.PartitionArrayAccordingToGivenPivotRev1
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import java.util.stream.Stream

internal class PartitionArrayAccordingToGivenPivotTest {

class InputArgumentsProvider : ArgumentsProvider {

override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of(
Arguments.of(
intArrayOf(9, 12, 5, 10, 14, 3, 10),
10,
intArrayOf(9, 5, 3, 10, 10, 12, 14)
),
Arguments.of(
intArrayOf(-3, 4, 3, 2),
2,
intArrayOf(-3, 2, 4, 3)
)
)
}

@Nested
inner class PartitionArrayAccordingToGivenPivotRev1Test {

@ParameterizedTest
@ArgumentsSource(InputArgumentsProvider::class)
fun `should partition the array according to the given pivot value`(
nums: IntArray,
pivot: Int,
expected: IntArray
) {
PartitionArrayAccordingToGivenPivotRev1().test(nums, pivot, expected)
}
}
}

private fun PartitionArrayAccordingToGivenPivot.test(nums: IntArray, pivot: Int, expected: IntArray) {
val actual = pivotArray(nums, pivot)
assertArrayEquals(expected, actual)
}

0 comments on commit 10753a9

Please sign in to comment.