The Famous Gauss You must know about Gauss, the famous mathematician. Back in late 1700’s, he was at elementary school. Gauss was asked to find the sum of the numbers from 1 to 100. The question was assigned as “busy work” by the teacher. He amazed his teacher with how quickly he found the sum of the integers from 1 to 100 to be 5050. Gauss recognized he had fifty pairs of numbers when he added the first and last number in the series, the second and second-last number in the series, and so on. For example: (1 + 100), (2 + 99), (3 + 98), ..., (50 + 51). Each pair has a sum of 101 and there are 50 pairs. History repeats itself. Jojo’s teacher assign a “busy work” to the students. The teacher believes that there will be no shortcut to finish this task in a minute. The teacher gives N integers A1, A2, ..., AN to the students. The teacher also gives Q questions. Each question contains two integers L and R asking the sum of all Ai where L <= Ai <= R. As a good friend of Jojo, help Jojo to amaze his teacher. Answer all the questions! Format Input There are T testcases. Every testcase consists of a line with an integers N followed by a line consists of N integers A1, A2, ..., AN as described above. Followed by a line consists of an integers Q and Q lines which each consists of two integers L and R as described above. Format Output Output T testcases with format “Case #X:”, where X indicates the testcase number and then followed by Q lines which each consists of an integers indicates the answer of each question. Constraints • 1 ≤ T ≤ 3 • 1 ≤ N, Q ≤ 30000 • 1 ≤ Ai, L, R ≤ 109 Sample Input (standard input) 3 6 3 2 1 3 5 1 3 3 3 2 3 1 5 5 4 5 6 7 8 7 4 4 4 5 4 6 4 7 4 8 4 9 3 8 5 11 12 13 14 15 3 1 10 16 20 1 20 Sample Output (standard output) Case #1: 6 8 15 Case #2: 4 9 15 22 30 30 30 Case #3: 0 0 65

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

                                              The Famous Gauss


You must know about Gauss, the famous mathematician. Back in late 1700’s, he was at elementary school. Gauss was asked to find the sum of the numbers from 1 to 100. The question was assigned as “busy work” by the teacher. He amazed his teacher with how quickly he found the sum of the integers from 1 to 100 to be 5050. Gauss recognized he had fifty pairs of numbers when he added the first and last number in the series, the second and second-last number in the series, and so on. For example:
(1 + 100), (2 + 99), (3 + 98), ..., (50 + 51). Each pair has a sum of 101 and there are 50 pairs. History repeats itself. Jojo’s teacher assign a “busy work” to the students. The teacher believes that there will be no shortcut to finish this task in a minute. The teacher gives N integers A1, A2, ..., AN to the students. The teacher also gives Q questions. Each question contains two integers L and R asking the sum of all Ai where L <= Ai <= R. As a good friend of Jojo, help Jojo to amaze his teacher. Answer all the questions!


Format Input
There are T testcases. Every testcase consists of a line with an integers N followed by a line consists of N integers A1, A2, ..., AN as described above. Followed by a line consists of an integers Q and Q lines which each consists of two integers L and R as described above.


Format Output
Output T testcases with format “Case #X:”, where X indicates the testcase number and then followed by Q lines which each consists of an integers indicates the answer of each question.


Constraints
• 1 ≤ T ≤ 3
• 1 ≤ N, Q ≤ 30000
• 1 ≤ Ai, L, R ≤ 109

Sample Input (standard input)
3
6
3 2 1 3 5 1
3
3 3
2 3
1 5
5
4 5 6 7 8
7
4 4
4 5
4 6
4 7
4 8
4 9
3 8
5
11 12 13 14 15
3
1 10
16 20
1 20


Sample Output (standard output)
Case #1:
6
8
15
Case #2:
4
9
15
22
30
30
30
Case #3:
0
0
65

Please use binary search and C language. Thx.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Introduction to computer system
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
  • SEE MORE 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