Sunteți pe pagina 1din 4

17SCSE101144 Ujjwal Singh 1713101137

Program 1:

#include<iostream>

using namespace std;

void buildLowestNumberRec(string str, int n, string &res)

if (n == 0)

res.append(str);

return;

int len = str.length();

if (len <= n)

return;

int minIndex = 0;

for (int i = 1; i<=n ; i++)

if (str[i] < str[minIndex])

minIndex = i;

res.push_back(str[minIndex]);

string new_str = str.substr(minIndex+1, len-minIndex);

buildLowestNumberRec(new_str, n-minIndex, res);

string buildLowestNumber(string str, int n)

{
17SCSE101144 Ujjwal Singh 1713101137

string res = "";

buildLowestNumberRec(str, n, res);

return res;

int main()

string str = "1432219";

int n = 3;

cout << buildLowestNumber(str, n);

return 0;

Output:

Program 2:
#include<bits/stdc++.h>

using namespace std;

int getMaxArea(int hist[], int n)

stack<int> s;
17SCSE101144 Ujjwal Singh 1713101137

int max_area = 0;

int tp;

int area_with_top;

int i = 0;

while (i < n)

if (s.empty() || hist[s.top()] <= hist[i])

s.push(i++);

else

tp = s.top();

s.pop();

area_with_top = hist[tp] * (s.empty() ? i :

i - s.top() - 1);

if (max_area < area_with_top)

max_area = area_with_top;

while (s.empty() == false)

tp = s.top();

s.pop();

area_with_top = hist[tp] * (s.empty() ? i :

i - s.top() - 1);

if (max_area < area_with_top)

max_area = area_with_top;
17SCSE101144 Ujjwal Singh 1713101137

return max_area;

int main()

int hist[] = {6, 2, 5, 4, 5, 1, 6};

int n = sizeof(hist)/sizeof(hist[0]);

cout << "Maximum area is " << getMaxArea(hist, n);

return 0;

Output :

S-ar putea să vă placă și