Subsequence with Minimum Average Difference
Given a sequence of n (>10) integers, not all the elements of S are same, write a pseudo-code and the subsequent C program, to compute the arithmetic average of the sequences called k-th sequence of
(here k < 1/2), denoted as Sk, formed from the sequence S, by removing the first k elements of S and the last k-elements of the sequence Your program should print the pair subsequences whose
absolute difference of the averages is minimum.
For eg, consider the sequences: < 2,4 6 7,8 9,5,7,6, 1>
S1: <4,6,7,8,9,5,7,6> and AS1 = 6.5
S2: <6,7,8,9,5,7> and AS2 - 7
S3: <7,8,9,5> and AS3 - 7.25
O CSE1001:Problem Solvi 17:29
S4: <8,9> and AS4 = 8.5
Absolute difference between the averages of the sequences are:
S1 S2 - 05
here to search
Answers
Answer:
Subsequence with Minimum Average Difference
Given a sequence of n (>10) integers, not all the elements of S are same, write a pseudo-code and the subsequent C program, to compute the arithmetic average of the sequences called k-th sequence of
(here k < 1/2), denoted as Sk, formed from the sequence S, by removing the first k elements of S and the last k-elements of the sequence Your program should print the pair subsequences whose
absolute difference of the averages is minimum.
For eg, consider the sequences: < 2,4 6 7,8 9,5,7,6, 1>
S1: <4,6,7,8,9,5,7,6> and AS1 = 6.5
S2: <6,7,8,9,5,7> and AS2 - 7
S3: <7,8,9,5> and AS3 - 7.25
O CSE1001:Problem Solvi 17:29
S4: <8,9> and AS4 = 8.5
Absolute difference between the averages of the sequences are:
S1 S2 - 05
here to search
Subsequence with Minimum Average Difference
Explanation:
#include <stdio.h>
#include <math.h>
int main() {
int n;
scanf("%d", &n);
int arri[n];
for (int i=0; i<n; i++){
scanf("%d", &arri[i]);
}
printf("Array= ");
for (int i=0; i<n;i++){
printf("%d ",arri[i]);
}
int mid=(n+1)/2;
float AM[mid];
for (int j; j<mid; j++){
int sum=0;
for (int i=j; i<mid;i++){
if ((n%2==1) && i==mid-1) sum+=arri[i];
else sum+=arri[i]+arri[n-i-1];
}
printf("\nfor S%d sum=%d", j, sum);
float am= sum/(float)(n-2*j);
printf("\tam=%f",am);
AM[j]=am;
}
float diff=9999999;
int X,Y;
for (int x=0; x<mid;x++){
float temp=0;
for (int y=x+1; y<mid;y++){
temp=fabs(AM[y]-AM[x]);
if (temp<diff) {
diff=temp;
X=x;
Y=y;
}
}
}
printf("\nMin difference=%f, between sequences S%d, S%d", diff, X, Y);
return 0;
}