We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
private static object Sol(int[] data, int n, int m)
{
var dict = GetDict(data, n);
for (var p = 0; p < n; p++)
{
var dif = m - data[p];
if (dict.ContainsKey(dif))
{
var list = dict[dif];
if (list[0] != p)
return FormatResult(p, list[0]);
if (list.Count > 1)
return FormatResult(p, list[1]);
}
}
return ""; // Should never happen.
}
private static Dictionary<int, List<int>> GetDict(int[] data, int n)
{
var dict = new Dictionary<int, List<int>>(n);
for (var p = 0; p < n; p++)
{
if (!dict.ContainsKey(data[p]))
dict[data[p]] = new List<int>(n);
dict[data[p]].Add(p);
}
return dict;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
Managing duplicates is not so hard.