Skip to content

Instantly share code, notes, and snippets.

@DeepakCarpenter
Last active June 16, 2018 09:27
Show Gist options
  • Save DeepakCarpenter/62f0049d481aa9fcbaeae13ffe21e640 to your computer and use it in GitHub Desktop.
Save DeepakCarpenter/62f0049d481aa9fcbaeae13ffe21e640 to your computer and use it in GitHub Desktop.
Immediate Larger Number
--------------------------------------------------- ---------------------------------------------------
--------------------------------------------------- Problem Statement ---------------------------------------------------
--------------------------------------------------- ---------------------------------------------------
Given an array A of n elements. Each element of A is an integer between 0 and 9. n <11. Given an integer number K < 1,000,000.
An operation number(A) is defined by concatenating every elements in A from left to right.
For example, if A = [0, 3, 2] then number(A) = 032
Please find an array B so that number(B) is the smallest possible number that satisfies all of the following conditions:
. Array B is a permutation of array A
. number(B) > number(A)
. number(B) is divisible by K
Solution with K=10.
B = [2, 3, 0]
Input: has 2 lines. The first line contains elements of an array A, which are separated by a single space. The second line contains the number K.
Output: Print out the array B, each number is separated by a space. If B doesn't exist, return a string NONE
For example:
Input:
0 3 2
10
Output:
2 3 0
Please implement this program in your preferred programming language and have unit tests for all cases you can think of. Thank you for your participation.
After you finish, please send us your code + unit tests to this email thanhpham@trustingsocial.com. If Gmail doesn't allow you to send the zip file, you can upload this zip file to google drive and share us your google drive link.
If you have any question, please email thanhpham@trustingsocial.com immediately.
---HINT---
- Going from the right of array A to the left, find an element A[i] that is smaller A[i+1]
- You need to find k between i+1 and n, swap A[i] with A[k], sort A[i+1..n] so that the new array B has number(B) that is the immediate larger than number(A).
- If number(B) is divisible by K, print B. Otherwise, repeat these 3 steps above again.
--------------------------------------------------- ---------------------------------------------------
--------------------------------------------------- Problem Solution ---------------------------------------------------
--------------------------------------------------- ---------------------------------------------------
print(mainFunction(integerArray: [0,3,2]))
func mainFunction(integerArray : [Int]) -> Int{
let input = integerArray.map{String($0)} //convert integer array to string array
let combinations = returnAllPermutation(string: input.joined(separator: ""))//get combinations
let firstNumber = (Int)(input.joined(separator: "")) //convert input array into number
for combination in combinations {
if let combinationNumber = (Int)(combination), let firstNum = firstNumber {
if combinationNumber > firstNum {
if combinationNumber % 10 == 0 {
if combinationNumber > firstNum {
return combinationNumber
}
}
}
}
}
return 0
}
public func returnAllPermutation(string: String) -> [String] {
let set = generatePerm(input: string)
var result = [String]()
for item in set {
result.append(item)
}
return result
}
private func generatePerm(input: String) -> Set<String> {
return generatePerm(prefix: "", input: input)
}
private func generatePerm(prefix: String, input: String) -> Set<String> {
let prefixCharacters = Array(prefix.characters)
var inputCharacters = Array(input.characters)
var result = Set<String>()
let n = inputCharacters.count
if n == 0 {
result.insert(prefix)
} else {
for i in 0...n-1 {
let prefixAndCharacterAtIndex = String(prefixCharacters) + String(inputCharacters[i])
let subStringBeforeIndex = String(inputCharacters[0..<i])
let subStringAfterIndex = String(inputCharacters [i+1..<n])
result = result.union(generatePerm(prefix: prefixAndCharacterAtIndex, input: subStringBeforeIndex + subStringAfterIndex))
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment