***BLOOD RELATION WILL BE UPDATED SOON ***
*** CLICK ON ADS **
Technical Mcq
1. d
2. b
3. a
4. b
5. a
6. b
7. b
8. c
9. c
10. b
**************************************
*** CLICK ON ADS **
PROGRAMMING
1.
Problem Statement
Given an array arr[] of N non-negative integers representing the height of blocks. If width of each
block is 1, compute how much water can be trapped between the blocks during the rainy season.
Input Format
First line of input contains n-the size of array. Next line of input contains n integers-the elements of
the array.
Constraints
3<=n<=10^5
0 <= Ai <= 10^8
Output Format
Print the total amount of water that can be trapped.
For example, in first sample test case:- Bars of input are {3,0,0,2,0,4}. So, Water trapped between
0th bar and 1st bar=3 Water trapped between 1st bar and 2nd bar=3 Water trapped between 2nd
bar and 3rd bar=1 Water trapped between 3rd bar and 4th bar=3 Total Water trapped=10.
C CODE:
#include <stdio.h>
#include <limits.h>
int max(int x, int y) {
return (x > y) ? x : y;
}
int min(int x, int y) {
return (x < y) ? x : y;
}
// Function to find the amount of water that can be trapped within
// a given set of bars in linear time and extra space
int trap(int bars[], int n)
{
int water = 0;
// `left[i]` stores the maximum height of a bar to the left
// of the current bar
int left[n-1];
left[0] = INT_MIN;
// process bars from left to right
for (int i = 1; i < n - 1; i++) {
left[i] = max(left[i-1], bars[i-1]);
}
/*
int right[n];
right[n - 1] = INT_MIN;
for (int i = n - 2; i >= 0; i--) {
right[i] = max(right[i+1], bars[i+1]);
}
for (int i = 1; i < n - 1; i++)
{
if (min(left[i], right[i]) > bars[i]) {
water += min(left[i], right[i]) - bars[i];
}
}
*/
// `right` stores the maximum height of a bar to the right
// of the current bar
int right = INT_MIN;
// process bars from right to left
for (int i = n - 2; i >= 1; i--)
{
right = max(right, bars[i+1]);
// check if it is possible to store water in the current bar
if (min(left[i], right) > bars[i]) {
water += min(left[i], right) - bars[i];
}
}
return water;
}
int main(void)
{int h;scanf("%d",&h);
int heights[h];
for(int i=0;i<h;i++)
{
scanf("%d",&heights[i]);
}
int n = sizeof(heights) / sizeof(heights[0]);
printf("%d",trap(heights, n));
return 0;
}
**************************************
2.
Problem Statement
«Polygon» is a system which allows to create programming tasks in a simple and professional way.
When you add a test to the problem, the corresponding form asks you for the test index. As in most
cases it is clear which index the next test will have, the system suggests the default value of the
index. It is calculated as the smallest positive integer which is not used as an index for some
previously added test.
You are to implement this feature. Create a program which determines the default index of the next
test, given the indexes of the previously added tests.
Input Format
The first line contains one integer n — the amount of previously added tests. The second line
contains n distinct integers a1, a2, ..., an-indexes of these tests.
Constraints
(1 ≤ n ≤ 3000) (1 ≤ ai ≤ 3000)
Output Format
Output the required default value for the next test index.
PYTHON CODE:
n = int(input())
s = input()
a = s.split()
for i in range(len(a)):
a[i] = int(a[i])
dem = 1
while dem in a:
dem += 1
print(dem)
************************
3.
Problem Statement
Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it. In
other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5.....
(considering the increasing lexicographical order).
Input Format
First line of input contains n-the size of array. Next line of input contains n integers-the elements of
array.
Constraints
1 ≤ n ≤ 10^6
0 ≤ Ai ≤10^7
Output Format
Print the array which should be sorted in wave like pattern.
PYTHON CODE:
class Solution:
def wave(self, A):
A = sorted(A)
for i in range(0, len(A)-1, 2):
A[i], A[i+1] = A[i+1], A[i]
return A
x=int(input())
x1=Solution()
f=list(map(int,input().split()))
print(*x1.wave(f),sep=" ")
Post a Comment