Array Loop moving space complexity is O (1)

Source: Internet
Author: User

The principle is very simple: Flip the previous array elements, flip the array elements that follow, and finally flip the entire array element


#include<iostream>
#defineN -
using namespacestd;

void Reverse (int L,int R,int* Array)
{
double mid= (l+r)/2.0;
For (int i=l;i<mid;++i)
{
Array[i]=array[r+l-i]+array[i];
Array[r+l-i]=array[i]-array[r+l-i];
Array[i]=array[i]-array[r+l-i];
}
}
int main ()
{
int array[n],a,b;
cout<<"Array Length:";
Cin>>a;
cout<<"Please enter an array element:";
For (int i=0;i<a;++i)
Cin>>array[i];
cout<<"where the loop starts:";
Cin>>b;
Reverse (0,a-b-1,array);
Reverse (a-b,a-1,array);
Reverse (0,a-1,array);
For (int i=0;i<a;i++)
cout <<* (array+i)<<"";
return 0;
}

Array Loop moving space complexity is O (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.