Find 2 integers that multiply to 20 - coding interview question

//Brute-force technic

import java.io.*;
import java.util.*;

class Solution {
  public static void main(String[] args) {
   
    int[] array = {2 , 4, 1 , 6 , 5 , 40 , -1};
   
    HashMap<Integer,Integer> multipliers = new HashMap<Integer,Integer>();
   
    for(int i = 0 ; i < array.length -1; i++){
      for(int j = i+1; j < array.length -1 - i; j++){
        int firstItem = array[i];
        int secondItem = array[j];
        if( firstItem *  secondItem == 20){
            multipliers.put(firstItem, secondItem);
        }
      }
    }
   
      for (int item: multipliers.keySet()){
            int key = item;
            int value = multipliers.get(item); 
            System.out.println(key + " " + value); 
      }
  }
}

Bu blogdaki popüler yayınlar

SDLC - Software Development Life Cycle

@SerializedName and @Expose annotations