Java > Array-2 >zeroMax (CodingBat Solulion)

Problem:

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}


Solution:

public int[] zeroMax(int[] nums) 
{
int max = 0;
for (int j =0; j < nums.length -1;j++)
{
if (nums[j] == 0)
{
for (int i = j + 1; i <=nums.length -1;i++)
{
if ( nums[i] > max && nums[i] % 2 == 1 )
max = nums[i];
}
nums[j] = max;
max = 0;
}
}
return nums;
}


11 comments :

  1. public static int[] sameEnds(int[] nums) {
    int max = 0;

    for (int i = nums.length-1; i >= 0; i--) {
    if (nums[i] % 2 != 0)
    max = Math.max(max, nums[i]);
    if (nums[i] == 0)
    nums[i] = max;
    }
    return nums;


    }

    }

    ReplyDelete
  2. more easy logic---
    public int[] zeroMax(int[] nums) {
    int max = 0;

    for (int i = nums.length - 1; i >= 0; i--) {
    if(nums[i]!=0 && nums[i]%2==1 && max<nums[i]){
    max=nums[i];
    }
    if(nums[i]==0){
    nums[i]=max;
    }
    }
    return nums;
    }

    ReplyDelete
  3. public int[] zeroMax(int[] nums) {
    int index = 0;

    for (int i = nums.length - 1; i >= 0; i--) {

    if (nums[i] % 2 != 0) {

    index = index < nums[i] ? nums[i] : index;

    }

    if (nums[i] == 0) {

    nums[i] = index;

    }

    }


    return nums;

    ReplyDelete
  4. public int[] zeroMax(int[] nums) {
    int x = 0;
    for (int i = 0; i < nums.length; i++){
    if (nums[i] == 0){
    for (int j = i; j < nums.length; j++){
    if (nums[j]%2 == 1){
    if (nums[j] > x){
    x = nums[j];
    }
    }
    }
    nums[i] = x;
    x = 0;
    }
    }
    return nums;
    }

    ReplyDelete
  5. public int[] zeroMax(int[] nums) {
    int max =0;
    for(int i=0 ; i < nums.length ; i++){
    if(nums[i]==0){
    for(int j=i+1 ; j < nums.length ; j++){
    if(nums[j]%2!=0 &&nums[j]>=max )max=nums[j]; // لو فردي واكبر من اكبر قيمه فردية
    }
    nums[i]=max; // حط أكبر قيمة فردية مكان الصفر
    max=0; // رجع اكبر قيمه فردية صفرتاني عشان انا معرفها بره اللوب فعشان متفضلش زي المرة القديمة
    }}
    return nums;
    }
    ----------------
    public int[] zeroMax(int[] nums) {
    int max = 0;
    for (int i = nums.length-1; i >= 0; i--) {
    if (nums[i] % 2 != 0)
    max = Math.max(max, nums[i]);
    if (nums[i] == 0)
    nums[i] = max;
    }
    return nums;

    }

    ReplyDelete
  6. public int[] zeroMax(int[] nums)
    {
    int counter = 0;
    for(int i = 0; i < nums.length - 1; i++)
    {
    if(nums[i] == 0)
    {
    for(int j = i + 1; j < nums.length; j++)
    {
    if(nums[j] % 2 != 0 && nums[j] > counter)
    {
    counter = nums[j];
    }
    }
    nums[i] = counter;
    counter = 0;
    }
    }
    return nums;
    }

    ReplyDelete
  7. public static int[] zeroMax(int[] ar ){
    int j=0,n=1;
    for(int i=0;i<ar.length;i++)
    {
    if(ar[i]==0)
    {

    if((ar[i+n])%2==0)
    { while((ar[i+n])%2==0)
    {

    n++;
    }
    }
    else
    {
    j=ar[i+n];
    }
    }
    else
    {
    continue;
    }
    ar[i]=j;
    }

    return ar;
    }

    ReplyDelete
  8. public int[] zeroMax(int[] nums) {
    int temp;
    for(int i=0;i<nums.length;i++){
    temp=0;
    if(nums[i]==0){
    for(int j=i+1;j<nums.length;j++){
    if(nums[j]%2==1){
    temp=(temp<nums[j])?nums[j]:temp;
    }
    }
    nums[i]=temp;
    }

    }

    return nums;
    }

    ReplyDelete
  9. public int[] zeroMax(int[] nums) {
    int odd=0;
    for(int i=nums.length-1;i>=0;i--){
    if(nums[i]!=0){
    if(nums[i]%2==1&&odd<nums[i]){
    odd=nums[i];
    }
    }
    if(nums[i]==0&&i!=nums.length-1){
    nums[i]=odd;
    }
    }
    return nums;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact