FastPrepHouse Robber II

House Robber II

Kotak Mahindra Bank logoKotak Mahindra Bank● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Houses stand in a circle. The nonnegative integer nums[i] is the value available in house i. Select houses with no two adjacent and return the maximum total value.

The first and last houses are adjacent. For this exercise, assume there is at least one house, selecting none is allowed, and a single house may be selected.

Function

robCircular(nums: int[]) → int

Examples

Example 1

nums = [2,3,2]return = 3

The two houses worth 2 are adjacent around the circle, so choose the middle house worth 3.

Example 2

nums = [1,2,3,1]return = 4

Choose values 1 and 3 at indices 0 and 2; they are not adjacent.

Example 3

nums = [5]return = 5

The only house may be selected.

Constraints

  • 1 <= nums.length <= 10^5.
  • 0 <= nums[i] <= 10^4.
  • The result fits in a signed 32-bit integer.

More Kotak Mahindra Bank problems

See Kotak Mahindra Bank hiring insights
public int robCircular(int[] nums) {
    // Write your code here.
}
nums[2,3,2]
expected3
Checking account…