Solved

zeroMAx challenge

Posted on 2016-08-14
20
102 Views
Last Modified: 2016-08-22
Hi,

I am working on below challenge

http://codingbat.com/prob/p187050

My psuedo code of the logic is
1. loop through given array
2. find occurence of zero as long as not a last elemet
3. find the biggest odd after occurence of zero
4 replace i th element with biggest odd.
5 return array


I wrote my code as below
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;
}

Open in new window



To find max odd
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);
    }
}

Open in new window



I am failing tests

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 10
  • 7
  • 2
  • +1
20 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41756459
What tests are failing?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756534
none of the tests passing. i still need to refine my code
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41756568
OK
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 27

Expert Comment

by:d-glitch
ID: 41756665
You need to work some examples by hand and refine your pseudo code first.
1
 
LVL 35

Accepted Solution

by:
mccarl earned 250 total points
ID: 41759331
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759940
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
how to find max element from remaining array?
what happens if i see one other 0? how to handle that scenario?
0
 
LVL 27

Assisted Solution

by:d-glitch
d-glitch earned 250 total points
ID: 41759952
each zero value in the array is replaced by the largest odd value to the right

If the first loop finds a zero, the second loop looks to the right to find the largest odd number to replace the zero.
If you find a zero with the second loop, you ignore it because you are only looking for odd numbers.
If there are more zeros in the array, the first loop will find them eventually.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759966
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;

	}

Open in new window

Expected      Run            
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
i passed all tests with one 0. How pass tests with more than one 0?
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;

	}

}

Open in new window

value is[5, 5, 3, 3]
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759972
i think i have to tweak my incrementer to go to next 0 somehow like i=i+i like that
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759977
oops i need to find largest odd number not lagest
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759982
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;

	}

Open in new window


above passed all tests. any improvements, modifications, optimization, refactoring of my code? please advise
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41760004
You can do the challenge much more efficiently (with one loop) by working right to left, keeping track of the largest odd integer, and replacing 0's as you go.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760011
keeping track of the largest odd integer
how?
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41760020
Just like you did here:
if(nums[j]>largest&&nums[j]%2==1){
     largest=nums[j];

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41760088
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;

	}

Open in new window

something like above?
failing some tests?
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41760112
Something like that.  But you need to establish the value for largest odd first.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760168
But you need to establish the value for largest odd first.
i thought i did as below?
int largest=nums[j];
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41760177
largest has no meaning until you find an odd integer.
But you can give it a clever initial value that allows it to work as a flag and simplify your program.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41764739
not sure on your approach. can you provide the your approach code?
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41765376
Pseudo code:
maxodd = 0
loop backwards through the array
     if nums[i] is odd  &&  nums[i] > maxodd
          then  maxodd = nums[i]
     if nums[i] = 0
          then nums[i] = maxodd

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 63
Advice in Xamarin 21 79
Configure a Bean in an XML file 4 32
How to concatenate fields in zpl, and how to use conditional statement? 3 27
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question