-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathHashTablesIceCreamParlor.cs
75 lines (65 loc) · 1.98 KB
/
HashTablesIceCreamParlor.cs
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
65
66
67
68
69
70
71
72
73
74
75
// Hash Tables: Ice Cream Parlor
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution
{
// Complete the whatFlavors function below.
static void whatFlavors(int[] cost, int money)
{
var dic = new Dictionary<int, List<int>>();
for (var index = 0; index < cost.Length; index++)
{
if (dic.TryGetValue(cost[index], out var val))
val.Add(index + 1);
else
dic.Add(cost[index], new List<int> { index + 1 });
}
var first = 0;
var second = 0;
foreach (var c in cost)
{
var needed = money - c;
if (needed == c)
{
if (dic[c].Count > 1)
{
first = dic[c][0];
second = dic[c][1];
break;
}
}
else if (dic.TryGetValue(needed, out var list))
{
first = dic[c][0];
second = list[0];
break;
}
}
var min = Math.Min(first, second);
var max = Math.Max(first, second);
Console.WriteLine($"{min} {max}");
}
static void Main(string[] args)
{
int t = Convert.ToInt32(Console.ReadLine().Trim());
for (int tItr = 0; tItr < t; tItr++)
{
int money = Convert.ToInt32(Console.ReadLine().Trim());
int n = Convert.ToInt32(Console.ReadLine().Trim());
int[] cost = Array.ConvertAll(Console.ReadLine().Trim().Split(' ')
.Select(x => x.Trim()).ToArray(), costTemp => Convert.ToInt32(costTemp));
whatFlavors(cost, money);
}
}
}