Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max slice sorted array #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
104 changes: 104 additions & 0 deletions src/main/java/com/company/problem/hard/SliceSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.company.problem.hard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
* PROBLEM:
*
* We are given an array A consisting of N distinct integers.
* We would like to sort array A into ascending order using a simple algorithm.
* First, we divide it into one or more slices (a slice is a contiguous subarray).
* Then we sort each slice. After that, we join the sorted slices in the same order.
* Write a function solution that returns the maximum number of slices for which the algorithm will return a correctly sorted array.
*
* 1. Given A = [2,4,1,6,5,9,7] will return e.
* [2,4,1], [6,5], [9,7] then after soring each slice and joining them together, the whole array will be sorted into ascending order.
*
* A= [4,3,2,6,1] will return 1. the array cannot be split into smaller slices; it has to be sorted all at once.
*
*
* 3. A = [2,1,6,4,3,7] will return 3.
*
* N range [1..100,000]
* element range [1..1,000,000,000]
*/
public class SliceSort {

public int sort(Integer[] array) {
int count = 0;

// 1) Slice until asc order stops (Note: a last element included)
List<Integer[]> slices = slice(array);

// Exit condition: Only a single slice exits
if (slices.size() == 1) {
Arrays.sort(array);
return 1;
}
// 2) Recursive call sort() for each sliced array
for (Integer[] s: slices) {
count += sort(s);
}
// 3) Merge sliced arrays sequentially and verify elements are asc ordered.
List<Integer> merged = new LinkedList<>();

Integer prev = null;
boolean sorted = true;
for (Integer[] slice: slices) {

for (Integer el: slice) {
merged.add(el);
if (prev == null) {
prev = el;
continue;
}
if (el < prev) {
sorted = false;
}
}
}
// 4-1) if so, Assign the sorted array to the given array
// 4-2) otherwise, Re-sort the merged array and slice count is 1
if (sorted) {
merged.toArray(array);
} else {
count = 1;
Arrays.sort(array);
}
// 5) Return the count of slices
return count;
}

private List<Integer[]> slice(Integer[] array) {
List<Integer[]> slices = new ArrayList<>();

Integer prev = null;
List<Integer> slice = new LinkedList<>();
for (int i = 0; i < array.length; i++) {
if (prev == null) {
prev = array[i];
slice.add(array[i]);
continue;
}

if (array[i] < prev) {
slice.add(array[i]);

Integer[] e = slice.toArray(new Integer[]{});
slices.add(e);
prev = null;
slice = new LinkedList<>();
} else {
prev = array[i];
slice.add(array[i]);
if (i == array.length -1) {
slices.add(slice.toArray(new Integer[]{}));
}
}
}
return slices;
}
}
46 changes: 46 additions & 0 deletions src/test/java/com/company/problem/hard/SliceSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.company.problem.hard;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import static org.junit.jupiter.api.Assertions.*;

public class SliceSortTest {
public SliceSort sliceSort = new SliceSort();

@Test
public void sample1() {
// Given
Integer[] given = {2, 4, 1, 6, 5, 9, 7};

// When
int slicedCount = sliceSort.sort(given);

// Then
Assertions.assertEquals(3, slicedCount);
}

@Test
public void sample2() {
// Given
Integer[] given = {4, 3, 2, 6, 1};

// When
int slicedCount = sliceSort.sort(given);

// Then
Assertions.assertEquals(1, slicedCount);
}

@Test
public void sample3() {
// Given
Integer[] given = {2, 1, 6, 4, 3, 7};

// When
int slicedCount = sliceSort.sort(given);

// Then
Assertions.assertEquals(3, slicedCount);
}
}