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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Find the first and the second minimum index of each row in 2d array

Greenhorn
Posts: 7
Hello! I'm trying to find the index of first minimum value and the index second minimum value of each row in 2D array.
For example, the input: int arr[][] = {{1, 2, 3, 4, 5, 6},{1, 2, 3, 4, 5, 6}};  the output must be:The smallest element index of each row is [0, 0] and second Smallest element index of each row is [1, 1].

Marshal
Posts: 70224
282
• 1
That isn't a 2D array; Java® only supports 1D arrays. What that is is an array of arrays, which is better. I think you should start by finding the smallest elements in a 1D array, because you can scale it up to more dimensions later.
Please show us the algorithm you are using, as well as the definition of next‑smallest element. Every array with ≥ 2 elements has a next‑smallest element, so you can't say there is no next‑smallest element. Maybe you mean smallest not equal to the actual smallest element; that is something different. Or can you impose a restriction that your arrays are free from repeated values?
I don't understand the code you have been given; I think you will end up deleting it. I tried that code and it didn't work. So forget it. I suggest you start by finding the smallest element in an array, and print,

The smallest element was −43752083 at 97.

Once you have the smallest element, work out how to find the next‑smallest element. It is easy to do with two passes through the array, ut it should be possible by iterating the array only once.

nanouha abab
Greenhorn
Posts: 7
Actually, I need to find only the second index of the minimum value of each array of 2 arrays. I try to code with one array and it works. What I must do to find the index of min of each row of  int[] []a ={ {5, 4, 0, 2, 10, 30, 3},{5, 4, 0, 2, 10, 30, 3}};

Sheriff
Posts: 7108
184
I believe what you need to do is put another loop outside of the existing loop that changes the index to the internal arrays.

Campbell Ritchie
Marshal
Posts: 70224
282
. . . and what happens when you use more irregular arrays? Try this several times:-That will show you a 100‑element array, “randomly” filled. That will give a better test of your method.
Your technique looks good. But move it out of the main() method. Try getting it to return an int[] comprising the two indices:-See how that works. Once you have that sort of array, you can copy the values into the arrays you want.

Bartender
Posts: 7296
65

nanouha abab wrote:Actually, I need to find only the second index of the minimum value of each array of 2 arrays. I try to code with one array and it works. What I must do to find the index of min of each row of  int[] []a ={ {5, 4, 0, 2, 10, 30, 3},{5, 4, 0, 2, 10, 30, 3}};

You are not properly updating 'secondSmallesIndex'.

Bartender
Posts: 4066
156
An alternative way, and a bit more flexible in that you can also obtain the index of the nth smallest element, is to take a copy of your array, sort it, take element n, and do a search for it in the original array. The Arrays class can help you here.

 Willie Smits can speak 40 languages. This tiny ad can speak only one: Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature