Dada una array arr[] de tamaño N , la tarea es verificar si dos enteros A y B coinciden en un solo punto en la recta numérica después de desplazarlos una distancia arr[(A%N +N)%N] y arr[(B%N +N)%N] respectivamente. Si no es posible, escriba “No” . De lo contrario, escriba «Sí» .
Ejemplos:
Entrada: arr[] = {5, 4, 3}
Salida: Sí
Explicación:
Deje que el valor de A = -4 y B = -6 coincidan en el punto -1 en la recta numérica.
Después de cambiar,
A = -4 + arr[(-4 % 3 + 3) % 3] = -4 + arr[2] = -4 + 3 = -1.
B = -6 + arr[(-6 % 3 + 3) % 3] = -6 + 5 = -1.Entrada: arr[]= {-4, 4, 4, 4}
Salida: No
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice un Hashmap , digamos mp, para almacenar la posición final de los enteros.
- Inicialice una string, digamos ans, como » No» para almacenar la respuesta requerida.
- Iterar sobre el rango [0, N – 1] usando la variable i y realizar los siguientes pasos:
- Almacene la posición final de i en una variable, digamos temp = ((i + arr[i]) % N + N) % N .
- Si el valor de temp está presente en mp , establezca ans como «Sí» y salga del bucle .
- Marque la temperatura como visitada incrementando mp[temp] en 1 .
- Después de completar los pasos anteriores, imprima el valor de ans como la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
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>
Yes
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por swatijha0908 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA