Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1051187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:53:31+00:00 2026-05-16T16:53:31+00:00

I am solving this problem of rotating an array and got algorithm and code

  • 0

I am solving this problem of rotating an array and got algorithm and code working

 int[] Rotate(int[] ar,int k)
        {
            if (k <= 0 || k > ar.Length - 1)
                return ar;
            Reverse(ar, 0, k - 1);
            Reverse(ar, k, ar.Length - 1);
            Reverse(ar, 0, ar.Length - 1);
            return ar;            
        }

 void Reverse(int[] ar,int start, int end)
        {
            while (start < end)
            {
                int temp = ar[start];
                ar[start] = ar[end];
                ar[end] = temp;
                start++;
                end--;
            }
        }

Now I want to do this in LINQ and I got the below code, I think this can be done much better.

 int[] Rotate(int[] ar,int k)
    {
        if (k <= 0 || k > ar.Length - 1)
            return ar;
        int[] ar1=ar.Take(k-1).Reverse().ToArray();
        int[] ar2=ar.Skip(k - 1).Take(ar.Length - k+1).Reverse().ToArray();
        int[] ar3 = ar1.Concat(ar2).Reverse().ToArray();
        return ar3;
    }

This is a well known algorithm from Programming pearls – http://books.google.com/books?id=kse_7qbWbjsC&lpg=PA14&ots=DfzTzQCSar&dq=rotate%20an%20array%20programming%20pearls&pg=PA14#v=onepage&q&f=false

And in general how to develop my LINQ skills, if I am given a programming problem, right now I am only thinking in for loops or foreach loops, how to think in terms of linq operators. I am reading C# 4.0 nutshell, other than practicing any advice?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:53:31+00:00Added an answer on May 16, 2026 at 4:53 pm

    Starting with your code:

    int[] ar1=ar.Take(k-1).Reverse().ToArray(); 
    int[] ar2=ar.Skip(k - 1).Take(ar.Length - k+1).Reverse().ToArray(); 
    int[] ar3 = ar1.Concat(ar2).Reverse().ToArray(); 
    

    Since you just want to get all of the remaining elements, the Take in the second line isn’t needed.

    ar1 and ar2 are just enumerated, so they don’t need to be arrays. The ToArray calls aren’t needed. With a bit of creative renaming thrown in, we have:

    IEnumerable<int> revFirst = ar.Take(k-1).Reverse(); 
    IEnumerable<int> revLast = ar.Skip(k-1).Reverse(); 
    int[] ar3 = revFirst.Concat(revLast).Reverse().ToArray(); 
    

    Now we have

    rev ( rev(first) + rev(last) )

    distributing the outer rev gives

    rev ( rev(last) ) + rev ( rev(first) )

    which is the same as

    last + first
    

    applying the same operations to the code gives

    IEnumerable<int> first = ar.Take(k-1); 
    IEnumerable<int> last = ar.Skip(k-1); 
    int[] ar3 = last.Concat(first).ToArray(); 
    

    which further simplifies to

    int[] ar3 = ar.Skip(k-1).Concat(ar.Take(k-1)).ToArray(); 
    

    and now we have Jon Skeet’s answer so we must be done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
Summary: How would I go about solving this problem? Hi there, I'm working on
Ok, I have been working on solving this problem all day, and I am
I tried solving this problem in SPOJ using Booth Algorithm in O(n) time, but
I need some help in solving this problem. We have a large amount of
I've been problem solving this for the past few hours and frustratingly can't find
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
This question is more about guidance than actually solving my problem: I need to
I'm having trouble solving this problem. I have to find all simple paths starting
what is the best approach of solving this problem: If you have an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.