Problem Statement:
Given a list of N (1 <= N <= 50000) integer. You are required to compute the number of pairs of indexes whose values are the same, and also the maximum distance between two indexes with equal values. N will be given in the first line of the input. N lines follow each one with one integer from the list. If no identical pairs are found, print NA, otherwise print a single line with two integers, the number of identical pairs, and the maximum distance between indexes with equal values.
Sample Input1:6
1
2
3
2
4
2
Sample Output1:
2 4
Sample Input2:
4
1
2
3
4
Sample Output2:
NA
Solution in | C | JAVA | PYTHON |
---|
#include<stdio.h>
int main() {
int n,ary[30],i,j,ele,maxdist=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&ary[i]);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(ary[i]==ary[j]){
if(maxdist<j-i){
ele = ary[i];
maxdist = j-i;
}
}
}
}
if(maxdist==0){
printf("NA");
}else{
printf("%d %d",ele,maxdist);
}
return 0;
}
int main() {
int n,ary[30],i,j,ele,maxdist=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&ary[i]);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(ary[i]==ary[j]){
if(maxdist<j-i){
ele = ary[i];
maxdist = j-i;
}
}
}
}
if(maxdist==0){
printf("NA");
}else{
printf("%d %d",ele,maxdist);
}
return 0;
}