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

📅  最后修改于: 2021-04-18 02:22:52             🧑  作者: Mango

给定的阵列ARR大小为N[]中,任务是检查是否有任何两个整数AB重合在上的数字线的单个点的距离ARR [(A%N + N)%N]和换挡它们后arr [(B%N + N)%N] 。如果不可能,则打印“否” 。否则,打印“是”

例子:

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

  • 初始化一个哈希表,例如mp,以存储整数的最终位置。
  • 初始化一个字符串(例如ans)为“ No”,以存储所需的答案。
  • 使用变量i遍历[0,N – 1]范围并执行以下步骤:
    • i的最终位置存储在变量中,例如temp = ((i + arr [i])%N + N)%N
    • 如果mp中存在temp的值,则将ans设置为“ Yes”并退出循环。
    • 为已访问通过递增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.


输出:
Yes

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