What is the neatest algorithm for that ? Can it be done without helping/static variable ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use two “for” loops. Assuming that you don’t need to compare elements against themselves, and you only need to test each pair one way around rather than both ways:
If you want all pairs both ways, just change
j < itoj < ints.length. If you want pairs both ways, excluding self-comparisons, either addif (i != j)in the inner loop or else do one inner loop from 0 toi-1, and another fromi+1toints.length.