📌  相关文章
📜  检查一对整数 A 和 B 是否可以通过将它们移动距离 arr[(A%N +N)%N] 和 arr[(B%N +N)%N] 来重合

📅  最后修改于: 2021-10-28 01:38:17             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是检查任意两个整数AB在将它们移动距离arr[(A%N +N)%N]arr[(B%N +N)%N]分别。如果不可能,则打印“否” 。否则,打印“是”

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个 Hashmap,比如mp,来存储整数的最终位置。
  • 初始化一个字符串,比如ans,为“否”以存储所需的答案。
  • 使用变量i在范围[0, N – 1] 上迭代并执行以下步骤:
    • i的最终位置存储在一个变量中,比如temp = ((i + arr[i]) % N + N) % N
    • 如果mp 中存在temp的值,则将ans设置为“是”并跳出循环。
    • 为已访问通过递增1熔点[温度]标记温度
  • 完成以上步骤后,打印ans的值作为必填答案。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if two
// integers coincide at a point
void checkSamePosition(int arr[], int n)
{
    // Store the final position
    // of integers A and B
    unordered_map mp;
 
    // Iterate over the range[0, n]
    for (int i = 0; i < n; i++) {
 
        // Store the final position
        // of the integer i
        int temp = ((i + arr[i]) % n + n) % n;
 
        // If temp is present in the Map
        if (mp.find(temp) != mp.end()) {
 
            // Print Yes and return
            cout << "Yes";
            return;
        }
 
        // Mark its final
        // position as visited
        mp[temp]++;
    }
 
    // If every integer stored
    // in the Map is unique
    cout << "No";
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 4, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    checkSamePosition(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
  // Function to check if two
  // integers coincide at a point
  static void checkSamePosition(int[] arr, int n)
  {
     
    // Store the final position
    // of integers A and B
    Map mp = new HashMap();
 
    // Iterate over the range[0, n]
    for (int i = 0; i < n; i++) {
 
      // Store the final position
      // of the integer i
      int temp = ((i + arr[i]) % n + n) % n;
 
      // If temp is present in the Map
      if (mp.get(temp) == null) {
 
        // Print Yes and return
        System.out.println("Yes");
        return;
      }
 
      // Mark its final
      // position as visited
 
      mp.get(temp + 1);
    }
 
    // If every integer stored
    // in the Map is unique
    System.out.println("No");
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 5, 4, 3 };
    int N = arr.length;
 
    checkSamePosition(arr, N);
  }
}
 
// This code is contributed by splevel62.


Python3
# Python 3 program for the above approach
 
# Function to check if two
# integers coincide at a point
def checkSamePosition(arr, n):
    # Store the final position
    # of integers A and B
    mp = {}
 
    # Iterate over the range[0, n]
    for i in range(n):
        # Store the final position
        # of the integer i
        temp = ((i + arr[i]) % n + n) % n
 
        # If temp is present in the Map
        if temp in mp:
            # Print Yes and return
            print("Yes")
            return
 
        # Mark its final
        # position as visited
        if(temp in mp):
            mp[temp] += 1
        else:
            mp[temp] = mp.get(temp,0)+1
 
    # If every integer stored
    # in the Map is unique
    print("No")
 
# Driver Code
if __name__ == '__main__':
    arr =  [5, 4, 3]
    N = len(arr)
    checkSamePosition(arr, N)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to check if two
    // integers coincide at a point
    static void checkSamePosition(int[] arr, int n)
    {
       
        // Store the final position
        // of integers A and B
        Dictionary mp = new Dictionary();
      
        // Iterate over the range[0, n]
        for (int i = 0; i < n; i++) {
      
            // Store the final position
            // of the integer i
            int temp = ((i + arr[i]) % n + n) % n;
      
            // If temp is present in the Map
            if (mp.ContainsKey(temp)) {
      
                // Print Yes and return
                Console.Write("Yes");
                return;
            }
      
            // Mark its final
            // position as visited
            mp[temp] = 1;
        }
      
        // If every integer stored
        // in the Map is unique
        Console.Write("No");
    }
 
  // Driver code
  static void Main() {
    int[] arr = { 5, 4, 3 };
    int N = arr.Length;
  
    checkSamePosition(arr, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
Yes

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程