给定数组A []。任务是确定是否可以选择两个索引“ i”和“ j”以使满足以下条件:
- A[i] is not equal to A[j].
- A[A[i]] is equal to A[A[j]].
注意:数组中元素的值小于N的值,即,对于每个i,arr [i]
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]
方法:
- 通过运行两个循环开始遍历数组Arr []。
- 变量i指向索引0,变量j指向i的下一个。
- 如果Arr [i]不等于Arr [j],则检查Arr [Arr [i] – 1]是否等于Arr [Arr [j] – 1]。如果是,则返回true。
否则,还检查Arr [Arr [i] -1]和Arr [Arr [j] – 1]是否有其他索引。 - 重复上述步骤,直到遍历所有元素/索引。
- 如果找不到这样的索引,则返回false。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
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
Javascript
输出:
Yes
时间复杂度: O(N 2 )
辅助空间: O(1)