• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

move element in array without shifting the array elements

 
Greenhorn
Posts: 28
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.....
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic