Longest chain of arr[i], arr[arr[i]], .. without repetition
Last Updated :
30 Aug, 2022
Given an array of size n such that elements in array are distinct and in range from 0 to n-1. We need to find out length of the longest chain {arr[i], arr[arr[i]], arr[arr[arr[i]]]……} such that no set element repeats.
Examples:
Input : arr[] = [5, 4, 0, 3, 1, 6, 2]
Output :4
Explanation:
The longest chain without repetition is
{arr[0], arr[5], arr[6], arr[2]} = {5, 6, 2, 0}
Input : arr[] = {1, 0, 4, 2, 3}
Output :3
Explanation:
The longest chain without repetition is
{arr[2], arr[4], arr[3]} = {4, 2, 3}
A naive solution is to find length of longest chain beginning from every element. To keep track of visited nodes, keep a visited array and reset this array after every finding longest chain from an element.
An efficient solution is to traverse each index and set them as -1. Once we see an index that we have set as -1, we know we have come across the same index so we stop and compare if the current count is greater than the max we have seen so far. We can do this setting to -1 in place so that we don’t keep revisiting the same indexes again and again. Why this works is because since each number is distinct, there is always just one way to reach a particular index. So no matter which index you start at in the particular cycle, you will always see the same cycle and hence the same count. So once a cycle is completely visited we can just skip checking for all the indexes in this cycle.
Implementation:
C++
#include <iostream>
using namespace std;
void aNesting( int arr[], int start, int & max)
{
int c_max = 0;
while (arr[start] != -1) {
c_max++;
int temp = arr[start];
arr[start] = -1;
start = temp;
}
if (c_max > max) max = c_max;
}
int maxLength( int arr[], int n)
{
int max = 0;
for ( int i = 0; i < n; i++) {
aNesting(arr, i, max);
}
return max;
}
int main()
{
int arr[] = { 5, 4, 0, 3, 1, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxLength(arr, n);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int aNesting( int arr[], int start, int max)
{
int c_max = 0 ;
while (arr[start] != - 1 )
{
c_max++;
int temp = arr[start];
arr[start] = - 1 ;
start = temp;
}
if (c_max > max)
max = c_max;
return max;
}
static int maxLength( int [] arr, int n)
{
int max = 0 ,max1;
for ( int i = 0 ; i < n; i++)
{
max1 = aNesting(arr, i, max);
if (max1>max)
max = max1;
}
return max;
}
public static void main(String args[])
{
int arr[] = { 5 , 4 , 0 , 3 , 1 , 6 , 2 };
int n = arr.length;
System.out.println(maxLength(arr, n));
}
}
|
Python3
def aNesting(arr,start, max ):
c_max = 0
while (arr[start] ! = - 1 ):
c_max + = 1
temp = arr[start]
arr[start] = - 1
start = temp
if (c_max > max ):
max = c_max
return max
def maxLength(arr, n):
max = 0
for i in range ( 0 , n, 1 ):
max__ = aNesting(arr, i, max )
if (max__> max ):
max = max__
return max__
if __name__ = = '__main__' :
arr = [ 5 , 4 , 0 , 3 , 1 , 6 , 2 ]
n = len (arr)
print (maxLength(arr, n))
|
C#
using System;
class GFG
{
static int aNesting( int [] arr, int start, int max)
{
int c_max = 0;
while (arr[start] != -1)
{
c_max++;
int temp = arr[start];
arr[start] = -1;
start = temp;
}
if (c_max > max)
max = c_max;
return max;
}
static int maxLength( int [] arr, int n)
{
int max = 0, max1;
for ( int i = 0; i < n; i++)
{
max1 = aNesting(arr, i, max);
if (max1>max)
max = max1;
}
return max;
}
public static void Main()
{
int [] arr = { 5, 4, 0, 3, 1, 6, 2 };
int n = arr.Length;
Console.WriteLine(maxLength(arr, n));
}
}
|
PHP
<?php
function aNesting( $arr , $start , & $max )
{
$c_max = 0;
while ( $arr [ $start ] != -1)
{
$c_max ++;
$temp = $arr [ $start ];
$arr [ $start ] = -1;
$start = $temp ;
}
if ( $c_max > $max )
$max = $c_max ;
}
function maxLength( $arr , $n )
{
$max = 0;
for ( $i = 0; $i < $n ; $i ++)
{
aNesting( $arr , $i , $max );
}
return $max ;
}
$arr = array (5, 4, 0, 3, 1, 6, 2 );
$n = sizeof( $arr );
echo maxLength( $arr , $n );
?>
|
Javascript
<script>
function aNesting(arr, start, max)
{
var c_max = 0;
while (arr[start] != -1)
{
c_max++;
var temp = arr[start];
arr[start] = -1;
start = temp;
}
if (c_max > max)
max = c_max;
return max;
}
function maxLength(arr, n)
{
var max = 0, max1;
for ( var i = 0; i < n; i++)
{
max1 = aNesting(arr, i, max);
if (max1 > max)
max = max1;
}
return max;
}
var arr = [ 5, 4, 0, 3, 1, 6, 2 ];
var n = arr.length;
document.write(maxLength(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...