Question

use c ++ Description Find the maximum value of an array with length N after X...

use c ++
Description
Find the maximum value of an array with length N after X operations. The initial value of all elements of the array is 0

Constraint
10 <= N <= 10 ^ 6

10 <= X <= 10 ^ 6

10 <= array [N] <= 10 ^ 6

Example
Input:

11 4
1 4 5
3 10 2
7 9 10
2 5 6
Output:

13
How to count

Line length (n) = 11
Many operations (x) = 4

| Operation to | start (s) | end (e) | value (v) |
| 1 | 1 | 4 | 5 | ... (1)
| 2 | 3 | 10 | 2 | ... (2)
| 3 | 7 | 9 | 10 | ... (3)
| 4 | 2 | 5 | 6 | ... (4)


Index -> 1- 2- 3- 4- 5- 6- 7- 8- 9- 10 11
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] <- Initial value of the row
        [5, 5, 5, 5, 0, 0, 0, 0, 0, 0] <- Value after 1st operation
        [5, 5, 7, 7, 2, 2, 2, 2, 2, 2, 0] <- Value after the 2nd operation
        [5, 5, 7, 7, 2, 2, 12, 12, 12, 2, 0] <- Value after the 3rd operation
        [5, 11, 13, 13, 8, 2, 12, 12, 12, 2, 0] <- Value after the 4th operation

Max-> 13

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer to the question is given below.

The code in c++ is given below.

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n,k;
   cin>>n>>k;
   int arr[n]={0}; /*creating array of n elements with value 0*/
   /* while loop for k operation */
   while(k--)
   {
       int start,end,value;
       cin>>start>>end>>value;
       /*appling the operation on element from start to end*/
       for(int i=start;i<=end;i++)
       {
           arr[i-1]=arr[i-1]+value;
       }
   }
   /* displaying the maximum element in the array*/
   cout<<"Max Element after these operation: "<<*max_element(arr,arr+n);
   return 0;
}

The screenshot of the running code is given below.






The screenshot of the output is given below.






If the answer helped please upvote it means a lot. For any query please comment.

Add a comment
Know the answer?
Add Answer to:
use c ++ Description Find the maximum value of an array with length N after X...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ==> Please In C Language <== Insert a value in a sorted array: Given an integer...

    ==> Please In C Language <== Insert a value in a sorted array: Given an integer array a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 }, read in an integer k from the screen and insert k into the array so the new array remains sorted. Note that array a has size 9, but there are only 8 initial values. This ensures that no value will be lost after the insertion. For example, when k=12 , the...

  • Show the contents of the array below, once the “pivot” element is placed at its appropriate...

    Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in ascending order (from smallest to largest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set 19,       20,       1,         13,       16,       5,         4,         9,         14,       7 Index 0 1 2 3 4 5 6 7...

  • Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12]

    1.      Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in place. You should divide this update task among the n threads as evenly as possible. Print the array both before and after...

  • C++ Language Given an array length 1 or more of ints, return the difference between the...

    C++ Language Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Examples: largeDiff (410, 3, 5, 6}, 4) - 7 largeDiff ({7, 2, 10, 9}, 4) - 8 largeDiff ({2, 10, 7, 2}, 4) + 8 largeDiff ({2}, 1) = 0 code.cpp + New I Full Screen 2- int largeDiff (const int a[], int n) { 3 4 5}

  • F a as a sequence of adjacent array elements such that each value in the run array was of size 10...

    f a as a sequence of adjacent array elements such that each value in the run array was of size 10 9. We define a "run" of elements o (except for the first) is one greater than the previous and looked like: value. For example, say the 3 2 16 9 7 8 9 2 a: position 0 3 We have three runs in this array: (1) between 9,10,1, 12) and, (3) between positions 7 and 8, (15, 16) positions...

  • JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...

    JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-30, -9, -6, -4, -2, -1, 0, 2, 4, 10, 12, 17, 22, 30}; ​ // search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values...

  • Matlab! Use Matlab to calculate array X and array Y. The first column is time (1,...

    Matlab! Use Matlab to calculate array X and array Y. The first column is time (1, 2, 3 ...), and the second column is value (0 and 1). When the value of array X is 0, the value of array Y is itself at the same time. When the value of array X is 1, the value of array Y will become the value of the previous Y(i-1) The data of the array is given, just how to change the...

  • 2) Sorting (a) (5 pts) In a Merge Sort of 8 elements, the Merge function gets...

    2) Sorting (a) (5 pts) In a Merge Sort of 8 elements, the Merge function gets called 7 times. Consider a Merge Sort being executed on the array shown below. What does the array look like right AFTER the sixth call to the Merge function completes? نرا index value 0 40 2 12 4 11 5 99 6 31 7 16 27 18 0 1 2 زيا 4 5 6 7 Index Value (b) (5 pts) Consider sorting the array...

  • Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find...

    Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find all subsequences in an array of int that add up to a given sum. */ #include <iostream> #include <cstdlib> using namespace std; void printSeq(int* a, int s, int e); // -------------------------------------------- functions to be implemented by you void longestBlock(const int* a, int n, int& s, int& e) { // s = start index of the block // e = end index of the block...

  • Python HELP How can a get the diagonals of an n x n list without using...

    Python HELP How can a get the diagonals of an n x n list without using numpy. For example if have a list: L = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Then I can get the diagonals: 1, 6, 11, 16 2, 7 , 12 3, 8 5 , 10 ,15 9, 14 2, 5 3, 6, 9 4, 7, 10, 13 8, 11, 14 12,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT