vposkatcheev
BAN USERI've recently came across the same problem on an Amazon HackerRank test.
My solution (in Java) and unit tests can be found on github: git.io/vukVM
Implemented in Java. Runtime O(n), where n equals number of elements in array. Space O(1).
public class OrderArrayProblem {
public static void main( String[] args ) {
String[] elements = { "C", "D", "E", "F", "G" };
int[] indices = { 3, 0, 4, 1, 2 };
System.out.println( Arrays.asList( elements ) );
sort( elements, indices );
System.out.println( Arrays.asList( elements ) );
}
/**
* @throws NullPointerException if elements or indices are null
*/
public static void sort( String[] elements, int[] indices ) {
if ( elements.length != indices.length ) {
return;
}
for ( int i = 0; i < elements.length; i++ ) {
while ( indices[ i ] != i ) {
swap( elements, indices, i, indices[ i ] );
}
}
}
private static void swap( String[] elements, int[] indices, int fromIndex, int toIndex ) {
String element = elements[ fromIndex ];
int index = indices[ fromIndex ];
elements[ fromIndex ] = elements[ toIndex ];
indices[ fromIndex ] = indices[ toIndex ];
elements[ toIndex ] = element;
indices[ toIndex ] = index;
}
}
Thanks for pointing this out. Adjusted original solution.
- vposkatcheev November 10, 2015Implemented in Java. Runtime O(n), where n equals number of elements in array. Space O(1).
/**
* @throws NullPointerException if elements or indices are null
*/
public static void sort( String[] elements, int[] indices ) {
if ( elements.length != indices.length ) {
return;
}
for ( int i = 0; i < elements.length; i++ ) {
while ( indices[ i ] != i ) {
swap( elements, indices, i, indices[ i ] );
}
}
}
private static void swap( String[] elements, int[] indices, int fromIndex, int toIndex ) {
String element = elements[ fromIndex ];
int index = indices[ fromIndex ];
elements[ fromIndex ] = elements[ toIndex ];
indices[ fromIndex ] = indices[ toIndex ];
elements[ toIndex ] = element;
indices[ toIndex ] = index;
}
Repsandrafdenn, Associate at CareerCup
Je suis développeur web dans une société de Wild Oats Markets, j'adore faire du shopping et j'ai une ...
RepOffers ammunition for sale form top brands
Repleroydperry, Backend Developer at AppNexus
I am from the USA. I am 27 year old. I drive the train for my company Monk Home Funding ...
Reprickyjwerner, Area Sales Manager at Accolite software
I am Ricky, I live in Houston USA, I am working as a distribution manager in Total Serve company. I ...
Repkylieblindner, abc at ASAPInfosystemsPvtLtd
Hello, I am Kylie. I am working in a store as Supply chain managers promote the design, development, and implementation ...
Repmakaylamelua, Blockchain Developer at AMD
I am a sound editing and music composer with experience of handling a wide variety of programs.During my free ...
Repcharlesndouglass, Employee at VANS
I am Michael from Nantucket USA. I am working as a Power plant dispatcher in Matrix Design company. I am ...
RepI am Frank, 29 year old. I have worked with numerous branches, including payroll and human resources, which allows me ...
Repshanamkyler38, AT&T Customer service email at ABC TECH SUPPORT
Hi, I am Shana and I live in Chicago USA .I am working as a Tax preparer in an Adaptabiz ...
Reptaniajclover, Analyst at Amdocs
Hi, I am Tania from Tuckahoe USA, I am working as a Public relations representative in Gamble-Skogmo company. I like ...
Repmariawharris2, Computer Scientist at Adjetter Media Network Pvt Ltd.
Hi I am an IT Project Management Professional with 2 years' experience,Handled project development and documentation of copier rentals ...
Repileenjconnelly, Cloud Support Associate at Absolute Softech Ltd
I have extensive experience in providing breaking news and giving precise details to the viewers. Interested in leading/working with ...
Repnancysimms14, Backend Developer at ASAPInfosystemsPvtLtd
I am Nancy from California,Handling project development and documentation is my job. Passionate determined.Looking for an open project ...
Reprealspellcaster4, Cloud Support Associate at Aricent
Hi, I am from PA, United states. I believe in making the impossible possible because there’s no fun in ...
Rephollymclark8, Apple Phone Number available 24/7 for our Customers at Accenture
I am clinical laboratory technologist in Stratapro company. I have Excellent clinical laboratory skills, with commended performance conducting/analyzing laboratory ...
Repjoycepwise, Blockchain Developer at Accenture
Hello, I’m Joyce. I’m a business developer living in Tampa, FL. I am a fan of music, travel ...
RepEviePaul, Member Technical Staff at Abs india pvt. ltd.
I am a Studio camera operator from Florida USA.I love to relax n' chill. love when it's cloudy ...
RepHelanZelda, Consultant at Advent
HelanZelda from Atlanta, GA, United States. I have a passion for exploring/sharing new ideas and experiences throughout the interdisciplinary ...
Solution written in Java and finds missing element in O(log(n)). For more in-depth and unit tests, see github: git.io/vu3iJ .
- vposkatcheev January 04, 2016