+ 1
can anyone write a java program to sort alternative positive and negative numbers
the give input has to be in array and the output has to be stored in another array .
8 Answers
+ 4
-2 is greater than -8 so, you are trying to sort using absolute value, right?
And
another question:
is there always the no.of positive ints == no.of negative ints ?
for eg. in the example you wrote
2 +ve ints and 2 -ve ints.
+ 4
Tanay i found a question asked that was similar to this one
https://stackoverflow.com/questions/26304587/alternate-positive-and-negative-numbers-in-an-array
+ 2
I don't get what you are trying to do.
can you post the sample for input and output?
+ 2
Bharathi Priya
finally got some time to write a code for this problem.
check this out:
https://code.sololearn.com/cz5qYg48PVLl/?ref=app
+ 1
Can you please post your attempt(s) at coding this?
+ 1
yes
+ 1
import java.util.Scanner;
class nonrpt{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[100];
int c=0;
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]<0)
{
c=1;
}
else
{
c=0;
break;
}
}
}
if(c==1)
{
for (i=0;i<n;i=i+2)
System.out.println("nos:"+a[i]);
}
if(c==1)
{
for (i=1;i<n;i=i+2)
System.out.println("nos:"+a[i]);
}
}
}
}
+ 1
same input
input []={1,6,-2,-8}
output []={-2,1,-8,6}