Arrays: Left Rotation

  • + 2 comments

    Did something similar in C#..

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    class Solution {
        
        static string rotate(int rot, int[] arr)
        {
            string left = string.Join(
                " ", arr.Take(rot).ToArray()
            );
            string right = string.Join(
                " ", arr.Skip(rot).ToArray()
            );
            return right + ' ' + left;
        }
    
        static void Main(String[] args) {
            string[] tokens_n = Console.ReadLine().Split(' ');
            int n = Convert.ToInt32(tokens_n[0]);
            int k = Convert.ToInt32(tokens_n[1]);
            string[] a_temp = Console.ReadLine().Split(' ');        
            int[] a = Array.ConvertAll(a_temp,Int32.Parse);
    
            // rotate and return as string
            string result = Solution.rotate(k, a);
            // print result        
            Console.WriteLine(result);
        }
    }