Explore

Edit

Edit

# Program to find the minimum scalar product of two vectors (dot product) | FACE Prep

Published on 10 Mar 2020

Program to find the minimum scalar product of two vectors (dot product) is discussed here. Given two arrays, find the minimum scalar product of all permutations of the two given arrays.

Sample Input 1:

3 (Number of elements of the array)

1 3 5 (Array 1 elements)

2 4 1 (Array 2 elements)

Sample Output 1:

15

Calculation:

Minimum scalar product = 1 * 4 + 3 * 2 + 5 * 1

= 4 + 6 + 5

= 15

Sample Input 2:

5

1 2 3 4 5

1 0 1 0 1

Sample Output 2:

6

Calculation:

Minimum scalar product = 5 * 0 + 4 * 0 + 3 * 1 + 2 * 1 + 1 * 1

= 0 + 0 + 3 + 2 + 1

= 6

## Algorithm to find the minimum scalar product of two vectors

• Input the number of elements of the arrays.
• Input the array 1 and array 2 elements.
• Initialize sum = 0.
• Sort the array 1 in ascending order.
• Sort the array 2 in descending order.
• Repeat from i = 1 to n
• sum = sum + (arr1[i] * arr2[i])
• Return sum.

## Program to find the minimum scalar product of two vectors (dot product)

C
C++
Java
Python 3

Output
2 (size of each array)
3 (array 1 elements)
4
2 (array 2 elements)
5
23 (minimum scalar product)

## Program to find the minimum scalar product of two vectors (dot product) using pointers

// C Program to find the minimum scalar product of two vectors (dot product) USING POINTERS
#include<stdio.h>
#include<stdlib.h>
int i,j,temp;
int* createarray(int);
int getelements(int*,int);
int ascending(int*,int);
int minscalar(int*,int*,int);
int main()
{
int n,*a,*b;
scanf(“%d”,&n);
a=createarray(n);
getelements(a,n);
b=createarray(n);
getelements(b,n);
ascending(a,n);
ascending(b,n);
minscalar(a,b,n);
return 0;
}
int* createarray(int n)
{
int *a;
a=(int*)malloc(n*sizeof(int));
return a;
}
int getelements(int *a,int n)
{
for(i=0;i<n;i++)
scanf(“%d”,a+i);
return 0;
}
int ascending(int *a,int n)
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(a+i)>*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
return 0;
}
int minscalar(int *a,int *b,int n)
{
int sum=0,p,q;
for(i=0;i<n;i++)
{
p=*(a+i);
q=*(b+n-1-i);
sum=sum+(p*q);
}

printf(“%d”,sum);
return 0;
}


Recommended Programs