15,796,734 members
See more:
hi guys, i am new in the programming field and i want some help to write a program that find the longest distance between Two equal cells in array
Posted
Updated 2-Mar-20 12:59pm
Tarun Mangukiya 18-Dec-12 1:52am
Needs more explanation...
whitefang4773 18-Dec-12 3:36am
• Consider an Array of Integer values with size N, having values as in this
Example

7 0 0 0 5 6 7 5 0 7 5 3

Your task will be to write a program find the longest distance between
Two equal cells. In this example. The distance is measured by the number
Of cells- for example, the distance between the first and the fourth cell is
2 (cell 2 and cell 3).
In the example above, the longest distance is between the first 7 and the
10th 7, with a distance of 8 cells, i.e. the number of cells between the 1st
And the 10th 7s.
Note:
- Array values will be taken from the user
- If you have input like 1111111 then the distance is the number of
Cells between the first and the last cell.
Tarun Mangukiya 18-Dec-12 8:52am
Just code as you explained me.
You know the technique to find. Just implement into code.
whitefang4773 18-Dec-12 3:48am
André Kraak
really thank you , you help me alot to know how to think of this problem
Really thank you

## Solution 1

Best way to start is writing down the problem on paper and figure out the solution.

So you have an row of for example characters like so:
```0 1 2 3 4 5 6 7 8 9
a h K ; a , a K h k```

I would start at the beginning with 'a', then the best way to find the greatest 'distance' for an equal character would be to look for it from the end of the row. For 'a' this would be position 6, so the current maximum distance is 5.
Now onto the next character 'h', which can be found from the end on position 8. So now 'h' becomes the new largest distance of 6.
You continue this way until you reach the end of the row at which time you now the character with the largest 'distance'.

You can implement this by the use of two loops, one which start at the beginning and goes to the end. The second start at the end until it find the character you are looking for.
Each time you find the character in the second you check if the distance is greater than the one you already found and if so make that the new largest one.

I hope this helps you to code the solution your problem.

I gave no code because if you want to learn it is best to do it yourself.

## Solution 2

You can implement this by using two loops one for the first item [0] and the second for second item [1] and find compare till the last item of the array get max value is the difference between first and the last one find

ex :
C#
```int[] Arr;
Arr = new int[ArrSize];
for(int i= 0; i<Arr.Length;i++)
{
Console.Write("enter value ( " + i + ", " + ArrSize  + ") :");
}

int max = 0;
int n = 0;
int n1 = 0;
for (int x = 0; x < Arr.Length; x++)
{
//   Console.WriteLine(Arr[x]);

for (int j = x + 1; j < Arr.Length; j++)
{
if (Arr[x] == Arr[j])
n = j;
n1 = x;

if (n - n1 > max)
{
max = n - n1;
}
}
}
Console.WriteLine(max - 1);```

Dave Kreskowiak 2-Mar-20 20:17pm
Doing someone's homework for them is frowned upon here.

Also, I seriously doubt the OP still has this assignment seven years later.
CHill60 3-Mar-20 7:49am
This is just an explicit implementation of Solution 1 - but note Andre's comment "I gave no code because if you want to learn it is best to do it yourself." That is why doing homework for people is frowned upon - you're not helping them.
It's commendable that you want to help, but stick to answering newer posts where the OP still needs help and try to make sure you are bringing something new to the thread

Top Experts
Last 24hrsThis month
 Richard Deeming 168 Maciej Los 90 merano99 80 Rick York 40 Richard MacCutchan 30
 Richard Deeming 458 Andre Oosthuizen 300 Dave Kreskowiak 215 Maciej Los 215 Rick York 170

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900