Java Program to Find Maximum Sum of Two Integers in an Array of Integers

In this article we are going to see how to Find Maximum Sum of Two Integers in an Array of Integers.

Java Program to Find Maximum Sum of Two Integers in an Array of Integers

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to Find Maximum Sum of Two Integers in an Array of Integers.

Method-1: Java Program to Find Maximum Sum of Two Integers in an Array of Integers By Static Initialization of Array Elements

Approach:

  • Create two integer variable as max1 and max2 and assign value as 0.
  • Iterate the array.
  • If current value is more than max1, put current value in max1 and max1 value in max2.
  • Else if current value is more than max2, put current value in max2
  • Then get the result by adding max1 and max2.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        int[] nums = { 3, 2, 7, -1, -4, 9, 5 };
        //calling the maxSum() method
        maxSum(nums);
    }

    //maxSum() method to find maximum sum of two array elements 
    public static void maxSum(int[] nums) 
    {
        int max1 = 0;
        int max2 = 0;
        for (int i : nums) 
        {
            if (i > max1) 
            {
                max2 = max1;
                max1 = i;
            } 
            else if (i > max2) 
            {
                max2 = i;
            }
        }
        int res=max1+max2;
        System.out.println("Maximum Sum is: " + max1 + " + " + max2 + " = " + res);
    }
}
Output:

Maximum Sum is: 9 + 7 = 16

Method-2: Java Program to Find Maximum Sum of Two Integers in an Array of Integers By Dynamic Initialization of Array Elements

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Create two integer variable as max1 and max2 and assign value as 0.
  6. Iterate array.
  7. If current value is more than max1, put current value in max1 and max1 value in max2.
  8. Else if current value is more than max2, put current value in max2.
  9. Then get the result by adding max1 and max2.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        //calling maxSum() method
        maxSum(arr);
    }
    //maxSum() method to find maximum sum of two array elements 
    public static void maxSum(int[] nums) 
    {
        int max1 = -1;
        int max2 = -1;
        for (int i : nums) 
        {
            if (i > max1) 
            {
                max2 = max1;
                max1 = i;
            } else if (i > max2) 
            {
                max2 = i;
            }
        }
        int res=max1+max2;
        System.out.println("Maximum sum is: " + max1 + " + " + max2 + " = " + res);
    }
}

Output:

Enter the size of array: 9
Enter array elements: 1 2 3 4 5 6 7 8 9
Maximum product is: 9 + 8 = 17

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Leave a Comment