Check whether (i,j) exists such that arr[i] != arr[j] and arr[arr[i]] is equal to arr[arr[j]]

Given an array A[]. The task is to determine if it is possible to choose two indices ‘i’ and ‘j’ such that the below conditions gets satisfied:-

  1. A[i] is not equal to A[j].
  2. A[A[i]] is equal to A[A[j]].

Note: The value of the elements in an array is less than the value of N i.e. For every i, arr[i] < N.

Examples:



Input: N = 4, A[] = {1, 1, 2, 3}
Output: Yes
As A[3] != to A[1] but A[A[3]] == A[A[1]]

Input: N = 4, A[] = {2, 1, 3, 3}
Output: No
As A[A[3]] == A[A[4]] but A[3] == A[4]

Approach:

  1. Start traversing the Array Arr[] by running two loops.
  2. The variable i point at the index 0 and variable j point to the next of i.
  3. If Arr[i] is not equal to Arr[j] then check if Arr[Arr[i] – 1] is equal to Arr[Arr[j] – 1]. If yes then return true.
    Else check Arr[Arr[i]- 1] and Arr[Arr[j] – 1] for other indices also.
  4. Repeat the above step till all the elements/index gets traversed.
  5. If no such indices found return false.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that will tell whether
// such Indices present or Not.
bool checkIndices(int Arr[], int N)
{
  
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
  
            // Checking 1st condition i.e whether
            // Arr[i] equal to Arr[j] or not
            if (Arr[i] != Arr[j]) {
  
                // Checking 2nd condition i.e whether
                // Arr[Arr[i]] equal to Arr[Arr[j]] or not.
                if (Arr[Arr[i] - 1] == Arr[Arr[j] - 1])
                    return true;
            }
        }
    }
  
    return false;
}
  
// Driver Code
int main()
{
    int Arr[] = { 3, 2, 1, 1, 4 };
    int N = sizeof(Arr) / sizeof(Arr[0]);
  
    // Calling function.
    checkIndices(Arr, N) ? cout << "Yes"
                         : cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
  
// Function that calculates marks. 
class GFG
    static boolean checkIndices(int Arr[], int N) 
    
    
        for (int i = 0; i < N - 1; i++) { 
            for (int j = i + 1; j < N; j++) { 
    
                // Checking 1st condition i.e whether 
                // Arr[i] equal to Arr[j] or not 
                if (Arr[i] != Arr[j]) { 
      
                    // Checking 2nd condition i.e whether 
                    // Arr[Arr[i]] equal to Arr[Arr[j]] or not. 
                    if (Arr[Arr[i] - 1] == Arr[Arr[j] - 1]) 
                        return true
                
            
        }
        return false;
    }
  
    // Driver code 
    public static void main(String args[]) 
    
        int Arr[] = { 3, 2, 1, 1, 4 }; 
        int N = Arr.length;
          
        if(checkIndices(Arr, N))
            System.out.println("Yes");
        else
            System.out.println("No");
    
  
// This code is Contributed by 
// Naman_Garg

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the 
# above approach
  
# Function that will tell whether
# such Indices present or Not.
def checkIndices(Arr, N):
  
    for i in range(N - 1):
        for j in range(i + 1, N):
  
            # Checking 1st condition i.e whether
            # Arr[i] equal to Arr[j] or not
            if (Arr[i] != Arr[j]):
  
                # Checking 2nd condition i.e whether
                # Arr[Arr[i]] equal to Arr[Arr[j]] or not.
                if (Arr[Arr[i] - 1] == Arr[Arr[j] - 1]):
                    return True
  
    return False
  
# Driver Code
if __name__ == "__main__":
      
    Arr = [ 3, 2, 1, 1, 4 ]
    N =len(Arr)
  
    # Calling function.
    if checkIndices(Arr, N):
        print("Yes")  
    else:
        print("No")
  
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
  
class GFG
{
      
// Function that calculates marks. 
static bool checkIndices(int []Arr, int N) 
    for (int i = 0; i < N - 1; i++)
    
        for (int j = i + 1; j < N; j++) 
        
  
            // Checking 1st condition i.e whether 
            // Arr[i] equal to Arr[j] or not 
            if (Arr[i] != Arr[j]) 
            
  
                // Checking 2nd condition i.e 
                // whether Arr[Arr[i]] equal
                // to Arr[Arr[j]] or not. 
                if (Arr[Arr[i] - 1] == Arr[Arr[j] - 1]) 
                    return true
            
        
    
    return false
  
// Driver code 
static public void Main ()
{
    int []Arr = { 3, 2, 1, 1, 4 }; 
    int N = Arr.Length; 
      
    if(checkIndices(Arr, N)) 
        Console.WriteLine("Yes"); 
    else
        Console.WriteLine("No"); 
  
// This code is Contributed by Sachin

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the 
// above approach
  
// Function that will tell whether
// such Indices present or Not.
function checkIndices($Arr, $N)
{
    for ($i = 0; $i < $N - 1; $i++) 
    {
        for ($j = $i + 1;
                  $j < $N; $j++)
        {
  
            // Checking 1st condition i.e 
            // whether Arr[i] equal to
            // Arr[j] or not
            if ($Arr[$i] != $Arr[$j]) 
            {
  
                // Checking 2nd condition i.e 
                // whether Arr[Arr[i]] equal to 
                // Arr[Arr[j]] or not.
                if ($Arr[$Arr[$i] - 1] == $Arr[$Arr[$j] - 1])
                    return true;
            }
        }
    }
  
    return false;
}
  
// Driver Code
$Arr = array(3, 2, 1, 1, 4);
$N = sizeof($Arr);
  
// Calling function.
if(checkIndices($Arr, $N)) 
    echo "Yes";
else
    echo "No";
  
// This code is contributed 
// by Akanksha Rai
?>

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.