Question:
How to Reverse array in groups? C++

Summary:

The code defines a C++ class Solution with a member function reverseInGroups that takes a vector of long long integers, its size, and an integer k. The function reverses every sub-array group of size k in the given vector. It uses the reverse function from the C++ Standard Template Library (STL) to reverse each sub-array. The main loop iterates through the vector in steps of k, and for each iteration, it reverses a sub-array of size k, handling the case where the last sub-array may have fewer than k elements.


Solution:

//User function template for C++


class Solution{

public:

    //Function to reverse every sub-array group of size k.

    void swap(int *a, int *b) {

    int temp = *a;

    *a = *b;

    *b = temp;

    }

    // void rev(int arr[],int n){

    //     low = 0; high = n-1;

    //         if(low<high){

    //             swap(arr[low],arr[high]);

    //             low++;

    //             high--;

    //         }

    //     }

    // }

    void reverseInGroups(vector<long long>& arr, int n, int k){

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

            if(i+k < n){

                reverse(arr.begin()+i,arr.begin()+(i+k));

            }

            else{

                reverse(arr.begin()+i,arr.end());

            }

        }

    }

};


Code Explanation:

  • The class Solution contains a member function reverseInGroups that takes a vector (arr), its size (n), and an integer (k).

  • The function uses a loop to iterate through the vector in steps of size k.

  • Inside the loop, it checks whether there are at least k elements remaining in the vector starting from the current position. If yes, it reverses a sub-array of size k using the reverse function from the STL.

  • If there are fewer than k elements remaining, it reverses the remaining sub-array.

  • The reverse function is applied to sub-arrays, effectively reversing the order of elements within each group of size k.

  • The function performs these operations until the entire vector is traversed.


Note: The commented-out code (// void rev(int arr[],int n) {...}) appears to be an attempt at a different approach for array reversal, but it is currently commented out and not used in the final implementation.


Suggested blogs:

>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>Testing react components using Hooks and Mocks

>Use Firebase Realtime Database with ASP.NET MVC App

>How to Install mariaDB client efficiently inside docker?

>Can I call an API to find out the random seed in Python `hash()` function?

>How remove residual lines after merge two regions using Geopandas in python?

>How to configure python in jmeter?

>How to mock a constant value in Python?

>Creating a pivot table by 6 month interval rather than year

>How to do PHP Decryption from Node.js Encryption

>How to Set up the Android emulator?

>How to Set up the local environment for Angular development?

>How to solve encoding issue when writing to a text file, with Python?


Nisha Patel

Nisha Patel

Submit
0 Answers