Reverse array queries hackerrank solution Amazon

Reverse array queries hackerrank solution Amazon
Array Reversal in C - Hacker Rank Solution


Given an array, of size n, reverse it. Example: If array, arr = [1,2,3,4,5], after reversing it, the array should be, arr[5,4,3,2,1].

The first line contains an integer, n, denoting the size of the array. The next line contains n space-separated integers denoting the elements of the array.

  • 1<=n<=1000
  • 1<=arri<=1000, where arri is the ith element of the array.

The output is handled by the code given in the editor, which would print the array.


Given array, arr = [16,13,7,2,1,12]. After reversing the array, arr = [12,1,2,7,13,16]




1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stdio.h> #include <stdlib.h> int main() { int num, *arr,*arr2,i; scanf("%d", &num); arr = (int*) malloc(num * sizeof(int)); for(i = 0; i < num; i++) { scanf("%d", arr + i); } arr2 = (int*) malloc(num * sizeof(int)); for(i=1;i<=num;i++) { arr2[i-1] = arr[num-i]; } for(i = 0; i < num; i++) printf("%d ", *(arr2 + i)); return 0; }

HackerRank,HackerRank C

A functional approach to this task would be making a variable copy of the original array, iterate each operation making sure each one has two elements (indices) and the first index is less than the second one. Then you can just replace the collection subrange with it reversed subsequence:

func performOperations(arr: [Int], operations: [[Int]]) -> [Int] { var arr = arr operations.forEach { guard $0.count == 2, $0[0] < $0[1] else { return } arr.replaceSubrange($0[0]...$0[1], with: arr[$0[0]...$0[1]].reversed()) } return arr } let arr = [640, 26, 276, 224, 737, 677, 893, 87, 422, 30] let operations = [[0, 9], [2, 2], [5, 5], [1, 6], [5, 6], [5, 9], [0, 8], [6, 7], [1, 9], [3, 3]] performOperations(arr: arr, operations: operations) // [87, 422, 30, 737, 224, 677, 893, 640, 26, 276]

Given an array, reverse every sub-array formed by consecutive k elements.

Examples: 



Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] k = 3 

Output: 


[3, 2, 1, 6, 5, 4, 9, 8, 7]

Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 5 

Output: 


[5, 4, 3, 2, 1, 8, 7, 6]

Input: arr = [1, 2, 3, 4, 5, 6] k = 1 

Output: 


[1, 2, 3, 4, 5, 6]

Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 10 

Output: 


[8, 7, 6, 5, 4, 3, 2, 1] 

Approach: Consider every sub-array of size k starting from the beginning of the array and reverse it. We need to handle some special cases. If k is not multiple of n where n is the size of the array, for the last group we will have less than k elements left, we need to reverse all remaining elements. If k = 1, the array should remain unchanged. If k >= n, we reverse all elements present in the array.

Below image is a dry run of the above approach: 

Below is the implementation of the above approach:

void reverse(int arr[], int n, int k)

    for (int i = 0; i < n; i += k)

        int right = min(i + k - 1, n - 1);

            swap(arr[left++], arr[right--]);

    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

    int n = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < n; i++)

    static void reverse(int arr[], int n, int k)

        for (int i = 0; i < n; i += k)

            int right = Math.min(i + k - 1, n - 1);

    public static void main(String[] args)

        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

        for (int i = 0; i < n; i++)

            System.out.print(arr[i] + " ");

        right = min(i + k - 1, n - 1)

            arr[left], arr[right] = arr[right], arr[left]

public static void reverse(int[] arr,

    for (int i = 0; i < n; i += k)

        int right = Math.Min(i + k - 1, n - 1);

public static void Main(string[] args)

    int[] arr = new int[] {1, 2, 3, 4,

    for (int i = 0; i < n; i++)

        Console.Write(arr[i] + " ");

function reverse($arr, $n, $k)

    for ($i = 0; $i < $n; $i += $k)

        $right = min($i + $k - 1, $n - 1);

            $arr[$left] = $arr[$right];

$arr = array(1, 2, 3, 4, 5, 6, 7, 8);

$arr1 = reverse($arr, $n, $k);

for ($i = 0; $i < $n; $i++)

function reverse(arr, n, k)

    for(let i = 0; i < n; i += k)

        let right = Math.min(i + k - 1, n - 1);

let arr = new Array(1, 2, 3, 4, 5, 6, 7, 8);

let arr1 = reverse(arr, n, k);

for(let i = 0; i < n; i++)

    document.write(arr1[i] + " ");

void reverse(int arr[], int n, int k)

    for (int i = 0; i < n; i += k)

    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

    int n = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < n; i++)

Output: 

3 2 1 6 5 4 8 7

Time complexity of above solution is O(n). 
Auxiliary space used by the program is O(1).

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Article Tags :

Practice Tags :