Knowee
Questions
Features
Study Tools

Given an array a of n positive integers. The task is to find the maximum of j - i subjected to the constraint of a[i] < a[j] and i < j.

Question

Given an array a of n positive integers. The task is to find the maximum of j - i subjected to the constraint of a[i] < a[j] and i < j.

🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using an efficient approach that involves two pointers and two auxiliary arrays. Here are the steps:

  1. Create two auxiliary arrays, LMin[] and RMax[].
  2. Traverse the input array from the beginning and fill LMin[] such that LMin[i] contains the smallest element from 0 to i in the array.
  3. Traverse the input array from the end and fill RMax[] such that RMax[j] contains the largest element from j to end in the array.
  4. Initialize i and j as 0 (pointers to LMin[] and RMax[] arrays).
  5. Run a loop while i < n and j < n. If LMin[i] < RMax[j], then calculate the difference j - i and update the maximum difference if it's greater than the current maximum difference. Then, increment j. If LMin[i] is greater than or equal to RMax[j], then increment i.
  6. Return the maximum difference.

This approach ensures that the elements a[i] and a[j] are such that i < j and a[i] < a[j], and the difference j - i is maximized. The time complexity of this approach is O(n), and it uses O(n) extra space.

This problem has been solved

Similar Questions

You have been given an array 'A' of N integers. You need to find the maximum value of j - i subjected to the constraint of A[i] <= A[j], where ‘i’ and ‘j’ are the indices of the array.For example :If 'A' = {3, 5, 4, 1}then the output will be 2.Maximum value occurs for the pair (3, 4)Detailed explanation ( Input/output format, Notes, Images )Constraints:1 <= T <= 1001 <= N <= 10 ^ 4-10 ^ 5 <= A[i] <= 10 ^ 5Time limit: 1 sec.Sample Input 1:1934 8 10 3 2 80 30 33 1Sample Output 1:6Explanation:Maximum value occurs for the pair (8, 33)Sample Input 2:1109 2 3 4 5 6 7 8 18 0Sample Output 2:8Explanation:Maximum value occurs for the pair (9, 18)

Find the maximum element from the given array of integers.

Given two arrays (arr1[], arr2[]) of integers, display the largest number in arr1, where that element should not be present in arr2. If the constraint is not satisfied return 0.Variable Constraints:Size of the array <= 5;Array data type = integer.Input:Size of array 1Elements of array 1Size of array 2Elements of array 2Output:Largest element in array 1

Given an array of integers, find the largest number that can be constructed by concatenating all the elements of the given array.Input FormatFirst line of input contains T - number of test cases. Its followed by 2T lines. First line of each test case contains N - size of the array and the second line contains N integers - elements of the array.Constraints1 <= T <= 10001 <= N <= 10000 <= ar[i] <= 1000Output FormatFor each test case, print the largest number that can be constructed by concatenating all the elements of the given array, separated by newline.Sample Input 03849 73 58 30 72 44 78 23 469 9 57 60 240 4 Sample Output 078737258494430239696057440

Problem StatementReport An IssueNinja has an array 'A' of length 'N'. He wanted to find the maximum element between index 'X' and 'Y', including both.Return the maximum element between 'X' and 'Y', including both.Consider 0-based indexing.Example:'N' = 5'X' = 1'Y' = 3'A' = [1, 2, 5, 2, 4]The maximum element in the range [1, 3] is 5.Hence, the answer for this case is 5.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.