Non-Overlapping Pairs of Sublists - Google Top Interview Questions
Problem Statement :
Given a list of integers nums and a positive integer k, return the number of pairs of non-overlapping sublists such that all the elements in each sublist have value greater than or equal to k. Mod the result by 10 ** 9 + 7. Constraints n ≤ 100,000 where n is the length of nums 1 ≤ k Example 1 Input nums = [3, 4, 4, 9] k = 4 Output 5 Explanation These are the pairs of sublists: [4], [9] [4], [9] (using other 4) [4], [4] [4, 4], [9] [4], [4, 9]
Solution :
Solution in C++ :
const int MOD = 1000000007;
long long self(int len) {
long long ret = 0;
for (int i = 0; i < len; i++) {
long long lhs = i + 1;
long long leftover = len - 1 - i;
long long rhs = leftover * (leftover + 1) / 2;
ret += lhs * rhs;
ret %= MOD;
}
return ret;
}
int solve(vector<int>& nums, int k) {
vector<int> lens;
long long ret = 0;
for (int i = 0; i < nums.size();) {
if (nums[i] < k) {
i++;
continue;
}
int j = i + 1;
while (j < nums.size() && nums[j] >= k) j++;
lens.push_back(j - i);
i = j;
}
for (int out : lens) {
ret += self(out);
}
long long inc = 0;
for (int len : lens) {
long long currentOption = (len * (len + 1LL)) / 2;
currentOption %= MOD;
long long cand = inc;
cand *= currentOption;
ret += cand;
ret %= MOD;
inc += currentOption;
inc %= MOD;
}
return ret % MOD;
}
Solution in Java :
import java.util.*;
class Solution {
public int solve(int[] nums, int k) {
long MOD = 1000000007L;
ArrayList<Long> arr = new ArrayList<Long>();
long cur = 0;
long sum = 0;
for (int n : nums) {
if (n >= k) {
cur++;
} else {
if (cur > 0)
arr.add(cur);
sum = (sum + cur * (cur + 1) / 2) % MOD;
cur = 0;
}
}
if (cur > 0)
arr.add(cur);
sum = (sum + cur * (cur + 1) / 2) % MOD;
long ans = 0;
// the sublists come from different runs of the array
for (long n : arr) {
long d = (n * (n + 1) / 2) % MOD;
sum = (sum - d + MOD) % MOD;
ans = (ans + d * sum) % MOD;
}
// the sublists come from the same run of the array
for (long n : arr) {
if (n == 1)
continue;
// using stars and bars, the formula for size n is (n+2)C4.
long num = 1L;
for (int i = -1; i <= 2; i++) num = (num * (n + i)) % MOD;
long denInv = 41666667L; // mod inverse of 24
ans = (ans + num * denInv) % MOD;
}
return (int) ans;
}
}
Solution in Python :
class Solution:
def solve(self, nums, k):
MOD = 10 ** 9 + 7
n = len(nums)
suffix = [0] * (n + 1)
end = n - 1
for start in range(n - 1, -1, -1):
num = nums[start]
if num < k:
end = start - 1
length = end - start + 1
suffix[start] += suffix[start + 1] + length
suffix[start] %= MOD
res = 0
start = 0
for end in range(n):
num = nums[end]
if num < k:
start = end + 1
length = end - start + 1
res += length * suffix[end + 1]
res %= MOD
return res
View More Similar Problems
Print the Elements of a Linked List
This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode
View Solution →Insert a Node at the Tail of a Linked List
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink
View Solution →Insert a Node at the head of a Linked List
Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below
View Solution →Insert a node at a specific position in a linked list
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e
View Solution →Delete a Node
Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo
View Solution →Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →