Odd one Out

Alice and Bob are getting bored so they decided to play a game.

Alice has n cards having the first n odd numbers written on them. He removes one of the cards at random and hands the remaining n-1 cards to Bob. Help Bob to find the value of the card Alice has removed.

Example:

Input:  noOfCards=5, arrOddNum[] = {3,1,9,5,0};
Output: 7

Approach

Java

import java.util.Arrays;

public class OddOneOut {

    public static void main(String args[] ) throws Exception {
       int noOfCards =5;
       int arrOddNum[] = {3,1,9,5,0};
       Arrays.sort(arrOddNum);
       int counter=1;
       for(int i=1 ; i<noOfCards; i++){
            if(counter==arrOddNum[i])
                counter +=2;  
            else
               break;
                   
       } 
       System.out.println(counter);
              
    }
}


No comments:

Post a Comment