AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Java Program-Zero Max

Write a program to rearrange the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array. If there is no odd value to the right of the zero, leave the zero as a zero Input Specification line 1 array size line 2 array elements Output Specification line 1 Rearranged array Input: 4 0 5 0 3 Output: 5 5 3 3

corejava x 352
programming x 168
Posted On : 2016-12-17 14:30:37.0
profile Aarti Kumar - anyforum.in Aarti Kumar
5900
up-rate
4
down-rate

Answers


Check and let me know if your conditions satisfied or not.

import java.util.*;
class Rearrange
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int N[]=new int[n];
for(int i=0;i<n;i++)
{
N[i]=s.nextInt();
}
for(int i=0;i<n;i++)
{
int temp=0;
if(N[i]==0)
{
for(int j=i;j<n;j++)
{
if(N[j]%2!=0 && N[j]>temp)
{
temp =N[j];
}
}
N[i]=temp;
System.out.print(N[i]+" ");
}
else
{
System.out.print(N[i]+" ");
}
}
}
}

Posted On : 2016-12-18 19:08:00
Satisfied : 1 Yes  1 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
2
down-rate
Comments
I´d like to make few changes in above code just to make the code more reliable.

import java.util.*;
class ZeroMax
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
s.nextLine();
String S[]=s.nextLine().split(" ");
Integer N[]=new Integer[S.length];
for(int i=0;i<S.length;i++){
N[i]=Integer.parseInt(S[i]);
}
for(int i=0;i<n;i++)
{
int temp=0;
if(N[i]==0)
{
for(int j=i;j<n;j++)
{
if(N[j]%2!=0 && N[j]>temp)
{
temp =N[j];
}
}
N[i]=temp;
System.out.print(N[i]+" ");
}
else
{
System.out.print(N[i]+" ");
}
}
}
}
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  21961
Posted On :2016-12-18 23:43:15.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in