Medium csharp

Rotate array right by 3 #3

Problem

Rotate [1..7] right by 3 positions; print space-separated.

Hints
  • Reverse whole, reverse first k, reverse rest

Your solution

TestStatusDetails
Ready — edit the code above and click Run or Submit.

Solution

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] a = { 1, 2, 3, 4, 5, 6, 7 };
        int k = 3 % a.Length;
        Array.Reverse(a); Array.Reverse(a, 0, k); Array.Reverse(a, k, a.Length - k);
        Console.WriteLine(string.Join(" ", a));
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Triple-reverse rotation trick.

Discussion

0

Sign in to join the discussion.

No discussions yet — ask the first question!

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details