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

INFEED

PBT | Daily Test | 11 jun (Coding)

 

 ***** in 3 rd program only 2 test will pass ****

 1.

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 <bits/stdc++.h>
using namespace std;
int maxWater(int arr[],int n)
{
 int res=0;
 for(int i=1;i<n-1;i++)
 {
 int left=arr[i];
 for(int j=0;j<i;j++)
 left=max(left,arr[j]);
 int right=arr[i];
 for(int j=i+1;j<n;j++)
 right=max(right,arr[j]);
 res=res+(min(left,right)-arr[i]);
 }
 return res;
}
int main()
{
 int n;
 cin>>n;
 int arr[n];
 for(int i=0;i<n;i++)
 cin>>arr[i];
 cout<<maxWater(arr,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

 

 c++ Code :

#include <bits/stdc++.h>
using namespace std;
long long n,t,l,r,k,s,d,max1=0,b,c,mod=1e9+7;
long long a[100005];
map<long long,long long>m,p;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
 cin>>t;
 m[t]++;
}
for(int i=1;i<=3001;i++)
{
 if(m[i]==0)
 {
 cout<<i;
 break;
 }
}
return 0;
}
 

3.

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

Previous Post Next Post