Dada una array, la tarea es invertir la array sin usar el signo de resta ‘-‘ en ninguna parte de su código. No es difícil invertir una array, pero lo principal es no usar el operador ‘-‘.
Preguntado en: Entrevista a Moonfrog
A continuación se presentan diferentes enfoques:
Método 1:
- Almacene los elementos de la array en un vector en C++ .
- Luego invierta el vector usando funciones predefinidas.
- Luego almacene los elementos invertidos en la array.
Método 2:
- Almacene los elementos de la array en una pila .
- Como la pila sigue a Last In First Out, podemos almacenar elementos desde la parte superior de la pila en la array, que será ella misma de manera inversa.
Método 3:
- En este método, la idea es usar un signo negativo pero almacenándolo en una variable.
- Al usar esta declaración x = (INT_MIN/INT_MAX), obtenemos -1 en una variable x.
- Como INT_MIN e INT_MAX tienen los mismos valores pero de signos opuestos, al dividirlos dará -1.
- Entonces ‘x’ se puede usar para disminuir el índice desde el último.
Implementación:
C++
// C++ program to reverse an array without // using "-" sign #include <bits/stdc++.h> using namespace std; // Function to reverse array void reverseArray(int arr[], int n) { // Trick to assign -1 to a variable int x = (INT_MIN / INT_MAX); // Reverse array in simple manner for (int i = 0; i < n / 2; i++) // Swap ith index value with (n-i-1)th // index value swap(arr[i], arr[n + (x * i) + x]); } // Drivers code int main() { int arr[] = { 5, 3, 7, 2, 1, 6 }; int n = sizeof(arr) / sizeof(arr[0]); reverseArray(arr, n); // print the reversed array for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
Java
// Java program to reverse an array without // using "-" sign class GFG { // Function to reverse array static void reverseArray(int arr[], int n) { // Trick to assign -1 to a variable int x = (Integer.MIN_VALUE / Integer.MAX_VALUE); // Reverse array in simple manner for (int i = 0; i < n / 2; i++) // Swap ith index value with (n-i-1)th // index value swap(arr, i, n + (x * i) + x); } static int[] swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Drivers code public static void main(String[] args) { int arr[] = { 5, 3, 7, 2, 1, 6 }; int n = arr.length; reverseArray(arr, n); // print the reversed array for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } } // This code has been contributed by 29AjayKumar
Python3
# Python program to reverse an array without # using "-" sign # Function to reverse array def reverseArray(arr, n): import sys # Trick to assign - 1 to a variable x = -sys.maxsize // sys.maxsize # Reverse array in simple manner for i in range(n//2): # Swap ith index value with (n-i-1)th # index value arr[i], arr[n + (x*i) + x] = arr[n + (x*i) + x], arr[i] # Driver code if __name__ == "__main__": arr = [5, 3, 7, 2, 1, 6] n = len(arr) reverseArray(arr, n) # print the reversed array for i in range(n): print(arr[i], end=" ") # This code is contributed by # sanjeev2552
C#
// C# program to reverse an array without // using "-" sign using System; class GFG { // Function to reverse array static void reverseArray(int[] arr, int n) { // Trick to assign -1 to a variable int x = (int.MinValue / int.MaxValue); // Reverse array in simple manner for (int i = 0; i < n / 2; i++) // Swap ith index value with (n-i-1)th // index value swap(arr, i, n + (x * i) + x); } static int[] swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Drivers code public static void Main() { int[] arr = { 5, 3, 7, 2, 1, 6 }; int n = arr.Length; reverseArray(arr, n); // print the reversed array for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } } /* This code contributed by PrinciRaj1992 */
PHP
<?PHP // PHP program to reverse an array without // using "-" sign // Function to reverse array function reverseArray(&$arr, $n) { // Trick to assign -1 to a variable $x = (PHP_INT_MIN / PHP_INT_MAX); // Reverse array in simple manner for ($i = 0; $i < $n / 2; $i++) // Swap ith index value with (n-i-1)th // index value swap($arr, $i, $n + ($x * $i) + $x); } function swap(&$arr, $i, $j) { $temp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $temp; return $arr; } // Drivers code $arr = array( 5, 3, 7, 2, 1, 6 ); $n = sizeof($arr); reverseArray($arr, $n); // print the reversed array for ($i = 0; $i < $n; $i++) echo($arr[$i] . " "); // This code is contributed by Code_Mech
Javascript
<script> //javascript program to reverse an array without // using "-" sign // Function to reverse array function reversearray(arr,n) { // Trick to assign -1 to a variable let x = parseInt(-2147483648 / 2147483647, 10); // Reverse array in simple manner for (let i = 0; i < parseInt(n / 2, 10); i++) { // Swap ith index value with (n-i-1)th // index value let temp = arr[i]; arr[i] = arr[n + (x * i) + x]; arr[n + (x * i) + x] = temp; } } let arr = [ 5, 3, 7, 2, 1, 6 ]; let n = arr.length; reversearray(arr, n); // print the reversed array for (let i = 0; i < n; i++) document.write(arr[i] +" "); // This code is contributed by vaibhavrabadiya117. </script>
6 1 2 7 3 5
Complejidad temporal: O(n)
Espacio auxiliar: O(1)
Método 4:
In this method 4, the idea is to use bitwise operator to implement subtraction i.e. A - B = A + ~B + 1 so, i-- can be written as i = i +~1 +1
Implementación:
C++
// C++ program to reverse an array without // using "-" sign #include <bits/stdc++.h> using namespace std; // Function to reverse array void reverseArray(int arr[], int n) { // Reverse array in simple manner for (int i = 0; i < n / 2; i++) // Swap ith index value with (n-i-1)th // index value // Note : A - B = A + ~B + 1 // So n - i = n + ~i + 1 then // n - i - 1 = (n + ~i + 1) + ~1 + 1 swap(arr[i], arr[(n + ~i + 1) + ~1 + 1]); } // Driver code int main() { int arr[] = { 5, 3, 7, 2, 1, 6 }; int n = sizeof(arr) / sizeof(arr[0]); reverseArray(arr, n); // print the reversed array for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
Java
// Java program to reverse an array without // using "-" sign import java.util.Arrays; class GFG { // Function to reverse array static void reverseArray(int arr[], int n) { // Reverse array in simple manner for (int i = 0; i < n / 2; i++) // Swap ith index value with (n-i-1)th // index value // Note : A - B = A + ~B + 1 // So n - i = n + ~i + 1 then // n - i - 1 = (n + ~i + 1) + ~1 + 1 { swap(arr, i, (n + ~i + 1) + ~1 + 1); } } static int[] swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Driver code public static void main(String args[]) { int arr[] = { 5, 3, 7, 2, 1, 6 }; int n = arr.length; reverseArray(arr, n); // print the reversed array for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } } // This code contributed by Rajput-Ji
Python3
# Python program to reverse an array without # using "-" sign # Function to reverse array def reverseArray(arr, n): # Reverse array in simple manner for i in range(n//2): # Swap ith index value with (n-i-1)th # index value # Note : A - B = A + ~B + 1 # So n - i = n + ~i + 1 then # n - i - 1 = (n + ~i + 1) + ~1 + 1 arr[i], arr[(n + ~i + 1) + ~1 + 1] = arr[(n + ~i + 1) + ~1 + 1],arr[i] # Driver code arr = [ 5, 3, 7, 2, 1, 6 ] n = len(arr) reverseArray(arr, n) # print the reversed array for i in range(n): print(arr[i],end=" ") # This code is contributed by ankush_953
C#
// C# program to reverse an array without // using "-" sign using System; class GFG { // Function to reverse array static void reverseArray(int[] arr, int n) { // Reverse array in simple manner for (int i = 0; i < n / 2; i++) // Swap ith index value with (n-i-1)th // index value // Note : A - B = A + ~B + 1 // So n - i = n + ~i + 1 then // n - i - 1 = (n + ~i + 1) + ~1 + 1 { swap(arr, i, (n + ~i + 1) + ~1 + 1); } } static int[] swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Driver code public static void Main(String[] args) { int[] arr = { 5, 3, 7, 2, 1, 6 }; int n = arr.Length; reverseArray(arr, n); // print the reversed array for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); } } } // This code has been contributed by 29AjayKumar
PHP
<?PHP // PHP program to reverse an array without // using "-" sign // Function to reverse array function reverseArray(&$arr, $n) { // Reverse array in simple manner for ($i = 0; $i < $n / 2; $i++) // Swap ith index value with (n-i-1)th // index value // Note : A - B = A + ~B + 1 // So n - i = n + ~i + 1 then // n - i - 1 = (n + ~i + 1) + 1 + 1 { swap($arr, $i, ($n + ~$i + 1) + ~1 + 1); } } function swap(&$arr, $i, $j) { $temp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $temp; return $arr; } // Driver code { $arr = array( 5, 3, 7, 2, 1, 6 ); $n = sizeof($arr); reverseArray($arr, $n); // print the reversed array for ($i = 0; $i < $n; $i++) { echo($arr[$i] . " "); } } // This code contributed by Code_Mech
Javascript
<script> // Javascript program to reverse an array without using "-" sign // Function to reverse array function reverseArray(arr, n) { // Reverse array in simple manner for (let i = 0; i < parseInt(n / 2, 10); i++) // Swap ith index value with (n-i-1)th // index value // Note : A - B = A + ~B + 1 // So n - i = n + ~i + 1 then // n - i - 1 = (n + ~i + 1) + ~1 + 1 { swap(arr, i, (n + ~i + 1) + ~1 + 1); } } function swap(arr, i, j) { let temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } let arr = [ 5, 3, 7, 2, 1, 6 ]; let n = arr.length; reverseArray(arr, n); // print the reversed array for (let i = 0; i < n; i++) { document.write(arr[i] + " "); } // This code is contributed by mukesh07. </script>
6 1 2 7 3 5
Complejidad temporal: O(n)
Espacio auxiliar: O(1)
Este artículo es una contribución de Sahil Chhabra . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA