Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

Stacks | 21 May

Problem Statement 

Given a stack of integers, write a function checkpairconsecutive() that checks whether numbers in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should retain the original stack content. 

Input Format

 Read the numbers in the stack. 

Output Format

 Check whether the stack elements are pairwise consecutive or not

Python Program:

def pairWiseConsecutive(s):

    aux = []

    while (len(s) != 0):

        aux.append(s[-1])

        s.pop()

    result = True

    while (len(aux) > 1):

        x = aux[-1]

        aux.pop()

        y = aux[-1]

        aux.pop()

        if (abs(x - y) != 1):

            result = False

        s.append(x)

        s.append(y)

 

    if (len(aux) == 1):

        s.append(aux[-1])

 

    return result


if __name__ == '__main__':

    s = list(map(int,input().split()))

    if (pairWiseConsecutive(s)):

        print("Yes")

    else:

        print("No") 

******************************************************************

Problem 2:

Problem Statement Skyline Real Estate Developers is planning to demolish a number of old,

unoccupied buildings and construct a shopping mall in their place. Your task is to nd the largest solid

area in which the mall can be constructed. There are a number of buildings in a certain twodimensional landscape. Each building has a height, given by hi where i belongs to 1,n . If you join k

adjacent buildings, they will form a solid rectangle of area . k*min(hi,hi+1,…...hi+k-1). For example, the

heights array h=3,2,3. A rectangle of height h=2 and length k=3 can be constructed within the

boundaries. The area formed is h.k=2.3=6 . Input Format The rst line contains n , the number of

buildings. The second line contains n space-separated integers, each representing the height of a

building. Output Format Print a long integer representing the maximum area of rectangle formed.


C++ Program:

#include <bits/stdc++.h>
using namespace std;

typedef long long           lli;

#define pb push_back


vector < lli > height;
vector < int > s;
lli Histogram(vector<lli> &height)
{
    s.clear();
    height.push_back(0);

    lli sum = 0;
    int i = 0;
    while(i < height.size())
    {
        if(s.empty() || height[i] > height[s.back()])
        {
            s.push_back(i);
            i++;
        }
        else
        {
            int t = s.back();
            s.pop_back();

            sum = max(sum, height[t] * (s.empty() ? i : i - s.back() - 1));
        }
    }

    return sum;
}

int main(void)
{
    int i,j,k,kase=0;

    int n;
    while( scanf("%d",&n)==1 )
    {
        height.assign(n, 0);
        for(i=0; i<n; i++) scanf("%lld",&height[i]);
        printf("%lld\n",Histogram(height));
    }
    return 0;
}
***************************************************************************

Post a Comment

Previous Post Next Post