How to write an algorithm to check if the sum of any two numbers in an array/list matches a given number?

前端 未结 14 2207
萌比男神i
萌比男神i 2021-01-30 11:29

How can I write an algorithm to check if the sum of any two numbers in an array/list matches a given number with a complexity of nlogn?

14条回答
  •  不知归路
    2021-01-30 12:15

    1. Solved the question in Swift 4.0
    2. Solved in 3 different ways (with 2 different type of return -> Boolean and Indexes)

    A) TimeComplexity => 0(n Log n) SpaceComplexity => 0(n).

    B) TimeComplexity => 0(n^2) SpaceComplexity => 0(1).

    C) TimeComplexity => 0(n) SpaceComplexity => 0(n)

    1. Choose Solution A, B or C depending on TradeOff.

      //***********************Solution A*********************//
      //This solution returns TRUE if any such two pairs exist in the array
      func binarySearch(list: [Int], key: Int, start: Int, end: Int) -> Int? { //Helper Function
      
                  if end < start {
                      return -1
                  } else {
                      let midIndex = (start + end) / 2
      
                      if list[midIndex] > key {
                          return binarySearch(list: list, key: key, start: start, end:  midIndex - 1)
                      } else if list[midIndex] < key {
                          return binarySearch(list: list, key: key, start: midIndex + 1, end: end)
                      } else {
                          return midIndex
                      }
                  }
              }
      
              func twoPairSum(sum : Int, inputArray: [Int]) -> Bool {
      
                  //Do this only if array isn't Sorted!
                  let sortedArray = inputArray.sorted()
      
                  for (currentIndex, value)  in sortedArray.enumerated() {
                      if let indexReturned =  binarySearch(list: sortedArray, key: sum - value, start: 0, end: sortedArray.count-1) {
                          if indexReturned != -1 && (indexReturned != currentIndex) {
                              return true
                          }
                      }
                  }
                  return false
              }
      
       //***********************Solution B*********************//
       //This solution returns the indexes of the two pair elements if any such two pairs exists in the array
       func twoPairSum(_ nums: [Int], _ target: Int) -> [Int] {
      
                  for currentIndex in 0.. Bool {//Helper Function
                  return (firstElement + secondElement) == target
              }
      
          //*******************Solution C*********************//
         //This solution returns the indexes of the two pair elements if any such two pairs exists in the array
         func twoPairSum(_ nums: [Int], _ target: Int) -> [Int] {
      
                  var dict = [Int: Int]()
      
                  for (index, value) in nums.enumerated() {
                      dict[value] = index
                  }
      
                  for (index, value) in nums.enumerated() {
                      let otherIndex = dict[(target - value)]
                      if otherIndex != nil && otherIndex != index {
                          return [index, otherIndex!]
                      }
                  }
      
                  return []
              }
      

提交回复
热议问题