Online Occurrences

Company: Cisco_8th june_on campus

Difficulty: medium

Problem Statement

Online Occurrences Problem Description You are given an array a of length n , indexed from 1 to n . For a subarray a[l..r] (both ends included), its beauty beauty(l, r) is the largest positive integer x such that the value x occurs at least x times in a[l..r] . If no positive integer has this property, beauty(l, r) = 0 . You are also given q queries, each holding two integers l and r . The queries must be answered online : before answering the i -th query, let S be the sum of the answers of all earlier queries ( S = 0 for the first query) and decode the query as follows: l = (l + S) mod n + 1 r = (r + S) mod n + 1 if l > r, swap l and r The answer to the query is beauty(l, r) for the decoded l and r , and that answer is added to S before the next query is decoded. Find the sum of the answers to all q queries, modulo 10^9 + 7 . Complete the function get_ans(n, q, a, queries) , which receives the array size n , the number of queries q , the array a and the list of encoded queries, and