Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

optimal solution for following scenario using java?

integer array has given. Even numbers should in even indexes and odd numbers should in odd indexes. you have to check whether given array is satisfied that condition.

my implementation is here....

public void isSatisfied(int [] arr){

    for(int i=0;i<arr.length;i++){  

        int r_val=arr[i]%2;
        int r_index=i%2;

        if((r_val==1)&&(r_index==1)){

            if(i==arr.length-1){
                System.out.println("yes");
            }
            continue;
        }
        else if((r_val==0)&&(r_index==0)){
            if(i==arr.length-1){
                System.out.println("yes");
            }
            continue;
        }
        else{
            System.out.println("no");
            break;
        }
    }
}

what will be the best implementation?

Upvotes: 1

Views: 132

Answers (5)

bellum
bellum

Reputation: 3710

public void isSatisfied(int [] arr){

    for(int i=0;i<arr.length;i++){  

        int r_val=arr[i]%2;
        int r_index=i%2;

        if(r_val!=r_index){
            System.out.println("no");
            return;
        }
    }
    System.out.println("yes");
}

Upvotes: 1

Juvanis
Juvanis

Reputation: 25950

The sum of a particular index and the value at that index must be even, otherwise the array doesn't satisfy your condition:

public boolean isSatisfied(int[] arr) 
{
    for (int i = 0; i < arr.length; i++) 
    {
        if ((i + arr[i]) % 2 != 0)
            return false;
    }
    return true;
}

Upvotes: 4

mtk
mtk

Reputation: 13709

This is just a little modified version of your code. package com.mtk;

public class ArrayTest {

    public static void main(String[] args) {
        int[] arr = {2,3,4,5,6};        
        isSatisfied(arr);
    }

    private static void isSatisfied(int[] arr) {
        // TODO Auto-generated method stub
        for (int i = 0; i < arr.length; i++) {
            if( i%2 == 0 && arr[i]%2 == 0) ; // do nothing
            else if (i%2 == 1 && arr[i]%2 == 1) ; // do nothing
            else {
                System.out.println("No");
                return;
            }
        }
        System.out.println("Yes");
    }
}

Upvotes: 0

Gabriel Negut
Gabriel Negut

Reputation: 13960

It makes more sense to have a method named isX return boolean.

public boolean isSatisfied(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        if (i % 2 == 0 && arr[i] % 2 != 0)
            return false;
        if (i % 2 == 1 && arr[i] % 2 == 0)
            return false;
    }
    return true;
}

Upvotes: 0

SJuan76
SJuan76

Reputation: 24780

"Best" is often a quetion of opinion. Your code is good enough, here are a few tips:

1) Don't mix logic with presentation. In your loop, save the state (array is correct or not) to a boolean variable. After the loop, print whatever you want based in the variable (and so you won't have several "yes" repeated (and followed maybe by a "no"), which I would find confusing.

2) If you use the if-else-if construct, the continues are not needed. Use one form of the other to improve readability.

Upvotes: 1

Related Questions