Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, July 22, 2017

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;
    }
}

0 Comments:

Post a Comment

Stats