GeeksforGeeks App
Open App
Browser
Continue

# Reverse an array without using subtract sign ‘-‘ anywhere in the code

Given an array, the task is to reverse the array without using subtract sign ‘-‘ anywhere in your code. It is not tough to reverse an array but the main thing is to not use ‘-‘ operator.

Below are different approaches:

Method 1:

1. Store array elements into a vector in C++
2. Then reverse the vector using predefined functions.
3. Then store reversed elements into the array back.

Method 2:

1. Store array elements into a stack
2. As the stack follows Last In First Out, so we can store elements from top of the stack into the array which will be itself in a reverse manner.

Method 3:

1. In this method, the idea is to use a negative sign but by storing it into a variable.
2. By using this statement x = (INT_MIN/INT_MAX), we get -1 in a variable x.
3. As INT_MIN and INT_MAX have same values just of opposite signs, so on dividing them it will give -1.
4. Then ‘x’ can be used in decrementing the index from last.

Implementation:

## C++

 `// C++ program to reverse an array without``// using "-" sign``#include ``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

 `

## Javascript

 ``

Output

`6 1 2 7 3 5 `

Time Complexity: O(n)
Auxiliary Space: O(1)

Method 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 ```

Implementation:

## C++

 `// C++ program to reverse an array without``// using "-" sign``#include ``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

 `

## Javascript

 ``

Output

`6 1 2 7 3 5 `

Time Complexity: O(n)
Auxiliary Space: O(1)

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up