From 35a2375f7eebb031cae958db0911cd41bf7e39ef Mon Sep 17 00:00:00 2001 From: Buduf Date: Thu, 14 Apr 2022 13:34:35 +0200 Subject: [PATCH] problem 1 --- TwoSum.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 TwoSum.cs diff --git a/TwoSum.cs b/TwoSum.cs new file mode 100644 index 0000000..599b2a0 --- /dev/null +++ b/TwoSum.cs @@ -0,0 +1,26 @@ +using System; + +public class Solution { + public int[] TwoSum(int[] nums, int target) { + int[] sorted = new int[nums.Length]; + nums.CopyTo(sorted, 0); + Array.Sort(sorted); + int i = 0, j = sorted.Length - 1; + while (sorted[i] + sorted[j] != target) { + while (sorted[i] + sorted[j] > target) { + j--; + } + while (sorted[i] + sorted[j] < target) { + i++; + } + } + int[] result = new int[2]; + int r = 0; + for (int index = 0; r < 2; index++) { + if (nums[index] == sorted[i] || nums[index] == sorted[j]) { + result[r++] = index; + } + } + return result; + } +} \ No newline at end of file