From 5307e7ef8583f8f1cc44f25e9854b7bd1dea6d2c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 1 Jun 2024 23:49:40 +0200 Subject: [PATCH] =?UTF-8?q?swift:=20add=20=C2=AB724.=20Find=20Pivot=20Inde?= =?UTF-8?q?x=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- swift/find-pivot-index.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 swift/find-pivot-index.swift diff --git a/swift/find-pivot-index.swift b/swift/find-pivot-index.swift new file mode 100644 index 0000000..4987c02 --- /dev/null +++ b/swift/find-pivot-index.swift @@ -0,0 +1,18 @@ +class Solution { + func pivotIndex(_ nums: [Int]) -> Int { + var fromLeft = 0 + var fromRight = nums.reduce(0, +) + + for (i, x) in nums.enumerated() { + fromRight -= x + + if fromLeft == fromRight { + return i + } + + fromLeft += x + } + + return -1 + } +}