I’m practicing algorithms and let’s say we have an array with elements 2, 3, 9, 12, 7, 18
, then I want to print 18 only because it’s double of 9. When I print the result it always displays much more lines, however, the printed numbers (if there are any) are good. How can I manage displaying the result properly? (Using only arrays.) I know it’s a silly question but I tried so many ways and it was only worse.
Now, it shows me this for example (140 is correct, but the other line are not needed):
The following number is doubled of another number from the array: 0 The following number is doubled of another number from the array: 0 The following number is doubled of another number from the array: 140 The following number is doubled of another number from the array: 0 The following number is doubled of another number from the array: 0
My code:
public class DoubleNums { public static void main(String[] args) { Random random = new Random(); int[] array = new int[50]; int[] even; int[] doubleNum; int count = 0; int doubles = 0; for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(200); System.out.println(array[i] + " "); } for (int j = 0; j < array.length; j++) { even = new int[array.length]; if (array[j] % 2 == 0) { even[count] = array[j]; count++; } for (int k = 0; k < array.length; k++) { doubleNum = new int[array.length]; if (array[j] / 2 == array[k]) { even[doubles] = k; doubles++; System.out.println("The following number is doubled of another number from the array: " + even[doubles]); } } } System.out.println("Number of even nums: " + count); } }
Answer
I saw that you asked for a solution that started with your own code…
I know it’s faster but I assumed that we can find a solution using my code with some minor changes.
So I tried to understand what you were trying to do and I have not been able to decipher your method so starting from your macro I created a clearer version.
Changes
- I used an ArrayList (
list
) as support instead of your array (even
). TheList
is very convenient in your case because it has thecontains()
function and also its size is variable. - I took an even number in the array and looked for if there was at least one “half” of it.
Code
public static void main(String[] args) { Random random = new Random(); int[] array = new int[20]; ArrayList<Integer> list = new ArrayList<>(); int evenNumber = 0; for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(100); } System.out.println(Arrays.toString(array)); for (int i = 0; i < array.length; i++) { // If I found an even number and this is NOT already in the list then I'll study it if (array[i] % 2 == 0 && !list.contains(array[i])) { evenNumber = array[i]; for (int j = 0; j < array.length; j++) { // I add "evenNumber" to the list only if at least one its "half" exist in the array if (array[j] * 2 == evenNumber) { list.add(evenNumber); // I dont need to search more, I already found its "half"! break; } } } } System.out.println("Doubled nums: " + list); }
Sample output
[71, 88, 45, 97, 64, 31, 54, 12, 14, 86, 22, 42, 35, 44, 70, 65, 93, 85, 99, 14] Doubled nums: [88, 44, 70]