You are given an arrangement an of length n comprising of integers from 1 to n. The grouping may contain duplicates (for example a few components can be equal).    Track down the number of tuples of m=3 components

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Correct answer will be upvoted else Multiple Downvoted. Computer science.

You are given an arrangement an of length n comprising of integers from 1 to n. The grouping may contain duplicates (for example a few components can be equal). 

 

Track down the number of tuples of m=3 components with the end goal that the maximum number in the tuple varies from the base by close to k=2. Formally, you want to view as the number of triples of lists i<j<z with the end goal that 

 

max(ai,aj,az)−min(ai,aj,az)≤2. 

 

For example, on the off chance that n=4 and a=[1,2,4,3], there are two such triples (i=1,j=2,z=4 and i=2,j=3,z=4). In the event that n=4 and a=[1,1,1,1], all four potential triples are suitable. 

 

Input 

 

The principal line contains a solitary integer t (1≤t≤2⋅105) — the number of experiments. Then, at that point, t experiments follow. 

 

The principal line of each experiment contains an integer n (1≤n≤2⋅105) — the length of the succession a. 

 

The following line contains n integers a1,a2,… ,an (1≤ai≤n) — the grouping a. 

 

It is guaranteed that the amount of n for all experiments doesn't surpass 2⋅105. 

 

Output 

 

Output t answers to the given experiments. Each answer is the necessary number of triples of components, to such an extent that the maximum value in the triple varies from the base by close to 2. Note that in distinction to the hard form of the issue, you don't have to output the answer by modulo. You should output the exact value of the answer.

 

 

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Counting Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education