https://github.com/ygit/algorithms
https://github.com/ygit/algorithms
Last synced: 22 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ygit/algorithms
- Owner: ygit
- License: apache-2.0
- Created: 2021-02-13T16:51:09.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-14T13:27:55.000Z (about 5 years ago)
- Last Synced: 2025-09-04T18:47:31.192Z (6 months ago)
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Algorithms
1. Maximum Pairwise Product
Given a sequence of n non-negative integers to find the maximum pairwise product.
Input: [1,2,3]
Output: 6
Input: [5,6,7]
Output: 42
Solution -
func maximumPairwiseProduct(array: [Int]) -> Int? {
guard var maxProduct = array.first else {
return nil
}
for (index, number) in array.enumerated() {
var anotherIndex = index + 1
while anotherIndex < array.count {
let anotherNumber = array[anotherIndex]
let product = number * anotherNumber
if product > maxProduct {
maxProduct = product
}
anotherIndex += 1
}
}
return maxProduct
}
maximumPairwiseProduct(array: [5,6,7]) => 42
2. Get largest number from string
Input: 132
Output: 321
Input: 123456789
Output: 987654321
Solution -
func getLargestNumber(_ string: String) -> String {
guard string.count > 0 else {
return ""
}
var array = string.compactMap { Int(String($0)) }
var largestNumber = array.first!
var largestNumberIndex = 0
for (index, number) in array.enumerated() {
if number > largestNumber {
largestNumber = number
largestNumberIndex = index
}
}
array.remove(at: largestNumberIndex)
let newArray = array.map { String($0) }
let newString = newArray.joined()
print(largestNumberIndex, largestNumber, newString)
return "\(largestNumber)" + getLargestNumber(newString)
}
getLargestNumber("19848301023")
getLargestNumber("1234567890")