This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes move element in array without shifting the array elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "move element in array without shifting the array elements" Watch "move element in array without shifting the array elements" New topic
Author

move element in array without shifting the array elements

prabal nandi
Greenhorn

Joined: Jun 28, 2011
Posts: 28

Need code to move element from middle of array to end, without shifting the array elements.
-----------------------------------------------------------------------
| 23 | 34 | 45 | 50 | 55 | 10 | 22 | 67 | 78 | 98 | 33 | 54 | 97 |
-----------------------------------------------------------------------
| ^
|_______________________|

----------------------------------------------------------------------
| 23 | 34 | 45 | 50 | 55 | 10 | 22 | 78 | 98 | 33 | 54 | 97 | 67 |
-----------------------------------------------------------------------

Conditions 1. Array cannot be broken into two parts.
2. New Array cannot be used.
3. Elements should not be moved.

Can this be done???

Can someone provide the code for this??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

prabal nandi wrote:
Can this be done???


Depends on the complete requirements. In the real world, for any normal interpretation of the requirements you gave, no, it's not possible. But in the real world, those requirements wouldn't arise, and if for some reason they did, there are alternate approaches.

In an educational setting, yes, I'm sure there's some way to do what the instructor wants while satisfying his requirements as he interprets them, even if they're not well phrased, or impossible to satisfy when taken strictly and literally.

Can someone provide the code for this??

This site is NotACodeMill(⇐click). If we do your work for you, you won't learn anything except to rely on others to do your work for you.

So clear up the requirements, give it your best shot, and when you get stuck and need a nudge in the right direction, post the details here.

Good luck! And welcome to the Ranch!
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
prabal nandi wrote:Need code to move element from middle of array to end, without shifting the array elements.
...
3. Elements should not be moved.

Can you clarify this? Your illustration seems to show a result in which elements are shifted. If elements are not moved, then there would be an empty space where "67" used to be.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

how is that?

pseudo code :

1. get the copy/value of the elemnet which is at index i ; o(1) since it is an array

2. insert this value at end of the array 0(1)

3. in case you dont want duplicate replace i th position with DUMMY value.


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Seetharaman Venkatasamy wrote:how is that?

pseudo code :

1. get the copy/value of the elemnet which is at index i ; o(1) since it is an array

2. insert this value at end of the array 0(1)

3. in case you dont want duplicate replace i th position with DUMMY value.




That is one possible solution, depending on his actual requirements. Since they weren't clearly specified, and since the instructor seems to be imposing arbitrary restrictions, I choose not to speculate.
prabal nandi
Greenhorn

Joined: Jun 28, 2011
Posts: 28

@jeff: This question was asked to me in an Interview (not by any instructor in any class- so not looking for exact code to copy paste).The interviewer was imposing the restrictions for every approach i was coming up with..
So was wondering whether this can be done or not.

The below approach wont server the purpose, as it was asked to remove the cell completely.....
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3420
    
  12
prabal nandi wrote:@jeff: This question was asked to me in an Interview (not by any instructor in any class- so not looking for exact code to copy paste).The interviewer was imposing the restrictions for every approach i was coming up with

Then either you misunderstood the question or the interviewer was probably seeing how you reacted under the pressure of constant changes to a specification - something that you will come up against often in the real world.


Joanne
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Joanne Neal wrote:... or the interviewer was probably seeing how you reacted under the pressure of constant changes to a specification - something that you will come up against often in the real world.

This doesn't seem to be the best way to do that. In the real world, the client usually doesn't refute every approach you come up with. Changing requirements is one thing, micromanaging how an array will be processed completely another. It just seems to me that the interviewer had a solution on his mind and was trying to nudge the unfortunate interviewee into it.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

prabal nandi wrote:@jeff: This question was asked to me in an Interview (not by any instructor in any class- so not looking for exact code to copy paste).The interviewer was imposing the restrictions for every approach i was coming up with..


Okay, so he was testing your thought process for finding unconventional solutions and/or your knowledge of the language and core API.

So was wondering whether this can be done or not.


My original answer to that still stands.

The below approach wont server the purpose, as it was asked to remove the cell completely.....


Then no, not possible. Arrays' sizes can't be changed after creation. (Well, maybe you could alter the length variable with reflection. Not sure, never tried it. But it would never be a valid real-world solution anyway, and if that's the answer he was looking for, he may very well be an idiot. Not to mention the fact that you'd still have to shift everything.)

The whole question really doesn't make sense.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3420
    
  12
Martin Vajsar wrote:This doesn't seem to be the best way to do that.

True. Bur as Jeff says, the interviewer might be an idiot.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

May be he was expecting a reply on the lines of - "While this is not possible with an array, it can be achieved with another data-structure called LinkedList." (... followed by a set of questions on other data-structures).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Jeff Verdegan wrote: . . . maybe you could alter the length variable with reflection. Not sure, never tried it. . . .
That sounds fun. You could convert a Java™ array into a C++ array and get a nonsense value at the end. Or even break the object next on the heap after that array.
prabal nandi
Greenhorn

Joined: Jun 28, 2011
Posts: 28

i second martin, i think interviewer was having some answer in his mind and after changing the requirements few times he also forgot,what he was trying to ask..
and please don't tell me that i can face these type of questions in real life scenario.. i have been working as a enterprise app developer for past 3 years.. never faced anything thing remotely closed to the question.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

prabal nandi wrote:please don't tell me that i can face these type of questions in real life scenario.. i have been working as a enterprise app developer for past 3 years.. never faced anything thing remotely closed to the question.


First, where did anybody here say that you would face that in a real-life scenario?

Second, just because you haven't seen it in 3 years doesn't mean it doesn't happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: move element in array without shifting the array elements
 
Similar Threads
an interesting arithmetic, need help.
http://xml.apache.org/axis/ HttpErrorCode:401
Project Euler problems with Scala
Exception in thread "AWT-EventQueue-1"
OC4J 10g StandAlone using HTTPS Tunneling Problem