Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Created August 7, 2019 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/5f82d93c2cd804ed3f44f1b29ad9b137 to your computer and use it in GitHub Desktop.
Save crazy4groovy/5f82d93c2cd804ed3f44f1b29ad9b137 to your computer and use it in GitHub Desktop.
How many groups (more than 1 number per group) of an integer list can be split out, each sorted, and concatenated together to produce the same result as sorting the list?
function solution(A) {
let index = 0
let results = 0
let remainder = 0
let number
while (index < A.length) {
number = A[index]
for (let i = index + 1; i < A.length; i++) {
if (number > A[i]) {
index = i
}
}
if (number !== A[index]) {
results += 1
remainder = false
}
else remainder = true
index += 1
}
if (remainder) results += 1
return results > 1 ? results : 0
}
console.log(solution([1,2,3,4,5])) // 0 => no subgroups, just sort it
console.log(solution([5,4,3,2,1])) // 0 => no subgroups, just sort it
console.log(solution([2,1,3,4,5])) // 2 => [2,1],[3,4,5]
console.log(solution([2,1,4,3,6,5])) // 3 => [2,1],[4,3],[6,5]
console.log(solution([2,1,5,6,3])) // 2 => [2,1],[5,6,3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment