Expedia SDE1 | Array Generator | Binary Game

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-10-22Added By Anonymous
Source: Link
Expedia SDE1 | Array Generator | Binary Game

A professor in the Computer Science department of HackerLand College wants to generate an array. Given an array of integers of length and two integers/ and find another array, br, such that:

* l<=brr[i]<=r
* brr[i] - arr[i] < brr[i+1] - arr[i+1]
* brr[i] <= brr[i+1]
*
Among all such arrays, return the lexicographically smallest one if there in not an array that satisfies the conditions then return an array with the single element -1.

vector<int> getSmallestArray(vector<int> arr, int l, int r){
    int lastDiff = INT_MIN, currDiff = 0;
    res[0] = l;
    for(int i=0;i<arr.size();i++){
        lastDiff = res[i-1] - arr[i-1];
        currDiff = lastDiff+1;
        int curr = currDiff + arr[i];
        curr = max(curr, res[i-1]);
        res[i] = curr;
        if(curr>r)  return {-1};
    }
    return res;
}

A binary string is a string consisting only of digits 0 and 1. A binary string is said to be good if:
* The digit 1 only appears in groups of size one group, if it appears at all. For example, for one group = 2, "011110110" satisfies this condition while "01101010" does not.
* The digit 0 only appears in groups of size zero_group, if it appears at all. For example, for zero group = 3, "11" satisfies this condition while "101010" does not. For example, for one group = 2, zero group = 1, the strings "011", "000" are good while the strings "001" and "111" are not.

Given four integers:
* min_length: min length of a valid string
* max_length: max length of a valid string
* one_group: group size of digit 1
* zero_group: group size of digit 0

min length = 1, max length = 3, one group = 2 and zero group = 1. The good strings satisfying the criteria are: (“0”, “00”, "11", "011", "110", "000"]. Hence, the result is 6.

int countGoodStrings(int min_length, int max_length, int one_group, int zero_group){
    vector<long long int> dp(max_length+1, 0);
    long long int res = 0;
    dp[0] = 1;
    for(int i=1;i<= max_length;i++){
        if(i>=zero_group){
            dp[i] += dp[i-zero_group];
        }
        if(i>=one_group){
            dp[i] += dp[i-one_group];
        }
        dp[i] %= (1000000007);
    }
    for(int i=min_length; i<= max_length; i++){
        res += dp[i] % (1000000007);
    }
    
    return (int)(res % (1000000007));
}
Comments