id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

srp4379@lausd.net 1-arraybasics > getSmallest3_DifferentLengthsB
prev  |  next  |  chance

public int getSmallest3_DifferentLengthsB(int[] nums)

Given an int[] array whose length is anywhere from 0-3,
i.e. it has either zero, one, two or three elements,
return the smallest value.

This is the same kind problem as getSmallest2_DifferentLengths(),
but solved a bit differently.

As in that problem, there are different sections for arrays of different lengths.
The difference, however, is that they are NOT mutually exclusive.
More than one if statement will be executed, depending upon the length of the array.

public int getSmallest3_DifferentLengthsB(int[] nums) {
  int smallest = 0; // default if len == 0
  int len = nums.length;

  if (len >= 0) {
    smallest = 0;
  }

  if (len >= 1) {
    smallest = nums[0];
  }

  if (len >= 2) {
    smallest = Math.min( smallest, nums[1] );
  }

  if (len >= 3) {
    smallest = Math.min( smallest, nums[2] );
  }

  return smallest;
}
Note how what we call the flow of control works in this solution.

Arrays of all lengths will execute the statement(s) within the
block governed by if (len >= 0)

Arrays with lengths > 0 will execute the statement(s)
within the block governed by if (len >= 1)

Arrays with lengths > 1 will execute the statement(s)
within the block governed by if (len >= 2)

Arrays with lengths > 2 will execute the statement(s)
within the block governed by if (len >= 3)

Notice that the longer the array, the more sections it will execute,
in a cascading sort of fashion.

getSmallest3_DifferentLengthsB([]) → 0
getSmallest3_DifferentLengthsB([5]) → 5
getSmallest3_DifferentLengthsB([3, 6]) → 3

...Save, Compile, Run (ctrl-enter)

public int getSmallest3_DifferentLengthsB(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 1

Copyright Nick Parlante 2017 - privacy