Statistics

Problem Statement for "Coherence"

Problem Statement

We want to display a two-color pattern on a rectangular array of pixels, with k of the pixels in the foreground color. We want to choose the pattern so that it minimizes the boundary between the foreground and the background.

The length of the boundary is the number of vertical and horizontal pixel edges that separate a foreground pixel from a background pixel. For example, this picture shows a rectangular array with three rows and six columns that has 5 foreground pixels (indicated by 'X'). The boundary in this case has length equal to 6: the upper left X is adjacent to 1 background pixel, the upper middle X is adjacent to 1, the rightmost X is adjacent to 3, the lower left X is adjacent to 0, and the lower right X is adjacent to 1.

   - - - - - -
   X X X - - -
   X X - - - - 

Create a class Coherence that contains the method minBndry that takes three int inputs, numRows (the height of the array), numCols (the width of the array), and k (the number of foreground pixels), and returns the length of the minimum possible boundary.

Definition

Class:
Coherence
Method:
minBndry
Parameters:
int, int, int
Returns:
int
Method signature:
int minBndry(int numRows, int numCols, int k)
(be sure your method is public)

Constraints

  • numRows is between 1 and 30 inclusive
  • numCols is between 1 and 30 inclusive
  • k is between 0 and numRows*numCols inclusive

Examples

  1. 6

    6

    5

    Returns: 5

    X X X - - - X X - - - - - - - - - - - - - - - - - - - - - - - - - - - - The upper right foreground pixel has 2 boundary edges, the bottom right one has 2 boundary edges, and the bottom left one has 1 boundary edge.

  2. 4

    6

    16

    Returns: 4

    X X X X - - X X X X - - X X X X - - X X X X - -

  3. 9

    5

    0

    Returns: 0

  4. 29

    3

    87

    Returns: 0

  5. 28

    22

    145

    Returns: 23

  6. 30

    30

    156

    Returns: 25

  7. 15

    16

    157

    Returns: 16

  8. 27

    28

    157

    Returns: 26

  9. 5

    5

    6

    Returns: 5

  10. 7

    6

    36

    Returns: 5

  11. 7

    6

    6

    Returns: 5

  12. 30

    30

    895

    Returns: 5

  13. 10

    10

    90

    Returns: 7

  14. 1

    1

    1

    Returns: 0

  15. 30

    30

    870

    Returns: 11

  16. 30

    30

    24

    Returns: 10

  17. 30

    30

    36

    Returns: 12

  18. 30

    30

    884

    Returns: 8

  19. 5

    9

    5

    Returns: 5

  20. 4

    4

    16

    Returns: 0

  21. 28

    29

    666

    Returns: 25

  22. 30

    30

    841

    Returns: 16

  23. 30

    30

    31

    Returns: 12

  24. 6

    6

    25

    Returns: 7

  25. 5

    5

    2

    Returns: 3

  26. 5

    5

    23

    Returns: 3

  27. 6

    10

    56

    Returns: 4

  28. 20

    25

    437

    Returns: 16

  29. 5

    2

    4

    Returns: 2

  30. 2

    2

    4

    Returns: 0

  31. 3

    9

    11

    Returns: 4


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: