Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace TwoSum
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] array1 = { 2, 7, 11, 15 };
- int target1 = 9;
- (int first1, int second1) = new TwoTermsFinder().FindIndexes(array1, target1);
- Console.WriteLine($"Two terms have indexes {first1} and {second1}");
- int[] array2 = { 13, 7, 9, 1, 11, 18 };
- int target2 = 19;
- (int first2, int second2) = new TwoTermsFinder().FindIndexes(array2, target2);
- Console.WriteLine($"Two terms have indexes {first2} and {second2}");
- }
- }
- class TwoTermsFinder
- {
- Dictionary<int, int> number_index = new Dictionary<int, int>();
- public (int, int) FindIndexes(int[] array, int target)
- {
- for(int i = 0; i < array.Length; i++)
- {
- if (number_index.ContainsKey(target - array[i]))
- return (number_index[target - array[i]], i);
- number_index.Add(array[i], i);
- }
- return (0, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment