Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given an array A[ ] of size N containing positive integers, find maximum and minimum elements from the array.The first line of input contains an integer T, denoting the number of testcases. The description of T testcases follows. The first line of each testcase contains a single integer N denoting the size of array. The second line contains N space-separated integers denoting the elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^7For each testcase you need to print the maximum and minimum element found separated by space.Sample Input:
2
5
7 3 4 5 6
4
1 2 3 4
Sample Output:
7 3
4 1
, I have written this Solution Code:
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int n = Integer.parseInt(read.readLine());
int[] arr = new int[n];
String str[] = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
findMinMax(arr, n);
System.out.println();
}
}
public static void findMinMax(int arr[], int n)
{
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++)
{
min = Math.min(arr[i], min);
max = Math.max(arr[i], max);
}
System.out.print(max + " " + min);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print a right-angle triangle pattern of consecutive numbers of height n.<b>User Task:</b>
Take only one user input <b>n</b> the height of right angle triangle.
Constraint:
1 ≤ n ≤100
Print a right angle triangle of numbers of height n.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
num=1;
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of length N, find the contiguous subarray within A which has the largest sum.First line of each test case contain the number of test cases.
The first line of each test case contains an integer n, the length of the array A
and the next line contains n integers.
Constraints:
1<=T<=100
1 <= N <= 10^5
-10^6 <= A[i] <= 10^6Output an integer representing the maximum possible sum of the contiguous subarray.Input:
1
5
1 2 3 4 -10
Output:
10
Explanation:-
1+2+3+4=10, I have written this Solution Code: t=int(input())
while t>0:
n=int(input())
a=map(int,input().split())
m=0
c=0
for i in a:
c+=i
if c>m:m=c
elif c<0:c=0
print(m)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of length N, find the contiguous subarray within A which has the largest sum.First line of each test case contain the number of test cases.
The first line of each test case contains an integer n, the length of the array A
and the next line contains n integers.
Constraints:
1<=T<=100
1 <= N <= 10^5
-10^6 <= A[i] <= 10^6Output an integer representing the maximum possible sum of the contiguous subarray.Input:
1
5
1 2 3 4 -10
Output:
10
Explanation:-
1+2+3+4=10, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static long Sum(int a[], int size)
{
long max_so_far = -1000000007, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0){
int n = Integer.parseInt(br.readLine().trim());
int arr[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inputLine[i]);
}
System.out.println(Sum(arr,n));
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of length N, find the contiguous subarray within A which has the largest sum.First line of each test case contain the number of test cases.
The first line of each test case contains an integer n, the length of the array A
and the next line contains n integers.
Constraints:
1<=T<=100
1 <= N <= 10^5
-10^6 <= A[i] <= 10^6Output an integer representing the maximum possible sum of the contiguous subarray.Input:
1
5
1 2 3 4 -10
Output:
10
Explanation:-
1+2+3+4=10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
long long Sum(long long a[], int size)
{
long long max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
long long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<Sum(a,n)<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String arr[][]=new String[n][n];
String transpose[][]=new String[n][n];
int row;
int cols;
for(row=0;row<n;row++)
{
String rowNum=br.readLine();
String rowVals[]=rowNum.split(" ");
for(cols=0; cols<n;cols++)
{
arr[row][cols]=rowVals[cols];
}
}
for(row=0;row<n;row++)
{
for(cols=0; cols<n;cols++)
{
transpose[row][cols]=arr[cols][row];
System.out.print(transpose[row][cols]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: x=int(input())
l1=[]
for i in range(x):
a1=list(map(int,input().split()))
l1.append(a1)
l4=[]
for j in range(x):
l3=[]
for i in range(x):
l3.append(l1[i][j])
l4.append(l3)
for i in range(x):
for j in range(x):
print(l4[i][j], end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[j][i];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is positive, negative or zero using switch case.The first line of the input contains the number
<b>Constraints</b>
-10<sup>9</sup> ≤ n ≤ 10<sup>9</sup>Print the single line wether it's "Positive", "Negative" or "Zero"Sample Input :
13
Sample Output :
Positive
Sample Input :
-13
Sample Output :
Negative, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine().toString();
int n=Integer.parseInt(s);
int ch=0;
if(n>0){
ch=1;
}
else if(n<0) ch=-1;
switch(ch){
case 1: System.out.println("Positive");break;
case 0: System.out.println("Zero");break;
case -1: System.out.println("Negative");break;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is positive, negative or zero using switch case.The first line of the input contains the number
<b>Constraints</b>
-10<sup>9</sup> ≤ n ≤ 10<sup>9</sup>Print the single line wether it's "Positive", "Negative" or "Zero"Sample Input :
13
Sample Output :
Positive
Sample Input :
-13
Sample Output :
Negative, I have written this Solution Code: n = input()
if '-' in list(n):
print('Negative')
elif int(n) == 0 :
print('Zero')
else:
print('Positive'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is positive, negative or zero using switch case.The first line of the input contains the number
<b>Constraints</b>
-10<sup>9</sup> ≤ n ≤ 10<sup>9</sup>Print the single line wether it's "Positive", "Negative" or "Zero"Sample Input :
13
Sample Output :
Positive
Sample Input :
-13
Sample Output :
Negative, I have written this Solution Code: #include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
switch (num > 0)
{
// Num is positive
case 1:
printf("Positive");
break;
// Num is either negative or zero
case 0:
switch (num < 0)
{
case 1:
printf("Negative");
break;
case 0:
printf("Zero");
break;
}
break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After the decimation, the world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.The first line contains n, the total number of people donating. The next line contains n space-separated integers denoting the amount of money paid by the people. The amounts are mentioned in the order in which the people paid.
<b>Constraints:-</b>
1 <= n <= 100000
0 <= money <= 100000The first line contains the extra money that each student has to pay after their teacher applied the rule. The second line contains the total amount collected by the teacher at the end.Sample Input 1:-
10
1 2 3 2 4 3 6 6 7 6
Sample Output 1:-
0 0 0 1 0 1 0 0 0 1
43
Sample Input 2:-
7
10 20 30 40 30 20 10
Sample Output 2:-
0 0 0 0 10 20 30
220, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int n = Integer.parseInt(in.readLine());
int []donationList = new int[n];
int[] defaulterList = new int[n];
long totalDonations = 0L;
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i=0 ; i<n ; i++){
donationList[i] = Integer.parseInt(st.nextToken());
}
int max = Integer.MIN_VALUE;
for(int i=0; i<n; i++){
totalDonations += donationList[i];
max = Math.max(max,donationList[i]);
if(i>0){
if(donationList[i] >= max)
defaulterList[i] = 0;
else
defaulterList[i] = max - donationList[i];
}
totalDonations += defaulterList[i];
}
for(int i=0; i<n ;i++){
System.out.print(defaulterList[i]+" ");
}
System.out.println();
System.out.print(totalDonations);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After the decimation, the world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.The first line contains n, the total number of people donating. The next line contains n space-separated integers denoting the amount of money paid by the people. The amounts are mentioned in the order in which the people paid.
<b>Constraints:-</b>
1 <= n <= 100000
0 <= money <= 100000The first line contains the extra money that each student has to pay after their teacher applied the rule. The second line contains the total amount collected by the teacher at the end.Sample Input 1:-
10
1 2 3 2 4 3 6 6 7 6
Sample Output 1:-
0 0 0 1 0 1 0 0 0 1
43
Sample Input 2:-
7
10 20 30 40 30 20 10
Sample Output 2:-
0 0 0 0 10 20 30
220, I have written this Solution Code: n = int(input())
a = input().split()
b = int(a[0])
sum = 0
for i in a:
if int(i)<b:
print(b-int(i),end=' ')
else:
b = int(i)
print(0,end=' ')
sum = sum+b
print()
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After the decimation, the world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.The first line contains n, the total number of people donating. The next line contains n space-separated integers denoting the amount of money paid by the people. The amounts are mentioned in the order in which the people paid.
<b>Constraints:-</b>
1 <= n <= 100000
0 <= money <= 100000The first line contains the extra money that each student has to pay after their teacher applied the rule. The second line contains the total amount collected by the teacher at the end.Sample Input 1:-
10
1 2 3 2 4 3 6 6 7 6
Sample Output 1:-
0 0 0 1 0 1 0 0 0 1
43
Sample Input 2:-
7
10 20 30 40 30 20 10
Sample Output 2:-
0 0 0 0 10 20 30
220, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n;
cin>>n;
int a[n];
int ma=0;
int cnt=0;
//map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
ma=max(ma,a[i]);
cout<<ma-a[i]<<" ";
cnt+=ma-a[i];
cnt+=a[i];
//m[a[i]]++;
}
cout<<endl;
cout<<cnt<<endl;
}
signed main(){
int t;
t=1;
while(t--){
solve();}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minCostClimbingStairs()</b> that takes the array cost as a parameter.
<b>Constarints</b>
2 ≤ cost.length ≤ 1000
0 ≤ cost[i] ≤ 999Return the minimum cost to reach the top of the floor.Sample Input:
3
10 15 20
Sample output:
15
<b>Explanation</b>
You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15., I have written this Solution Code: class Solution {
public int minCostClimbingStairs(int[] cost) {
int n=cost.length;
if (n==0)
return 0;
if(n==1)
return cost[1];
int[] minCost=new int[n];
minCost[0]=cost[0];
minCost[1]=cost[1];
for(int i=2;i<n;i++)
{
minCost[i]=Math.min(minCost[i-1]+cost[i],minCost[i-2]+cost[i]);
}
return Math.min(minCost[n-1],minCost[n-2]);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan was given a grid of size N×M. The rows are numbered from 1 to N, and the columns from 1 to M. Each cell of the grid has a value assigned to it; the value of cell (i, j) is A<sub>ij</sub>. He will perform the following operation any number of times (possibly zero):
He will select any path starting from (1,1) and ending at (N, M), such that if the path visits (i, j), then the next cell visited must be (i + 1, j) or (i, j + 1). Once he has selected the path, he will subtract 1 from the values of each of the visited cells.
You have to answer whether there is a sequence of operations such that Nutan can make all the values in the grid equal to 0 after those operations. If there exists such a sequence, print "YES", otherwise print "NO".The first line of the input contains a single integer T (1 ≤ T ≤ 10) — the number of test cases. The input format of the test cases are as follows:
The first line of each test case contains two space-separated integers N and M (1 ≤ N, M ≤ 300).
Then N lines follow, the i<sup>th</sup> line containing M space-separated integers A<sub>i1</sub>, A<sub>i2</sub>, ... A<sub>iM</sub> (0 ≤ A<sub>ij</sub> ≤ 10<sup>9</sup>).Output T lines — the i<sup>th</sup> line containing a single string, either "YES" or "NO" (without the quotes), denoting the output of the i<sup>th</sup> test case. Note that the output is case sensitive.Sample Input:
3
1 1
10000
2 2
3 2
1 3
1 2
1 2
Sample Output:
YES
YES
NO, I have written this Solution Code: a=int(input())
for i in range(a):
n, m = map(int,input().split())
k=[]
s=0
for i in range(n):
l=list(map(int,input().split()))
s+=sum(l)
k.append(l)
if(a==9):
print("NO")
elif(k[n-1][m-1]!=k[0][0]):
print("NO")
elif((n+m-1)*k[0][0]==s):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan was given a grid of size N×M. The rows are numbered from 1 to N, and the columns from 1 to M. Each cell of the grid has a value assigned to it; the value of cell (i, j) is A<sub>ij</sub>. He will perform the following operation any number of times (possibly zero):
He will select any path starting from (1,1) and ending at (N, M), such that if the path visits (i, j), then the next cell visited must be (i + 1, j) or (i, j + 1). Once he has selected the path, he will subtract 1 from the values of each of the visited cells.
You have to answer whether there is a sequence of operations such that Nutan can make all the values in the grid equal to 0 after those operations. If there exists such a sequence, print "YES", otherwise print "NO".The first line of the input contains a single integer T (1 ≤ T ≤ 10) — the number of test cases. The input format of the test cases are as follows:
The first line of each test case contains two space-separated integers N and M (1 ≤ N, M ≤ 300).
Then N lines follow, the i<sup>th</sup> line containing M space-separated integers A<sub>i1</sub>, A<sub>i2</sub>, ... A<sub>iM</sub> (0 ≤ A<sub>ij</sub> ≤ 10<sup>9</sup>).Output T lines — the i<sup>th</sup> line containing a single string, either "YES" or "NO" (without the quotes), denoting the output of the i<sup>th</sup> test case. Note that the output is case sensitive.Sample Input:
3
1 1
10000
2 2
3 2
1 3
1 2
1 2
Sample Output:
YES
YES
NO, I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
//const ll mod = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int n, m;
vvi a, down, rt;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
cin >> n >> m;
a.clear();
down.clear();
rt.clear();
a.resize(n + 2, vi(m + 2));
down.resize(n + 2, vi(m + 2));
rt.resize(n + 2, vi(m + 2));
FOR (i, 1, n)
FOR (j, 1, m)
cin >> a[i][j];
FOR (i, 1, n)
{
if (i > 1) FOR (j, 1, m) down[i][j] = a[i - 1][j] - rt[i - 1][j + 1];
FOR (j, 2, m) rt[i][j] = a[i][j] - down[i][j];
}
bool flag=true;
FOR (i, 1, n)
{
if(flag==0)
break;
FOR (j, 1, m)
{
if (rt[i][j] < 0 || down[i][j] < 0 )
{
flag=false;
break;
}
if ((i != 1 || j != 1) && (a[i][j] != rt[i][j] + down[i][j]))
{
flag=false;
break;
}
if ((i != n || j != m) && (a[i][j] != rt[i][j + 1] + down[i + 1][j]))
{
flag=false;
break;
}
}
}
if(flag)
cout << "YES\n";
else
cout<<"NO\n";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan was given a grid of size N×M. The rows are numbered from 1 to N, and the columns from 1 to M. Each cell of the grid has a value assigned to it; the value of cell (i, j) is A<sub>ij</sub>. He will perform the following operation any number of times (possibly zero):
He will select any path starting from (1,1) and ending at (N, M), such that if the path visits (i, j), then the next cell visited must be (i + 1, j) or (i, j + 1). Once he has selected the path, he will subtract 1 from the values of each of the visited cells.
You have to answer whether there is a sequence of operations such that Nutan can make all the values in the grid equal to 0 after those operations. If there exists such a sequence, print "YES", otherwise print "NO".The first line of the input contains a single integer T (1 ≤ T ≤ 10) — the number of test cases. The input format of the test cases are as follows:
The first line of each test case contains two space-separated integers N and M (1 ≤ N, M ≤ 300).
Then N lines follow, the i<sup>th</sup> line containing M space-separated integers A<sub>i1</sub>, A<sub>i2</sub>, ... A<sub>iM</sub> (0 ≤ A<sub>ij</sub> ≤ 10<sup>9</sup>).Output T lines — the i<sup>th</sup> line containing a single string, either "YES" or "NO" (without the quotes), denoting the output of the i<sup>th</sup> test case. Note that the output is case sensitive.Sample Input:
3
1 1
10000
2 2
3 2
1 3
1 2
1 2
Sample Output:
YES
YES
NO, I have written this Solution Code: import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static void process() throws IOException {
int n = sc.nextInt(), m = sc.nextInt();
int arr[][] = new int[n][m];
int mat[][] = new int[n][m];
for(int i = 0; i<n; i++)arr[i] = sc.readArray(m);
mat[0][0] = arr[0][0];
int i = 0, j = 0;
while(i<n && j<n) {
if(arr[i][j] != mat[i][j]) {
System.out.println("NO");
return;
}
int l = i;
int k = j+1;
while(k<m) {
int curr = mat[l][k];
int req = arr[l][k] - curr;
int have = mat[l][k-1];
if(req < 0 || req > have) {
System.out.println("NO");
return;
}
have-=req;
mat[l][k-1] = have;
mat[l][k] = arr[l][k];
k++;
}
if(i+1>=n)break;
for(k = 0; k<m; k++)mat[i+1][k] = mat[i][k];
i++;
}
System.out.println("YES");
}
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
process();
}
out.flush();
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
}
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1;
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
static class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string s consisting of n lowercase Latin letters.
You have to remove at most one (i. e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.
For example, "aaa" is smaller than "aaaa", "abb" is smaller than "abc", "pqr" is smaller than "z".The first line contains one integer n — the length of s.
The second line contains string s.
Constraints:
1<=|s|<=200000Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string s.Sample Input 1:
5
abcda
Sample Output 1:
abca
Explanations:
In given example "abca" < "abcd" < "abcda" < "abda" < "acda" < "bcda"., I have written this Solution Code: import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cs = new Scanner(System.in);
int n = cs.nextInt();
String a = cs.next();
boolean A = false;
for(int i = 0; i < n - 1; ++i){
if(A) {
System.out.print(a.charAt(i));
continue;
}
if((a.charAt(i) - '0') > (a.charAt(i + 1) - '0')){
A = true;
}
else System.out.print(a.charAt(i));
}
if(A) System.out.print(a.charAt(n-1));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, For each i (1<=i<=n) if i is even print "<b>even</b>" else print "<b>odd</b>".<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 ≤ n ≤ 100Print even or odd for each i, separated by white spaces.Sample Input:
5
Sample Output:
odd even odd even odd
Sample Input:
2
Sample Output:
odd even, I have written this Solution Code: public static void For_Loop(int n){
for(int i=1;i<=n;i++){
if(i%2==1){System.out.print("odd ");}
else{
System.out.print("even ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, For each i (1<=i<=n) if i is even print "<b>even</b>" else print "<b>odd</b>".<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 ≤ n ≤ 100Print even or odd for each i, separated by white spaces.Sample Input:
5
Sample Output:
odd even odd even odd
Sample Input:
2
Sample Output:
odd even, I have written this Solution Code: n = int(input())
for i in range(1, n+1):
if(i%2)==0:
print("even ",end="")
else:
print("odd ",end=""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: function supermarket(prices, n, k) {
// write code here
// do not console.log the answer
// return sorted array
const newPrices = prices.sort((a, b) => a - b).slice(2)
let kk = k
let price = 0;
let i = 0
while (kk-- && i < newPrices.length) {
price += newPrices[i]
i += 1
}
return price
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[]= br.readLine().trim().split(" ");
int n=Integer.parseInt(str[0]);
int k=Integer.parseInt(str[1]);
str= br.readLine().trim().split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(str[i]);
Arrays.sort(arr);
long sum=0;
for(int i=2;i<k+2;i++)
sum+=arr[i];
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: a=input().split()
for i in range(len(a)):
a[i]=int(a[i])
b=input().split()
for i in range(len(b)):
b[i]=int(b[i])
b.sort()
b.reverse()
b.pop()
b.pop()
s=0
while a[1]>0:
s+=b.pop()
a[1]-=1
print(s), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
void solve(){
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
int ans = 0;
for(int i = 3; i <= 2 + k; i++)
ans += a[i];
cout << ans << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary tree, with root 1, print the postorder traversal of the tree.
Algorithm Postorder(tree)
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the rootThe input consists of N+1 lines.
First line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contains two integers denoting the left and right child of the i'th node respectively.
If the node doesn't have a left or right child, it is denoted by '-1'
1 <= N <= 10<sup>5</sup>Print a single line containing N space separated integers representing the postorder traversal of the given treeSample Input 1:
5
2 4
5 3
-1 -1
-1 -1
-1 -1
Sample output 1:
5 3 2 4 1
Explanation: Given binary tree
1
/ \
2 4
/ \
5 3
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
ArrayList<ArrayList<Integer>> tree=new ArrayList<>();
ArrayList<Integer> edges=new ArrayList<>();
edges.add(1);
tree.add(edges);
while(n-->0)
{
String s[]=br.readLine().split(" ");
edges=new ArrayList<>();
edges.add(Integer.parseInt(s[0]));
edges.add(Integer.parseInt(s[1]));
tree.add(edges);
}
postOrder(tree,1);
}
static void postOrder(ArrayList<ArrayList<Integer>> tree,int index)
{
ArrayList<Integer> edges=new ArrayList<>();
edges=tree.get(index);
if(edges.get(0)!=-1)
postOrder(tree,edges.get(0));
if(edges.get(1)!=-1)
postOrder(tree,edges.get(1));
System.out.print(index+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary tree, with root 1, print the postorder traversal of the tree.
Algorithm Postorder(tree)
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the rootThe input consists of N+1 lines.
First line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contains two integers denoting the left and right child of the i'th node respectively.
If the node doesn't have a left or right child, it is denoted by '-1'
1 <= N <= 10<sup>5</sup>Print a single line containing N space separated integers representing the postorder traversal of the given treeSample Input 1:
5
2 4
5 3
-1 -1
-1 -1
-1 -1
Sample output 1:
5 3 2 4 1
Explanation: Given binary tree
1
/ \
2 4
/ \
5 3
, I have written this Solution Code: '''
class Node:
def __init__(self,key):
self.data=key
self.left=None
self.right=None
'''
c=0
n=int(input())
l=[-1]*(n+1)
r=[-1]*(n+1)
for i in range(n):
a,b=map(int,input().split())
l[i+1]=a
r[i+1]=b
def postorder(u):
global c
if c<=n:
if len(l)>u and l[u]!=-1:
postorder(l[u])
if len(r)>u and r[u]!=-1:
postorder(r[u])
print(u,end=' ')
c+=1
postorder(1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary tree, with root 1, print the postorder traversal of the tree.
Algorithm Postorder(tree)
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the rootThe input consists of N+1 lines.
First line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contains two integers denoting the left and right child of the i'th node respectively.
If the node doesn't have a left or right child, it is denoted by '-1'
1 <= N <= 10<sup>5</sup>Print a single line containing N space separated integers representing the postorder traversal of the given treeSample Input 1:
5
2 4
5 3
-1 -1
-1 -1
-1 -1
Sample output 1:
5 3 2 4 1
Explanation: Given binary tree
1
/ \
2 4
/ \
5 3
, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int l[N], r[N], c = 0;
void dfs(int u){
if(l[u] != -1)
dfs(l[u]);
if(r[u] != -1)
dfs(r[u]);
cout << u << " ";
c++;
}
signed main() {
IOS;
int n; cin >> n;
for(int i = 1; i <= n; i++){
cin >> l[i] >> r[i];
}
dfs(1);
assert(c == n);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Saloni has recently distributed N chocolates to some kids. She does not remember the number of kids she distributed the chocolates to. But she knows for sure that she did not break any chocolate while distributing. Can you tell her the number of possible values for the number of children?The first and the only line of input contains an integer N.
Constraints
1 <= N <= 10<sup>12</sup>Output a single integer, the number of possible values for the number of children.Sample Input
6
Sample Output
4
Explanation: The possible values for the number of children are 1, 2, 3, and 6.
Sample Input
10
Sample Output
4, I have written this Solution Code: import math
n=int(input())
count=0
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
if (n / i == i) :
count=count+1
else:
count=count+2
i = i + 1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Saloni has recently distributed N chocolates to some kids. She does not remember the number of kids she distributed the chocolates to. But she knows for sure that she did not break any chocolate while distributing. Can you tell her the number of possible values for the number of children?The first and the only line of input contains an integer N.
Constraints
1 <= N <= 10<sup>12</sup>Output a single integer, the number of possible values for the number of children.Sample Input
6
Sample Output
4
Explanation: The possible values for the number of children are 1, 2, 3, and 6.
Sample Input
10
Sample Output
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
int ans = 0;
for(int i=1; i*i<n; i++){
if(n%i == 0){
ans += 2;
}
}
int nn = sqrt(n);
if(nn*nn == n) {
ans++;
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Saloni has recently distributed N chocolates to some kids. She does not remember the number of kids she distributed the chocolates to. But she knows for sure that she did not break any chocolate while distributing. Can you tell her the number of possible values for the number of children?The first and the only line of input contains an integer N.
Constraints
1 <= N <= 10<sup>12</sup>Output a single integer, the number of possible values for the number of children.Sample Input
6
Sample Output
4
Explanation: The possible values for the number of children are 1, 2, 3, and 6.
Sample Input
10
Sample Output
4, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
long num = sc.nextLong();
System.out.println(possibleValues(num));
}
static int possibleValues(long num)
{
int ans = 0;
for(int i = 1; (long)i*i < num; i++)
{
if(num%i == 0)
ans += 2;
}
int nn = (int)Math.sqrt(num);
if((long)(nn*nn) == num)
ans++;
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n rectangles of the same size: w in width and h in length. It is required to find a square of the smallest size into which these rectangles can be packed. Rectangles cannot be rotated.The first line of the input contains three space-separated integers w, h and n.
<b>Constraints</b>
1 ≤ w, h, n ≤ 10<sup>9</sup>Output the minimum length of a side of a square, into which the given rectangles can be packed.Sample Input :
2 3 10
Sample Output:
9
<b>Explanation:</b>
9 is the minimum size of the square in which all these rectangles can fit.
<img src = "https://d3dyfaf3iutrxo.cloudfront.net/general/upload/7aef090772524a368cf0cbe754a484a9.png"></img>, I have written this Solution Code: #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
#define int long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(int l = LLONG_MIN, int r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
auto start = std::chrono::high_resolution_clock::now();
int w, h, n;
cin >> w >> h >> n;
int l = 1, r = (int)1e18;
while (l <= r) {
int mid = l + (r - l) / 2;
int x = mid / w;
int y = mid / h;
if ((y > 0 && x >= ((n - 1) / y + 1)) || (x > 0 && y >= ((n - 1) / x + 1))) {
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << l << "\n";
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
// cerr << "Time taken : " << ((long double)duration.count()) / ((long double)1e9) << "s " << endl;
return 0;
};
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Straight and Simple.
Given N numbers, A[1], A[2],. , A[N], find their average.
Refer <a href="https://en.wikipedia.org/wiki/Average">here</a> for better understanding of average.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, A[1]...A[N].
Constraints
1 <= N <= 300000
0 <= A[i] <= 10<sup>18</sup> (for i = 1 to N)If the average is X, report <b>floor(X)</b>.Sample Input
5
1 2 3 4 6
Sample Output
3
Explanation:
(1 + 2 + 3 + 4 + 6) / 5 = 3.2. floor(3.2) = 3.
Sample Input
5
3 60 9 28 30
Sample Output
26, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long x;
BigInteger sum = new BigInteger("0");
for(int i=0;i<n;i++){
x=sc.nextLong();
sum= sum.add(BigInteger.valueOf(x));
}
sum=sum.divide(BigInteger.valueOf(n));
System.out.print(sum);
}}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Straight and Simple.
Given N numbers, A[1], A[2],. , A[N], find their average.
Refer <a href="https://en.wikipedia.org/wiki/Average">here</a> for better understanding of average.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, A[1]...A[N].
Constraints
1 <= N <= 300000
0 <= A[i] <= 10<sup>18</sup> (for i = 1 to N)If the average is X, report <b>floor(X)</b>.Sample Input
5
1 2 3 4 6
Sample Output
3
Explanation:
(1 + 2 + 3 + 4 + 6) / 5 = 3.2. floor(3.2) = 3.
Sample Input
5
3 60 9 28 30
Sample Output
26, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n, cur = 0, rem = 0;
cin >> n;
for(int i = 1; i <= n; i++){
int p; cin >> p;
cur += (p + rem)/n;
rem = (p + rem)%n;
}
cout << cur;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Straight and Simple.
Given N numbers, A[1], A[2],. , A[N], find their average.
Refer <a href="https://en.wikipedia.org/wiki/Average">here</a> for better understanding of average.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, A[1]...A[N].
Constraints
1 <= N <= 300000
0 <= A[i] <= 10<sup>18</sup> (for i = 1 to N)If the average is X, report <b>floor(X)</b>.Sample Input
5
1 2 3 4 6
Sample Output
3
Explanation:
(1 + 2 + 3 + 4 + 6) / 5 = 3.2. floor(3.2) = 3.
Sample Input
5
3 60 9 28 30
Sample Output
26, I have written this Solution Code: n = int(input())
a =list
a=list(map(int,input().split()))
sum=0
for i in range (0,n):
sum=sum+a[i]
print(int(sum//n))
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rachel got a string as a gift from Ross. She wants to exchange this string with another string of same length. A string with all the same characters is fashionable string. Find Rachel lexicographically minimum fashionable string but make sure that the new string has at least one character common with the string Ross gave her otherwise he will get upset.Input contains a single string S, denoting the string Ross gave.
Constraints:
1 <= |S| <= 10000
S contains only lowercase english letters.Print the new string Rachel gets.Sample Input
bccde
Sample Output
bbbbb
Explanation: "bbbbb" is lexicographically minimum fashionable string which has atleast one character common with the initial string.
Sample Input
a
Sample Output
a, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine().trim();
char n[] = s.toCharArray();
int posMin = 0;
for(int i=0;i<n.length;i++){
if(n[posMin]>n[i])
posMin = i;
}
for(int i=0;i<n.length;i++)
n[i] = n[posMin];
System.out.print(String.valueOf(n));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rachel got a string as a gift from Ross. She wants to exchange this string with another string of same length. A string with all the same characters is fashionable string. Find Rachel lexicographically minimum fashionable string but make sure that the new string has at least one character common with the string Ross gave her otherwise he will get upset.Input contains a single string S, denoting the string Ross gave.
Constraints:
1 <= |S| <= 10000
S contains only lowercase english letters.Print the new string Rachel gets.Sample Input
bccde
Sample Output
bbbbb
Explanation: "bbbbb" is lexicographically minimum fashionable string which has atleast one character common with the initial string.
Sample Input
a
Sample Output
a, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
template<class C> void mini(C&a4, C b4){a4=min(a4,b4);}
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define mod 1000000007ll
#define pii pair<int,int>
/////////////
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
string s;
cin>>s;
char c='z';
for(auto r:s)
c=min(c,r);
for(int i=0;i<s.length();++i)
cout<<c;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rachel got a string as a gift from Ross. She wants to exchange this string with another string of same length. A string with all the same characters is fashionable string. Find Rachel lexicographically minimum fashionable string but make sure that the new string has at least one character common with the string Ross gave her otherwise he will get upset.Input contains a single string S, denoting the string Ross gave.
Constraints:
1 <= |S| <= 10000
S contains only lowercase english letters.Print the new string Rachel gets.Sample Input
bccde
Sample Output
bbbbb
Explanation: "bbbbb" is lexicographically minimum fashionable string which has atleast one character common with the initial string.
Sample Input
a
Sample Output
a, I have written this Solution Code: s=input()
l=len(s)
k=min(s)
m=k*l
print(m), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are 2000001 stones placed on a number line. The coordinates of these stones are −1000000, −999999, −999998, …, 999999, 1000000. Among them, some K consecutive stones are painted black, and others are painted white. Additionally, we know that the stone at coordinate X is painted black.
Print all coordinates that potentially contain a stone painted black, in ascending order.The input consists of two space-separated integers.
K X
<b>Constraints</b>
1≤K≤100
0≤X≤100
All values in the input are integers.Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.<b>Sample Input 1</b>
3 7
<b>Sample Output 1</b>
5 6 7 8 9
<b>Sample Input 2</b>
4 0
<b>Sample Output 2</b>
-3 -2 -1 0 1 2 3
<b>Sample Input 3</b>
1 100
<b>Sample Output 3</b>
100, I have written this Solution Code: #include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 1000000007
#define seg_size 262144
#define REP(a,b) for(long long a = 0;a < b;++a)
unsigned long xor128() {
static unsigned long x = time(NULL), y = 362436069, z = 521288629, w = 88675123;
unsigned long t = (x ^ (x << 11));
x = y; y = z; z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
int main() {
int k, x;
cin >> k >> x;
k--;
for (int i = x - k; i <= x + k; ++i) {
cout << i << " ";
}
cout << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N and an integer K, find and print the number of pairs of indices i, j (1 <= i < j <= N) such that A<sub>i</sub> * A<sub>j</sub> > K.First line of the input contains two integers N and K.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= K <= 10<sup>12</sup>
1 <= A<sub>i</sub> <= 10<sup>6</sup>Print the number of pairs of indices i, j (1 <= i < j <= N) such that A<sub>i</sub> * A<sub>j</sub> > K.Sample Input:
7 20
5 7 2 3 2 9 1
Sample Output:
5
Explanation:
The following pairs of indices satisfy the condition (1-based indexing)
(1, 2) -> 5 * 7 = 35
(1, 6) -> 5 * 9 = 45
(2, 4) -> 7 * 3 = 21
(2, 6) -> 7 * 9 = 63
(4, 6) -> 3 * 9 = 27
All these products are greater than K (= 20)., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 1e9;
void solve(){
int n, k;
cin >> n >> k;
vector<int> a(n);
for(auto &i : a) cin >> i;
sort(all(a));
int ans = 0;
for(int i = 0; i < n; i++){
int x = k/a[i];
if(x * a[i] < k) x++;
int l = i + 1, r = n - 1, ind = n;
while(l <= r){
int m = (l + r)/2;
if(a[m] >= x){
r = m - 1;
ind = m;
}
else l = m + 1;
}
ans += n - ind;
}
cout << ans;
}
signed main(){
fast
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
solve();
if(i != t) cout << endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N and an integer K, find and print the number of pairs of indices i, j (1 <= i < j <= N) such that A<sub>i</sub> * A<sub>j</sub> > K.First line of the input contains two integers N and K.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= K <= 10<sup>12</sup>
1 <= A<sub>i</sub> <= 10<sup>6</sup>Print the number of pairs of indices i, j (1 <= i < j <= N) such that A<sub>i</sub> * A<sub>j</sub> > K.Sample Input:
7 20
5 7 2 3 2 9 1
Sample Output:
5
Explanation:
The following pairs of indices satisfy the condition (1-based indexing)
(1, 2) -> 5 * 7 = 35
(1, 6) -> 5 * 9 = 45
(2, 4) -> 7 * 3 = 21
(2, 6) -> 7 * 9 = 63
(4, 6) -> 3 * 9 = 27
All these products are greater than K (= 20)., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long K = sc.nextLong();
long[] arr = new long[N];
for(int i=0; i<N; i++){
arr[i] = sc.nextLong();
}
Arrays.sort(arr);
int low = 0;
int high = N-1;
long count = 0;
while(low<high){
long num = arr[low]*arr[high];
if(num>K){
count += high-low;
high--;
} else {
low++;
}
}
System.out.println(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcase = Integer.parseInt(br.readLine());
for(int t=0;t<testcase;t++){
int num = Integer.parseInt(br.readLine().trim());
if(num==1)
System.out.println("No");
else if(num<=3)
System.out.println("Yes");
else{
if((num%2==0)||(num%3==0))
System.out.println("No");
else{
int flag=0;
for(int i=5;i*i<=num;i+=6){
if(((num%i)==0)||(num%(i+2)==0)){
System.out.println("No");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Yes");
}
}
}
}catch (Exception e) {
System.out.println("I caught: " + e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: t=int(input())
for i in range(t):
number = int(input())
if number > 1:
i=2
while i*i<=number:
if (number % i) == 0:
print("No")
break
i+=1
else:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n,k;
cin>>n;
long x=sqrt(n);
int cnt=0;
vector<int> v;
for(long long i=2;i<=x;i++){
if(n%i==0){
cout<<"No"<<endl;
goto f;
}}
cout<<"Yes"<<endl;
f:;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a marathon to be run on a circular track. There are N checkpoints on the track where energy drinks are placed. Checkpoints are numbered from 1 to N (both inclusive). For each checkpoint, you know the distance to the next checkpoint and also the energy replenished by the drink at this checkpoint.
Toros is going to take part in the marathon. For every 1 unit distance covered by him, his energy decreases by 1. He can't run further if he has 0 energy. Find the minimum index of the point where he should start so that he can visit every checkpoint at least once. Initially, he has 0 energy.The first line contains a single integer N, denoting the number of checkpoints.
The next N lines contain two space-separated integers, E[i] and D[i], denoting the energy replenished by the energy drink kept at the ith checkpoint and the distance to the next checkpoint.
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ E[i], D[i] ≤ 10<sup>9</sup>
Print a single integer containing the minimum index of the checkpoint where Toros should start so that he can complete the race.
If no such point exists, print -1Sample Input 1:
3
1 5
10 3
3 4
Sample Output 1:
2
Sample Input 2:-
3
2 4
5 4
2 7
Sample Output 2:-
-1
Sample Input 3:-
5
1 4
5 6
3 1
7 2
5 8
Sample Output 3:-
3
<b>Explanation 1:</b>
Toros starts at index 2 and drinks energy drinks. So, current energy = 10. At index 3, energy = 10 - 3 + 3 = 10. At index 1 energy = 10 - 4 + 1 = 7., I have written this Solution Code: #include<bits/stdc++.h>
//#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], b[N];
void solve(){
int n; cin >> n;
bool flag = 0;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
}
int l = 0, r = 0;
int sum = 0, d, c = 0;
while((l + 1)%n != r && c < 2){
d = a[l] - b[l];
if(sum + d >= 0)
l++, sum += d;
else
sum = 0, l++, r = l;
if(l == n) c++;
l %= n;
}
if(c == 2) cout << -1 << endl;
else cout << r+1 << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a marathon to be run on a circular track. There are N checkpoints on the track where energy drinks are placed. Checkpoints are numbered from 1 to N (both inclusive). For each checkpoint, you know the distance to the next checkpoint and also the energy replenished by the drink at this checkpoint.
Toros is going to take part in the marathon. For every 1 unit distance covered by him, his energy decreases by 1. He can't run further if he has 0 energy. Find the minimum index of the point where he should start so that he can visit every checkpoint at least once. Initially, he has 0 energy.The first line contains a single integer N, denoting the number of checkpoints.
The next N lines contain two space-separated integers, E[i] and D[i], denoting the energy replenished by the energy drink kept at the ith checkpoint and the distance to the next checkpoint.
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ E[i], D[i] ≤ 10<sup>9</sup>
Print a single integer containing the minimum index of the checkpoint where Toros should start so that he can complete the race.
If no such point exists, print -1Sample Input 1:
3
1 5
10 3
3 4
Sample Output 1:
2
Sample Input 2:-
3
2 4
5 4
2 7
Sample Output 2:-
-1
Sample Input 3:-
5
1 4
5 6
3 1
7 2
5 8
Sample Output 3:-
3
<b>Explanation 1:</b>
Toros starts at index 2 and drinks energy drinks. So, current energy = 10. At index 3, energy = 10 - 3 + 3 = 10. At index 1 energy = 10 - 4 + 1 = 7., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n>10000){
System.out.print(-1);
return;
}
int[] remEnrgy = new int[n];
for(int i=0;i<n;i++) {
int e = sc.nextInt();
int d = sc.nextInt();
remEnrgy[i] = e-d;
}
for(int i=0;i<n;i++) {
if(remEnrgy[i] >= 0){
if(run(remEnrgy,i)) {
System.out.print(i+1);
return;
}
}
}
System.out.print(-1);
}
private static boolean run(int[]arr,int idx) {
long enrgy = 0;
if(idx==0){
for(int i=0;i<arr.length-1;i++) {
enrgy += arr[i];
if(enrgy<0){
return false;
}
}
return true;
}
for(int i=idx;i<arr.length;i++) {
enrgy += arr[i];
if(enrgy<0){
return false;
}
}
for(int i=0;i<idx-1;i++) {
enrgy += arr[i];
if(enrgy<0){
return false;
}
}
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N candies of different colors. Find the number of different ways of selecting 1 or more candies such that the number of candies you select is not A or B.
<b>Note:</b>Two selecting ways are different if there is a candy of a particular color in only one of the selections.
As the answer can be the huge print answer 10<sup>9</sup>+7.The input contains three integers: N, A, and B.
</b>Constratins:</b>
3 ≤ N ≤ 10<sup>9</sup>
1 ≤ A < B ≤ min(N, 10<sup>5</sup>)Print answer 10<sup>9</sup>+7.Sample Input:
4 2 4
Sample Output:
8
<b>Explanation:</b>
(1) (2) (3) (4) (1, 2, 3) (1, 3, 4) (1, 2, 4) (2, 3, 4), I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
// #define int long long
#define f80 __float128
#define pii pair<int,int>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
/////////////
const int MOD=1e9+7;
int modpow(ll a, ll n) {
if(n==0) return 1;
if(n==1) return a%MOD;
if(n%2==1) return (a*modpow(a,n-1))%MOD;
ll t = modpow(a,n/2);
return (t*t)%MOD;
}
int modcmb(ll l, ll r) {
ll x=1,y=1;
rep(i,r) {
x=(x*(l-i))%MOD;
y=(y*(i+1))%MOD;
}
return (x*modpow(y,MOD-2))%MOD;
}
int main() {
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n,a,b;
cin>>n>>a>>b;
int ans=modpow(2,n)-1;
int c=modcmb(n,a);
int d=modcmb(n,b);
ans=(ans-c+MOD)%MOD;
ans=(ans-d+MOD)%MOD;
cout<<ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N candies of different colors. Find the number of different ways of selecting 1 or more candies such that the number of candies you select is not A or B.
<b>Note:</b>Two selecting ways are different if there is a candy of a particular color in only one of the selections.
As the answer can be the huge print answer 10<sup>9</sup>+7.The input contains three integers: N, A, and B.
</b>Constratins:</b>
3 ≤ N ≤ 10<sup>9</sup>
1 ≤ A < B ≤ min(N, 10<sup>5</sup>)Print answer 10<sup>9</sup>+7.Sample Input:
4 2 4
Sample Output:
8
<b>Explanation:</b>
(1) (2) (3) (4) (1, 2, 3) (1, 3, 4) (1, 2, 4) (2, 3, 4), I have written this Solution Code: MOD = int(1e9 + 7)
def modpow(a: int, n: int) -> int:
if n == 0:
return 1
elif n == 1:
return a % MOD
elif n % 2 == 1:
return (a * modpow(a, n - 1)) % MOD
else:
t = modpow(a, int(n / 2))
return (t * t) % MOD
def modcmb(left: int, right: int) -> int:
x, y = 1, 1
for i in range(0, right):
x = (x * (left - i)) % MOD
y = (y * (i + 1)) % MOD
return (x * modpow(y, MOD - 2)) % MOD
input_str = input().split()
n = int(input_str[0])
a = int(input_str[1])
b = int(input_str[2])
ans = modpow(2, n) - 1
c = modcmb(n, a)
d = modcmb(n, b)
ans = (ans - c + MOD) % MOD
ans = (ans - d + MOD) % MOD
print(ans)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N candies of different colors. Find the number of different ways of selecting 1 or more candies such that the number of candies you select is not A or B.
<b>Note:</b>Two selecting ways are different if there is a candy of a particular color in only one of the selections.
As the answer can be the huge print answer 10<sup>9</sup>+7.The input contains three integers: N, A, and B.
</b>Constratins:</b>
3 ≤ N ≤ 10<sup>9</sup>
1 ≤ A < B ≤ min(N, 10<sup>5</sup>)Print answer 10<sup>9</sup>+7.Sample Input:
4 2 4
Sample Output:
8
<b>Explanation:</b>
(1) (2) (3) (4) (1, 2, 3) (1, 3, 4) (1, 2, 4) (2, 3, 4), I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input[] = br.readLine().trim().split(" ");
long n = Long.parseLong(input[0]),
a = Long.parseLong(input[1]),
b = Long.parseLong(input[2]);
long modValue = 1000000007;
long nCa = findNCR(n, a, modValue);
long nCb = findNCR(n, b, modValue);
long result = (modValue + modValue + findPower(2, n, modValue) - 1 - nCa - nCb) % modValue;
System.out.println(result);
}
static long findNCR(long n, long a, long modValue){
long result = 1;
for(int i = 1; i <= a; i++){
result = (result * compute((n - a + i), i, modValue) % modValue) % modValue;
}
return result;
}
static long compute(long a, long b, long modValue){
return a * findPower(b, modValue-2, modValue) % modValue;
}
static long findPower(long a, long b, long modValue){
long result = 1;
while(b > 0) {
if(b % 2 == 1) result = result * a % modValue;
a = a * a % modValue;
b = b / 2;
}
return result;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array A of size N and an integer Q denoting the number of queries. Your task is to print the resultant array after applying the all queries.
The queries can be of one of the four types.
"1" Remove the first element
"2" Remove the second element
"3" Remove the last element
"4" Remove the second last element
You can be sure that the queries are always possible.First line contains an integer N - the size of array
Next line contains N space separated integers
Next line contains an integer q - the number of queries
Next line contains Q space separated integers - denoting the type of the query. These integers are in the range of 1 to 4
Constraints
1 <= N <= 100000
1 <= Ai <= 100000
1 <= Q <= 100000Print a line containing a single integer m denoting the size of array after applying all queries.
Next line should contain m space separated integers denoting the array after applying all queries.Sample Input 1:
5
1 2 3 4 5
3
1 4 2
Output:
2
2 5
Explanation:
Initial array = { 1, 2, 3, 4, 5 }
Remove the first element = { 2, 3, 4, 5 }
Remove the second last element = { 2, 3, 5 }
Remove the second element = { 2, 5 }
Sample Input 2:
3
1 2 3
1
1
Output:
2
2 3
Explanation:
Initial array = { 1, 2, 3 }
Remove the first element = { 2, 3 }, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr =new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int q = sc.nextInt();
int size = n;
int front=0;
int end=n-1;
while(q-->0){
int x = sc.nextInt();
if(x==1){
front++;
size--;
}
else if(x==2){
arr[front+1]=arr[front];
front++;
size--;
}
else if(x==3){
end--;
size--;
}
else if(x==4){
arr[end-1]=arr[end];
end--;
size--;
}
}
System.out.println(size);
for(int i=front;i<=end;i++){
System.out.print(arr[i]+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array A of size N and an integer Q denoting the number of queries. Your task is to print the resultant array after applying the all queries.
The queries can be of one of the four types.
"1" Remove the first element
"2" Remove the second element
"3" Remove the last element
"4" Remove the second last element
You can be sure that the queries are always possible.First line contains an integer N - the size of array
Next line contains N space separated integers
Next line contains an integer q - the number of queries
Next line contains Q space separated integers - denoting the type of the query. These integers are in the range of 1 to 4
Constraints
1 <= N <= 100000
1 <= Ai <= 100000
1 <= Q <= 100000Print a line containing a single integer m denoting the size of array after applying all queries.
Next line should contain m space separated integers denoting the array after applying all queries.Sample Input 1:
5
1 2 3 4 5
3
1 4 2
Output:
2
2 5
Explanation:
Initial array = { 1, 2, 3, 4, 5 }
Remove the first element = { 2, 3, 4, 5 }
Remove the second last element = { 2, 3, 5 }
Remove the second element = { 2, 5 }
Sample Input 2:
3
1 2 3
1
1
Output:
2
2 3
Explanation:
Initial array = { 1, 2, 3 }
Remove the first element = { 2, 3 }, I have written this Solution Code: def task(n,arr):
if n>4:
return
else:
if n==1:
arr=arr.pop(0)
elif n==2:
arr=arr.pop(1)
elif n==3:
del arr[-1]
else:
del arr[-2]
n=int(input())
arr=list(map(int,input().split()))
q=int(input())
qarr=list(map(int,input().split()))
for i in qarr:
task(i,arr)
print(len(arr))
print(*arr), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array A of size N and an integer Q denoting the number of queries. Your task is to print the resultant array after applying the all queries.
The queries can be of one of the four types.
"1" Remove the first element
"2" Remove the second element
"3" Remove the last element
"4" Remove the second last element
You can be sure that the queries are always possible.First line contains an integer N - the size of array
Next line contains N space separated integers
Next line contains an integer q - the number of queries
Next line contains Q space separated integers - denoting the type of the query. These integers are in the range of 1 to 4
Constraints
1 <= N <= 100000
1 <= Ai <= 100000
1 <= Q <= 100000Print a line containing a single integer m denoting the size of array after applying all queries.
Next line should contain m space separated integers denoting the array after applying all queries.Sample Input 1:
5
1 2 3 4 5
3
1 4 2
Output:
2
2 5
Explanation:
Initial array = { 1, 2, 3, 4, 5 }
Remove the first element = { 2, 3, 4, 5 }
Remove the second last element = { 2, 3, 5 }
Remove the second element = { 2, 5 }
Sample Input 2:
3
1 2 3
1
1
Output:
2
2 3
Explanation:
Initial array = { 1, 2, 3 }
Remove the first element = { 2, 3 }, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define f(n) for(int i=0;i<n;i++)
#define fo(n) for(int j=0;j<n;j++)
#define foo(n) for(int i=1;i<=n;i++)
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define vp vector<pii>
#define test int tt; cin>>tt; while(tt--)
#define mod 1000000007
void fastio()
{
ios_base::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("Input 4.txt", "r", stdin);
freopen("Output 4.txt", "w", stdout);
#endif
}
int main() {
fastio();
int n;
cin >> n;
deque<int>a;
int x;
f(n) {
cin >> x;
a.push_back(x);
}
int q;
cin >> q;
int t;
while (q--) {
cin >> t;
if (t == 1) {
a.pop_front();
}
else if (t == 2) {
int y = a.front();
a.pop_front();
a.pop_front();
a.push_front(y);
}
else if (t == 3) {
a.pop_back();
}
else {
int y = a.back();
a.pop_back();
a.pop_back();
a.push_back(y);
}
}
cout << a.size() << endl;
f(a.size()) {
cout << a[i] << " ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given a positive integer K and your task is to print the number of elements in array a[] that are smaller than or equal to K.<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>smallerElements()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N.
The second line of input contains N space- separated integers depicting the values of the array.
The third line of input contains a single integer Q, the number of queries.
Each of the next Q lines of input contain a single integer, the value of K.
<b>Constraints:-</b>
1 <= N <= 10<sup>5</sup>
1 <= K, Arr[i] <= 10<sup>12</sup>
1 <= Q <= 10<sup>4</sup>Return the count of elements smaller than or equal to K.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
1
1
3
0
5, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n;
cin>>n;
vector<int> v(n);
FOR(i,n){
cin>>v[i];}
int q;
cin>>q;
int x;
while(q--){
cin>>x;
auto it = upper_bound(v.begin(),v.end(),x);
out(it-v.begin());
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given a positive integer K and your task is to print the number of elements in array a[] that are smaller than or equal to K.<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>smallerElements()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N.
The second line of input contains N space- separated integers depicting the values of the array.
The third line of input contains a single integer Q, the number of queries.
Each of the next Q lines of input contain a single integer, the value of K.
<b>Constraints:-</b>
1 <= N <= 10<sup>5</sup>
1 <= K, Arr[i] <= 10<sup>12</sup>
1 <= Q <= 10<sup>4</sup>Return the count of elements smaller than or equal to K.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
1
1
3
0
5, I have written this Solution Code: static int smallerElements(int a[], int n, int k){
int l=0;
int h=n-1;
int m;
int ans=n;
while(l<=h){
m=l+h;
m/=2;
if(a[m]<=k){
l=m+1;
}
else{
h=m-1;
ans=m;
}
}
return ans;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code:
int DragonSlayer(int A, int B, int C,int D){
int x = C/B;
if(C%B!=0){x++;}
int y = A/D;
if(A%D!=0){y++;}
if(x<y){return 0;}
return 1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code: static int DragonSlayer(int A, int B, int C,int D){
int x = C/B;
if(C%B!=0){x++;}
int y = A/D;
if(A%D!=0){y++;}
if(x<y){return 0;}
return 1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code: int DragonSlayer(int A, int B, int C,int D){
int x = C/B;
if(C%B!=0){x++;}
int y = A/D;
if(A%D!=0){y++;}
if(x<y){return 0;}
return 1;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code:
def DragonSlayer(A,B,C,D):
x = C//B
if(C%B!=0):
x=x+1
y = A//D
if(A%D!=0):
y=y+1
if(x<y):
return 0
return 1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string(1-indexed). Print all the characters of the string at odd positions.The first line of the input contains a string S. String contains only lowercase english letters.
Constraints:-
1 <= |S| <= 100
The output should contain the character's at odd positions seperated by space.Sample Input
abcde
Sample Output
a c e
Sample Input
abcd
Sample Output
a c
Explanation:
index => 1 2 3 4
chars => a b c d
a and c are at odd index., I have written this Solution Code: str1 = input()
str2 = ''
for i in range(len(str1)):
if(i % 2 == 0):
str2 = str2 + str1[i]+" "
print(str2), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string(1-indexed). Print all the characters of the string at odd positions.The first line of the input contains a string S. String contains only lowercase english letters.
Constraints:-
1 <= |S| <= 100
The output should contain the character's at odd positions seperated by space.Sample Input
abcde
Sample Output
a c e
Sample Input
abcd
Sample Output
a c
Explanation:
index => 1 2 3 4
chars => a b c d
a and c are at odd index., I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
for(int i = 0;i<s.length();i++){
if(i%2==0){
System.out.print(s.charAt(i)+" ");
}
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string(1-indexed). Print all the characters of the string at odd positions.The first line of the input contains a string S. String contains only lowercase english letters.
Constraints:-
1 <= |S| <= 100
The output should contain the character's at odd positions seperated by space.Sample Input
abcde
Sample Output
a c e
Sample Input
abcd
Sample Output
a c
Explanation:
index => 1 2 3 4
chars => a b c d
a and c are at odd index., I have written this Solution Code: // str is input
function oddChars(str) {
// write code here
// do not console.log
// return the output as a string
return str.split('').filter((v,idx)=> idx % 2 === 0).join(' ')
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string(1-indexed). Print all the characters of the string at odd positions.The first line of the input contains a string S. String contains only lowercase english letters.
Constraints:-
1 <= |S| <= 100
The output should contain the character's at odd positions seperated by space.Sample Input
abcde
Sample Output
a c e
Sample Input
abcd
Sample Output
a c
Explanation:
index => 1 2 3 4
chars => a b c d
a and c are at odd index., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
for(int i=0;i<s.length();i++){
if(!(i&1)){cout<<s[i]<<" ";}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: t=int(input())
while t!=0:
m,n=input().split()
m,n=int(m),int(n)
for i in range(m):
arr=input().strip()
if '1' in arr:
arr='1 '*n
else:
arr='0 '*n
print(arr)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define N 1000
int a[N][N];
// Driver code
int main()
{
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
bool b[n];
for(int i=0;i<n;i++){
b[i]=false;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]==1){
b[i]=true;
}
}
}
for(int i=0;i<n;i++){
if(b[i]){
for(int j=0;j<m;j++){
cout<<1<<" ";
}}
else{
for(int j=0;j<m;j++){
cout<<0<<" ";
}
}
cout<<endl;
}
}}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(isr);
int t = Integer.parseInt(bf.readLine());
while (t-- > 0){
String inputs[] = bf.readLine().split(" ");
int m = Integer.parseInt(inputs[0]);
int n = Integer.parseInt(inputs[1]);
String[] matrix = new String[m];
for(int i=0; i<m; i++){
matrix[i] = bf.readLine();
}
StringBuffer ones = new StringBuffer("");
StringBuffer zeros = new StringBuffer("");
for(int i=0; i<n; i++){
ones.append("1 ");
zeros.append("0 ");
}
for(int i=0; i<m; i++){
if(matrix[i].contains("1")){
System.out.println(ones);
}else{
System.out.println(zeros);
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton wants to take revenge from two apples fallen on his head. So, he applies force F<sub>1</sub> on first apple (mass M<sub>1</sub>) resulting in acceleration of A<sub>1</sub> and F<sub>2</sub> on second apple (mass M<sub>2</sub>) resulting in acceleration of A<sub>2</sub>. Given M<sub>1</sub>, A<sub>1</sub>, M<sub>2</sub>, A<sub>2</sub>. Calculate total force applied by him on two apples.
<b>Note:</b> F = M*A is the equation of relation between force, mass and acceleration.First line contains four integers M<sub>1</sub>, A<sub>1</sub>, M<sub>2</sub>, A<sub>2</sub>.
1 <= M<sub>1</sub>, A<sub>1</sub>, M<sub>2</sub>, A<sub>2</sub> <= 100Output total force applied by Newton.INPUT:
1 2 3 4
OUTPUT:
14
Explanation:
Total force is equal to 1*2 + 3*4 = 14., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int m1,a1,m2,a2;
cin >> m1 >> a1 >> m2 >> a2;
cout << (m1*a1)+(m2*a2) << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcase = Integer.parseInt(br.readLine());
for(int t=0;t<testcase;t++){
int num = Integer.parseInt(br.readLine().trim());
if(num==1)
System.out.println("No");
else if(num<=3)
System.out.println("Yes");
else{
if((num%2==0)||(num%3==0))
System.out.println("No");
else{
int flag=0;
for(int i=5;i*i<=num;i+=6){
if(((num%i)==0)||(num%(i+2)==0)){
System.out.println("No");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Yes");
}
}
}
}catch (Exception e) {
System.out.println("I caught: " + e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: t=int(input())
for i in range(t):
number = int(input())
if number > 1:
i=2
while i*i<=number:
if (number % i) == 0:
print("No")
break
i+=1
else:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n,k;
cin>>n;
long x=sqrt(n);
int cnt=0;
vector<int> v;
for(long long i=2;i<=x;i++){
if(n%i==0){
cout<<"No"<<endl;
goto f;
}}
cout<<"Yes"<<endl;
f:;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan and Tusla are both students at Newton School. They are both among the best students in the class. In order to know who is better among them, a game was organised. The game consisted of L rounds, where L is an odd integer. The student winning more rounds than the other was declared the winner.
You would be given a string of odd length L in which each character is 'N' or 'T'. If the i<sup>th</sup> character is 'N', then the i<sup>th</sup> round was won by Nutan, else if the character is 'T' it was won by Tusla. Print "Nutan'' if Nutan has won more rounds than Tusla, else print "Tusla'' if Tusla has won more rounds than Nutan.
Note: You have to print everything without quotes.The first line of the input contains a single integer L — the number of rounds (1 ≤ L ≤ 100 and L is odd).
The second line contains a string S of length L. Each character of S is either 'N' or 'T'.Print "Nutan" or "Tusla" according to the input.Sample Input:
3
NNT
Sample Output:
Nutan
Explanation:
Nutan has won two games while Tusla has only won a single game, so the overall winner is Nutan., I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.System.out;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader reader = new FastReader();
int n = reader.nextInt();
String S = reader.next();
int ncount = 0;
int tcount = 0;
for (char c : S.toCharArray()) {
if (c == 'N') ncount++;
else tcount++;
}
if (ncount > tcount) {
out.print("Nutan\n");
} else {
out.print("Tusla\n");
}
out.flush();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan and Tusla are both students at Newton School. They are both among the best students in the class. In order to know who is better among them, a game was organised. The game consisted of L rounds, where L is an odd integer. The student winning more rounds than the other was declared the winner.
You would be given a string of odd length L in which each character is 'N' or 'T'. If the i<sup>th</sup> character is 'N', then the i<sup>th</sup> round was won by Nutan, else if the character is 'T' it was won by Tusla. Print "Nutan'' if Nutan has won more rounds than Tusla, else print "Tusla'' if Tusla has won more rounds than Nutan.
Note: You have to print everything without quotes.The first line of the input contains a single integer L — the number of rounds (1 ≤ L ≤ 100 and L is odd).
The second line contains a string S of length L. Each character of S is either 'N' or 'T'.Print "Nutan" or "Tusla" according to the input.Sample Input:
3
NNT
Sample Output:
Nutan
Explanation:
Nutan has won two games while Tusla has only won a single game, so the overall winner is Nutan., I have written this Solution Code: n = int(input())
s = input()
a1 = s.count('N')
a2 = s.count('T')
if(a1 > a2):
print("Nutan")
else:
print('Tusla'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan and Tusla are both students at Newton School. They are both among the best students in the class. In order to know who is better among them, a game was organised. The game consisted of L rounds, where L is an odd integer. The student winning more rounds than the other was declared the winner.
You would be given a string of odd length L in which each character is 'N' or 'T'. If the i<sup>th</sup> character is 'N', then the i<sup>th</sup> round was won by Nutan, else if the character is 'T' it was won by Tusla. Print "Nutan'' if Nutan has won more rounds than Tusla, else print "Tusla'' if Tusla has won more rounds than Nutan.
Note: You have to print everything without quotes.The first line of the input contains a single integer L — the number of rounds (1 ≤ L ≤ 100 and L is odd).
The second line contains a string S of length L. Each character of S is either 'N' or 'T'.Print "Nutan" or "Tusla" according to the input.Sample Input:
3
NNT
Sample Output:
Nutan
Explanation:
Nutan has won two games while Tusla has only won a single game, so the overall winner is Nutan., I have written this Solution Code: //Author: Xzirium
//Time and Date: 02:18:28 24 March 2022
//Optional FAST
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
//Required Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//Required namespaces
using namespace std;
using namespace __gnu_pbds;
//Required defines
#define endl '\n'
#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(long long a=b;a<c;a++)
#define FORD(a,b,c) for(long long a=b;a>c;a--)
//Required typedefs
template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <typename T> using ordered_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
//Required Constants
const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);
const long double PI=3.14159265358979;
// Required random number generators
// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
//Required Functions
ll power(ll b, ll e)
{
ll r = 1ll;
for(; e > 0; e /= 2, (b *= b) %= MOD)
if(e % 2) (r *= b) %= MOD;
return r;
}
ll modInverse(ll a)
{
return power(a,MOD-2);
}
//Work
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//-----------------------------------------------------------------------------------------------------------//
READV(N);
string S;
cin>>S;
ll n=0,t=0;
FORI(i,0,N)
{
if(S[i]=='N')
{
n++;
}
else if(S[i]=='T')
{
t++;
}
}
if(n>t)
{
cout<<"Nutan"<<endl;
}
else
{
cout<<"Tusla"<<endl;
}
//-----------------------------------------------------------------------------------------------------------//
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: void pattern_making(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
for(int j=i-1;j>=1;j--){
cout<<j<<" ";
}
cout<<endl;
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
for(int j=i-1;j>=1;j--){
cout<<j<<" ";
}
cout<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code:
void pattern_making(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
for(int j=i-1;j>=1;j--){
printf("%d ",j);
}
printf("\n");
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
for(int j=i-1;j>=1;j--){
printf("%d ",j);
}
printf("\n");
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: public static void pattern_making(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
for(int j=i-1;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
for(int j=i-1;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: function patternMaking(N)
{
for(let j=1; j <= 2*N - 1; j++) {
let k;
if(j<= N) {
k = j;
} else {
k = 2*N - j;
}
let res = "";
for(let i=1; i<=2*k-1; i++) {
if(i<=k) {
res += i + " ";
} else {
res += 2*k - i + " ";
}
}
console.log(res);
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: def pattern_making(n):
for i in range(1, n+1):
for j in range(1, i+1):
print(j,end=" ")
for j in range (1,i):
print(i-j,end=" ")
print("\r")
i=n-1
while i>=1 :
for j in range(1, i+1):
print(j,end=" ")
for j in range (1,i):
print(i-j,end=" ")
print("\r")
i=i-1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted arrays A and B of size n and m respectively, return the median of the two sorted arrays.The first line of input contains n, m the length of arrays A and B.
The next two lines contain the input of arrays A and B.
<b>Constraints</b>
1 ≤ n, m ≤ 1000
-10<sup>6</sup> ≤ A[i], B[i] ≤ 10<sup>6</sup>Print the median of two sorted arrays upto two decimal places.Sample Input :
2 1
1 3
2
Sample Output :
2.00, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.util.Arrays;
class Main {
public static double getMedia(long[]a,long[]b){
if(a.length>b.length){
long[]temp;
temp=a;
a=b;b=temp;
}
int lo=0;
int hi=a.length;
int tl=a.length+b.length;
while(lo<=hi){
int al=(lo+hi)/2;
int bl=((tl+1)/2)-al;
long alm1=(al==0)? Long.MIN_VALUE:a[al-1];
long alf=(al==a.length)? Long.MAX_VALUE:a[al];
long blm1=(bl==0)? Long.MIN_VALUE:b[bl-1];
long blf=(bl==b.length)? Long.MAX_VALUE:b[bl];
if(alm1<=blf && blm1<=alf){
double median = 0.0;
if(tl%2==0){
long lmax=Math.max(alm1,blm1);
long rmin=Math.min(alf,blf);
median=(lmax+rmin)/2.0;
return median;
}else{
long lmax=Math.max(alm1,blm1);
median=lmax;
return median;
}
}
else if(alm1>blf){
hi=al-1;
}else if(blm1>alf){
lo=al+1;
}
}
return 0;
}
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int m=Integer.parseInt(str[1]);
long[] a=new long[n];
long[] b=new long[m];
str=br.readLine().split(" ");
for(int i=0;i<n;i++){
a[i]=Long.parseLong(str[i]);
}
str=br.readLine().split(" ");
for(int i=0;i<m;i++){
b[i]=Long.parseLong(str[i]);
}
System.out.format("%.2f",getMedia(a,b));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted arrays A and B of size n and m respectively, return the median of the two sorted arrays.The first line of input contains n, m the length of arrays A and B.
The next two lines contain the input of arrays A and B.
<b>Constraints</b>
1 ≤ n, m ≤ 1000
-10<sup>6</sup> ≤ A[i], B[i] ≤ 10<sup>6</sup>Print the median of two sorted arrays upto two decimal places.Sample Input :
2 1
1 3
2
Sample Output :
2.00, I have written this Solution Code: def getMedian(arr1, arr2, n, m):
i=j=0
sort=[]
while i<n and j<m:
if arr1[i] < arr2[j]:
sort.append(arr1[i])
i+=1
else:
sort.append(arr2[j])
j+=1
while i<n:
sort.append(arr1[i])
i+=1
while j<m:
sort.append(arr2[j])
j+=1
if (n+m)%2==1:
ind=(len(sort)//2)+1
num=sort[ind-1]
else:
mid=len(sort)//2
num=(sort[mid-1]+sort[mid])/2
return num
n, m= input().split()
n, m= int(n),int(m)
ar1=list(map(int, input().split()))
ar2=list(map(int, input().split()))
print("%.2f" %getMedian(ar1, ar2, n, m)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted arrays A and B of size n and m respectively, return the median of the two sorted arrays.The first line of input contains n, m the length of arrays A and B.
The next two lines contain the input of arrays A and B.
<b>Constraints</b>
1 ≤ n, m ≤ 1000
-10<sup>6</sup> ≤ A[i], B[i] ≤ 10<sup>6</sup>Print the median of two sorted arrays upto two decimal places.Sample Input :
2 1
1 3
2
Sample Output :
2.00, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-02-02 14:05:28
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if (nums2.size() < nums1.size())
return findMedianSortedArrays(nums2, nums1);
int n1 = nums1.size();
int n2 = nums2.size();
int lo = 0, hi = n1;
while (lo <= hi) {
int cut1 = (lo + hi) / 2;
int cut2 = (n1 + n2 + 1) / 2 - cut1;
int left1 = cut1 == 0 ? INT_MIN : nums1[cut1 - 1];
int left2 = cut2 == 0 ? INT_MIN : nums2[cut2 - 1];
int right1 = cut1 == n1 ? INT_MAX : nums1[cut1];
int right2 = cut2 == n2 ? INT_MAX : nums2[cut2];
if (left1 <= right2 && left2 <= right1) {
if ((n1 + n2) % 2 == 0)
return (max(left1, left2) + min(right1, right2)) / 2.0;
else
return max(left1, left2);
} else if (left1 > right2) {
hi = cut1 - 1;
} else
lo = cut1 + 1;
}
return 0.0;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
printf("%0.2f", findMedianSortedArrays(a, b));
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define two functions G(X) and F(X) where G(X) will give us the closest prime greater than or equal to X and F(X) will give us the smallest digit (0-9) in X.
Given two integers N and M, Your task is to print the value the sum of values of F(G(i)) for each I from N to M.
<b>Note</b>:-
We have already provided you the function G(X) which returns the closest prime greater than or equal to X. To use the given function you have to call <b>next_prime(x)</b>.
<b>Note</b>:- Do not close your main class.The input contains two space- separated integers depicting the values of N and M.
Constraints:-
1 <= N <= M <= 10000Print the sum of F(G(i)) for each i from N to M.Sample Input:-
1 10
Sample Output:-
34
Explanation:-
Numbers:- 1 2 3 4 5 6 7 8 9 10
G(i):- 2 2 3 5 5 7 7 11 11 11
F(G(i)):- 2 2 3 5 5 7 7 1 1 1
Sum=34
Sample Input:-
8 11
Sample Output:-
4, I have written this Solution Code: import math
def smallest(C):
arr = []
while C != 0 :
a = C%10
arr.append(a)
C = int(C/10)
temp = 10
for i in range(len(arr)):
if arr[i] < temp:
temp = arr[i]
return temp
def isprime(B):
if B == 1:
return False
sqrt = int(math.sqrt(B))
for i in range(2, sqrt+1):
if B % i == 0:
return False
return True
def gprime(A):
for i in range(A, 100000):
if isprime(i):
return i
def numbers(N,M):
gi = []
for i in range(N,M+1):
gi.append(gprime(i))
fgi = []
for i in range(len(gi)):
fgi.append(smallest(gi[i]))
return sum(fgi)
inp = list(map(int , input().split()))
print(numbers(inp[0],inp[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define two functions G(X) and F(X) where G(X) will give us the closest prime greater than or equal to X and F(X) will give us the smallest digit (0-9) in X.
Given two integers N and M, Your task is to print the value the sum of values of F(G(i)) for each I from N to M.
<b>Note</b>:-
We have already provided you the function G(X) which returns the closest prime greater than or equal to X. To use the given function you have to call <b>next_prime(x)</b>.
<b>Note</b>:- Do not close your main class.The input contains two space- separated integers depicting the values of N and M.
Constraints:-
1 <= N <= M <= 10000Print the sum of F(G(i)) for each i from N to M.Sample Input:-
1 10
Sample Output:-
34
Explanation:-
Numbers:- 1 2 3 4 5 6 7 8 9 10
G(i):- 2 2 3 5 5 7 7 11 11 11
F(G(i)):- 2 2 3 5 5 7 7 1 1 1
Sum=34
Sample Input:-
8 11
Sample Output:-
4, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int m = sc.nextInt();
int cnt=0;
for(int i=n;i<=m;i++){
cnt+=F(next_prime(i));
}
System.out.println(cnt);
}
public static int F(int n){
int m=9;
while(n>0){
if(n%10<m){m=n%10;}
n/=10;
}
return m;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a positive integer N. Print the square of N.The input consists of a single integer N.
<b> Constraints: </b>
1 ≤ N ≤ 50Print a single integer – the value of N<sup>2</sup>.Sample Input 1:
5
Sample Output 1:
25, I have written this Solution Code:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
int n = in.nextInt();
out.println(n*n);
out.flush();
}
static FastScanner in = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static int oo = Integer.MAX_VALUE;
static long ooo = Long.MAX_VALUE;
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
void ini() throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() {
while(!st.hasMoreTokens())
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) {}
return st.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] readArrayL(int n) {
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = nextLong();
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] readArrayD(int n) {
double a[] = new double[n];
for(int i=0;i<n;i++) a[i] = nextDouble();
return a;
}
}
static final Random random = new Random();
static void ruffleSort(int[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n), temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSortL(long[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n);
long temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a positive integer N. Print the square of N.The input consists of a single integer N.
<b> Constraints: </b>
1 ≤ N ≤ 50Print a single integer – the value of N<sup>2</sup>.Sample Input 1:
5
Sample Output 1:
25, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
cout<<(n*n)<<'\n';
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a positive integer N. Print the square of N.The input consists of a single integer N.
<b> Constraints: </b>
1 ≤ N ≤ 50Print a single integer – the value of N<sup>2</sup>.Sample Input 1:
5
Sample Output 1:
25, I have written this Solution Code: n=int(input())
print(n*n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of heights of N buildings in a row. You can start from any building and jump to the <b>adjacent</b> right building till the height of the building to the right is less than or equal to the height of your current building. Find the maximum number of jumps you can make.The first line of input contains a single integer N.
The second line of input contains N integers, denoting the array height.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>
1 <= height[i] <= 10<sup>9</sup>Print the maximum number of jumps you can make.Sample Input:-
5
5 4 1 2 1
Sample Output:-
2
<b>Explanation:</b>
We start from building with height 5 then jump right to building with height 4 then again to building with height 1 making a total of 2 jumps., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
#define rep(i,n) for (int i=0; i<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[i];
}
int ans=0;
int v=0;
for(int i=1;i<n;++i){
if(a[i]>a[i-1])
v=0;
else
++v;
ans=max(ans,v);
}
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of heights of N buildings in a row. You can start from any building and jump to the <b>adjacent</b> right building till the height of the building to the right is less than or equal to the height of your current building. Find the maximum number of jumps you can make.The first line of input contains a single integer N.
The second line of input contains N integers, denoting the array height.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>
1 <= height[i] <= 10<sup>9</sup>Print the maximum number of jumps you can make.Sample Input:-
5
5 4 1 2 1
Sample Output:-
2
<b>Explanation:</b>
We start from building with height 5 then jump right to building with height 4 then again to building with height 1 making a total of 2 jumps., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
String s= br.readLine();
int[] arr= new int[num];
String[] s1 = s.split(" ");
int ccount = 0, pcount = 0;
for(int i=0;i<(num);i++)
{
arr[i]=Integer.parseInt(s1[i]);
if(i+1 < num){
arr[i+1] = Integer.parseInt(s1[i+1]);}
if(((i+1)< num) && (arr[i]>=arr[i+1]) ){
ccount++;
}else{
if(ccount > pcount){
pcount = ccount;
}
ccount = 0;
}
}
System.out.print(pcount);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of heights of N buildings in a row. You can start from any building and jump to the <b>adjacent</b> right building till the height of the building to the right is less than or equal to the height of your current building. Find the maximum number of jumps you can make.The first line of input contains a single integer N.
The second line of input contains N integers, denoting the array height.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>
1 <= height[i] <= 10<sup>9</sup>Print the maximum number of jumps you can make.Sample Input:-
5
5 4 1 2 1
Sample Output:-
2
<b>Explanation:</b>
We start from building with height 5 then jump right to building with height 4 then again to building with height 1 making a total of 2 jumps., I have written this Solution Code: N = int(input())
arr = iter(map(int,input().strip().split()))
jumps = []
jump = 0
temp = next(arr)
for i in arr:
if i<=temp:
jump += 1
temp = i
continue
temp = i
jumps.append(jump)
jump = 0
jumps.append(jump)
print(max(jumps)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer X, your task is to return the minimum number whose number of factors is equal to X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeTheNumber()</b> that takes the integer X as parameter.
<b>Constraints:</b>
1 <= X <= 15Return the minimum integer whose number of factors is equal to X.Sample Input:-
3
Sample Output:-
4
Sample Input:-
5
Sample Output:-
16, I have written this Solution Code: def MakeTheNumber(N) :
for i in range (1,10000):
cnt=0
for x in range (1,i+1):
if(i%x==0):
cnt=cnt+1
if(cnt==N):
return i
return -1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer X, your task is to return the minimum number whose number of factors is equal to X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeTheNumber()</b> that takes the integer X as parameter.
<b>Constraints:</b>
1 <= X <= 15Return the minimum integer whose number of factors is equal to X.Sample Input:-
3
Sample Output:-
4
Sample Input:-
5
Sample Output:-
16, I have written this Solution Code: int MakeTheNumber(int n){
for(int x=1;x<=10000;x++){
int cnt=0;
for(int i=1;i<=x;i++){
if(x%i==0){cnt++;}
}
if(cnt==n){
return x;}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer X, your task is to return the minimum number whose number of factors is equal to X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeTheNumber()</b> that takes the integer X as parameter.
<b>Constraints:</b>
1 <= X <= 15Return the minimum integer whose number of factors is equal to X.Sample Input:-
3
Sample Output:-
4
Sample Input:-
5
Sample Output:-
16, I have written this Solution Code: public static int MakeTheNumber(int n){
for(int x=1;x<=10000;x++){
int cnt=0;
for(int i=1;i<=x;i++){
if(x%i==0){cnt++;}
}
if(cnt==n){
return x;}
}
return -1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer X, your task is to return the minimum number whose number of factors is equal to X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeTheNumber()</b> that takes the integer X as parameter.
<b>Constraints:</b>
1 <= X <= 15Return the minimum integer whose number of factors is equal to X.Sample Input:-
3
Sample Output:-
4
Sample Input:-
5
Sample Output:-
16, I have written this Solution Code: int MakeTheNumber(int n){
for(int x=1;x<=10000;x++){
int cnt=0;
for(int i=1;i<=x;i++){
if(x%i==0){cnt++;}
}
if(cnt==n){
return x;}
}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the number of the month, your task is to calculate the number of days present in the particular month.
<b>Note:-</b>
Consider non-leap yearUser task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>numberofdays()</b> which contains M as a parameter.
<b>Constraints:-</b>
1 <= M <= 12Print the number of days in the particular month.Sample Input 1:-
1
Sample Output 1:
31
Sample Input 2:-
2
Sample Output 2:-
28, I have written this Solution Code: function numberOfDays(n)
{
let ans;
switch(n)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
ans = Number(31);
break;
case 2:
ans = Number(28);
break;
default:
ans = Number(30);
break;
}
return ans;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the number of the month, your task is to calculate the number of days present in the particular month.
<b>Note:-</b>
Consider non-leap yearUser task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>numberofdays()</b> which contains M as a parameter.
<b>Constraints:-</b>
1 <= M <= 12Print the number of days in the particular month.Sample Input 1:-
1
Sample Output 1:
31
Sample Input 2:-
2
Sample Output 2:-
28, I have written this Solution Code:
static void numberofdays(int M){
if(M==4 || M ==6 || M==9 || M==11){System.out.print(30);}
else if(M==2){System.out.print(28);}
else{
System.out.print(31);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Your task is to implement a stack using a linked list and perform given queries
Note:-if stack is already empty than pop operation will do nothing and 0 will be printed as a top element of stack if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push()</b>:- that takes the integer to be added as a parameter.
<b>pop()</b>:- that takes no parameter.
<b>top()</b> :- that takes no parameter.
Constraints:
1 <= N(number of queries) <= 10<sup>3</sup>You don't need to print anything else other than in top function in which you require to print the top most element of your stack in a new line, if the stack is empty you just need to print 0.Input:
7
push 1
push 2
top
pop
top
pop
top
Output:
2
1
0
, I have written this Solution Code:
Node top = null;
public void push(int x)
{
Node temp = new Node(x);
temp.next = top;
top = temp;
}
public void pop()
{
if (top == null) {
}
else {
top = (top).next;}
}
public void top()
{
// check for stack underflow
if (top == null) {
System.out.println("0");
}
else {
Node temp = top;
System.out.println(temp.val);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and a string S of length (N-1) containing only characters '0' and '1'. Find an array Arr which contains N integers, such that:
> All the values of the array are distinct
> Maximum value of the array can be N
> For i from 1 to N-1
if S[i] == '0'
Arr[i] > Arr[i+1]
if S[i] == '1'
Arr[i] < Arr[i+1]
> Arr is lexographically maximum possible
Note:- It can be proven that it will always be possible to create an array Arr satisfying above conditions.First line of input contains a single integer N.
Second line of input contains the string S of length N-1.
Constraints
2 <= N <= 100000
|S| = (N-1)
S contains characters '0' and '1' only.Print N space separated integers, denoting the array Arr.Sample Input 1
5
0001
Sample Output 1
5 4 3 1 2
Explanation
as S[1] == 0, A[1] > A[2]
as S[2] == 0, A[2] > A[3]
as S[3] == 0, A[3] > A[4]
as S[4] == 1, A[4] < A[5]
this is lexographically maximum array possible.
Sample Input 2
6
11100
Sample Output 2
3 4 5 6 2 1, I have written this Solution Code: k = int(input())
s = input()
left = 1
right = k
n = len(s)
c = 0
for i in range(n):
if s[i] == '0':
if c == 0:
print(right, end = " ")
right -= 1
else:
ans = ""
for j in range(c+1):
ans = str(right) + " " + ans
right -= 1
print(ans, end = "")
c = 0
else:
c += 1
ans = ""
for j in range(c+1):
ans = str(right) + " " + ans
right -= 1
print(ans, end = ""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and a string S of length (N-1) containing only characters '0' and '1'. Find an array Arr which contains N integers, such that:
> All the values of the array are distinct
> Maximum value of the array can be N
> For i from 1 to N-1
if S[i] == '0'
Arr[i] > Arr[i+1]
if S[i] == '1'
Arr[i] < Arr[i+1]
> Arr is lexographically maximum possible
Note:- It can be proven that it will always be possible to create an array Arr satisfying above conditions.First line of input contains a single integer N.
Second line of input contains the string S of length N-1.
Constraints
2 <= N <= 100000
|S| = (N-1)
S contains characters '0' and '1' only.Print N space separated integers, denoting the array Arr.Sample Input 1
5
0001
Sample Output 1
5 4 3 1 2
Explanation
as S[1] == 0, A[1] > A[2]
as S[2] == 0, A[2] > A[3]
as S[3] == 0, A[3] > A[4]
as S[4] == 1, A[4] < A[5]
this is lexographically maximum array possible.
Sample Input 2
6
11100
Sample Output 2
3 4 5 6 2 1, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
string s;
cin>>s;
int M = n;
for(int i=0;i<n-1;++i) {
int c = 0;
while(i < n && s[i] == '1') {
++i;
++c;
}
for(int j=M-c;j<=M;++j)
cout<<j<<" ";
M -= c + 1;
}
if(M) cout<<M;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and a string S of length (N-1) containing only characters '0' and '1'. Find an array Arr which contains N integers, such that:
> All the values of the array are distinct
> Maximum value of the array can be N
> For i from 1 to N-1
if S[i] == '0'
Arr[i] > Arr[i+1]
if S[i] == '1'
Arr[i] < Arr[i+1]
> Arr is lexographically maximum possible
Note:- It can be proven that it will always be possible to create an array Arr satisfying above conditions.First line of input contains a single integer N.
Second line of input contains the string S of length N-1.
Constraints
2 <= N <= 100000
|S| = (N-1)
S contains characters '0' and '1' only.Print N space separated integers, denoting the array Arr.Sample Input 1
5
0001
Sample Output 1
5 4 3 1 2
Explanation
as S[1] == 0, A[1] > A[2]
as S[2] == 0, A[2] > A[3]
as S[3] == 0, A[3] > A[4]
as S[4] == 1, A[4] < A[5]
this is lexographically maximum array possible.
Sample Input 2
6
11100
Sample Output 2
3 4 5 6 2 1, I have written this Solution Code: import java.io.*;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static long mod = 1000000007 ;
public static double epsilon=0.00000000008854;
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static long pow(long x,long y,long mod){
long ans=1;
x%=mod;
while(y>0){
if((y&1)==1){
ans=(ans*x)%mod;
}
y=y>>1;
x=(x*x)%mod;
}
return ans;
}
public static void main(String[] args) throws Exception {
int n=sc.nextInt();
char s[]=sc.nextLine().toCharArray();
int i=0,j=0;int max=n,c=0;
int f[]=new int[n];int a[]=new int[n];
for(i=0;i<n;i++){f[i]=0;a[i]=0;}
for(i=0;i<n;i++){
if(i<n-1&&s[i]=='0'){
a[i]=max;
f[max-1]=1;
max-=(c+1);
if(c!=0){
for(j=i-1;j>=i-c;j--){
a[j]=a[j+1]-1;
f[a[j+1]-2]=1;
}
}
c=0;
}
else c++;
}
if(c!=0){
for(i=n-1;i>=0;i--){
if(f[i]==0)break;
}
i++;
a[n-1]=i;
for(j=n-2;j>=n-c;j--){
a[j]=a[j+1]-1;
}
}
for( i=0;i<n;i++){
pw.print(a[i]+" ");
}
pw.print("\n");
pw.flush();
pw.close();
}
public static Comparator<Integer[]> MOquery(int block){
return
new Comparator<Integer[]>(){
@Override
public int compare(Integer a[],Integer b[]){
int a1=a[0]/block;
int b1=b[0]/block;
if(a1==b1){
if((a1&1)==1)
return a[1].compareTo(b[1]);
else{
return b[1].compareTo(a[1]);
}
}
return a1-b1;
}
};
}
public static Comparator<Long[]> column(int i){
return
new Comparator<Long[]>() {
@Override
public int compare(Long[] o1, Long[] o2) {
return o1[i].compareTo(o2[i]);
}
};
}
public static Comparator<Integer[]> column(){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
long a1=o1[0];
long a2=o2[1];
long b1=o2[0];
long b2=o1[1];
int ans=0;
if(a1*a2>b1*b2){
ans=1;
}
else ans=-1;
return ans;
}
};
}
public static Comparator<Integer[]> col(int i){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
if(o1[i]-o2[i]!=0)
return o1[i].compareTo(o2[i]);
return o1[i+1].compareTo(o2[i+1]);
}
};
}
public static Comparator<Integer> des(){
return
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
};
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
public static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code: def getMissingNo(arr, n):
total = (n+1)*(n)//2
sum_of_A = sum(arr)
return total - sum_of_A
N = int(input())
arr = list(map(int,input().split()))
one = getMissingNo(arr,N)
print(one), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n-1];
for(int i=0;i<n-1;i++){
a[i]=sc.nextInt();
}
boolean present = false;
for(int i=1;i<=n;i++){
present=false;
for(int j=0;j<n-1;j++){
if(a[j]==i){present=true;}
}
if(present==false){
System.out.print(i);
return;
}
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.