Move all negative elements to end
PROBLEM :
Given an unsorted array having both negative and positive integers. The task is place all negative element at the end of array without changing the order of positive element and negative element.
Examples:
Input : A[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output : 1 3 2 11 6 -1 -7 -5
Input : A[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output : 7 9 10 11 -5 -3 -4 -1
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
For each test case in a new line output the modified array.
Constraints:
1<=T<=100
1<=N<=100
-1000<=A[]<=1000
Example:
Input:
2
8
1 -1 3 2 -7 -5 11 6
8
-5 7 -3 -4 9 10 -1 11
Output:
1 3 2 11 6 -1 -7 -5
7 9 10 11 -5 -3 -4 -1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void negative_element(int [],int ) ;
int main()
{
int t,no,i ;
int arr[100] ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
negative_element(arr,no) ;
for(i=0;i<no;i++)
cout<<arr[i]<<" " ;
cout<<endl ;
}
return 0;
}
void negative_element(int arr[],int no)
{
int temp[no] ;
int i,k ;
k=0 ;
for(i=0;i<no;i++)
if(arr[i]>=0)
temp[k++]=arr[i] ;
for(i=0;i<no;i++)
if(arr[i]<0)
temp[k++]=arr[i] ;
for(i=0;i<no;i++)
arr[i]=temp[i] ;
}
---------------------------------------------------------------------------------
Given an unsorted array having both negative and positive integers. The task is place all negative element at the end of array without changing the order of positive element and negative element.
Examples:
Input : A[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output : 1 3 2 11 6 -1 -7 -5
Input : A[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output : 7 9 10 11 -5 -3 -4 -1
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
For each test case in a new line output the modified array.
Constraints:
1<=T<=100
1<=N<=100
-1000<=A[]<=1000
Example:
Input:
2
8
1 -1 3 2 -7 -5 11 6
8
-5 7 -3 -4 9 10 -1 11
Output:
1 3 2 11 6 -1 -7 -5
7 9 10 11 -5 -3 -4 -1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void negative_element(int [],int ) ;
int main()
{
int t,no,i ;
int arr[100] ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
negative_element(arr,no) ;
for(i=0;i<no;i++)
cout<<arr[i]<<" " ;
cout<<endl ;
}
return 0;
}
void negative_element(int arr[],int no)
{
int temp[no] ;
int i,k ;
k=0 ;
for(i=0;i<no;i++)
if(arr[i]>=0)
temp[k++]=arr[i] ;
for(i=0;i<no;i++)
if(arr[i]<0)
temp[k++]=arr[i] ;
for(i=0;i<no;i++)
arr[i]=temp[i] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment