Algorithmic Crush

QUESTION

You are given a list of size N, initialized with zeroes. You have to perform M operations on the list and output the maximum of final values of all the N elements in the list. For every operation, you are given three integers a,b and k and you have to add value k to all the elements ranging from index a to b(both inclusive).\n\nInput Format\nFirst line will contain two integers N and M separated by a single space.\nNext M lines will contain three integers a,b and k separated by a single space.\nNumbers in list are numbered from 1 to N.\nOutput Format\n\nA single line containing maximum value in the updated list.

“TESTCASE_1”: “5 3\n1 2 100\n2 5 100\n3 4 100\n###—###SEPERATOR—###—\n200”, “TESTCASE_2”: “4 3\n2 3 603\n1 1 286\n4 4 882\n###—###SEPERATOR—###—\n882”, “TESTCASE_3”: “40 30\n19 28 419\n4 23 680\n5 6 907\n19 33 582\n5 9 880\n10 13 438\n21 39 294\n13 18 678\n12 26 528\n15 30 261\n8 9 48\n21 23 131\n20 21 7\n13 40 65\n13 23 901\n15 15 914\n14 35 704\n20 39 522\n10 18 379\n16 27 8\n25 40 536\n5 9 190\n17 20 809\n8 20 453\n22 37 298\n19 37 112\n2 5 186\n21 29 184\n23 30 625\n2 8 960\n###—###SEPERATOR—###—\n6314”, “TESTCASE_4”: “40 30\n29 40 787\n9 26 219\n21 31 214\n8 22 719\n15 23 102\n11 24 83\n14 22 321\n5 22 300\n11 30 832\n5 25 29\n16 24 577\n3 10 905\n15 22 335\n29 35 254\n9 20 20\n33 34 351\n30 38 564\n11 31 969\n3 32 11\n29 35 267\n4 24 531\n1 38 892\n12 18 825\n25 32 99\n3 39 107\n12 37 131\n3 26 640\n8 39 483\n8 11 194\n12 37 502\n###—###SEPERATOR—###—\n8628”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
int main()
{
  int m,n,p=0;
  scanf("%d%d",&n,&m);
  int arr[n],i=0;
  for(p=0;p<n;p++)
  {
    arr[p]=0;
  }
  int a,b,k;
  while(i<m)
  {
    int j=0;
    scanf("%d%d%d",&a,&b,&k);
    for(j=a;j<=b;j++)
    {
      arr[j-1]+=k;
    }
    i++;
  }
  int max=0;
  max=arr[0];
  for(i=0;i<n;i++)
  {
    if(arr[i]>max)
    {
      max=arr[i];
    }
  }
  printf("%d",max);

	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO