Posted on by Kalkicode
Code Array

# Find max distance between similar occurrences

Here given code implementation process.

``````// C Program
// Find Max Distance between two occurrences of the same element
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
}
// Find maximum distance between 2 similar elements
void maxDistance(int arr[], int size)
{
// Define auxiliary resultant variables
int max = -1;
int element = 0;
// Loop controlling variables
int i = 0;
int j = 0;
// Outer loop
for (i = 0; i < size && i + max < size; ++i)
{
// Find similar elements which is exist in maximum distance in right side
for (j = size - 1; j > i; --j)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
j = i;
// Or use break statement here
}
}
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
printf("\n None");
}
else
{
printf("\n Maximum Distance of similar elements [%d] is : %d\n", element, max);
}
}
int main(int argc, char const *argv[])
{
// Define array of integer elements
int arr1[] = {
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
int arr2[] = {
1 , 4 , 2 , 3 , 2 , 7
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
printf(" Array elements \n");
printArray(arr1, size);
maxDistance(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
printf("\n Array elements \n");
printArray(arr2, size);
maxDistance(arr2, size);
return 0;
}``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````/*
Java Program
Find Max Distance between two occurrences of the same element
*/
public class Distance
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i] );
}
}
// Find maximum distance between 2 similar elements
public void maxDistance(int[] arr, int size)
{
// Define auxiliary resultant variables
int max = -1;
int element = 0;
// Loop controlling variables
int i = 0;
int j = 0;
// Outer loop
for (i = 0; i < size && i + max < size; ++i)
{
// Find similar elements which is exist in maximum distance in right side
for (j = size - 1; j > i; --j)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
}
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
System.out.print("\n None");
}
else
{
System.out.print("\n Maximum Distance of similar elements [" + element + "] is : " + max + "\n");
}
}
public static void main(String[] args)
{

// Define array of integer elements
int[] arr1 =
{
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
int[] arr2 =
{
1 , 4 , 2 , 3 , 2 , 7
};
// Get the size
int size = arr1.length;
System.out.print(" Array elements \n");
// Get the size
size = arr2.length;
System.out.print("\n Array elements \n");

}
}``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Find Max Distance between two occurrences of the same element
*/

class Distance
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
}
// Find maximum distance between 2 similar elements
void maxDistance(int arr[], int size)
{
// Define auxiliary resultant variables
int max = -1;
int element = 0;
// Loop controlling variables
int i = 0;
int j = 0;
// Outer loop
for (i = 0; i < size && i + max < size; ++i)
{
// Find similar elements which is exist in maximum distance in right side
for (j = size - 1; j > i; --j)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
}
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
cout << "\n None";
}
else
{
cout << "\n Maximum Distance of similar elements [" << element << "] is : " << max << "\n";
}
}
};
int main()
{
// Define array of integer elements
int arr1[] = {
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
int arr2[] = {
1 , 4 , 2 , 3 , 2 , 7
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
cout << " Array elements \n";
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
cout << "\n Array elements \n";
return 0;
}``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````// Include namespace system
using System;
/*
C# Program
Find Max Distance between two occurrences of the same element
*/
public class Distance
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
}
// Find maximum distance between 2 similar elements
public void maxDistance(int[] arr, int size)
{
// Define auxiliary resultant variables
int max = -1;
int element = 0;
// Loop controlling variables
int i = 0;
int j = 0;
// Outer loop
for (i = 0; i < size && i + max < size; ++i)
{
// Find similar elements which is exist in maximum distance in right side
for (j = size - 1; j > i; --j)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
}
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
Console.Write("\n None");
}
else
{
Console.Write("\n Maximum Distance of similar elements [" + element + "] is : " + max + "\n");
}
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
3 , 5 , 7 , 9 , 4 , 3 , 5 , 7 , 4 , 7 , 9 , 5 , 3 , 9 , 5 , 6
};
int[] arr2 = {
1 , 4 , 2 , 3 , 2 , 7
};
// Get the size
int size = arr1.Length;
Console.Write(" Array elements \n");
// Get the size
size = arr2.Length;
Console.Write("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````<?php
/*
Php Program
Find Max Distance between two occurrences of the same element
*/
class Distance
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
}
// Find maximum distance between 2 similar elements
public	function maxDistance( & \$arr, \$size)
{
// Define auxiliary resultant variables
\$max = -1;
\$element = 0;
// Loop controlling variables
\$i = 0;
\$j = 0;
// Outer loop
for (\$i = 0; \$i < \$size && \$i + \$max < \$size; ++\$i)
{
// Find similar elements which is exist in maximum distance in right side
for (\$j = \$size - 1; \$j > \$i; --\$j)
{
if (\$arr[\$i] == \$arr[\$j])
{
if (\$max < \$j - \$i)
{
// When get new result
\$max = \$j - \$i;
// Get element which is exist on higher distance
\$element = \$arr[\$i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
\$j = \$i;
}
}
}
if (\$max == -1)
{
// That means  no duplicate element exist in this loop
echo "\n None";
}
else
{
echo "\n Maximum Distance of similar elements [". \$element ."] is : ". \$max ."\n";
}
}
}

function main()
{
// Define array of integer elements
\$arr1 = array(3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6);
\$arr2 = array(1, 4, 2, 3, 2, 7);
// Get the size
\$size = count(\$arr1);
echo " Array elements \n";
// Get the size
\$size = count(\$arr2);
echo "\n Array elements \n";
}
main();``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````/*
Node Js Program
Find Max Distance between two occurrences of the same element
*/
class Distance
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
}
// Find maximum distance between 2 similar elements
maxDistance(arr, size)
{
// Define auxiliary resultant variables
var max = -1;
var element = 0;
// Loop controlling variables
var i = 0;
var j = 0;
// Outer loop
for (i = 0; i < size && i + max < size; ++i)
{
// Find similar elements which is exist in maximum distance in right side
for (j = size - 1; j > i; --j)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
}
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
process.stdout.write("\n None");
}
else
{
process.stdout.write("\n Maximum Distance of similar elements [" + element + "] is : " + max + "\n");
}
}
}

function main()
{
// Define array of integer elements
var arr1 = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6];
var arr2 = [1, 4, 2, 3, 2, 7];
// Get the size
var size = arr1.length;
process.stdout.write(" Array elements \n");
// Get the size
size = arr2.length;
process.stdout.write("\n Array elements \n");
}
main();``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````#  Python 3 Program
#  Find Max Distance between two occurrences of the same element

class Distance :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

#  Find maximum distance between 2 similar elements
def maxDistance(self, arr, size) :
#  Define auxiliary resultant variables
max = -1
element = 0
#  Loop controlling variables
i = 0
j = 0
#  Outer loop
while (i < size and i + max < size) :
#  Find similar elements which is exist in maximum distance in right side
j = size - 1
while (j > i) :
if (arr[i] == arr[j]) :
if (max < j - i) :
#  When get new result
max = j - i
#  Get element which is exist on higher distance
element = arr[i]

#  When elements are same then break the inner loop
#  Value which is break this inner loop
#  Or use break statement here
j = i

j -= 1

i += 1

if (max == -1) :
#  That means  no duplicate element exist in this loop
print("\n None", end = "")
else :
print("\n Maximum Distance of similar elements [", element ,"] is : ", max )

def main() :
#  Define array of integer elements
arr1 = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6]
arr2 = [1, 4, 2, 3, 2, 7]
#  Get the size
size = len(arr1)
print(" Array elements ")
#  Get the size
size = len(arr2)
print("\n Array elements ")

if __name__ == "__main__": main()``````

#### Output

`````` Array elements
3   5   7   9   4   3   5   7   4   7   9   5   3   9   5   6
Maximum Distance of similar elements [ 5 ] is :  13

Array elements
1   4   2   3   2   7
Maximum Distance of similar elements [ 2 ] is :  2``````
``````#  Ruby Program
#  Find Max Distance between two occurrences of the same element

class Distance
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

end

#  Find maximum distance between 2 similar elements
def maxDistance(arr, size)
#  Define auxiliary resultant variables
max = -1
element = 0
#  Loop controlling variables
i = 0
j = 0
#  Outer loop
while (i < size && i + max < size)
#  Find similar elements which is exist in maximum distance in right side
j = size - 1
while (j > i)
if (arr[i] == arr[j])
if (max < j - i)
#  When get new result
max = j - i
#  Get element which is exist on higher distance
element = arr[i]
end

#  When elements are same then break the inner loop
#  Value which is break this inner loop
#  Or use break statement here
j = i
end

j -= 1
end

i += 1
end

if (max == -1)
#  That means  no duplicate element exist in this loop
print("\n None")
else
print("\n Maximum Distance of similar elements [", element ,"] is : ", max ,"\n")
end

end

end

def main()
#  Define array of integer elements
arr1 = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6]
arr2 = [1, 4, 2, 3, 2, 7]
#  Get the size
size = arr1.length
print(" Array elements \n")
#  Get the size
size = arr2.length
print("\n Array elements \n")
end

main()``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2
``````
``````/*
Scala Program
Find Max Distance between two occurrences of the same element
*/
class Distance
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
}
// Find maximum distance between 2 similar elements
def maxDistance(arr: Array[Int], size: Int): Unit = {
// Define auxiliary resultant variables
var max: Int = -1;
var element: Int = 0;
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Outer loop
while (i < size && i + max < size)
{
// Find similar elements which is exist in maximum distance in right side
j = size - 1;
while (j > i)
{
if (arr(i) == arr(j))
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr(i);
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
j -= 1;
}
i += 1;
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
print("\n None");
}
else
{
print("\n Maximum Distance of similar elements [" + element + "] is : " + max + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Distance = new Distance();
// Define array of integer elements
var arr1: Array[Int] = Array(3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6);
var arr2: Array[Int] = Array(1, 4, 2, 3, 2, 7);
// Get the size
var size: Int = arr1.length;
print(" Array elements \n");
// Get the size
size = arr2.length;
print("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````
``````/*
Swift 4 Program
Find Max Distance between two occurrences of the same element
*/
class Distance
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
// Find maximum distance between 2 similar elements
func maxDistance(_ arr: [Int], _ size: Int)
{
// Define auxiliary resultant variables
var max: Int = -1;
var element: Int = 0;
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Outer loop
while (i < size && i + max < size)
{
// Find similar elements which is exist in maximum distance in right side
j = size - 1;
while (j > i)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
j -= 1;
}
i += 1;
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
print("\n None", terminator: "");
}
else
{
print("\n Maximum Distance of similar elements [", element ,"] is : ", max );
}
}
}
func main()
{
// Define array of integer elements
let arr1: [Int] = [3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6];
let arr2: [Int] = [1, 4, 2, 3, 2, 7];
// Get the size
var size: Int = arr1.count;
print(" Array elements ");
// Get the size
size = arr2.count;
print("\n Array elements ");
}
main();``````

#### Output

`````` Array elements
3   5   7   9   4   3   5   7   4   7   9   5   3   9   5   6
Maximum Distance of similar elements [ 5 ] is :  13

Array elements
1   4   2   3   2   7
Maximum Distance of similar elements [ 2 ] is :  2``````
``````/*
Kotlin Program
Find Max Distance between two occurrences of the same element
*/
class Distance
{
//Display elements of given array
fun printArray(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
}
// Find maximum distance between 2 similar elements
fun maxDistance(arr: Array <Int> , size: Int): Unit
{
// Define auxiliary resultant variables
var max: Int = -1;
var element: Int = 0;
// Loop controlling variables
var i: Int = 0;
var j: Int ;
// Outer loop
while (i < size && i + max < size)
{
// Find similar elements which is exist in maximum distance in right side
j = size - 1;
while (j > i)
{
if (arr[i] == arr[j])
{
if (max < j - i)
{
// When get new result
max = j - i;
// Get element which is exist on higher distance
element = arr[i];
}
// When elements are same then break the inner loop
// Value which is break this inner loop
// Or use break statement here
j = i;
}
j -= 1;
}
i += 1;
}
if (max == -1)
{
// That means  no duplicate element exist in this loop
print("\n None");
}
else
{
print("\n Maximum Distance of similar elements [" + element + "] is : " + max + "\n");
}
}
}

fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var arr1: Array <Int> = arrayOf(3, 5, 7, 9, 4, 3, 5, 7, 4, 7, 9, 5, 3, 9, 5, 6);
var arr2: Array <Int> = arrayOf(1, 4, 2, 3, 2, 7);
// Get the size
var size: Int = arr1.count();
print(" Array elements \n");
// Get the size
size = arr2.count();
print("\n Array elements \n");
}``````

#### Output

`````` Array elements
3  5  7  9  4  3  5  7  4  7  9  5  3  9  5  6
Maximum Distance of similar elements [5] is : 13

Array elements
1  4  2  3  2  7
Maximum Distance of similar elements [2] is : 2``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post