0

If I have two Arrays, and want to find out if an element(Int) in each equal a predetermind value(Int), how would I do that. I am new to Swift and a little lost.

func twoSum(a:[Int], b:[Int], c: Int) -> Bool {
     // iterate through arrays to see if an element in each equal c
     return false // if no values equal the sum c 
}

twoSum(a: [1,3,4,5], b: [1,3,4,5], c: 10)

I don't need to convert the arrays into Sets, But I'm wondering how to effectively iterate through the two arrays. How would I do this without a a bunch of for or if loops?

pacification
  • 5,838
  • 4
  • 29
  • 51
  • 1
    Could you clarify? Do you want to go through all the values in the first array with all the values in the second array and find if any pairs sum to a given amount? –  Sep 14 '17 at 04:14
  • Possibly related: https://stackoverflow.com/questions/43733537/beginner-swift-3-how-to-find-pairs-in-array-that-add-up-to-given-number – Martin R Sep 14 '17 at 04:16
  • I apologize for not being clear. What I want to find out is, if we ADD one element in Array a and one element in Array B does the SUM of the elements equal Int C. So adding the elements up. –  Sep 14 '17 at 04:18
  • So if 1 + 1 = 10, 3 + 3 = 10, 4 + 4 = 10 or 5 + 5 equals 10 return true ? – Leo Dabus Sep 14 '17 at 04:20
  • Yes! This would include anywhere in any of the arrays. So the first integer in array a might be 7 and the 4th integer in array b might be 3. Those add up for sum C. –  Sep 14 '17 at 04:26

3 Answers3

1

One solution can be this:

Use filter to see if a contains the difference of each value in b and c and if it does then the array returned by filter wont be empty and the function will return true otherwise false

func twoSum(a:[Int], b:[Int], c: Int) -> Bool {

        return !b.filter {a.contains(c-$0)}.isEmpty

}
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15
  • Clever. Although it does technically use two loops behind the scenes. One for `contains` and one for `filter`. It's doubtful they are implemented *somewhere* without them. –  Sep 14 '17 at 04:38
  • 1
    btw this will loop the whole array unnecessarily even if the first element sum equals true – Leo Dabus Sep 14 '17 at 04:46
  • 1
    @LeoDabus Yep, that's why I felt the nested solution was the best. Early escape from the loops can save a lot of time. –  Sep 14 '17 at 04:47
1

Best solution is with sets. As contains is of O(1) operation for sets. But as you have mentioned you don't want to use sets. Following is another solution which would work in O(nlogn) time.

func twoSum(a:[Int], b:[Int], c: Int) -> Bool {
    let sortedA = a.sorted()
    let sortedB = b.sorted()
    var i = 0, j = sortedB.count - 1
    repeat {
        let sum = sortedA[i] + sortedB[j]
        if sum == c {
            return true
        } else if sum < c {
            i += 1
        } else {
            j -= 1
        }
    } while( j >= 0 && i < sortedA.count)

    return false
}
Mohammad Sadiq
  • 5,070
  • 28
  • 29
0

You nest the loops:

func twoSum(a:[Int], b:[Int], c: Int) -> Bool {
  for aVal in a {
    for bVal in b {
      if aVal + bVal == c {
        return true
      }
    }
  }
  return false
}

This will sum each element in a with each element in b and return true if any of the sums equals c, false if none of them do. As @LeoDabus noted, the early return true if a match is found will cause the function to exit as soon as a single match is found. This could even be on the first addition and comparison. That's quite a gain in efficiency.

The sums will go like:

a[0] + b[0]
a[0] + b[1]
...
a[1] + b[0]
a[1] + b[1]
...
a[n] + b[n]

An alternative is:

func twoSum(a:[Int], b:[Int], c: Int) -> Bool {
  // thanks to @3stud1ant3 for the idea of using a difference
  let diff = Set(a.map { c - $0 })
  return !diff.isDisjoint(with: b)
}

Of course, this just hides some loops in other methods. map certainly uses one and isDisjoint uses a hash table to lookup the matches - probably with one or more loops.

  • I can't combine the two for loops with the && operator right? –  Sep 14 '17 at 04:29
  • Why can't you use nested loops? Is it a homework assignment that says you can't? There are other ways to do this but this is very straightforward and clear. Someone reading the code will immediately understand what you are trying to accomplish. –  Sep 14 '17 at 04:31
  • Oh I apologize! I thought doing a for loop with && was deprecated! –  Sep 14 '17 at 04:32
  • I don't see how using the `&&` operator will help you here. How do you mean to use it? –  Sep 14 '17 at 05:03