Finding minimum scalar product of two vectors in Python

Minimum scalar product of  two vectors in Python

Here, in this page you will find the program to find the minimum scalar product of two vectors in Python Programming Language. We will discuss different methods to find the product of given two vectors.

Finding minimum scalar product of two vectors

Methods Discussed :

  • Method 1 : Without Using Inbuilt sort() function.
  • Method 2 : Using sort() function.

Method 1 :

  • Sort the first array in ascending order,
  • Sort the second array in descending 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 Python

arr1 = [1, 2, 6, 3, 7]
arr2 = [10, 7, 45, 3, 7]

n = len(arr1)

#Sort arr1 in ascending order
for i in range(n):
    for j in range(i+1, n): 
        if arr1[i]>arr1[j] :
            arr1[i], arr1[j] = arr1[j], arr1[i]

#Sort arr2 in descending order
for i in range(n):
    for j in range(i+1, n): 
        if arr2[i]

Output

149

Method 2 :

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

  • For, sorting arr1 in ascending order, use arr1.sort()
  • For, sorting arr2 in descending order, use arr2.sort(reverse=True).
Minimum Scalar product in python

Method 2 : Code in Python

arr1 = [1, 2, 6, 3, 7]
arr2 = [10, 7, 45, 3, 7]
n = len(arr1)

#Sort arr1 in ascending order
arr1.sort()

#Sort arr2 in descending order
arr2.sort(reverse=True)

product = 0
for i in range(n):
    product += arr1[i]*arr2[i]

print(product)

Output:

149