Guess Number

Boys are playing a guessing number game. The boy chose two different positive integers N1 and N2. He then told the other boy several numbers were simply made up, but one of those numbers was N1 + N2 and another was N1 – N2. The other boy thinks the puzzle boy prefers large numbers. Given a int[] numbers containing the numbers the boy told the other boy, return the largest possible value of N1 * N2.Note:- There will exist at least one pair of positive integers (N1, N2) such that both N1 + N2 and N1 – N2 are elements of numbers.
Examples
1)i/p=>{1, 4, 5}, o/p=> 6 (ie.,the other boy can determine that (N1,N2) = (3, 2)).
2)i/p=>{1, 4, 5, 8}, o/p=> 12 (ie.,possible pairs (N1,N2) are(3,2) and (6,2), the values of N1 * N2 are 6 and 12, and the largest is 12).

3)i/p=>{9, 8, 7, 6, 5, 4, 3, 2, 1}, o/p=> 20

4)i/p=>{2, 100}, o/p=> 2499

5)i/p=>{50, 58, 47, 57, 40}, o/p=> 441

6)i/p=>{32, 34, 15, 37, 27, 30}, o/p=> 286

import java.util.*;
public class GuessNumber {
  private int getMuliply(int[] numbers){
    int output =1;
    int minus = 0;
    int kplus =1;
    Arrays.sort(numbers);
    minus= numbers[0];
    for (int i=0;i<numbers.length;i++){
      kplus =1;
      for (int k=numbers[i]-1;k>=minus;k--){
        minus = k - kplus;
        if(minus>0){
          for (int j=0;j<numbers.length;j++){
            if(minus==numbers[j]){
              if ((k * kplus)>output)output= k * kplus;
            }
          }
        }
        kplus++;
      }
    }
    return output;
}

  public static void main(String[] args) {
    GuessNumber sg = new GuessNumber();
    System.out.println(sg.getMuliply(new int[] {1, 4, 5} ));
    System.out.println(sg.getMuliply(new int[] {1, 4, 5, 8} ));
    System.out.println(sg.getMuliply(new int[] {9, 8, 7, 6, 5, 4, 3, 2, 1} ));
    System.out.println(sg.getMuliply(new int[] {2, 100} ));
    System.out.println(sg.getMuliply(new int[] {50, 58, 47, 57, 40} ));
    System.out.println(sg.getMuliply(new int[] {1, 3} ));
    System.out.println(sg.getMuliply(new int[] {3, 1} ));
    System.out.println(sg.getMuliply(new int[] {1, 99} ));
    System.out.println(sg.getMuliply(new int[] {99, 1} ));
    System.out.println(sg.getMuliply(new int[] {97, 99} ));
    System.out.println(sg.getMuliply(new int[] {99, 97} ));
    System.out.println(sg.getMuliply(new int[] {2, 4} ));
    System.out.println(sg.getMuliply(new int[] {4, 2} ));
    System.out.println(sg.getMuliply(new int[] {100, 2} ));
    System.out.println(sg.getMuliply(new int[] {98, 100} ));
    System.out.println(sg.getMuliply(new int[] {100, 98} ));
    System.out.println(sg.getMuliply(new int[] {4, 89, 12, 76, 91, 80} ));
    System.out.println(sg.getMuliply(new int[] {2, 6, 5, 11, 9, 10, 4, 7} ));
  }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: