Array Rearrangement trick [modified]
-
Let’s say we have an array of integers
int[] myArray = new int[] {1,2,3,4,5};
So the length of this array is 4 [i.e. n=4] since C# array index starts at 0 Yes the length will be 5 and not 4 as pointed out in the next post. Its my bad - Sorry! Define integer k such that 0<= k < n [n = length of an array] For example, If k = 2 then the output should be {3,4,5,1,2} i.e starting from kth position move all the array elements to the top of an array. If k = 3, output would be {4,5,1,2,3} Here is the challenge. Yes this is trivial if we write a loop that starts at 0 and goes up to n likefor(int i =0;i<n;i++){}
We want to optimize this loop so that it would not loop till n-1. anything less than n-1 is a good solution. [Tip: if you want to reverse this array like 5,4,3,2,1 – you can use the loop likefor(int i=0;i<n/2;i++)
modified on Wednesday, October 1, 2008 5:23 PM
-
Let’s say we have an array of integers
int[] myArray = new int[] {1,2,3,4,5};
So the length of this array is 4 [i.e. n=4] since C# array index starts at 0 Yes the length will be 5 and not 4 as pointed out in the next post. Its my bad - Sorry! Define integer k such that 0<= k < n [n = length of an array] For example, If k = 2 then the output should be {3,4,5,1,2} i.e starting from kth position move all the array elements to the top of an array. If k = 3, output would be {4,5,1,2,3} Here is the challenge. Yes this is trivial if we write a loop that starts at 0 and goes up to n likefor(int i =0;i<n;i++){}
We want to optimize this loop so that it would not loop till n-1. anything less than n-1 is a good solution. [Tip: if you want to reverse this array like 5,4,3,2,1 – you can use the loop likefor(int i=0;i<n/2;i++)
modified on Wednesday, October 1, 2008 5:23 PM
abhigad wrote:
So the length of this array is 4 [i.e. n=4] since C# array index starts at 0
The length of the array is
5
, independently if it is0
-based or1
-based. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Let’s say we have an array of integers
int[] myArray = new int[] {1,2,3,4,5};
So the length of this array is 4 [i.e. n=4] since C# array index starts at 0 Yes the length will be 5 and not 4 as pointed out in the next post. Its my bad - Sorry! Define integer k such that 0<= k < n [n = length of an array] For example, If k = 2 then the output should be {3,4,5,1,2} i.e starting from kth position move all the array elements to the top of an array. If k = 3, output would be {4,5,1,2,3} Here is the challenge. Yes this is trivial if we write a loop that starts at 0 and goes up to n likefor(int i =0;i<n;i++){}
We want to optimize this loop so that it would not loop till n-1. anything less than n-1 is a good solution. [Tip: if you want to reverse this array like 5,4,3,2,1 – you can use the loop likefor(int i=0;i<n/2;i++)
modified on Wednesday, October 1, 2008 5:23 PM
Your question is unclear.
-
Let’s say we have an array of integers
int[] myArray = new int[] {1,2,3,4,5};
So the length of this array is 4 [i.e. n=4] since C# array index starts at 0 Yes the length will be 5 and not 4 as pointed out in the next post. Its my bad - Sorry! Define integer k such that 0<= k < n [n = length of an array] For example, If k = 2 then the output should be {3,4,5,1,2} i.e starting from kth position move all the array elements to the top of an array. If k = 3, output would be {4,5,1,2,3} Here is the challenge. Yes this is trivial if we write a loop that starts at 0 and goes up to n likefor(int i =0;i<n;i++){}
We want to optimize this loop so that it would not loop till n-1. anything less than n-1 is a good solution. [Tip: if you want to reverse this array like 5,4,3,2,1 – you can use the loop likefor(int i=0;i<n/2;i++)
modified on Wednesday, October 1, 2008 5:23 PM
So you mean:
int[] in = new int[] {...}
int[] out = new int[in.Length];
Array.Copy(in (k -> length) => out (0 ...));
Array.Copy(in (0 -> k) => out (length - k));Or is this some school assignment where you have to shuffle in-place?
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.