Maximum decreasing adjacent elements in JavaScript


In the given problem statement we are asked to find out maximum decreasing adjacent elements with the help of javascript functionalities. This problem can be solved with the help of a simple algorithm in javascript.

Understanding the Logic

Explaining the logic of the problem statement in more detail.

The problem asks us to get the maximum number of decreasing adjacent items in an array or we can say we have to find the longest decreasing subarray in the input array.

So for solving this problem we need to work by iterating all over the array, one item at a time and will keep track of the current count of decreasing nearest items in a variable named count. So we will encounter a group of adjacent elements which are decreasing. Then we will increase the count.

Otherwise the adjacent elements are not in decreasing order so we will check if the current count is bigger than the current maxCount (which is set to 0 initially). If this condition is true then we will update the maxCount to the current count. Then we will reset the count to 0. Because we have found a nondecreasing group of items and the current sub array of decreasing adjacent elements will be ended.

Algorithm

Step 1: At the beginning of algorithm we will define a function called 'maxDecreasingAdjacent'. This function will find the decreasing adjacent of elements in the array.

Step 2: Then the function will begin by initializing two count variables as named maxCount and count. Both the variables have been initialized with 0.

Step 3: Now forwarding to the second step, we will initialize a for loop to iterate all the items present in the input array. And to keep track or elements we have already defined count variables in step 2.

Step 4: In this phase, we raise the count whenever we come across two adjacent components that are decreasing. When we come across two adjacent elements that aren't decreasing, we determine whether the current count exceeds the current maxCount.

Step 5: If so, we adjust maxCount to reflect the most recent count. The current subarray of decreasing adjacent items has terminated as a result of our contact with a non−decreasing pair of adjacent elements, therefore we then reset count to 0.

Step 6: In the last step we will get the result of decreasing adjacent elements from the array.

Example

// function to find decreasing adjacent
function maxDecreasingAdjacent(arr) {
    // object to store the decreasing adjacent
    let maxCount = 0;
    let count = 0;
    // initialize a loop till the length of array
    for (let i = 1; i < arr.length; i++) {
      if (arr[i] < arr[i-1]) {
        count++;
      } else {
        if (count > maxCount) {
          maxCount = count;
        }
        count = 0;
      }
    }
    if (count > maxCount) {
      maxCount = count;
    }
    return maxCount;
  }
 
  //define two different arrays
  const array1 = [5, 4, 3, 2, 1];
  const array2 = [1, 2, 3, 4, 5];
  const adjacent1 = maxDecreasingAdjacent(array1);
  const adjacent2 = maxDecreasingAdjacent(array2);
  //print the output
  console.log(`For ${array1} the decreasing adjacent is ${adjacent1}`);
  console.log(`For ${array2} the decreasing adjacent is ${adjacent2}`);

Output

For 5,4,3,2,1 the decreasing adjacent is 4
For 1,2,3,4,5 the decreasing adjacent is 0

The above algorithm is taking the two kind of arrays one is array1 and second is array2. The input arrays in the function maxDecreasingAdjacent is the array of integers. For that input array we need to find the maximum number of decreasing adjacent elements. The function returns the length of the lognest decreasing subarray in the array.

The output shows the problem, if the input array is [5, 4, 3, 2, 1], then the function is returning the result 4, because there are 4 decreasing adjacent items in the array. And in second array [1, 2, 3, 4, 5], the function is returning 0 as output because there are no decreasing elements in it. If the input array is [5, 4, 3, 2, 1, 3, 2, 1], then the function will return 4 again, since the longest decreasing subarray is [5, 4, 3, 2].

Time Complexity

The time and space complexity are two measures by which we can find that the algorithm is efficient or not. In the above code we have encountered that the complexity is O(n). Because it takes into account every subarray of decreasingly adjacent items in the array exactly once and keeps track of the length of the longest subarray seen, this technique is effective. The algorithm only needs to iterate through the array once, hence its time complexity is O(n).

Conclusion

This is how we can resolve the issue posed in the above problem statement. The easiest and most accurate way to determine the decreasing adjacent of an array. We can use a straightforward O(n) algorithm in JavaScript to calculate the maximum number of decreasing neighboring members in an array.

Updated on: 23-Aug-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements