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]
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:
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[2] = -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 <bits/stdc++.h> 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[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<Integer, Integer> mp = new HashMap<Integer, Integer>(); // 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
<script> // JavaScript program for the above approach // Function to check if two // integers coincide at a point function checkSamePosition(arr, n) { // Store the final position // of integers A and B var mp = new Map(); // Iterate over the range[0, n] for ( var i = 0; i < n; i++) { // Store the final position // of the integer i var temp = ((i + arr[i]) % n + n) % n; // If temp is present in the Map if (mp.has(temp)) { // Print Yes and return document.write( "Yes" ); return ; } // Mark its final // position as visited if (mp.has(temp)) { mp.set(temp, mp.get(temp)+1) } else mp.set(temp, 1) } // If every integer stored // in the Map is unique document.write( "No" ); } // Driver Code var arr = [5, 4, 3 ]; var N = arr.length; checkSamePosition(arr, N); </script> |
Output:
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...