Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Array Rearrangement trick [modified]

Array Rearrangement trick [modified]

Scheduled Pinned Locked Moved Algorithms
csharpcssdatabasedata-structurestutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abhigad
    wrote on last edited by
    #1

    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 like for(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 like for(int i=0;i<n/2;i++)

    modified on Wednesday, October 1, 2008 5:23 PM

    CPalliniC A M 3 Replies Last reply
    0
    • A abhigad

      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 like for(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 like for(int i=0;i<n/2;i++)

      modified on Wednesday, October 1, 2008 5:23 PM

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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 is 0-based or 1-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]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A abhigad

        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 like for(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 like for(int i=0;i<n/2;i++)

        modified on Wednesday, October 1, 2008 5:23 PM

        A Offline
        A Offline
        Alan Balkany
        wrote on last edited by
        #3

        Your question is unclear.

        1 Reply Last reply
        0
        • A abhigad

          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 like for(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 like for(int i=0;i<n/2;i++)

          modified on Wednesday, October 1, 2008 5:23 PM

          M Offline
          M Offline
          Mark Churchill
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups