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.
public static List<int> absolutePermutation(int n, int k)
{
var permutations = new List<int>();
var elementsArray = Enumerable.Range(1, n).ToHashSet();
if(k == 0)
return elementsArray.ToList();
for(int i = 1; i <= n; i++)
{
int lowerValue = i - k;
int upperValue = i + k;
if(elementsArray.Contains(lowerValue))
{
permutations.Add(lowerValue);
elementsArray.Remove(lowerValue);
}
else if(elementsArray.Contains(upperValue))
{
elementsArray.Remove(upperValue);
permutations.Add(upperValue);
}
else
return new List<int>{-1};
}
return permutations;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
C# Solution