array rotation
PROBLEM :
Given an array of N size. You have to rotate array by d elements.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements. The third line of each test case contains "d" .
Output:
Corresponding to each test case, in a new line, print the modified array.
Constraints:
1 = T = 100
1 = N = 200
1 = A[i] = 1000
Example:
Input
1
5
1 2 3 4 5
2
Output
3 4 5 1 2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t,no,a[100],i,j,k,temp ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
cin>>k ;
for(i=0;i<k;i++)
{
for(j=0;j<no-1;j++)
{
temp=a[j] ;
a[j]=a[j+1] ;
a[j+1]=temp ;
}
}
for(i=0;i<no;i++)
cout<<a[i]<<" " ;
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
BETTER SOLUTION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void rotate_array(int [],int ) ;
int main()
{
int t,no,a[200],i,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
cin>>k ;
for(i=0;i<k;i++)
rotate_array(a,no) ;
for(i=0;i<no;i++)
cout<<a[i]<<" " ;
cout<<endl ;
}
return 0;
}
void rotate_array(int a[],int no)
{
int i,ele ;
ele=a[0] ;
for(i=0;i<no-1;i++)
a[i]=a[i+1] ;
a[no-1]=ele ;
}
--------------------------------------------------------------------------------
BEST SOLUTION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void reverse_array(int [],int ,int ) ;
int main()
{
int t,no,a[200],i,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
cin>>k ;
reverse_array(a,0,k-1) ;
reverse_array(a,k,no-1) ;
reverse_array(a,0,no-1) ;
for(i=0;i<no;i++)
cout<<a[i]<<" " ;
cout<<endl ;
}
return 0;
}
void reverse_array(int a[],int start,int end)
{
int temp ;
while(start<end)
{
temp=a[start] ;
a[start]=a[end] ;
a[end]=temp ;
start++ ;
end-- ;
}
}
---------------------------------------------------------------------------------
Given an array of N size. You have to rotate array by d elements.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements. The third line of each test case contains "d" .
Output:
Corresponding to each test case, in a new line, print the modified array.
Constraints:
1 = T = 100
1 = N = 200
1 = A[i] = 1000
Example:
Input
1
5
1 2 3 4 5
2
Output
3 4 5 1 2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t,no,a[100],i,j,k,temp ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
cin>>k ;
for(i=0;i<k;i++)
{
for(j=0;j<no-1;j++)
{
temp=a[j] ;
a[j]=a[j+1] ;
a[j+1]=temp ;
}
}
for(i=0;i<no;i++)
cout<<a[i]<<" " ;
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
BETTER SOLUTION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void rotate_array(int [],int ) ;
int main()
{
int t,no,a[200],i,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
cin>>k ;
for(i=0;i<k;i++)
rotate_array(a,no) ;
for(i=0;i<no;i++)
cout<<a[i]<<" " ;
cout<<endl ;
}
return 0;
}
void rotate_array(int a[],int no)
{
int i,ele ;
ele=a[0] ;
for(i=0;i<no-1;i++)
a[i]=a[i+1] ;
a[no-1]=ele ;
}
--------------------------------------------------------------------------------
BEST SOLUTION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void reverse_array(int [],int ,int ) ;
int main()
{
int t,no,a[200],i,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
cin>>k ;
reverse_array(a,0,k-1) ;
reverse_array(a,k,no-1) ;
reverse_array(a,0,no-1) ;
for(i=0;i<no;i++)
cout<<a[i]<<" " ;
cout<<endl ;
}
return 0;
}
void reverse_array(int a[],int start,int end)
{
int temp ;
while(start<end)
{
temp=a[start] ;
a[start]=a[end] ;
a[end]=temp ;
start++ ;
end-- ;
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment