# PROBLEM LINK

# DIFFICULTY

MEDIUM

# PREREQUISITES

Maths, Binary Search

# PROBLEM

You are given a list of N numbers, say A. Find the number of ranges (L, R) such that

- If C4 = number of 4s in A*, i = L to R, inclusive
- If C7 = number of 7s in A*, i = L to R, inclusive
- C4
^{C7}≤ R-L+1 - C4 ≠ 2 and C7 ≠ 2

# QUICK EXPLANATION

There are several tricks that we will use along the way to solve this problem.

Let, A4* be the number of 4s in A*.

Let, A7* be the number of 7s in A*.

Let, C4* be the number of 4s in A[ 1 … i ]. We set C4[0] = 0.

Let, C7* be the number of 7s in A[ 1 … i ]. We set C7[0] = 0.

In terms of sequences C4[] and C7[] the problem can be stated as follows: find all pairs (L; R) for which:

- 0 ≤ L < R ≤ N
- (C4[R] - C4[L])
^{C7[R] - C7[L]}≤ R - L - C4[R] - C4[L] ≠ 2
- C7[R] - C7[L] ≠ 2

The main idea is to fix differences C4[R] - C4[L] and C7[R] - C4[L].

We will use the following three methods to solve the problem. Note that all these methods are passed sorted arrays and can hence optimize the time complexity of the operations that they must perform.

- count1(A,v)
- Find the number of pairs (L, R) in A such that A[R]-A[L] = v

- count2(A,a,B,b)
- Find the number of pairs (L, R) such that
- a = A[R] - A[L]
- b = B[R] - B[L]
- L + a
^{b}≤ R

- count3(A,B)
- Find the number of pairs (L, R) such that
- A[R] - A[L] ≤ R - L
- B[R] - B[L] = 1

The answer can then be calculated using these methods.

We must use these methods for inclusion and exclusion of several pairs (L, R).

For a summary on how to use these methods and how they can be implemented, refer to the commented solution of the Tester. The method “smart” in the code refers to using count1, count2 and count3. The code has neat and clean definitions of the respective methods as well, but we will also be looking at them in detail, below.

# EXPLANATION

**count1(A,v)**

- Since A is sorted, we can iterate over L and maintain a range of values for R, within which A[R]-A[L] = v.
- As L increases, R1 can only increase, and hence, R2 can also only increase.

**count2(A,a,B,b)**

- Similar to count1, we iterate over L and maintain valid range of values for R.
- One set of values for the condition A[R]-A[L] = a
- Another set of values for the condition B[R]-B[L] = b

- We also want the minimum value of R to be L + a
^{b}- Hence we can find the closed interval within which there is a valid value of R, and add that range for each L.

**count3(A,B)**

This specifically handles the case when C7 = 1 and we want to find all the pairs such that C4[R]-C4[L] ≤ R - L.

- Similar to count1 and count2, we can iterate over L and try to find the range R, which satisfies the constraints.
- Since C7 = 1, we already have one insight
- For an L, R1 and R2 will be the next range that contains only one 7 in between L and R1 to R2.
- This means, that as soon as L becomes equal to R1, the next value of R1 will be R2 + 1, and next value of R2 will be the next 7, in C7 (or the end of the array).
- We will see why this is helpful below.

For a given range of R1 and R2, we still have to find R such that C4[R]-C4[L] ≤ R - L.

Everytime we make a selection of R1 and R2 (which is mutually exclusive from the previous selection), we can store the values C4[r]-r for each r between R1 and R2. This trick comes from the following rearrangement of our condition

- C4[R] - R ≤ C4[L] - L

Now, once we store all the values of C4[r] - r, and sort them, looking up the maxR for each L < R1 can be done via binary searching the matrix of C4[r]-r values.

**Complexity**

- The complexity of count1 is O(N).
- The complexity of count2 is O(N).
- The complexity of count3 is O(N log N).
- The number of iterations in considering each c4 and c7 where c4 ≥ 3, c7 ≥ 3 and c4
^{c7}≤ N is in fact very small: ~78 for N = 100000. But one can prove that complexity in this case is O(N * N^{1/3}).

# SETTERS SOLUTION

Can be found here.

# TESTERS SOLUTION

Can be found here.