Array and Queries

Problem Statement :

Given an array, you are asked to perform a number of queries and divide the array into what are called, beautiful subsequences.

The array A has length n. A function f(A)  is defined to be a minimal possible , such that it's possible to divide array A into x  beautiful subsequences. Note that each element of an array should belong to exactly one subsequence, and subsequence does not necessarily need to be consecutive.

A subsequence S with length len  is called beautiful if and only if:

len = 1

Let S'  be a sorted version of S. It must hold that  for every 

You need to find  modulo .

Input Format

The first line contains a single integer , representing the length of array .
The next line contains the array  given as space-separated integers.
The next line contains a single integer , representing the number of queries.
Each of the  lines contain two integers  and , which is described above.

Output Format

Print the required answer in one line.

Sample Input 0

2 2 1 1 1
3 2
5 5

Sample Output 0


Solution :


                        Solution in C++ :

special credits to  : 

Tara Mehta

#include <bits/stdc++.h>
using namespace std;
long int mod=1000000007 ;
int main(){
    int n;
    multiset<int> set1;
    int arr[n];
    for(int i=0;i<n;i++){
    multiset<int> set2=set1;
    int count=0;
        int cur=*(set2.begin());
   long long int q;
    long long int ans=0;
    for(long long int i=0;i<q;i++){
        int id,val;
        int curr=set1.count(arr[id]);
        int prev=set1.count(arr[id]-1);
          int next=set1.count(arr[id]+1);
          if(prev==0 && next==0)
          else if(curr>max(prev,next))
          else if(curr<=min(prev,next))
         if(prev==0 && next==0)
           else if((curr+1)>max(prev,next))
           else if((curr+1)<=min(prev,next))
    return 0;

View More Similar Problems

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →