this no-brainer came up to me in a technical interview. I played it safe and wrote this. public class Main {  public static void main(String[] args) {    Scanner in = new Scanner(System.in);    System.out.println(enter size of the array: );    if (!in.hasNextInt()) {      System.out.println(put an integer! );    }    int size = in.nextInt();    System.out.println(enter numbers for array);    int[] array = new int[size];    for (int i = 0; i < array.length; i++) {      array[i] = in.nextInt();    }    maxSum(array);    System.out.println(maxSum(array));  }  private static int maxSum(int[] array) {    int max = array[0];    for (int i = 0; i < array.length; i++) {      for (int j = i + 1; j < array.length; j++) {        int currentMax = array[i] + array[j];        if (currentMax > max) {          max = currentMax;        }      }    }    return max;  }}obviously O(n^2) is not the best approach but I just wanted to finish the question without spending to much time, what do you think?
Finding the maximum sum of any 2 elements in an array of integers
java;array
Your code tries all \$ n (n+1)/2 \$ combinations of array elements to find thecombination with the largest sum, so the complexity is \$ O(n^2) \$.A better solution would be to find the two largest elements in thearray, since adding those obviously gives the largest sum.Possible approaches are:Sort the array elements in increasing order and add the last twoelements. Efficient sorting algorithms (such as Quicksort) have anaverage complexity of \$ O(n \log(n)) \$.Traverse the array once and keep track of the largest and secondlargest element encountered so far. Then add those elements.The complexity is \$ O(n) \$.Other remarks:Your main program computes maxSum(array) twice,which is not necessary.You should check if the user entered at least 2 elements, otherwisethe problem is ill-defined.The if (!in.hasNextInt()) check is not really helpful. If the userenters a non-integer, put an integer!  is printed, but then in.nextInt() fails with an exception. You could for exampleskip the entire input line until a valid integer is entered:while (!in.hasNextInt()) {    System.out.println(Enter an integer! );    in.nextLine();}int size = in.nextInt();