Maximum scalar product of two vector using java

Maximum scalar product of two vector

In this section we learn about the maximum scalar product of two array.

There are we have a two array That is array 1 and array 2  so here we need to find the dot product of the two array. Dot product is also known as the scalar product of two vector.

For finding the maximum scalar product of arrays we need to multiply the minimum value of array to the minimum value of the array and add these all multiple value.

so here we need to sort the array1 and array2 in ascending order and ascending order respectively.

Example:

array 1 : 1 2 3 4

array 2 :  2,3,4,5

scalar product: 1*2+2*3+3*4+4*5
2+6+12+20
40
maximum scalar

Working

step 1.  Initialize two array.

step 2. Take the element for both array,

step 3. Sort the both array by calling sort method.

step 4.  print the maximum scalar product by calling MScalarProduct().

MScalarProduct(a1,a2,n)

step 1. initialize sum variable.

step 2. initialize for loop from 0 to size of an array.

step 3. multiply array same index element and add this with sum and store it in same variable.

step 4. return the sum;

sort(a1,a2,n)

step 1.  sort the array1 in ascending order. 

step 2. sort the array2 in ascending order. 

java program to find the maximum scalar product

import java.util.Scanner;

public class Main

{

    public static int MScalarProduct(int arr1[],int arr2[],int n)

    {

        int sum=0;

        for(int i=0;i<n;i++)

        {

            sum=sum+arr1[i]*arr2[i];

        }  

        return sum;  

    }

    public static void sort(int arr1[],int arr2[],int n)

    {

        int temp=0;

        for(int i=0;i<n;i++)

        {

            for(int j=0;j<n;j++)

            {

                if(arr1[i]<arr1[j])

                {

                    temp=arr1[i];

                    arr1[i]=arr1[j];

                    arr1[j]=temp;

                }

            }

             for(int j=0;j<n;j++)

            {

                if(arr2[i]<arr2[j])

                {

                    temp=arr2[i];

                    arr2[i]=arr2[j];

                    arr2[j]=temp;

                }

            }

        }

    }

public static void main(String[] args) 

{

    Scanner sc=new Scanner(System.in);

    int[] a1=new int[50];

    int[] a2=new int[50];

    System.out.println("enter size of an array");

    int size=sc.nextInt();

    System.out.println("enter elements of an array1");

    for(int i=0;i<size;i++)

    {

        a1[i]=sc.nextInt();

    }

    System.out.println("enter elements of an array2");

    for(int i=0;i<size;i++)

    {

        a2[i]=sc.nextInt();

    }

    sort(a1,a2,size);

  System.out.println("the maximum scalar product of two vector is "+MScalarProduct(a1,a2,size));

}

}

Output

enter size of an array
4
enter elements of an array1
1
2
3
6
enter elements of an array2
3
5
1
4
the maximum scalar product of two vector is 49

Please Login/Signup to comment