# Check if a pair of integers A and B can coincide by shifting them by distances arr[(A%N +N)%N] and arr[(B%N +N)%N]

• Last Updated : 20 May, 2021

Given an array arr[] of size N, the task is to check if any two integers A and B coincide at a single point on the number line after shifting them by a distance arr[(A%N +N)%N] and arr[(B%N +N)%N] respectively. If it is not possible, then print “No”. Otherwise, print “Yes”.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {5, 4, 3}
Output: Yes
Explanation:
Let the value of A = -4 and B = -6 coincide at point -1 on the number line.
After shifting,
A = -4 + arr[(-4 % 3 + 3) % 3] = -4 + arr = -4 + 3 = -1.
B = -6 + arr[(-6 % 3 + 3) % 3] = -6 + 5 = -1.

Input: arr[]= {-4, 4, 4, 4}
Output: No

Approach: Follow the steps below to solve the problem:

• Initialize a Hashmap, say mp, to store the final position of integers.
• Initialize a string, say ans, as “No” to store the required answer.
• Iterate over the range [0, N – 1] using the variable i and perform the following steps:
• Store the final position of i in a variable, say temp = ((i + arr[i]) % N + N) % N.
• If the value of temp is present in mp, then set ans as “Yes” and break out of the loop.
• Mark temp as visited by incrementing mp[temp] by 1.
• After completing the above steps, print the value of ans as the required answer.

Below is the implementation of the above approach:

## 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<``int``, ``int``> 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);` `    ``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<``int``, ``int``> mp = ``new` `Dictionary<``int``, ``int``>();``     ` `        ``// 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

 ``

Output:
`Yes`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up