Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Created October 11, 2021 11:39
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 alexpaul/8d3652bd87879f3c080590f7ce554f44 to your computer and use it in GitHub Desktop.
Save alexpaul/8d3652bd87879f3c080590f7ce554f44 to your computer and use it in GitHub Desktop.

HackerRank Questions

1. Mini-max Problem

HackerRank - Mini-max Problem

func miniMaxSum(arr: [Int]) -> Void {
    // Write your code here
    guard arr.count > 4 else { return }
    let arr = arr.sorted() 
    let minNums = arr.prefix(4).reduce(0, +)
    let maxNums = arr.suffix(4).reduce(0, +)
    
    print("\(minNums) \(maxNums)")
}

2. Birthday Cake Candles

HackerRank - Birthday Cake Candles

func birthdayCakeCandles(candles: [Int]) -> Int {
    // Write your code here
    let maxHeight = candles.max() ?? 0
    var freqDict = [Int: Int]()
    candles.forEach { freqDict[$0, default: 0] += 1 }
    
    let numberOfCandles = freqDict[maxHeight] ?? 0 
    
    return numberOfCandles
}

3. Between Two Sets

HackerRank - Between Two Sets

func getTotalX(a: [Int], b: [Int]) -> Int {
    // Write your code here
    var result = [Int]()
    for i in 1...100 {
        let divideEvenlyA = (a.filter({ i % $0 != 0 }).count == 0)
        let divideEvenlyB = (b.filter({ $0 % i != 0 }).count == 0)

        if divideEvenlyA && divideEvenlyB {
            result.append(i)
        }
    }
    return result.count
}

4. Number Line Jumps

HackerRank - Number Line Jumps

func kangaroo(x1: Int, v1: Int, x2: Int, v2: Int) -> String {
    if v1 == v2 {
        if x1 == x2 { return "YES"}
        return "NO"
    }
    let s:Float = (Float(x2) - Float(x1)) / Float(v1 - v2)
    if s > 0 && s == Float(Int(s)) { return "YES"}
    return "NO"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment