×

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j

For example

Input:

N = 2

A[] = {1, 20}

Output:

1

Explanation:

A[0]<A[1] so (j-i) is 1-0 = 1.

Input:

N = 10

A[] = {9, 2, 3, 4, 5, 6, 7, 8, 18, 0}

Output:

8

Explanation:

In the given array A[0] < A[8]

satisfying the required condition(A[i] < A[j]) thus giving the maximum difference of j - i which is 8(8-0).

Approach 1

The approach is simple but inefficient. Run two loops. The outer loop will pick the element from the left and the inner loop will pick the element from the right.

Check if the element picked in the inner loop is greater than the element picked in the outer loop. Stop the inner loop and store the difference of (j-i).

Likewise, do the process for the entire array and update the maximum difference of (j-i).

Code (C++)

#include <bits/stdc++.h>

using namespace std;

// Function to find the max difference

int maxIndexDiff(int arr[], int n)

{

          int maxDiff = -1; // Let current max diff is -1

          int i, j; // Initialize two variables




          for (i = 0; i < n; ++i) { // outer loop

                   for (j = n - 1; j > i; --j) { // inner loop

                             if (arr[j] > arr[i] && maxDiff < (j - i)) // if element is greater and current maxdiff is less than the new one update the maxdiff

                                      maxDiff = j - i;

                   }

          }




          return maxDiff; // return the max difference

}




int main()

{

          int arr[] = { 9, 2, 3, 4, 5, 6, 7, 8, 18, 0 };

          int n = sizeof(arr) / sizeof(arr[0]); // Find the size of the array

          int maxDiff = maxIndexDiff(arr, n); // Call the function to calculate maximum difference

          cout << "Maximum difference is " << maxDiff;

          return 0;

}

Output

Maximum difference is 8

Code (C language)

#include <stdio.h>

// Function to find the max difference

int maxIndexDiff(int arr[], int n)

{

          int maxDiff = -1; // Let current max diff is -1

          int i, j; // Initialize two variables




          for (i = 0; i < n; ++i) { // outer loop

                   for (j = n - 1; j > i; --j) { // inner loop

                             if (arr[j] > arr[i] && maxDiff < (j - i)) // if element is greater and current maxdiff is less than the new one update the maxdiff

                                      maxDiff = j - i;

                   }

          }




          return maxDiff; // return the max difference

}




int main()

{

          int arr[] = { 9, 2, 3, 4, 5, 6, 7, 8, 18, 0 };

          int n = sizeof(arr) / sizeof(arr[0]); // Find the size of the array

          int maxDiff = maxIndexDiff(arr, n); // Call the function to calculate maximum difference

          printf("Maximum difference is %d", maxDiff);

          return 0;

}

Output

Maximum difference is 8

Time complexity - O(n*n)

Approach 2

After analyzing the brute force approach, it is observed that for every element in the outer loop, we find the maximum in the inner loop. This means for every window we find a maximum element.

For example, in A =  [1, 5, 12, 4, 9]

9 is greater than 1, 5, 4. Now to avoid finding the maximum again and again we can keep the track of the maximum number moving from the end to start of the array.

  1. Traverse the array from the end and keep a track of the maximum number to the right of the current index including self.
  2. Now we have a monotonous decreasing array, and we know we can use binary search to find the index of the rightmost greater element
  3. Now we will just use binary search for each of the elements in the array and store the maximum difference of the indices and that’s it we are done.

Code

#include <bits/stdc++.h>

using namespace std;

int main()

{

          vector<long long int> v{ 9, 2, 3, 4, 5, 6, 7, 8, 18, 0 }; // Take a vector

          int n = v.size(); // Find the size of the vector

          vector<long long int> maxFromEnd(n + 1, INT_MIN); // A vector to keep the max from end




          // create an array maxfromEnd

          for (int i = v.size() - 1; i >= 0; i--) {

                   maxFromEnd[i] = max(maxFromEnd[i + 1], v[i]);

          }




          int result = 0; //store max difference as result




          // use  binary search here

          for (int i = 0; i < v.size(); i++) {

// set low and high pointer

                   int low = i + 1, high = v.size() - 1, ans = i;




                   while (low <= high) {

                             int mid = (low + high) / 2; // find the mid element




                             if (v[i] <= maxFromEnd[mid]) {

                           

                                      // We store this as current answer and look

                                      // for further larger number to the right

                                      // side

                                      ans = max(ans, mid); // calculate current max diff

                                      low = mid + 1;

                             }

                             else {

                                      high = mid - 1;

                             }

                   }

                   // keeping a track of the

                   // maximum difference in indices

                   result = max(result, ans - i);

          }

          cout <<"Maximum difference is " << result << endl;

}

Output

Maximum difference is 8

Time complexity - O(n * logn)

Approach 3

By taking special care of the duplicates the problem can be solved in less than quadratic complexity.

  • To handle the duplicates, traverse the array and store each element index in a list.
  • Sort the array and then traverse it by keeping track of the maximum difference of i and j.
  • For j, the last index of the list will be taken and for i keep the first index from the list.
  • Update the maximum index and traverse the array upto last.

Code

#include <bits/stdc++.h>

using namespace std;

int maxIndexDiff(vector<int>& arr, int n) // Function to find max difference

{




          unordered_map<int, vector<int> > hashmap; // create unordered map




// Loop till the array end

          for (int i = 0; i < n; i++) {

                   hashmap[arr[i]].push_back(i); // Insert the index of a particular element

          }




          // Sort arr

          sort(arr.begin(), arr.end());

          int maxDiff = INT_MIN; // Let max difference is mimimum in the begining

          int temp = n; // take the size of array in temp




          // Iterate in the array

          for (int i = 0; i < n; i++) {

                   if (temp > hashmap[arr[i]][0]) { // Compare the index to avoid overflow

                             temp = hashmap[arr[i]][0];

                   }

                   maxDiff = max( // find max difference

                             maxDiff,

                             hashmap[arr[i]][hashmap[arr[i]].size() - 1]

                                      - temp);

          }

          return maxDiff; // return max difference

}




int main()

{




          int n = 9; // size of vector

          vector<int> arr{ 34, 8, 10, 3, 2, 80, 30, 33, 1 }; // vector elements




          int ans = maxIndexDiff(arr, n); // call the function

          cout << "The maxIndexDiff is : " << ans << endl; // print the result




          return 1;

}

Output

The maxIndexDiff is: 6

Time complexity

O(nlogn)

Space complexity

O(1)


Related Topics

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.