# Minimum Scalar Product Program

06 May 2024

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

#### Description about Minimum Scalar Product Program

Sample input 1:

4

1 2 3 4

5 6 7 8

Sample output 1:

60

Explanation :

(4*5 + 3*6 + 2*7 + 1*8)  = 60

Sample input 2:

4

-1 -2 -3 -4

5 6 -7 -8

Sample output 2:

-17

Explanation :

(-1*-8 + -2*-7 + -3*6 + -4*5) = -17

Solution:

#### C Program for  Minimum Scalar Product

#include <stdio.h>

#include <limits.h>

// SpecialSort function sorts negetive numbers in array1 in ascending order

// and positive numbers and zero in descending order

void swap(int *x, int *y)

{

int temp = *x;

*x = *y;

*y = temp;

}

void SpecialSort(int *vec1,int n)

{

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

{

for(int k = 0 ; k<n-1-i ; k++)

{

if(vec1[k] > vec1[k+1])

{

swap(&vec1[k],&vec1[k+1]);

}

}

}

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

{

printf("%d ",vec1[i]);

}

printf("\n");

int idx=0;

while(vec1[idx] < 0)

{

idx++;

}

int start = idx,end = n-1;

while(start<end)

{

swap(&vec1[start],&vec1[end]);

start++;end--;

}

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

{

printf("%d ",vec1[i]);

}

printf("\n\n");

}

// Find min product and move the elements to left side of both arrays

int MinimumScalarProduct(int *vec1,int *vec2,int n)

{

int min,sop=0,id1,id2;

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

{

min = INT_MAX;

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

{

if((vec1[i]*vec2[j]) < min)

{

min = vec1[i]*vec2[j];

id1 = i; id2 = j;

}

}

sop = sop + min;

swap(&vec1[i],&vec1[id1]);

swap(&vec2[i],&vec2[id2]);

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

{

printf("%d ",vec1[i]);

}

printf("\n");

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

{

printf("%d ",vec2[i]);

}

printf("\n\n");

}

return sop;

}

int main()

{

int n;            scanf("%d",&n);

int vec1[n];

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

{

scanf("%d",&vec1[i]);

}

int vec2[n];

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

{

scanf("%d",&vec2[i]);

}

SpecialSort(vec1,n);

printf("%d",MinimumScalarProduct(vec1,vec2,n));

return 0;

}

#### C++ Program for  Minimum Scalar Product

#include <bits/stdc++.h>

using namespace std;

// SpecialSort function sorts negetive numbers in array1 in ascending order

// and positive numbers and zero in descending order

void SpecialSort(int vec1[],int n)

{

int idx=0;

sort(vec1,vec1+n);

while(vec1[idx] < 0)

{

idx++;

}

int start = idx,end = n-1;

while(start<end)

{

swap(vec1[start],vec1[end]);

start++;end--;

}

}

// Find min product and move the elements to left side of both arrays

int MinimumScalarProduct(int vec1[],int vec2[],int n)

{

int min,sop=0,id1,id2;

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

{

min = INT_MAX;

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

{

if((vec1[i]*vec2[j]) < min)

{

min = vec1[i]*vec2[j];

id1 = i; id2 = j;

}

}

sop = sop + min;

swap(vec1[i],vec1[id1]);

swap(vec2[i],vec2[id2]);

}

return sop;

}

int main()

{

int n;            cin>>n;

int vec1[n];

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

{

cin>>vec1[i];

}

int vec2[n];

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

{

cin>>vec2[i];

}

SpecialSort(vec1,n);

cout<<MinimumScalarProduct(vec1,vec2,n);

return 0;

}

#### JAVA Program for  Minimum Scalar Product

import java.util.*;

import java.lang.*;

import java.io.*;

class Main

{

static void swap(int arr[],int start, int end)

{

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

}

// SpecialSort function sorts negetive numbers in array1 in ascending order

// and positive numbers and zero in descending order

static void SpecialSort(int vec1[],int n)

{

Arrays.sort(vec1);

int idx=0;

while((idx<n) && (vec1[idx] < 0))

{

idx++;

}

int start = idx,end = n-1;

while(start<end)

{

swap(vec1,start,end);;

start++;end--;

}

}

// Find min product and move the elements to left side of both arrays

static int MinimumScalarProduct(int vec1[], int vec2[], int n)

{

int min,sop=0;

int id1=0,id2=0;

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

{

min = Integer.MAX_VALUE;

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

{

if((vec1[i]*vec2[j]) < min)

{

min = vec1[i]*vec2[j];

id1 = i; id2 = j;

}

}

sop = sop + min;

swap(vec1,i,id1);

swap(vec2,i,id2);

}

return sop;

}

public static void main(String[] args) throws java.lang.Exception

{

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int vec1[] = new int[n];

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

{

vec1[i] = sc.nextInt();

}

int vec2[] = new int[n];

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

{

vec2[i] = sc.nextInt();

}

SpecialSort(vec1,n);

System.out.print(MinimumScalarProduct(vec1,vec2,n));

}

}

#### Python Program for Minimum Scalar Product

def swap(vec1, pos1, pos2):

vec1[pos1], vec1[pos2] = vec1[pos2], vec1[pos1]

def SpeecialSort(vec1,n):

vec1.sort()

idx=0

while idx<n and vec1[idx] < 0 :

idx=idx+1

start = idx

end = n-1

while(start<end):

swap(vec1, start, end)

start = start + 1

end = end + 1

def MinimumScalarProduct(vec1,vec2,n):

sop=0

for i in range(0,n):

min = 2147483647

for j in range(i,n):

if(vec1[i]*vec2[j]) < min :

min = vec1[i]*vec2[j]

id1 = i

id2 = j

sop = sop + min

swap(vec1,i,id1)

swap(vec2,i,id2)

return sop

n = int(input())

vec1 = list(map(int,input().split(' ')))

vec2 = list(map(int,input().split(' ')))

SpeecialSort(vec1,n)

print(MinimumScalarProduct(vec1,vec2,n))

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

## Any Questions? Look Here.

##### Related Articles

Program to identify if the character is an alphabet or not

Program to Replace substring in a string

Check if two strings match where one string contains wildcard characters

Lower Triangular Matrix

MATRIX MULTIPLICATION Program

Maximum Product Subarray

Program for Binary to decimal conversion

Program for Binary to octal conversion

Program for Decimal to binary conversion

Program for Decimal to octal conversion

Program for Maximum Scalar Product

Program for Octal to binary conversion

Program for Octal to decimal conversion

Program To Add two matrices and print the resultant matrix

Program to calculate Maximum number of handshakes

Program to calculate the Frequency of characters in a string

Program to Capitalize the first and last letter of each word of a string

Program to change the given matrix to transpose of the matrix.

Program to check if String is a palindrome or not

Program to check if two arrays are the same or not

Program to check if two strings are Anagram or not

Program to compare two strings

Program to concatenate a string

Program to copy a string

Program to Count common subsequence in two strings

Program to Count the sum of numbers in a string

Program to express a number as a sum of two prime numbers

Program to find Area of a rectangle

Program to find Area of a Triangle

Program to find ASCII values of a character

Program to find equilibrium index of an array

Program to find Factorial of a number

Program to find Fibonacci series up to n

Program to find Frequency of each element of an array

Program to find LCM of two numbers

Program to find longest palindrome in an array

Program to find minimum sum of absolute difference of given array

Program to find Number of days in a given month of a given year

Program to find Number of digits in an integer

Program to find number of even and odd elements in an array

Program to find number of integers which has exactly 9 divisors

Program to find Permutations in which n people can occupy r seats in a classroom.

Program to find Power of a number

Program to find roots of a quadratic equation

Program to find second smallest element in the array

Program to find smallest and largest element in an array

Program to find Sum of digits of a number

Program to find sum of elements in an array

Program to find Sum of N natural numbers

Program to find Sum of numbers in a given range

Program to find sum of positive square elements in the array

Program to find the Area of a circle

Program to find the array type

Program to find the double of the given number without using arithmetic operator

Program to find the Factors of a number

Program to find the Quadrants in which coordinates lie

Program to find whether an array is a subset of another array or not.

Program to find whether Arrays are disjoint or not

Program to find whether the numbers of an array be made equal

Program to identify if the character is a vowel or consonant

Program to identify if the number is Armstrong number or not

Program to identify if the number is even or odd

Program to identify if the number is Palindrome or not

Program to identify if the number is Perfect number or not

Program to identify if the number is Prime number or not

Program to identify if the number is Strong number or not

Program to identify of the a number is positive or negative

Program to print Armstrong numbers between two intervals

Program to print Diamond pattern printing using stars

Program to print Diamond pattern using Column number

Program to print Diamond pattern using Row numbers

Program to print Floyd’s triangle

Program to print hollow rectangle star pattern

Program to print Length of the string without using strlen() function

Program to print Non-repeating characters in a string

Program to print Palindromic pyramid pattern printing

Program to print Pascal triangle

Program to print prime numbers in a given range

Program to print Pyramid pattern using numbers

Program to print Pyramid pattern using stars

Program to print Solid rectangle star pattern

Program to print the length of a string

Program to print the max elements in all the rows.

Program to Remove brackets from an algebraic expression

Program to Remove characters in a string except alphabets

Program to remove duplicate elements in an array

Program to Remove spaces from a string

Program to Remove vowels from a string

Program to Replace all 0’s with 1 in a given integer

Program to reverse a given number

Program to reverse a string

Program to reverse the array

Program to sort the array

Program to Toggle each character in a string

Subtract matrix2 from matrix1 and print the resultant matrix program

Upper Triangular Matrix