Answered step by step
Verified Expert Solution
Question
1 Approved Answer
QUESTION 77 0 GROUP Language Coding SECTION Coding Biven an array of Nintegers and another integer M, write a program to find S, which is
QUESTION 77 0 GROUP Language Coding SECTION Coding Biven an array of Nintegers and another integer M, write a program to find S, which is the sum of elements of all subarrays of N made a Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings anywhere in the program, as these contribute Constraints Number of elements, N>= 1 I) MEN Input Format: The first line of input contains an integer N, the size of the given array. The second line of input contains N integers each separated by a single white space, which are elements of the array. The third line of input contains an integer M, the size of consecutive subarrays. Output Format: The output should contain the sum S. Sample Input 1: 8 1 2 3 4 5 6 7 8 3 Sample Output 1: 81 Explanation 1: Array size, N=8 Array = {1,2,3,4,5,6,7,8) M=3 Subarrays of size 3 made of consecutive elements are {1,2,3}, {2,3,4}, {3,4,5}, {4,5,6}, {5,6,7}, {6,7,8). Sum of all elements of all subarrays are: 1 + 2 + 3 = 6 2 + 3 + 4 = 9 3+ 4+ 5 = 12 4+ 5+ 6 - 15 Sample Output 1: 81 Explanation 1: Array size, N=8 Array = {1,2,3,4,5,6,7,8} M= 3 Subarrays of size 3 made of consecutive elements are {1,2,3}, {2,3,4}, {3,4,5}, {4,5,6}, {5,6,7}, {6,7,8). Sum of all elements of all subarrays are: 1 + 2 + 3 = 6 2 + 3 + 4 = 9 3 +4 +5 = 12 4 + 5 + 6 = 15 5 + 6 +7 = 18 6 + 7 + 8 = 21 S = 6+9+12+15+18+21 = 81 which is printed as output. Sample Input 2: 6 10 20 30 40 50 60 4 Sample Output 2: 420 Explanation 2: N = 6. A = {10,20,30,40,50,60} M=4 76 M= 3 Subarrays of size 3 made of consecutive elements are {1,2,3), (2,3,4}, {3,4,5}, {4,5,6), (5,6,7), (6,7,8). Sum of all elements of all subarrays are: 1 + 2 + 3 = 6 2 +3 +4 = 9 3 +4 +5 = 12 4 +5 +6 = 15 5 + 6 +7 = 18 6 + 7 + 8 = 21 S=6+9+12+15+18+21 = 81 which is printed as output. Sample Input 2: 6 10 20 30 40 50 60 4 Sample Output 2: 420 Explanation 2: N = 6. A = {10,20,30,40,50,60} M = 4. Sum of all elements of subarrays of size 4: 10+20+30+40 = 100 20+30+40+50 = 140 30+40+50+60 = 180 S = 100+140+180 = 420 which is printed as output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started