This page is still under construction.

Parts of this page are still being built. What you see may change.

Gold Balanced Lineup

Interview

Time limit1sMemory limit128 MB

Summary
Given N cows each with a K-bit feature ID, find the longest contiguous range where every one of the K features appears the same number of times.
Level

Medium7 of 10

Topics
Hash map, Prefix sum, Bit manipulation, Array
Solved
No attempts yet

Problem

Farmer John's NN cows (1≤N≤100,0001 \le N \le 100{,}000) share many traits. FJ has narrowed those traits down to a list of only KK distinct features (1≤K≤301 \le K \le 30). For example, cows with feature #1 might have spots, cows with feature #2 might prefer C over Pascal, and so on.

Each cow is described by a feature ID: a single KK-bit integer whose binary representation encodes exactly which features the cow has. Reading the binary digits from right (least significant) to left, a 11 in the 2i−12^{i-1} place means the cow exhibits feature ii. For example, a feature ID of 1313 is 11011101 in binary, so that cow exhibits features 11, 33, and 44, but not feature 22.

FJ lines the cows up in a row as cows 1…N1 \dots N and notices that some contiguous ranges are balanced. A contiguous range of cows i…ji \dots j is balanced when every one of the KK features is exhibited by the same number of cows inside that range. Determine the size (number of cows) of the largest balanced range.

Input

  • Line 11: two space-separated integers NN and KK.
  • Lines 2…N+12 \dots N+1: line i+1i+1 contains a single KK-bit integer, the feature ID of cow ii. Its least-significant bit is 11 if the cow exhibits feature #1, and its most-significant bit is 11 if the cow exhibits feature #KK.

Output

  • A single integer: the number of cows in the largest contiguous balanced range. If no non-empty range is balanced, output 00.

Hint

The row has 77 cows with 33 features. The table below shows the correspondence:

             Feature 3:   1   1   1   0   0   1   0
             Feature 2:   1   1   1   1   0   0   1
             Feature 1:   1   0   1   0   1   0   0
             Key:         7   6   7   2   1   4   2
             Cow #:       1   2   3   4   5   6   7

In the range from cow #3 to cow #6 (size 44), each feature is exhibited by exactly 22 cows:

             Feature 3:     1   0   0   1  -> two total
             Feature 2:     1   1   0   0  -> two total
             Feature 1:     1   0   1   0  -> two total
             Key:           7   2   1   4
             Cow #:         3   4   5   6

Examples1

  1. Example 1

    Input
    7 3
    7
    6
    7
    2
    1
    4
    2
    
    Expected output
    4