

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Largest index difference with an increasing value in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers, arr. Our function should return the largest difference in indexes j - i such that arr[i] <= arr[j]
Example
The code for this will be −
const arr = [1, 2, 3, 4]; const findLargestDifference = (arr = []) => { const { length: len } = arr; let res = 0; for(let i = 0; i < len; i++){ for(let j = i + 1; j < len; j++){ if(arr[i] <= arr[j] && (j - i) > res){ res = j - i; }; }; }; return res; }; console.log(findLargestDifference(arr));
Output
And the output in the console will be −
3
- Related Questions & Answers
- Largest difference between element with a twist in JavaScript
- Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript
- Odd even index difference - JavaScript
- Index difference of tuples in JavaScript
- Returning number with increasing digits. in JavaScript
- Program to find largest kth index value of one list in Python
- Python Pandas - Return the int position of the largest value in the Index
- Dictionary with index as value in Python
- Find even odd index digit difference - JavaScript
- Greatest sum and smallest index difference in JavaScript
- Find the column number with largest value for each row in an R matrix.
- Reverse index value sum of array in JavaScript
- Constructing largest number from an array in JavaScript
- Maximum absolute difference of value and index sums in C
- Python Pandas - Fill NaN values with the specified value in an Index object
Advertisements