public int[] zeroMax(int[] nums) {
int i, smallEven=0, bigOdd=0, countOdd=0, countEven=0;
for(i=0; i<n; i++){
// System.out.print("Enter element "+(i+1));
nums[i]=sc.nextInt();
if(a[i]%2 == 1){
if(countOdd>0 && a[i]>bigOdd) bigOdd=a[i];
else if(countOdd==0) bigOdd=a[i];
countOdd++;
}
for(int i=0;i<nums.length-1;i++){
if( (nums[i]%2)==1 && i!=nums.len ) {
nums[i]=
}
}
return nums;
}
import java.util.Scanner;
public class ZeroMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] a = new int[n];
int i, smallEven=0, bigOdd=0, countOdd=0, countEven=0;
for(i=0; i<n; i++){
System.out.print("Enter element "+(i+1));
a[i]=sc.nextInt();
if(a[i]%2 == 1){
if(countOdd>0 && a[i]>bigOdd) bigOdd=a[i];
else if(countOdd==0) bigOdd=a[i];
countOdd++;
}
else{
if(countEven>0 && a[i]<smallEven) smallEven=a[i];
else if(countEven==0)smallEven=a[i];
countEven++;
}
}
System.out.println("The smallest even number is " + smallEven);
System.out.println("The largest odd number is " + bigOdd);
}
}
to find the max you need to look at each element from the one after the i-th element to the end of the arrayhow to find max element from remaining array?
each zero value in the array is replaced by the largest odd value to the right
public int[] zeroMax(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
if (nums[i] == 0) {
int largest=nums[i];
for (int j = i; j < nums.length; j++) {
if(nums[j]>largest){
largest=nums[j];
}
if(largest>0){
nums[i]=largest;
}
}
}
}
}
return nums;
}
Expected Runi passed all tests with one 0. How pass tests with more than one 0?
zeroMax([0, 5, 0, 3]) â†’ [5, 5, 3, 3] [5, 5, 3, 3] OK
zeroMax([0, 4, 0, 3]) â†’ [3, 4, 3, 3] [4, 4, 3, 3] X
zeroMax([0, 1, 0]) â†’ [1, 1, 0] [1, 1, 0] OK
zeroMax([0, 1, 5]) â†’ [5, 1, 5] [5, 1, 5] OK
zeroMax([0, 2, 0]) â†’ [0, 2, 0] [2, 2, 0] X
zeroMax([1]) â†’ [1] [1] OK
zeroMax([0]) â†’ [0] [0] OK
zeroMax([]) â†’ [] [] OK
zeroMax([7, 0, 4, 3, 0, 2]) â†’ [7, 3, 4, 3, 0, 2] [7, 4, 4, 3, 2, 2] X
zeroMax([7, 0, 4, 3, 0, 1]) â†’ [7, 3, 4, 3, 1, 1] [7, 4, 4, 3, 1, 1] X
zeroMax([7, 0, 4, 3, 0, 0]) â†’ [7, 3, 4, 3, 0, 0] [7, 4, 4, 3, 0, 0] X
zeroMax([7, 0, 1, 0, 0, 7]) â†’ [7, 7, 1, 7, 7, 7] [7, 7, 1, 7, 7, 7] OK
other tests
X
import java.util.Arrays;
import java.util.Scanner;
/*There's nothing particularly wrong with your pseudo-code it is
* just point 3 that needs to be expanded, ie. Now you know the
* i-th element is zero, HOW do you find the biggest odd number
* after the i-th element.
Hint: to find the max you need to look at each element from the
one after the i-th element to the end of the array, ie. you need
a second loop. This will be within the first, main loop.*/
public class ZeroMax2 {
public static void main(String[] args) {
int[] ar = { 0, 5, 0, 3 };
System.out.println("value is" + Arrays.toString(zeroMax(ar)));
/*
* Return a version of 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.
*
* zeroMax([0, 5, 0, 3]) â†’ [5, 5, 3, 3] zeroMax([0, 4, 0, 3]) â†’ [3, 4,
* 3, 3] zeroMax([0, 1, 0]) â†’ [1, 1, 0]
*
* Hint: to find the max you need to look at each element from the one
* after the i-th element to the end of the array, ie. you need a second
* loop. This will be within the first, main loop.
*/
}
public static int[] zeroMax(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
if (nums[i] == 0) {
int largest=nums[i];
for (int j = i; j < nums.length; j++) {
if(nums[j]>largest){
largest=nums[j];
}
if(largest>0){
nums[i]=largest;
}
}
}
}
}
return nums;
}
}
value is[5, 5, 3, 3]
public int[] zeroMax(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
if (nums[i] == 0) {
int largest=nums[i];
for (int j = i; j < nums.length; j++) {
if(nums[j]>largest&&nums[j]%2==1){
largest=nums[j];
}
if(largest>0){
nums[i]=largest;
}
}
}
}
}
return nums;
}
public int[] zeroMax(int[] nums) {
for (int j = nums.length-1; j >=0; j--) {
if(nums[j]==0){
int largest=nums[j];
if(nums[j-1]>largest&&nums[j]%2==1){
largest=nums[j-1];
}
if(largest>0){
nums[0]=largest;
}
}
}
//i=i+i;
return nums;
}
something like above?But you need to establish the value for largest odd first.i thought i did as below?
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
27 Experts available now in Live!