I did the solution in swift: (Not sure if the format will turn out nice)

private extension Array where Element == Int {

func pairSums(_ k: Int) -> Int {

var total = 0

var lookup = [Int:Int]()

var valCount = [Int:Int]()

for (index, val) in enumerated() {

if valCount[val] != nil {

valCount[val]! += 1

} else {

valCount[val] = 1

}

var x = k – val

if lookup[x] != nil , let vc = valCount[val] {

if x == val && vc > 2 {

total += (vc – 1)

} else {

total += 1

}

}

lookup[val] = 1

}

return total

}

}

The trick I take advantage of is if the difference and the value are equal to create “K” the incremental change in the additive relationship is the total times the same value is used in the array – 1 when over 2. Hope that is not confusing.

O(n) complexity with a space complexity of O(n)

I do negative numbers too and no bounding…

]]>Hi Lin,

Thanks for the message.

That are the constraints for the problem.

I assume there are other approaches that might be hindered by the number of entries.

This requirement would allow one to use an array that has a fixed length.

Given that the requirement is present we may use it in our advantage.

Regards;

John

Hi Lin,

Thanks for the message.

The problem requirements specifies such constraint among others.

Regards;

John

]]>Why the number in a given arr is assumed never greater than 99? I have seen similar approach used for other problems but don’t know what’s the logic behind. You never know how many elements will be in a given arr and the number associated with each element/string right?

Thanks ]]>

Good day Karthik,

Sorry I took so long to reply.

I am not sure I follow what you are saying.

Could you please rephrase the comment or send me a link to an article / reference that discusses the subject you brought up?

I will reply shortly after receiving your update.

Sincerely;

John

]]>Hi Rebecca,

Thanks for the message.

Tried looking for the typo on the post at no avail.

Will keep in mind your suggestion.

Regards;

John

Olav,

Thanks for the comment.

I have updated the post to include additional information.

I have clarified what I understand for the requirements of the problem.

Of course I MIGHT BE WRONG.

I believe you can not ask the interviewer for clarification.

Based on my assumptions the tree values represent the same information.

If you can let me know I missed in the requirements, I will make the necessary code changes.

Regards;

John

]]>