AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
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 ?.
Follow @anyforumin
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 353
programming x 169
Posted On :
2016-12-17 14:30:37.0
Aarti Kumar
59
0
0
4
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
2 No
SudheerKumar Javvadi
0
96
0
Reply This Thread
2
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]+" ");
}
}
}
}
Rishi Kumar
523
1882
41268
Posted On :
2016-12-18 23:43:15.0
Leave a Comment
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in