-
Notifications
You must be signed in to change notification settings - Fork 18
/
FindIntegers.swift
64 lines (51 loc) · 1.58 KB
/
FindIntegers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Sayed Mahmudul Alam
//Find intergers
struct FindIntegers {
//Find two interger that multiplies to a number
func findTwoIntegers(multipliesTo number: Int, array: [Int]) -> (Int, Int)? {
for i in 0..<array.count {
var k = 0
var l = 0
for i in array {
if(i <= 0) {
print("must not contain a zero")
return nil
}
}
if(number % array[i] == 0) {
k = number / array[i]
l = array[i]
}
for j in i..<array.count{
print(array[j])
if(array[j] == k) {
return (k, l)
}
}
}
return nil
}
//Find two intergers that summed up to a number
func findTwoIntegersBruteForce(summedUpTo number: Int, array: [Int]) -> (Int, Int)? {
for i in 0..<array.count {
let dif = number - array[i]
for j in i+1..<array.count{
if(array[j] == dif) {
return (i, j)
}
}
}
return nil
}
func findTwoIntegers(summedUpTo number: Int, array: [Int]) -> (Int, Int)? {
var map = [Int: Int]()
for i in 0..<array.count {
let dif = number - array[i]
if let val = map[dif] {
return (val,i)
}
map[array[i]] = i
}
return nil
}
}