Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Rotate array to the right of a given pivot
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); int []arr = { 1, 2, 3, 4, 5, 6 }; RotateArrayRight rot = new RotateArrayRight(); Console.Write(" The array before rotate : " ) ; rot.Display(arr); rot.Rotate(arr, 7); Console.WriteLine(); Console.Write(" The array after rotate : " ) ; rot.Display(arr); } } public class RotateArrayRight { //Rotate array to the right of a given pivot public int[] Rotate(int[] arr, int pivot) { if (pivot < 0 || arr == null) throw new Exception("Invalid argument"); // if pivot is grater then the array size, take remainder pivot = pivot % arr.Length; //Rotate first half arr = RotateSub(arr, 0, pivot - 1); //Rotate second half arr = RotateSub(arr, pivot, arr.Length - 1); //Rotate all arr = RotateSub(arr, 0, arr.Length - 1); return arr; } private int[] RotateSub(int[] subArray, int start, int end) { while (start < end) { int temp = subArray[start]; subArray[start] = subArray[end]; subArray[end] = temp; start++; end--; } return subArray; } public void Display(int []arr) { int n = arr.Length; for (int i = 0; i < n; ++i) Console.Write(arr[i] + " "); Console.WriteLine(); } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Collections demo
orytitoyt
Programmes
Is first character of MD5 hash evenly distributed?
Padma. C#
Chest Interaction Unity
Game_snake
c# problem
Programmes
Unity Wall Climbing script Version 1
Please log in to post a comment.