- Related Questions & Answers
- Find the shortest distance between any pair of two different good nodes in C++
- C# program to find the maximum of three numbers
- Find minimum difference between any two element in C++
- Find the minimum difference between Shifted tables of two numbers in Python
- C Program to find sum of two numbers without using any operator
- Python program to find the maximum of three numbers
- Find the minimum distance between two numbers in C++
- PHP program to find the total number of date between any two given dates
- 8085 program to find maximum of two 8 bit numbers
- C# program to list the difference between two lists
- C# Program to get the difference between two dates
- C# Program to return the difference between two sequences
- Find the compatibility difference between two arrays in C++
- Java program to find the LCM of two numbers
- C++ Program to Find Maximum Value of any Algebraic Expression

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given an array arr[] consisting of n integers. Our task is to create a program to find the maximum difference between the index of any two different numbers in C++.

**Code Description** − Here, we need to find the maximum difference between the index of integer values of the array, given that the two integers are different.

Let’s take an example to understand the problem,

arr[] = {4, 1, 3, 2, 1, 2, 4}

5

The difference between index 0, element 4, and index 5, element 2.

We will try to find the maximum possible difference between the index of unique elements from the array.

**Program to show the implementation of our solution,**

#include <iostream> using namespace std; int maximum(int a, int b){ if(a > b) return a; return b; } int CalcMaxIndDiff(int a[], int n) { int indDiff1 = 0, indDiff2 = 0; int i = 0; while(i < (n - 1)){ if(a[0] != a[i]){ indDiff2 = i; break; } i++; } i = (n - 1) ; while(i > 0){ if(a[0] != a[i]){ indDiff1 = i; break; } i--; } return maximum(indDiff1, indDiff2); } int main() { int arr[] = { 4, 1, 3, 2, 1, 2, 4 }; int n = 7; cout<<"The maximum difference between the index of any two different numbers is "<<CalcMaxIndDiff(arr, n); return 0; }

The maximum difference between the index of any two different numbers is 5

Advertisements