# Java program to find maximum scalar product of two vectors in an array

## Maximum scalar product of two vector

Here, in this page we will discuss the program to find maximum scalar product of two vector in java programming language. We are given with two arrays and need to return the maximum product that obtained.

Here, we will discuss the two different ways for solving the problem. These two different ways are :

• Method 1 : Sorting Without using inbuilt function.
• Method 2 : Sorting using inbuilt sort function.

## Method 1 :

• Sort the first and second array in ascending order,
• Declare a variable say product = 0.
• Run a loop from index 0 to n
• Set product += (arrr1[i]*arr2[i])
• After complete iteration print product.

### Time and Space Complexity :

• Time Complexity : O(n2)
• Space Complexity : O(1)

### Method 1 : Code in Java

Run
```class Main
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 6, 3, 7};
int arr2[] = {10, 7, 45, 3, 7};
int n = arr1.length;

//Sort arr1 in ascending order
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){ if(arr1[i]>arr1[j]){
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}

//Sort arr2 in ascending order
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){ if(arr2[i]>arr2[j]){
int temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
}
}

int product = 0;
for(int i=0; i<n; i++)
product += arr1[i]*arr2[i];

System.out.print(product);
}
}```

`413`

## Method 2 :

In this method we will use inbuilt sort function to sort the array.

• For, sorting arr1 in ascending order, use Arrays.sort(arr1)
• For, sorting arr2 in descending order, use Arrays.sort(arr2, Collections.reverseOrder());.

### Time and Space Complexity :

• Time Complexity : O(n log(n))
• Space Complexity : O(1)

### Method 2 : Code in Java

Run
```import java.util.Arrays;
import java.util.Collections;
class Main
{
public static void main (String[] args)
{
Integer arr1[] = {1, 2, 6, 3, 7};
Integer arr2[] = {10, 7, 45, 3, 7};
int n = arr1.length;

//Sort arr1 in ascending order
Arrays.sort(arr1);

//Sort arr2 in ascending order
Arrays.sort(arr2);

int product = 0;
for(int i=0; i<n; i++)
product += arr1[i]*arr2[i];

System.out.print(product);
}
}```

`413`

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### One comment on “Java program to find maximum scalar product of two vectors in an array”

• Dhivya

import java.util.Arrays;
import java.util.Scanner;

public class max_scalar_prdt {

public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();
int a1[]= new int[n];
int a2[]= new int[n];
for(int i=0;i<n;i++)
{
a1[i]= sc.nextInt();
}
for(int i=0;i<n;i++)
{
a2[i]= sc.nextInt();
}
Arrays.sort(a1);
Arrays.sort(a2);
int sum=0;
for(int i=0;i<n;i++) {
sum =sum+(a1[i]*a2[i]) ;
}
System.out.println(sum);
}

}