'Could you please explain me the the working of following code?

// This is a function to check if the given array is sorted or not by recurssion

#include<iostream>
using namespace std;
bool sorted(int arr[],int n)
{
    
    if(n==1)
    {
        return true;
    }

I am cofused here when n will reach 1 then it will return true to "restArray" after that if array is not sorted then how will "restArray" become false?

    bool restArray = sorted(arr+1, n-1);
    
    return (arr[0]<=arr[1] && restArray);
}

int main()
{
    int arr[]={1,6,3,4,5};
    cout<<sorted(arr,5);
    return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source