How to improve this array manipulation function?
Seems you might be overusing Linq since you are manipulating an array.  Here is an example that to me is a bit more readable (although the readability could be improved further) and it is faster.

private static int[] EquivalentOutput(int cap, int[] permutation)
{
    int[] copy = permutation.ToArray();

    for (int i = 0; i &lt; copy.Length; i++)
    {
        if (copy[i] == 0)
            continue;

        var consecutiveElements = copy.Skip(i).TakeWhile(x =&gt; x != 0).ToArray();
        var max = consecutiveElements.Max();
        if (max == cap)
        {
            for (int j = 0; j &lt; consecutiveElements.Length - 1; j++)
                copy[i++] = 1;
            copy[i++] = cap;
        }
        else
        {
            for (int j = 0; j &lt; consecutiveElements.Length; j++)
                copy[i] = copy[i++] == max ? max : 1;
        }
    }
    return copy;
}


Update:
@svick pointed out that it feels a bit "weird" changing the loop variable inside the loop so I am providing an alternative which some people will consider easier to read...

private static int[] EquivalentOutput(int cap, int[] permutation)
{
    int[] copy = permutation.ToArray();

    int i = 0;
    while (i &lt; copy.Length)
    {
        if (copy[i] &gt; 0)
        {
            var consecutiveElements = copy.Skip(i).TakeWhile(x =&gt; x != 0).ToArray();
            var max = consecutiveElements.Max();
            if (max == cap)
            {
                for (int j = 0; j &lt; consecutiveElements.Length - 1; j++)
                    copy[i++] = 1;
                copy[i++] = cap;
            }
            else
            {
                for (int j = 0; j &lt; consecutiveElements.Length; j++)
                    copy[i] = copy[i++] == max ? max : 1;
            }
        }

        i++;
    }
    return copy;
}