Max distance between same elements Geeks solution
Problem :
Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Code:
// your task is to complete this function
class xyz
{
int maxDistance(int arr[], int n)
{
int len=0;
for(int i=0;i<n;i++)
for(int j=n-1;j>i;j--)
{
if(arr[i]==arr[j])
{
len=Math.max(len,j-i);
}
}
return len;
}
}
Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Code:
// your task is to complete this function
class xyz
{
int maxDistance(int arr[], int n)
{
int len=0;
for(int i=0;i<n;i++)
for(int j=n-1;j>i;j--)
{
if(arr[i]==arr[j])
{
len=Math.max(len,j-i);
}
}
return len;
}
}
0 Comments:
Post a Comment