Pranav Mishra
Pranav Mishra

Reputation: 456

Find Maximum distance between adjacent elements of an array

I have an array of integers say int[] Arr={1, 4, 7, 3, 2 }. Now adjacent side means 2 consecutive numbers (of the number system not of the array) i.e. 1, 2 are adjacent. I wrote some line of codes please help me to find out short comings and optimize it.

    static void Main(string[] args)
    {
        int[] arr = { 1, 4, 7, 3, 2 };
        Console.WriteLine("adjacent indices with maximum value is {0}",Solution(arr));
        Console.ReadLine();
    }

    static int Solution(int[] arr)
    {
        int maxDistance = -1;
        int newMaxDistance = 0;
        int a = 0, b = 0;

        for (int i = 0; i < arr.Length; i++)
        {
            a = arr[i];
            if (i < arr.Length - 1)
            {
                b = arr[i + 1];
            }

            for (int j = i + 1; j < arr.Length; j++)
            {

                if (arr[j] < b && arr[j] > a)
                {
                    maxDistance = j - i;
                }
                else
                {
                    newMaxDistance = j - i;
                }
            }
        }
        if (newMaxDistance > maxDistance)
        {
            maxDistance = newMaxDistance;
        }
        return maxDistance;


    }

Upvotes: 1

Views: 7242

Answers (3)

Saddam Abu Ghaida
Saddam Abu Ghaida

Reputation: 6749

Scala solution looks something like this

 object abc extends App {

  def solution(a: Array[Int]): Int = {

    val arr = Array(0,3,3,7,5,3,11,1)

    val indexArray = a.zipWithIndex.toList.sortBy(_._1)

    indexArray.foldLeft((0,0)){
        case (acc, value) =>
          if (value._2 == 0) (value._2, 1)
          else (value._2 , acc._2 + value._1 - acc._1)
      }._2
  }

  val arr = Array(0,3,3,7,5,3,11,1)

  val res = solution(arr)

  println(res)

Upvotes: 0

user31264
user31264

Reputation: 6737

Transform each element to a pair (value, location), e.g. {1,4,7,3,2} -> {(1,0),(4,1),(7,2),(3,3),(2,4)}. Then sort pairs by values: {(1,0),(2,4),(3,3),(4,1),(7,2)}. Then go through the array, and each time you see two consequtive integers, calculate the difference between their locations.

Upvotes: 5

Archmede
Archmede

Reputation: 1852

If each number only appears once then you can store the index of each element inside a Dictionary.

So the Dictionary at number 1 would equal 0 because 1 is the 0th element in the array. And the Dictionary at 7 would be 2 because 7 is the second element in the array. (As an example)

Now, loop through the array and check if the adjacent numbers to each element are in the Dictionary. If they are in the Dictionary then subtract the indexes of the two numbers and take the absolute value.

So for instance for the number 3 from your example you'd check:

The index of the number 3 is 3 (since 3 is the third element in the array). You'd check the numbers 2 and 4 are in the Dictionary. (adjacent to 3) 2 is in the Dictionary and so the Dictionary would return 4 (since 2 is the fourth element in the array). You'd subtract the index of element 3 (which is 3) and the index of element 2 (which is 4). The answer would be the absolute value of -1 which is 1.

Continue this for the other numbers.

Upvotes: 0

Related Questions