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

Python Program to Find Minimum Scalar Product of Two Vectors in an Array

Minimum Scalar Product of two Vectors

 

In this section we learn how to Find minimum scalar product of two arrays using Python.

There we have two arrays i.e. array 1 and array 2, 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 array and add these all multiplied value. So here we need to sort the array 1 and array 2 in ascending order and descending order respectively.

Find Minimum Scalar Product of Two Vectors in an Array

Algorithm

  • Step 1 : Take size of array, array 1 elements, array 2 elements as an input
  • Step 2 : Sort both the arrays one in ascending and other in descending.
  • Step 3 : Print the minimum scalar product by calling minScalar(arr1,arr2,n) function.
minScalar(arr1,arr2,n):
  1. Initialize sumVariable variable with value 0
  2. Traverse each index from 0 to n
  3. Multiply array of same index element and add this with sumVariable and store it in the same variable
  4. return sumVariable

 

Python Code

 
#function to calculate minimum scalar value
def minScalar(arr1,arr2n):
   sumVariable=0
    for i in range(0,n):
       sumVariable+=arr1[i]*arr2[i]
    return sumVariable
 
#inputs
n=int(input(“Enter the size of Array: “))
print(“Enter the elements of First Array”)
arr1=list(map(int,input().split()))
print(“Enter the elements of Second Array”)
arr2=list(map(int,input().split()))
 
#sort arrays in ascending and descending order respectively
arr1.sort()
arr2.sort(reverse=True)
 
#print minimum scalar value of two arrays
print(“Minimum scalar product of two vectors :”, minScalar(arr1,arr2,n))
 
Output: 

Enter the size of Array: 4
Enter the elements of First Array
3 7 5 2
Enter the elements of Second Array
1 2 1 5
Minimum scalar product of two vectors :  28