{"id":"88c3b1cc-0dfc-4edd-9f5d-a407c024e68d","name":"Design A Stack With Increment Operation","description":"Design a stack which supports the following operations.\r\n\r\nImplement the CustomStack class:\r\n\r\n 1: void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.\r\n 2: int pop() Pops and returns the top of stack or -1 if the stack is empty.\r\n 3: void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: 1 &lt;= maxSize &lt;= 1000\r\n2: 1 &lt;= x &lt;= 1000\r\n3: 1 &lt;= k &lt;= 1000\r\n4: 0 &lt;= val &lt;= 100\r\n5: At most 1000 calls will be made to each method of increment, push and pop each separately.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nclass CustomStack{\npublic:\n vector<int> value;\n vector<int> increment;\n int index;\n \n CustomStack(int m)\n {\n value.resize(m);\n increment.resize(m);\n index = -1;\n }\n void push(int x)\n {\n //write your code here\n }\n \n int pop()\n {\n //write your code here\n }\n \n void increment1(int k, int val){\n //write your code here\n }\n};\n\nint main()\n{\n int maxSize;\n cin>>maxSize;\n \n CustomStack cs(maxSize);\n \n string task;\n cin >> task;\n while(task!=\"exit\")\n {\n \n if(task == \"push\")\n {\n int val;\n cin >> val;\n cs.push(val);\n }\n else if(task == \"pop\")\n {\n int val = cs.pop();\n cout << val << endl;\n }\n else if(task == \"increment\")\n {\n int k,val;\n cin>>k>>val;\n cs.increment1(k,val);\n }\n cin >> task;\n }\n cout<<\"exit\"<<endl;\n \n \n \n \n \n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class CustomStack {\r\n \r\n int value[];\r\n int increment[];\r\n int index;\r\n \r\n public CustomStack(int maxSize) {\r\n value = new int[maxSize];\r\n increment = new int[maxSize];\r\n index=-1;\r\n }\r\n \r\n public void push(int x) {\r\n // complete this function\r\n }\r\n \r\n public int pop() {\r\n // complete this function\r\n }\r\n \r\n public void increment(int k, int val) {\r\n // complete this function\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int maxsize = Integer.parseInt(read.readLine());\r\n\r\n CustomStack cs = new CustomStack(maxsize);\r\n \r\n while(true){\r\n String task[] = read.readLine().split(\" \");\r\n if(task[0].equals(\"push\")){\r\n cs.push(Integer.parseInt(task[1]));\r\n }else if(task[0].equals(\"pop\")){\r\n System.out.println(cs.pop());\r\n }else if(task[0].equals(\"increment\")){\r\n cs.increment(Integer.parseInt(task[1]), Integer.parseInt(task[2]));\r\n }else{\r\n break;\r\n }\r\n }\r\n System.out.println(\"exit\");\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\npush 1\r\npush 2\r\npop\r\npush 2\r\npush 3\r\npush 4\r\nincrement 5 100\r\nincrement 2 100\r\npop\r\npop\r\npop\r\npop\r\nexit","sampleOutput":"2\r\n103\r\n202\r\n201\r\n-1\r\nexit\r\n","questionVideo":"https://www.youtube.com/embed/-CXN5Nh9jOA","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"55c8102b-3a99-42e0-a33b-c415819535cf","name":"Design A Stack With Increment Operation","slug":"design-a-stack-with-increment-operation","type":1}],"next":{"id":"b62b87df-ad4f-41a1-a4b7-ae489cee17bc","name":"Design A Stack With Increment Operation MCQ","type":0,"slug":"design-a-stack-with-increment-operation-mcq"},"prev":{"id":"4d679138-3ee2-4437-9499-9c72d0149414","name":"Lexicographically Smallest Subsequence","type":3,"slug":"lexicographically-smallest-subsequence"}}}

Design A Stack With Increment Operation

Design a stack which supports the following operations. Implement the CustomStack class: 1: void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize. 2: int pop() Pops and returns the top of stack or -1 if the stack is empty. 3: void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.

{"id":"88c3b1cc-0dfc-4edd-9f5d-a407c024e68d","name":"Design A Stack With Increment Operation","description":"Design a stack which supports the following operations.\r\n\r\nImplement the CustomStack class:\r\n\r\n 1: void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.\r\n 2: int pop() Pops and returns the top of stack or -1 if the stack is empty.\r\n 3: void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1: 1 &lt;= maxSize &lt;= 1000\r\n2: 1 &lt;= x &lt;= 1000\r\n3: 1 &lt;= k &lt;= 1000\r\n4: 0 &lt;= val &lt;= 100\r\n5: At most 1000 calls will be made to each method of increment, push and pop each separately.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nclass CustomStack{\npublic:\n vector<int> value;\n vector<int> increment;\n int index;\n \n CustomStack(int m)\n {\n value.resize(m);\n increment.resize(m);\n index = -1;\n }\n void push(int x)\n {\n //write your code here\n }\n \n int pop()\n {\n //write your code here\n }\n \n void increment1(int k, int val){\n //write your code here\n }\n};\n\nint main()\n{\n int maxSize;\n cin>>maxSize;\n \n CustomStack cs(maxSize);\n \n string task;\n cin >> task;\n while(task!=\"exit\")\n {\n \n if(task == \"push\")\n {\n int val;\n cin >> val;\n cs.push(val);\n }\n else if(task == \"pop\")\n {\n int val = cs.pop();\n cout << val << endl;\n }\n else if(task == \"increment\")\n {\n int k,val;\n cin>>k>>val;\n cs.increment1(k,val);\n }\n cin >> task;\n }\n cout<<\"exit\"<<endl;\n \n \n \n \n \n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class CustomStack {\r\n \r\n int value[];\r\n int increment[];\r\n int index;\r\n \r\n public CustomStack(int maxSize) {\r\n value = new int[maxSize];\r\n increment = new int[maxSize];\r\n index=-1;\r\n }\r\n \r\n public void push(int x) {\r\n // complete this function\r\n }\r\n \r\n public int pop() {\r\n // complete this function\r\n }\r\n \r\n public void increment(int k, int val) {\r\n // complete this function\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int maxsize = Integer.parseInt(read.readLine());\r\n\r\n CustomStack cs = new CustomStack(maxsize);\r\n \r\n while(true){\r\n String task[] = read.readLine().split(\" \");\r\n if(task[0].equals(\"push\")){\r\n cs.push(Integer.parseInt(task[1]));\r\n }else if(task[0].equals(\"pop\")){\r\n System.out.println(cs.pop());\r\n }else if(task[0].equals(\"increment\")){\r\n cs.increment(Integer.parseInt(task[1]), Integer.parseInt(task[2]));\r\n }else{\r\n break;\r\n }\r\n }\r\n System.out.println(\"exit\");\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\npush 1\r\npush 2\r\npop\r\npush 2\r\npush 3\r\npush 4\r\nincrement 5 100\r\nincrement 2 100\r\npop\r\npop\r\npop\r\npop\r\nexit","sampleOutput":"2\r\n103\r\n202\r\n201\r\n-1\r\nexit\r\n","questionVideo":"https://www.youtube.com/embed/-CXN5Nh9jOA","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"55c8102b-3a99-42e0-a33b-c415819535cf","name":"Design A Stack With Increment Operation","slug":"design-a-stack-with-increment-operation","type":1}],"next":{"id":"b62b87df-ad4f-41a1-a4b7-ae489cee17bc","name":"Design A Stack With Increment Operation MCQ","type":0,"slug":"design-a-stack-with-increment-operation-mcq"},"prev":{"id":"4d679138-3ee2-4437-9499-9c72d0149414","name":"Lexicographically Smallest Subsequence","type":3,"slug":"lexicographically-smallest-subsequence"}}}
plane

Editor


Loading...

Design A Stack With Increment Operation

medium

Design a stack which supports the following operations. Implement the CustomStack class: 1: void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize. 2: int pop() Pops and returns the top of stack or -1 if the stack is empty. 3: void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.

Constraints

1: 1 <= maxSize <= 1000 2: 1 <= x <= 1000 3: 1 <= k <= 1000 4: 0 <= val <= 100 5: At most 1000 calls will be made to each method of increment, push and pop each separately.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

3 push 1 push 2 pop push 2 push 3 push 4 increment 5 100 increment 2 100 pop pop pop pop exit

Sample Output

2 103 202 201 -1 exit

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode