Program to find the maximum difference between the index of any two different numbers
Last Updated :
09 Jun, 2022
Given an array of N integers. The task is to find the maximum difference between the index of any two different numbers. Note that there is a minimum of two different numbers.
Examples:
Input: a[] = {1, 2, 3, 2, 3}
Output: 4
The difference between 1 and last 3.
Input: a[] = {1, 1, 3, 1, 1, 1}
Output: 3
The difference between the index of 3 and last 1.
Approach: Initially, check the first number which is different from a[0] starting from the end, store the difference of their index as ind1. Also, check for the first number which is different from a[n - 1] from the beginning, store the difference of their index as ind2. The answer will be max(ind1, ind2).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum difference
int findMaximumDiff(int a[], int n)
{
int ind1 = 0;
// Iteratively check from back
for (int i = n - 1; i > 0; i--) {
// Different numbers
if (a[0] != a[i]) {
ind1 = i;
break;
}
}
int ind2 = 0;
// Iteratively check from
// the beginning
for (int i = 0; i < n - 1; i++) {
// Different numbers
if (a[n - 1] != a[i]) {
ind2 = (n - 1 - i);
break;
}
}
return max(ind1, ind2);
}
// Driver code
int main()
{
int a[] = { 1, 2, 3, 2, 3 };
int n = sizeof(a) / sizeof(a[0]);
cout << findMaximumDiff(a, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum difference
static int findMaximumDiff(int []a, int n)
{
int ind1 = 0;
// Iteratively check from back
for (int i = n - 1; i > 0; i--)
{
// Different numbers
if (a[0] != a[i])
{
ind1 = i;
break;
}
}
int ind2 = 0;
// Iteratively check from
// the beginning
for (int i = 0; i < n - 1; i++)
{
// Different numbers
if (a[n - 1] != a[i])
{
ind2 = (n - 1 - i);
break;
}
}
return Math.max(ind1, ind2);
}
// Driver code
public static void main(String args[])
{
int []a = { 1, 2, 3, 2, 3 };
int n = a.length;
System.out.println(findMaximumDiff(a, n));
}
}
// This code is contributed by Akanksha_Rai
Python3
# Python3 implementation of the approach
# Function to return the maximum difference
def findMaximumDiff(a, n):
ind1 = 0
# Iteratively check from back
for i in range(n - 1, -1, -1):
# Different numbers
if (a[0] != a[i]):
ind1 = i
break
ind2 = 0
# Iteratively check from
# the beginning
for i in range(n - 1):
# Different numbers
if (a[n - 1] != a[i]):
ind2 = (n - 1 - i)
break
return max(ind1, ind2)
# Driver code
a = [1, 2, 3, 2, 3]
n = len(a)
print(findMaximumDiff(a, n))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum difference
static int findMaximumDiff(int []a, int n)
{
int ind1 = 0;
// Iteratively check from back
for (int i = n - 1; i > 0; i--)
{
// Different numbers
if (a[0] != a[i])
{
ind1 = i;
break;
}
}
int ind2 = 0;
// Iteratively check from
// the beginning
for (int i = 0; i < n - 1; i++)
{
// Different numbers
if (a[n - 1] != a[i])
{
ind2 = (n - 1 - i);
break;
}
}
return Math.Max(ind1, ind2);
}
// Driver code
static void Main()
{
int []a = { 1, 2, 3, 2, 3 };
int n = a.Length;
Console.WriteLine(findMaximumDiff(a, n));
}
}
// This code is contributed by mits
PHP
<?php
// PHP implementation of the approach
// Function to return the maximum difference
function findMaximumDiff($a, $n)
{
$ind1 = 0;
// Iteratively check from back
for ($i = $n - 1; $i > 0; $i--)
{
// Different numbers
if ($a[0] != $a[$i])
{
$ind1 = $i;
break;
}
}
$ind2 = 0;
// Iteratively check from
// the beginning
for ($i = 0; $i < $n - 1; $i++)
{
// Different numbers
if ($a[$n - 1] != $a[$i])
{
$ind2 = ($n - 1 - $i);
break;
}
}
return max($ind1, $ind2);
}
// Driver code
$a = array( 1, 2, 3, 2, 3 );
$n = count($a);
echo findMaximumDiff($a, $n);
// This code is contributed by Ryuga
?>
JavaScript
<script>
// javascript implementation of the approach
// Function to return the maximum difference
function findMaximumDiff(a , n) {
var ind1 = 0;
// Iteratively check from back
for (i = n - 1; i > 0; i--) {
// Different numbers
if (a[0] != a[i]) {
ind1 = i;
break;
}
}
var ind2 = 0;
// Iteratively check from
// the beginning
for (i = 0; i < n - 1; i++) {
// Different numbers
if (a[n - 1] != a[i]) {
ind2 = (n - 1 - i);
break;
}
}
return Math.max(ind1, ind2);
}
// Driver code
var a = [ 1, 2, 3, 2, 3 ];
var n = a.length;
document.write(findMaximumDiff(a, n));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Find the maximum difference after applying the given operations two times on a number Given an integer N, the task is to find the maximum difference after applying the given operation two times on the given integer. The operation is defined as follows: Choose any digit (0-9) from N and replace all instance of the same digit with any other digit (0-9).N after the operation cannot have
10 min read
Maximum sum of a subsequence having difference between their indices equal to the difference between their values Given an array A[] of size N, the task is to find the maximum sum of a subsequence such that for each pair present in the subsequence, the difference between their indices in the original array is equal to the difference between their values. Examples: Input: A[] = {10, 7, 1, 9, 10, 15}, N = 6Output
5 min read
Find a number M < N such that difference between their XOR and AND is maximum Given a natural number N, the task is to find a number M smaller than N such that the difference between their bitwise XOR (N ^ M) and bitwise AND (N & M) is maximum. Examples: Input: N = 4Output: 3Explanation: (4 ^ 0) - (4 & 0) = 4(4 ^ 1) - (4 & 1) = 5(4 ^ 2) - (4 & 2) = 6(4 ^ 3) -
8 min read
Maximize difference between the Sum of the two halves of the Array after removal of N elements Given an integer N and array arr[] consisting of 3 * N integers, the task is to find the maximum difference between first half and second half of the array after the removal of exactly N elements from the array. Examples: Input: N = 2, arr[] = {3, 1, 4, 1, 5, 9}Output: 1Explanation:Removal of arr[1]
11 min read
Find maximum difference between elements having a fixed index difference of K Given an integer K and an unsorted array of integers arr[], find the maximum difference between elements in the array having a fixed index difference of K. Examples: Input: arr[] = [2, 9, 7, 4, 6, 8, 3], K = 2Output: 5Explanation: The maximum absolute difference with an index difference of two is 5,
5 min read
Find Maximized difference between two elements for every index of given Arrays Given two arrays A[] and B[] of the same length N. the task is to find a pair (X, Y) for two numbers Ai (number at i index of array A) and Bi(number at i index of array B) such that the value of |X-Y| is maximized for each index and the pair satisfies the following conditions: 1 ⤠X, Y ⤠Bigcd(X, Y)
6 min read