0% found this document useful (0 votes)
3 views11 pages

Data Structure

The document outlines a programming assignment involving multiple questions that require writing C++ code to solve various algorithmic problems. Each question includes specific tasks such as creating a spiral matrix, encoding strings, finding next greater elements, balancing stacks, and calculating trapped rainwater. The document also provides input/output examples and marks for each question, indicating successful completion of tests.

Uploaded by

llyodfrontera298
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Data Structure

The document outlines a programming assignment involving multiple questions that require writing C++ code to solve various algorithmic problems. Each question includes specific tasks such as creating a spiral matrix, encoding strings, finding next greater elements, balancing stacks, and calculating trapped rainwater. The document also provides input/output examples and marks for each question, indicating successful completion of tests.

Uploaded by

llyodfrontera298
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Status Started Completed Duration Grade Finished Tuesday, 20 January 2026, 2:20 PM Tuesday, 20 January 2026, 4:00 PM 1 hour 40 mins 10.00 out of 10.00 (100%) ‘Question 1 correct Mark 2.00 out of 200, Write a code to print an n xn spiral matrix (of numbers from 1 to n2) in counter-clock wise direction (start with center of the matrix) forgiven input asn (nis an odd number) For example: if input n=5 is given then output will beg Only one space between any two number in a row) For example: Input Result S25 24.23 22.21 10987 2 n21619 1234518 1314.15 16.17 3 987 216 Bas Answer: (penalty regime: 0%) Language cpp ¥ 1 [#include 2 include 3 4 using namespace std; 5 6 -fint maing) ¢ 7] int ns 8+ if (cin >> n) ° vectorcvectorcint>> mateix(n, vectorcint>(n)); 16 n // Start at the center 2 int x =n / 25 3B int y =n / 25 4 int num = 15 15 int step = 15 16 v7 matrix[x][y] = aunts a8 194 while (num <= nt n) { 20 // Move Left a for (int 1 = 0 4 < step B& num <= nt ny +44) ( 2 yo 23 matrix[x][y] = nuns 24 > 25 26 11 Move Down 274 for (int 4 = 0; 4 < step BB num <= nt nj 44) ( 28 xe 23 matrix[x][y] = nuness 30 } 3 32 J/ Increase step size after Left and Down moves 43 steptty 34 35 1/ Move Right 364 for (int = 65 1 ¢ step && nun cn * nj 494) ( 7 yess 38 macrix[x]{y] = num 33 + 49 41 11 Move Up a2, for (int 4» 6; 4 ¢ step && num cn * m5 444) ( 3 pees aa natrdx[x]{y] = numess 45 , 46 “7 11 increase step size after Right and Up moves 48 steptss 49 > 50 5 11 Print the matrix 52 for (int i= 6; 4 < ms 44) £ Input Expected Got Os susan sm © 1098720 1098720 ni21619 1121619 1234518 1234518 33 14 15 16 17 13.14 15 16 17 Qs 987 say ® 216 216 345 345 Passed all tests! © Marks for this submission: 200/2.00 ‘question 2 correct Mark 2.00 out of 200, Given & strings of lowercase letters only, to be sent in encoded form. The rule for encoding every character of the string is: + The ith letter of english alphabet starting from the left, should be replaced with the ith letter starting from the right. For example, the string ‘abcd’ would be encoded to "zyxw' Fora list of N distinct strings consisting both original & encoded messages, determine the minimum possible value of K (initial number of messages) Input format ‘The first line contains an integer N, denoting the number of strings present in the final array. ‘+ The second line contains N space-separated strings consisting of lowercase letters only. Output format Print a single line containing a single integer representing the minimum possible number of strings that were used initially Result 3 2 aaa 222 pars 5 3 ‘aaa hack zzz abed s2xp Answer: (penalty regime: 0%) Language cpp ¥ 1 |finclude 2 |rinclude 3 |finclude 4 |rinclude 5 6 using namespace std; 7 8 fant main() ¢ 9 10 int ns ane] AF (ein >> a) 12 vector arr(n); 3 unordered setcstring> distinct; a4 35, for (int = 05 4 < ny itt) { 16 cin >> are i]s v7 distinct-insert(arr[i]); a8 » a9 20 Ant count = 5 a4 for (const string &s : arr) { 2,| Af ([Link](s)) { 2B counts+3 20 [Link](s); 26 string encoded 274 for (char &e + encoded) { 23 cele s(2- os 29 ? 30 314] if (distinct. count(encoded)) { 32 distinct erase(encoded); 3 } 3 } 35 > 36 cout << count < endl; wv] > 38 | return o5 x9 |) Input Expected Got 3 2 2 © ae 122 pars Os 3 2 © ‘aaa hack zzz abed s2xp Passed alll tests! @ Marks for this submission: 200/2.00 ‘question 3 correct Mark 2.00 out of 200, For each element in an array, find the next greater element to its right. Print the resultant array consisting of the next greater elements & also print the total number of greatest elements present in the resultant array. If none exists, output A Constraints 1> n) ¢ 9 vectorclong long> vin)s 204 for (int = 0; 4 < nj de) { un cin >> vEils 2 y B 14 vectorclong long> result(n); 5 stackelong long> $5 16 int count = 83 v 14 for (int 4 =n = 15 4 >= 05 S--) 294 while (Is-empty() 8& [Link]() <= v{t]) ¢ 20 [Link]()5 a y 2 234 AF (svempty()) { 28 result[i] = -1; 254 } else ( 26 result[i] = [Link]()s 27 count+5 28 > 29 [Link](v[4])s 30 » 3 324] for (int = 0; i < np it) { 33 cout << result[i] <<" "5 34 ) 35 36 cout << endl << count << endl 37 y aa] return 0; 39 | Input Expected Got Qa 5225-1 5252-1 @ 45225 3 3 Os 2345123452 0 123454 4 Passed all tests! © Marks for this submission: 200/2 00, ‘question 4 correct Mark 2.00 out of 200, Consider that you have 3 stacks, each stores positive integers. Your task is to find out number of minimum pops that are required collectively so that sum of elements of each stack is same as the other two. For example: if three stacks are (3 21 1 1), (43 2) and (1 14 1) (with top be the let most element. f ene element is removed from first two stacks and two elements from the third, they become (2 1 11) (@ 2) and (4 1) and sum of elements is each stack becomes . Thus final answer is 4 For example: Input Result 321564 14332 5723 231108 Answer: (penalty regime: 0.%) Language cpp Y 7 [include im 2 Jrinclude 3 |finclude 4 |einclude 5 |finclude 6 7 Jusing namespace st a 9 Jong long getsum(const vectorcint>& v, int start_index) { 28 ong long sum = 85 ais] for (sizet i = startindex; 4 < [Link](); +44) { 2 sun += v[1]5 B y 1a | return sums a5 |) 16 a7 faint main) ¢ 18 | vectorcint> st, 52, 835 19 | string Lines 20 | int val; a 22. if (getline(cin, Line) { 23 ‘stringstream ss(Line) ; 26 while (ss >> val) si.push_back(val); 25, ) 265] if (getline(cin, Line) ¢ 7 stringstream ss(line) ; 28 while (ss >> val) s2.push_back(val); 29 > 31 stringstream ss(line); 32 while (ss >> val) s3.push_back(val); 3 } 34 35 Jong long sumi = getSum(si, 8); 36 long long sun2 = getSum(s2, @); 37 ‘ong long sum3 = getsum(s3, @); 38 39 int i1 = @, i2 = @, 13 = 8; 40 int pops = 0; 41 42+| while (!(sunt == sum? & sum? == sum3)) { 43 Lf (sumd > sum2 @& sumz >= sum3) ( 48 sum -= si[ie+]; 45 popst+5 46 +| } else if (sum2 >= sun && sum2 >= sum3) { a7 sum2 -= s2[i2++]; 43 pops++5 49 «| } else ( 5e sum3 -= s3[i3+4]5 31 pops++5 52 y Input Expected Got Q 321564 4 © 14332 5723 @ 231108 8s © 11000 5308 Passed all tests! © Marks for this submission: 200/200 Question 5 correct Mark 2.00 out of 2.00, You are given an array height (J consisting of N non-negative integers, where each integer represents the height of a vertical block and the width of each block is 1 unit. After raining, water may get trapped between these blocks. Your task is to calculate the total amount of rainwater that can be trapped between the blocks. Input Format + An integer N — number of blocks + An array height] of size N where height represents the height ofthe i-th block Constraints TNs 1095 0 < height < 105 Width of each bloc Tunit ‘Output Format A single integer representing the total units of water trapped For example: Input Result 6 Fa aee204 e102 Answer: (penalty regime: 0%) Language cpp ¥ 2 [include ostream 2 |inelude 3 |rinclude 4 5 using namespace std; 6 7 fant main() { a] ant ns 94] af (ein >> a) a8 Vectorcint> height(n); nu for (int i= 05 i < nj +44) ( 2 cin >> heignt(i]3 3 > 1 a5 if (n == 0) as cout << 8 << endl; 7 return @; 18 ) 19 20 vectorcint> leftMex(n), rightMax(n); a 2 leftMaxio1 = heieht!o1 23,| for (int L= 4; 1¢nj sed) { 28 LeftMax[i] = max(leftMax[i - 1], height[i]); 25 » 26 27 righttax[n - 1] = height[n - 1]; 28, for (int i= m= 2; i >= 0; --i) { 29 rightMax[i] = max(rightMax[i + 1], height[i]); 38 y 31 32 long long trappediater = 0; 334 for (int 1 = 05 4 < ny +44) { 34 trappedwater += min(leftMax[i], rightMax[i]) - height[i]; 35 > 36 37 cout << trappedvater << endl; 38 » 39 return 0; 40 |) Input Expected Got Qs 10 2» @ 3ee204 Qa 1 1 0 e102 Passed all tests! @ Marks for this submission: 200/2.00

You might also like