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:-
- A[i] is not equal to A[j].
- 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:Ā Ā
- Start traversing the Array Arr[] by running two loops.
- The variable i point at the index 0 and variable j point to the next of i.
- 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. - Repeat the above step till all the elements/index gets traversed.
- If no such indices found return false.
Below is the implementation of the above approach:Ā
C++
// 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; } |
Java
// 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 |
Python 3
# 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 |
C#
// 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 |
PHP
<?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 ?> |
Javascript
<script> Ā
// Javascript implementation of the above approach Ā
// Function that will tell whether // such Indices present or Not. function checkIndices(Arr, N) { Ā
Ā Ā Ā Ā for ( var i = 0; i < N - 1; i++) { Ā Ā Ā Ā Ā Ā Ā Ā for ( var 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 var Arr = [ 3, 2, 1, 1, 4 ]; var N = Arr.length; Ā
// Calling function. checkIndices(Arr, N) ? document.write( "Yes" ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā : document.write( "No" ); Ā
Ā
</script> |
Yes
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Optimization: The above approach used can be optimized to O(n) with the help of an unordered_map.
The steps used in this approach are as follows:
- First, we use an unordered_map to store the mapping of Arr[i] to i+1.Ā
- Then, we iterate through the array and check if Arr[i] is already present in the hash table(unordered_map).Ā
- If it is present, we check if the corresponding index in the hash table has the same value as Arr[i]. If yes then we have found the required indices and return ātrueā. If not, we continue the iteration.Ā
- If we reach the end of the iteration and have not found the required indices, we return false.
Below is the code for the above approach.
C++
// 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) { Ā Ā Ā Ā // Hash table to store the mapping of Arr[i] to i+1 Ā Ā Ā Ā unordered_map< int , int > mp; Ā
Ā Ā Ā Ā for ( int i = 0; i < N; i++) { Ā Ā Ā Ā Ā Ā Ā Ā // 1st condition is checked here i.e whether Ā Ā Ā Ā Ā Ā Ā Ā // Arr[i] equal to any other previous element Ā Ā Ā Ā Ā Ā Ā Ā // or not Ā Ā Ā Ā Ā Ā Ā Ā if (mp.find(Arr[i]) != mp.end()) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j = mp[Arr[i]]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking the second condition i.e whether Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Arr[Arr[i]] equal to Arr[Arr[j-1]] or not. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Here we are taking j-1 as we are storing Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the Arr[i] in i+1. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Arr[j-1] == Arr[i]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā mp[Arr[i]] = i+1; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā 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; } Ā
// This code is contributed by Pushpesh Raj. |
Python3
# Python 3 implementation of the above approach Ā
# Function that will tell whether # such Indices present or Not. def checkIndices(arr, n): Ā Ā Ā Ā # Dictionary to store the mapping of arr[i] to i+1 Ā Ā Ā Ā mp = {} Ā
Ā Ā Ā Ā for i in range (n): Ā Ā Ā Ā Ā Ā Ā Ā # 1st condition is checked here i.e whether Ā Ā Ā Ā Ā Ā Ā Ā # arr[i] equal to any other previous element Ā Ā Ā Ā Ā Ā Ā Ā # or not Ā Ā Ā Ā Ā Ā Ā Ā if arr[i] in mp: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j = mp[arr[i]] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Checking the second condition i.e whether Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # arr[arr[i]] equal to arr[arr[j-1]] or not. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Here we are taking j-1 as we are storing Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the arr[i] in i+1. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if arr[j - 1 ] = = arr[i]: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return True Ā Ā Ā Ā Ā Ā Ā Ā mp[arr[i]] = i + 1 Ā
Ā Ā Ā Ā return False Ā
# Driver Code if __name__ = = "__main__" : Ā Ā Ā Ā arr = [ 3 , 2 , 1 , 1 , 4 ] Ā Ā Ā Ā n = len (arr) Ā
Ā Ā Ā Ā # Calling function. Ā Ā Ā Ā print ( "Yes" if checkIndices(arr, n) else "No" ) |
Java
// Java implementation of the above approach import java.io.*; Ā
class GFG { Ā Ā Ā Ā // Function that will tell whether Ā Ā Ā Ā // such Indices present or Not. Ā Ā Ā Ā static boolean checkIndices( int Arr[], int N) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // Hash table to store the mapping of Arr[i] to i+1 Ā Ā Ā Ā Ā Ā Ā Ā java.util.Map<Integer, Integer> mp Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new java.util.HashMap<>(); Ā
Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < N; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 1st condition is checked here i.e whether Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Arr[i] equal to any other previous element Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // or not Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (mp.containsKey(Arr[i])) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j = mp.get(Arr[i]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking the second condition i.e whether Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Arr[Arr[i]] equal to Arr[Arr[j-1]] or Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // not. Here we are taking j-1 as we are Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // storing the Arr[i] in i+1. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Arr[j - 1 ] == Arr[i]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mp.put(Arr[i], i + 1 ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā return false ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā public static void main(String[] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int Arr[] = { 3 , 2 , 1 , 1 , 4 }; Ā Ā Ā Ā Ā Ā Ā Ā int N = Arr.length; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calling function. Ā Ā Ā Ā Ā Ā Ā Ā if (checkIndices(Arr, N)) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "Yes" ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "No" ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; Ā
class Program { Ā Ā Ā Ā // Function that will tell whether Ā Ā Ā Ā // such Indices present or Not. Ā Ā Ā Ā static bool checkIndices( int [] Arr, int N) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // Hash table to store the mapping of Arr[i] to i+1 Ā Ā Ā Ā Ā Ā Ā Ā Dictionary< int , int > mp Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Dictionary< int , int >(); Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i < N; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 1st condition is checked here i.e whether Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Arr[i] equal to any other previous element Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // or not Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (mp.ContainsKey(Arr[i])) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int j = mp[Arr[i]]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking the second condition i.e whether Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Arr[Arr[i]] equal to Arr[Arr[j-1]] or Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // not. Here we are taking j-1 as we are Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // storing the Arr[i] in i+1. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Arr[j - 1] == Arr[i]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mp[Arr[i]] = i + 1; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā return false ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā static void Main( string [] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int [] Arr = { 3, 2, 1, 1, 4 }; Ā Ā Ā Ā Ā Ā Ā Ā int N = Arr.Length; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calling function. Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(checkIndices(Arr, N) ? "Yes" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā : "No" ); Ā Ā Ā Ā } } |
Javascript
// Function that will tell whether such Indices present or Not. function checkIndices(arr) { Ā Ā Ā Ā // Dictionary to store the mapping of arr[i] to i+1 Ā Ā Ā Ā let mp = {}; Ā
Ā Ā Ā Ā for (let i = 0; i < arr.length; i++) { Ā Ā Ā Ā Ā Ā Ā Ā // 1st condition is checked here i.e whether arr[i] equal to any other previous element or not Ā Ā Ā Ā Ā Ā Ā Ā if (arr[i] in mp) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let j = mp[arr[i]]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Checking the second condition i.e whether arr[arr[i]] equal to arr[arr[j-1]] or not. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Here we are taking j-1 as we are storing the arr[i] in i+1. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr[j-1] == arr[i]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā mp[arr[i]] = i+1; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā return false ; } Ā
// Driver Code let arr = [3, 2, 1, 1, 4]; Ā
// Calling function. console.log(checkIndices(arr) ? "Yes" : "No" ); |
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!