简体   繁体   中英

Determine if an array of numbers can divided into a set of k consecutive numbers

Given some array nums and a positive integer k, determine if its possible to divide this array into sets of k consecutive numbers.

Example:

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

Output true since [1,2], [3, 4]

My thoughts is that that size of the array nums has to be divisible by the integer k. But when I use that as the test I fail for this test case:

[15,16,17,18,19,16,17,18,19,20,6,7,8,9,10,3,4,5,6,20] k = 5

I get true but the answer is false and I am not sure why. Any ideas?

Here is my code:

int n = nums.size();
if(n % k == 0)
    return true;

return false;

Here are more examples if that helps:

在此处输入图片说明

The problem can be solved by sorting the array, counting duplicates, and then verifying the consecutive sequences.

Consider example 2, where k=3 and the array is

[3,2,1,2,3,4,3,4,5,9,10,11]

After sorting:

[1,2,2,3,3,3,4,4,5,9,10,11]

After counting duplicates (the top line has the unique numbers in the array, the bottom line has the duplicate count for each number):

1 2 3 4 5 9 10 11
1 2 3 2 1 1  1  1

Now check the sequences. The lowest number is 1, so the sequence [1,2,3] must exist in the array, or the output is false. 1, 2, and 3 all have non-zero counts, so the array does contain that sequence. Update the counts to remove that sequence:

1 2 3 4 5 9 10 11
0 1 2 2 1 1  1  1

Now 2 is the lowest number with a non-zero count, so the next sequence is [2,3,4] and the updated counts are:

1 2 3 4 5 9 10 11
0 0 1 1 1 1  1  1

Finish up with [3,4,5] and [9,10,11]

Solution:

step1. Use TreeMap to store array elements and their occurrence. Treemap helps us to store the element in sorting orders.

step2. Iterate over treemap until it's not empty.

step3. Takeout the first key (firstKey) from treemap and start searching next K consecutive element.

public static boolean isPossibleDivide(int[] arr, int k) {

    TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    for (int i = 0; i < arr.length; i++) {
        map.put(arr[i], map.get(arr[i]) != null ? map.get(arr[i]) + 1 : 1);
    }
    while (!map.isEmpty()) {
        int firstKey = map.firstKey();

        for (int i = 0; i < k; i++) {

            int key = firstKey + i;

            if (!map.containsKey(key)) {
                return false;
            } else {
                map.put(key, map.get(key) - 1);

                if (map.get(key) == 0)
                    map.remove(key);
            }
        }
    }
    return true;
}
bool isPossibleDivide(vector<int>& nums, int k) {
    int n = nums.size();
    if(n%k != 0)return false;
    sort(nums.begin(),nums.end());
    map<int,int> m;
    for(int i=0;i<n;i++){
        m[nums[i]]++;
    }
    int nu = m.size() - k + 1;
    for(auto it=m.begin();it!=m.end();it++){
        if(!nu)break;
        int x = it->second;
        int l=0;
        map<int,int> :: iterator s = it;
        map<int,int> :: iterator s1 = s ;
        while(l<k){
            s->second = s->second - x;
            s++;
            if(s->first - s1->first !=1 && l<k-1)return false;
            s1++;
            l++;
        }
        nu--;
    }
    /*for(auto it=m.begin();it!=m.end();it++){
        cout << it->first <<" "<<it->second <<endl;
    }*/
    for(auto it=m.begin();it!=m.end();it++){
        if(it->second != 0) return false;
    }
    return true;
}

I have done this, but I don't know why it is not working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM