Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created March 2, 2016 20:23
Show Gist options
  • Save krishnabhargav/a7e6b93dd87af2d3f4aa to your computer and use it in GitHub Desktop.
Save krishnabhargav/a7e6b93dd87af2d3f4aa to your computer and use it in GitHub Desktop.

static T[] Rotate(int n, T[] arr) { var temp = new T[arr.Length]; for (int i = 0; i < arr.Length; i++) { var destIndex = i - n; if (destIndex < 0) { var mul = -1 * destIndex / arr.Length; destIndex = (arr.Length * mul) + destIndex; if (destIndex < 0) destIndex = arr.Length + destIndex; } temp[destIndex] = arr[i]; }

return temp;

}

void Main() { Rotate(5000, new[] {1,2,3,4}).Dump(); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment