Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Rotate array to the right of a given pivot
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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(); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,3 sec, absolute running time: 0,2 sec, cpu time: 0,19 sec, average memory usage: 16 Mb, average nr of threads: 3, absolute service time: 0,52 sec
edit mode
|
history
|
discussion
Hello, world! The array before rotate : 1 2 3 4 5 6 The array after rotate : 2 3 4 5 6 1