Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java program to find minimum scalar of two vectors

Minimum scalar product of two vector

In this section we learn about the minimum 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 minimum scalar product of arrays we need to multiply the minimum value of array to the maximum value of the array and add these all multiple value. So here we need to sort the array1 and array2 in ascending order and descending order respectively.

Java program for calculating minimum scal product of vector in an array

Algorithm

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 minimum 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 descending order.

Java Code

import java.util.Scanner;

public PrepInsta

{

    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 minimum scalar product of two vector is "+MScalarProduct(a1,a2,size));

}

}
output
Enter size of array 1
4
Enter elements of array 1
2
3
5
7
Enter size of array 2
4
Enter element of array 2
5
2
1
1
Minimum scalar of product of two vectors
28