Reverse the array - 450DSA Cracker Solution

Reverse a String

Given a String S , print the reverse of the string as output.

class Solution {
  public:
    string revStr(string S) {
        for(int i = 0, j= S.length()-1; i<j ;i++,j--)
        {
            swap(S[i], S[j]);
        }
        return S;
    }
};

Reverse a String

You are given a string s. You need to reverse the string.

for(int i=0, j=str.size()-1; i <j ; i++, j--)
    {
        char ch = str[i];
        str[i] = str[j];
        str[j] = ch;
    }
    return str;

Reverse sub array

Provided an array Arr of N integers, you need to reverse a subarray of that array. The range of this subarray is given by L and R.

void reverseSubArray(int *arr, int n, int l, int r) {
        // code here
        while(l<=r){
            swap(arr[l-1],arr[r-1]);
            l++;
            r--;
    }
    }

Reverse array in groups

Given an array arr[] of positive integers of size N. Reverse every sub-array group of size K.

Note: If at any instance, there are no more subarrays of size greater than or equal to K, then reverse the last subarray (irrespective of its size). You shouldn't return any array, modify the given array in-place.

class Solution{
public:
    //Function to reverse every sub-array group of size k.
    void reverseInGroups(vector<long long>& arr, int n, int k){
        // code here
        int i;
        for(int i =0;i<n;i+=k)
        {
            if(i+k>n)
                reverse(arr.begin()+i, arr.end());
            else
                reverse(arr.begin()+i, arr.begin()+i+k);
        }
    }
};

Special array reversal


Given a string S, containing special characters and all the alphabets, reverse the string without affecting the positions of the special characters.

class Solution
{
    public:
    string reverse(string str)
    { 
        //code here.
        int i, j;
        for(i=0, j = str.length();i<j;)
        {
            while(!isalpha(str[i])) i++;
            while(!isalpha(str[j])) j--;
            if(i < j){
                swap(str[i] , str[j]);
                i++;
                j--;
        }
        }
                return str;
    } 
};

String Reversal

Given a string, say S, print it in reverse manner eliminating the repeated characters and spaces.

Example 1:

Input: S = "GEEKS FOR GEEKS"
Output: "SKEGROF"

Example 2:

Input: S = "I AM AWESOME"
Output: "EMOSWAI"
string reverseString(string s)
{
    string s1="";
    for (int i=s.length()-1; i >=0 ;i--)
    {
        if(s[i]==' ')
            continue;
        size_t found = s1.find(s[i]);
        if (found ==string::npos )
            s1=s1+s[i];
    }
    return s1;
}

Perfect Reversible String

You are given a string ‘str’, the task is to check that reverses of all possible substrings of ‘str’ are present in ‘str’. If yes then the answer is 1, otherwise, the answer will be 0.

Example 1:

Input: n = 2, str = "ab"
Output: 0
Explanation: All substrings are "a",
"b","ab" but reverse of "ab" is not 
present in str.


class Solution{
  public:
    int isReversible(string str, int n) { 
         //complete the function here
         string rev = str;
         reverse(rev.begin(), rev.end());

        if(rev == str)
            return 1;
        else
            return 0;
    } 
};

Inverse Permutation

Given an array A of size n of integers in the range from 1 to n, we need to find the inverse permutation of that array.
Inverse Permutation is a permutation which you will get by inserting position of an element at the position specified by the element value in the array. For better understanding, consider the following example:
Suppose we found element 4 at position 3 in an array, then in reverse permutation, we insert 3 (position of element 4 in the array) in position 4 (element value).


vector<int> inversePermutation(int arr[], int size) {
    vector<int> temp(size) ; 
    for(int i=0;i<size;i++)
        temp[arr[i]-1]  = i+1;
    return temp;
}

Reverse a string with spaces intact

Given a string, your task is to reverse the string keeping the spaces intact to their positions.

Example 1:

Input:
S = "Help others"
Output: sreh topleH
Explanation: The space is intact at index 4
while all other characters are reversed.
class Solution
{
  public:

        string reverseWithSpacesIntact (string s)
        {
            for(int i=0,j=s.size()-1; i<j ;)
            {
                if(s[i] == ' ')
                    i++;
                else if(s[j]==' ')
                    j--;
                else
                {
                    swap(s[i], s[j]);
                    i++; j--;
                }
            }
            return s;
        }


};

Reverse words in a given string

Given a String S, reverse the string without reversing its individual words. Words are separated by dots.

Example 1:

Input:
S = i.like.this.program.very.much
Output: much.very.program.this.like.i
Explanation: After reversing the whole
string(not individual words), the input
string becomes
much.very.program.this.like.i
class Solution
{
    public:
    //Function to reverse words in a given string.
    string reverseWords(string S) 
    { 
        string result="", s1="";
        for (int i =0;i<S.length();i++)
        {
            if (S[i] =='.')
            {
                s1 = S[i]+s1;
                result = s1 + result;
                s1="";
            }
            else
                s1 = s1 + S[i];
        }
        result = (s1) + result;
        return result;
    } 
};