Longest subset of nested elements from a given array
Last Updated :
03 May, 2021
Given an array arr[] consisting of a permutation of numbers in the range [0, N – 1], the task is to find the length of the longest subset from the array such that the elements in the subset are of the form {arr[i], arr[arr[i]], arr[arr[arr[i]]], …}
Examples:
Input: arr[] = {5, 4, 0, 3, 1, 6, 2}
Output: 4
Explanation:
arr[arr[0]] is equal to arr[5]
arr[arr[arr[0]]] is equal to arr[6]
arr[arr[arr[arr[0]]]] is equal to arr[2]
One of the possible subsets from the array are {arr[0], arr[5], arr[6], arr[2]} = {5, 6, 2, 0}, which is the longest subset satisfying the given condition. Therefore, the required output is 4.
Input: arr[] ={3, 1, 4, 0, 2}
Output: 2
Explanation:
arr[arr[0]] is equal to arr[3]
One of the possible subset from the array is {arr[0], arr[3]} = {3, 0}
Therefore, the required output is 2.
Approach: Follow the steps below to solve the problem:
- Initialize a variable res = 0 to store the length of the longest subset of the array that satisfying the condition.
- Traverse the array arr[] and perform the following operations:
- Check if arr[i] is equal to i or not. If found to be true, then update res = max(res, 1).
- Initialize a variable, say index = i, to store the index of elements of a subset from the given array.
- Iterate over elements of a subset while arr[index] != index, update the current element of the subset to the current index and also update index = arr[index].
- Finally, update the res to the maximum of res and the count of elements in the current subset.
- Finally, print the res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int arrayNesting(vector< int > arr)
{
int res = 0;
for ( int i = 0; i < arr.size(); i++)
{
if (arr[i] == i)
{
res = max(res, 1);
}
else
{
int count = 0;
int curr_index = i;
while (arr[curr_index] != curr_index)
{
int next_index = arr[curr_index];
arr[curr_index] = curr_index;
curr_index = next_index;
count++;
}
res = max(res, count);
}
}
return res;
}
int main()
{
vector< int > arr = { 5, 4, 0, 3, 1, 6, 2 };
int res = arrayNesting(arr);
cout<<res;
}
|
Java
import java.io.*;
import java.util.*;
class sol {
public int arrayNesting( int [] arr)
{
int res = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] == i) {
res = Math.max(res, 1 );
}
else {
int count = 0 ;
int curr_index = i;
while (arr[curr_index]
!= curr_index) {
int next_index = arr[curr_index];
arr[curr_index] = curr_index;
curr_index = next_index;
count++;
}
res = Math.max(res, count);
}
}
return res;
}
}
class GFG {
public static void main(String[] args)
{
sol st = new sol();
int [] arr = { 5 , 4 , 0 , 3 , 1 , 6 , 2 };
int res = st.arrayNesting(arr);
System.out.println(res);
}
}
|
C#
using System;
class sol {
public int arrayNesting( int [] arr)
{
int res = 0;
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] == i) {
res = Math.Max(res, 1);
}
else {
int count = 0;
int curr_index = i;
while (arr[curr_index] != curr_index) {
int next_index = arr[curr_index];
arr[curr_index] = curr_index;
curr_index = next_index;
count++;
}
res = Math.Max(res, count);
}
}
return res;
}
}
class GFG {
static public void Main()
{
sol st = new sol();
int [] arr = { 5, 4, 0, 3, 1, 6, 2 };
int res = st.arrayNesting(arr);
Console.WriteLine(res);
}
}
|
Python3
def arrayNesting(arr) :
res = 0
for i in range ( len (arr)) :
if arr[i] = = i :
res = max (res, 1 )
else :
count = 0
curr_index = i
while arr[curr_index] ! = curr_index :
next_index = arr[curr_index]
arr[curr_index] = curr_index
curr_index = next_index
count + = 1
res = max (res, count)
return res
if __name__ = = "__main__" :
arr = [ 5 , 4 , 0 , 3 , 1 , 6 , 2 ]
res = arrayNesting(arr)
print (res)
|
Javascript
<script>
function arrayNesting(arr)
{
let res = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == i) {
res = Math.max(res, 1);
}
else {
let count = 0;
let curr_index = i;
while (arr[curr_index]
!= curr_index) {
let next_index = arr[curr_index];
arr[curr_index] = curr_index;
curr_index = next_index;
count++;
}
res = Math.max(res, count);
}
}
return res;
}
let arr = [ 5, 4, 0, 3, 1, 6, 2 ];
let res = arrayNesting(arr);
document.write(res);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...