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.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Arrays
  4. Left Rotation
  5. Discussions

Left Rotation

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • ivan_rrr 4 years ago+ 0 comments

    It is not the best solution but it requires only one array:

    static void Main(String[] args) {
            string[] read = Console.ReadLine().Split(' ');
            int n = int.Parse(read[0]);
            int d = int.Parse(read[1]);
            int[] array = new int[n];
            array = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
            
            for(int q=0;q<d;q++)
                for(int i=0;i<n-1;i++)
                {
                    swap(array, i,i+1);
                }
            
            for(int y=0;y<n;y++)
                Console.Write(array[y] + " ");
            Console.WriteLine();
        }
        
        static void swap(int[] input, int indexA, int indexB)
        {
            int tmpA = input[indexA];
            int tmpB = input[indexB];
            input[indexA] = tmpB;
            input[indexB] = tmpA;
        }
    
    0|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature