+ 3
Longest Series Surrounded by M
An array of N integers and an integer M are passed as input. The program must print the longest series of integers surrounded by M on its both ends. The series must not contain the integer M. If more than one longest such series are found print the last occurring series. Boundary Condition(s): 3 <= N <= 1000 1 <= M <= 999999999 Input Format: The first line contains N and M separated by space(s). The second line contains N integers separated by space(s). Output Format: The first line contains the longest series of integers separated by a space. Example Input/Output 1: Input: 7 2 45 2 21 534 2 45 2 Output: 21 534 Example Input/Output 2: Input: 10 23 23 76 23 129 34 12 23 45 67 23 Output: 129 34 12
5 Respostas
+ 1
#<stdio.h>
int distinct_element(int arr[10],int n);
int disinct_elements(int arr[],int n)
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<i;j++)
if(arr[i]==arr[j])
break;
if(i==j)
printf("%d",arr[i]);
}
}
int main()
{
int n,arr[10];
arr[10]= {2,4,5,3,8,6,4};
n=sizeof(arr[10])/sizeof(arr[0]);
return disinct_elements(arr[10, n]);
}
0
I've changed the brief slightly
only enter M in the first line
and the series separated by space in the second.
Ex.
2
45 2 21 534 2 45 2
https://code.sololearn.com/cqHY1132xH4S/?ref=app
0
7 cases passed ! 1 failed !
#include<stdio.h>
#include <stdlib.h>
int main()
{
long int n;
long int m;
scanf("%ld %ld ",&n,&m);
long int a[n];
long int max=0;
long int c=0;
long int l=0;
long int f=0;
long int end=0;
for( long int i=0;i<n;i++)
{
scanf("%ld ",&a[i]);
}
for(long int i=0;i<n-1;i++)
{
if(a[i]==m&&f==0)
{
++i;
c++;
f++;
while(a[i]!=m)
{
c++;
i++;
}
if(c>max)
{
max=c;
l=c;
end=i;
}
i--;
f=0;
c=0;
}
}
//start=end-c;
//printf("%d %d ",l,end);
//for(int i=start+1;i<end;i++)
for(long int i=end-l+1;i<=end-1;i++)
{
printf("%ld ",a[i]);
}
}
0
#include<stdio.h>
#include <stdlib.h>
int main()
{
int N,M,a[10000],i,j,max1=0,max2=0;
scanf("%d %d",&N,&M);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
for(i=0;i<N;i++)
if(a[i]==M){
for(j=i+1;j<N;j++)
if(a[j]==M)
{
if(j-i>=max2-max1){
max1=i;
max2=j;
}
break;
}
}
for(++max1;max1<max2;max1++)
printf("%d ",a[max1]);
}
0
Please someone correct this code