We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Good thought process! Step II isn't necessary as you could just traverse the arrays from the end instead of the beginning. I like the idea of starting with the smallest array and looking if that value exists, I just had a while loop and decremented one of the 3 counters each time until I reached values that were all the same. This means that I only traverse each array once whereas yours you might traverse the two other arrays for each value that you look at. Here is my for loop:
//This values reflect what cylinder index we are looking at in each column
int col1 = 0;
int col2 = 0;
int col3 = 0;
//While the heights that we are looking at aren't equal, decrement one
while( h1[col1] != h2[col2] || h2[col2] != h3[col3] )
{
if( h1[col1] > h2[col2] || h1[col1] > h3[col3] )
col1++;
else if( h2[col2] > h1[col1] || h2[col2] > h3[col3] )
col2++;
else if( h3[col3] > h2[col2] || h3[col3] > h1[col1])
col3++;
}
System.out.println(h1[col1]); //Once they're equal, print this
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Good thought process! Step II isn't necessary as you could just traverse the arrays from the end instead of the beginning. I like the idea of starting with the smallest array and looking if that value exists, I just had a while loop and decremented one of the 3 counters each time until I reached values that were all the same. This means that I only traverse each array once whereas yours you might traverse the two other arrays for each value that you look at. Here is my for loop: