Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Write a program to sort the odd elements descending order and even elements in ascending order
/* Write a program to sort the odd elements descending order and even elements in ascending order Eg 1: Input: 13,2 4,15,12,10,5 Output: 13,2,12,10,5,15,4 Eg 2: Input: 1,2,3,4,5,6,7,8,9 Output: 9,2,7,4,5,6,3,8,1 */ import java.util.*; import java.lang.*; import java.util.Scanner; class Rextester { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.println("Enter the number of inputs :"); int inputCount=input.nextInt(); int odd[]=new int[100]; int even[]=new int[100]; int oddit=0,evenit=0; for(int i=0;i<inputCount;i++) { if(i%2!=0) { even[evenit]=input.nextInt(); evenit++; } else { odd[oddit]=input.nextInt(); oddit++; } } /* for(int i=0;i<oddit;i++) System.out.println(odd[i]); */ //Descending odd elements for(int i=0;i<oddit-1;i++) { for(int j=i+1;j<oddit;j++) { if(odd[i]<odd[j]) { int temp=odd[i]; odd[i]=odd[j]; odd[j]=temp; } } } //Ascending even elements for(int i=0;i<evenit-1;i++) { for(int j=i+1;j<evenit;j++) { if(even[i]>even[j]) { int temp=even[i]; even[i]=even[j]; even[j]=temp; } } } int limit=oddit>evenit?oddit:evenit; for(int i=0;i<limit;i++) { if(i<oddit) System.out.print(" "+odd[i]); if(i<evenit) System.out.print(" "+even[i]); } } }
run
|
edit
|
history
|
help
0
PE #9
om using stars
tilt
jb6.13 varargs
Stack implementation
postincrement2
Airbnb-QueueLinkedList-2/3
JAVA # Klavyeden Girilen cümlelerin en uzununu bulma
print alphabets using stars
Problem: Single_digit