SORT17
QUESTION DESCRIPTION
Given two arrays, A and B, of equal size n, the task is to find the minimum value of A[0] * B[0] + A[1] * B[1] +…+ A[n-1] * B[n-1], where shuffling of elements of arrays A and B is allowed.
Madatory conditions are "void result(int a[],int b[],int n)"
Examples:
Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}.
Output : 23 Minimum value of S = 1*6 + 1*5 + 3*4 = 23.
Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 }
Output : 80. Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80.
Input:
The first line of input contains an integer denoting the no of test cases.
Then T test cases follow. Each test case contains three lines.
The first line of input contains an integer N denoting the size of the arrays.
In the second line are N space separated values of the array A[], and
In the last line are N space separated values of the array B[].
Output:
For each test case in a new line print the required result.
Constraints:
1<=T<=100
1<=N<=50
1<=A[]<=20
Given two arrays, A and B, of equal size n, the task is to find the minimum value of A[0] * B[0] + A[1] * B[1] +…+ A[n-1] * B[n-1], where shuffling of elements of arrays A and B is allowed.
Madatory conditions are "void result(int a[],int b[],int n)"
Examples:
Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}.
Output : 23 Minimum value of S = 1*6 + 1*5 + 3*4 = 23.
Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 }
Output : 80. Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80.
Input:
The first line of input contains an integer denoting the no of test cases.
Then T test cases follow. Each test case contains three lines.
The first line of input contains an integer N denoting the size of the arrays.
In the second line are N space separated values of the array A[], and
In the last line are N space separated values of the array B[].
Output:
For each test case in a new line print the required result.
Constraints:
1<=T<=100
1<=N<=50
1<=A[]<=20
TEST CASE 2
INPUT
INPUT
2
4
3 8 1 6
1 7 5 1
5
6 4 7 1 3
4 2 6 9 1
OUTPUT36
62
#include <stdio.h>
void result(int a[],int b[],int n);
int main()
{
int t, i, j, arr1[30], arr2[30], x, temp;
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
for(i=0;i<x;i++)
{
scanf("%d",&arr1[i]);
}
for(i=0;i<x;++i)
{
for (j=i+1;j<x;++j)
{
if (arr1[i] > arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
for(i=0;i<x;i++)
{
scanf("%d",&arr2[i]);
}
for(i=0;i<x;++i)
{
for (j=i+1;j<x;++j)
{
if (arr2[i] < arr2[j])
{
temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
}
}
result(arr1, arr2, x);
}
return 0;
}
void result(int a[],int b[],int n)
{
int i, fin=0, sum;
for(i=0;i<n;i++)
{
fin+=a[i]*b[i];
sum=fin;
}
printf("%d\n",sum);
}
Comments
Post a Comment