Rearranging array
PROBLEM :
Given a list of integers, rearrange the list such that it consists of alternating minimum maximum elements using only list operations. The first element of the list should be minimum and second element should be maximum of all elements present in the list. Similarly, third element will be next minimum element and fourth element is next maximum element and so on. Use of extra space is not permitted
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 two 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.
Output:
Corresponding to each test case, in a new line, print the modified list.
Constraints:
1 = T = 100
1 = N = 200
1 = A[i] = 500
Example:
Input
1
5
4 5 1 2 3
Output
1 5 2 4 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void Rearranging_array(int [],int ) ;
void sort(int [],int ,int ) ;
int main()
{
int t,no,arr[200],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
Rearranging_array(arr,no) ;
for(i=0;i<no;i++)
cout<<arr[i]<<" " ;
cout<<endl ;
}
return 0;
}
void Rearranging_array(int arr[],int no)
{
sort(arr,0,no) ;
int a,b,c,temp,i ;
a=0;
while(a<no-2)
{
b=a+1 ;
c=no-1 ;
temp=arr[b] ;
arr[b]=arr[c] ;
arr[c]=temp ;
a=a+2 ;
sort(arr,a,no) ;
}
}
void sort(int arr[],int start,int end)
{
int i,j,temp2;
for(i=start;i<end;i++)
{
for(j=i+1;j<end;j++)
{
if(arr[i]>arr[j])
{
temp2=arr[i];
arr[i]=arr[j];
arr[j]=temp2;
}
}
}
}
---------------------------------------------------------------------------------
Given a list of integers, rearrange the list such that it consists of alternating minimum maximum elements using only list operations. The first element of the list should be minimum and second element should be maximum of all elements present in the list. Similarly, third element will be next minimum element and fourth element is next maximum element and so on. Use of extra space is not permitted
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 two 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.
Output:
Corresponding to each test case, in a new line, print the modified list.
Constraints:
1 = T = 100
1 = N = 200
1 = A[i] = 500
Example:
Input
1
5
4 5 1 2 3
Output
1 5 2 4 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void Rearranging_array(int [],int ) ;
void sort(int [],int ,int ) ;
int main()
{
int t,no,arr[200],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
Rearranging_array(arr,no) ;
for(i=0;i<no;i++)
cout<<arr[i]<<" " ;
cout<<endl ;
}
return 0;
}
void Rearranging_array(int arr[],int no)
{
sort(arr,0,no) ;
int a,b,c,temp,i ;
a=0;
while(a<no-2)
{
b=a+1 ;
c=no-1 ;
temp=arr[b] ;
arr[b]=arr[c] ;
arr[c]=temp ;
a=a+2 ;
sort(arr,a,no) ;
}
}
void sort(int arr[],int start,int end)
{
int i,j,temp2;
for(i=start;i<end;i++)
{
for(j=i+1;j<end;j++)
{
if(arr[i]>arr[j])
{
temp2=arr[i];
arr[i]=arr[j];
arr[j]=temp2;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment