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:
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
#include <bits/stdc++.h>
using namespace std;
void checkSamePosition( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
int temp = ((i + arr[i]) % n + n) % n;
if (mp.find(temp) != mp.end()) {
cout << "Yes" ;
return ;
}
mp[temp]++;
}
cout << "No" ;
}
int main()
{
int arr[] = { 5, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
checkSamePosition(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkSamePosition( int [] arr, int n)
{
Map<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++) {
int temp = ((i + arr[i]) % n + n) % n;
if (mp.get(temp) == null ) {
System.out.println( "Yes" );
return ;
}
mp.get(temp + 1 );
}
System.out.println( "No" );
}
public static void main(String[] args)
{
int [] arr = { 5 , 4 , 3 };
int N = arr.length;
checkSamePosition(arr, N);
}
}
|
Python3
def checkSamePosition(arr, n):
mp = {}
for i in range (n):
temp = ((i + arr[i]) % n + n) % n
if temp in mp:
print ( "Yes" )
return
if (temp in mp):
mp[temp] + = 1
else :
mp[temp] = mp.get(temp, 0 ) + 1
print ( "No" )
if __name__ = = '__main__' :
arr = [ 5 , 4 , 3 ]
N = len (arr)
checkSamePosition(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void checkSamePosition( int [] arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
int temp = ((i + arr[i]) % n + n) % n;
if (mp.ContainsKey(temp)) {
Console.Write( "Yes" );
return ;
}
mp[temp] = 1;
}
Console.Write( "No" );
}
static void Main() {
int [] arr = { 5, 4, 3 };
int N = arr.Length;
checkSamePosition(arr, N);
}
}
|
Javascript
<script>
function checkSamePosition(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++) {
var temp = ((i + arr[i]) % n + n) % n;
if (mp.has(temp)) {
document.write( "Yes" );
return ;
}
if (mp.has(temp))
{
mp.set(temp, mp.get(temp)+1)
}
else
mp.set(temp, 1)
}
document.write( "No" );
}
var arr = [5, 4, 3 ];
var N = arr.length;
checkSamePosition(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...