Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Hi, it's Monica!
Monica looks at her FRIENDS circle and wonders if her circle is bigger than yours. Please let her know if her friends' circle is bigger than yours, given she has a friends' circle of size 6.The first and the only line of input contains a single integer N, the size of your friends' circle.
Constraints
1 <= N <= 10Output "Yes" if the size of Monica's friends circle has more friends than yours, else output "No".Sample Input
3
Sample Output
Yes
Sample Input
10
Sample Output
No, I have written this Solution Code: n=int(input())
if(n>=6):
print("No")
else:
print("Yes"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble 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>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: static int candies(int X, int Y){
if(X<=Y){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble 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>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: int candies(int X, int Y){
if(X<=Y){return 1;}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble 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>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: def candies(X,Y):
if(X<=Y):
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble 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>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: int candies(int X, int Y){
if(X<=Y){return 1;}
return 0;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The universe contains a magic number <b>z</b>. Thor's power is known to <b>x</b> and Loki's power to be <b>y</b>.
One's strength is defined to be <b>z - a</b>, if his power is <b>a</b>. Your task is to find out who among Thor and Loki has the highest strength, and print that strength.
<b>Note:</b> The input and answer may not fit in a 32-bit integer type. In particular, if you are using C++ consider using <em>long long int</em> over <em>int</em>.The first line contains one integer t — the number of test cases.
Each test case consists of one line containing three space-separated integers x, y and z.
<b> Constraints: </b>
1 ≤ t ≤ 10<sup>4</sup>
1 ≤ x, y ≤ 10<sup>15</sup>
max(x, y) < z ≤ 10<sup>15</sup>For each test case, print a single value - the largest strength among Thor and Loki.Sample Input
2
2 3 4
1 1 5
Sample Output
2
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
long z=0,x=0,y=0;
int choice;
Scanner in = new Scanner(System.in);
choice = in.nextInt();
String s="";
int f = 1;
while(f<=choice){
x = in.nextLong();
y = in.nextLong();
z = in.nextLong();
System.out.println((long)(Math.max((z-x),(z-y))));
f++;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The universe contains a magic number <b>z</b>. Thor's power is known to <b>x</b> and Loki's power to be <b>y</b>.
One's strength is defined to be <b>z - a</b>, if his power is <b>a</b>. Your task is to find out who among Thor and Loki has the highest strength, and print that strength.
<b>Note:</b> The input and answer may not fit in a 32-bit integer type. In particular, if you are using C++ consider using <em>long long int</em> over <em>int</em>.The first line contains one integer t — the number of test cases.
Each test case consists of one line containing three space-separated integers x, y and z.
<b> Constraints: </b>
1 ≤ t ≤ 10<sup>4</sup>
1 ≤ x, y ≤ 10<sup>15</sup>
max(x, y) < z ≤ 10<sup>15</sup>For each test case, print a single value - the largest strength among Thor and Loki.Sample Input
2
2 3 4
1 1 5
Sample Output
2
4, I have written this Solution Code: n = int(input())
for i in range(n):
l = list(map(int,input().split()))
print(l[2]-min(l)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The universe contains a magic number <b>z</b>. Thor's power is known to <b>x</b> and Loki's power to be <b>y</b>.
One's strength is defined to be <b>z - a</b>, if his power is <b>a</b>. Your task is to find out who among Thor and Loki has the highest strength, and print that strength.
<b>Note:</b> The input and answer may not fit in a 32-bit integer type. In particular, if you are using C++ consider using <em>long long int</em> over <em>int</em>.The first line contains one integer t — the number of test cases.
Each test case consists of one line containing three space-separated integers x, y and z.
<b> Constraints: </b>
1 ≤ t ≤ 10<sup>4</sup>
1 ≤ x, y ≤ 10<sup>15</sup>
max(x, y) < z ≤ 10<sup>15</sup>For each test case, print a single value - the largest strength among Thor and Loki.Sample Input
2
2 3 4
1 1 5
Sample Output
2
4, 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 mod2 = 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;
}
signed main()
{
read(t);
assert(1 <= t && t <= ll(1e4));
while (t--)
{
readc(x, y, z);
assert(1 <= x && x <= ll(1e15));
assert(1 <= y && y <= ll(1e15));
assert(max(x, y) < z && z <= ll(1e15));
int r = 2*z - x - y - 1;
int l = z - max(x, y);
print(r - l + 1);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The universe contains a magic number <b>z</b>. Thor's power is known to <b>x</b> and Loki's power to be <b>y</b>.
One's strength is defined to be <b>z - a</b>, if his power is <b>a</b>. Your task is to find out who among Thor and Loki has the highest strength, and print that strength.
<b>Note:</b> The input and answer may not fit in a 32-bit integer type. In particular, if you are using C++ consider using <em>long long int</em> over <em>int</em>.The first line contains one integer t — the number of test cases.
Each test case consists of one line containing three space-separated integers x, y and z.
<b> Constraints: </b>
1 ≤ t ≤ 10<sup>4</sup>
1 ≤ x, y ≤ 10<sup>15</sup>
max(x, y) < z ≤ 10<sup>15</sup>For each test case, print a single value - the largest strength among Thor and Loki.Sample Input
2
2 3 4
1 1 5
Sample Output
2
4, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int t;
cin>>t;
while(t--)
{
int x, y, z;
cin>>x>>y>>z;
cout<<max(z - y, z- x)<<endl;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#endif
solve();
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix <b>A</b> of dimensions <b>n x m</b>. The task is to perform <b>boundary traversal</b> on the matrix in clockwise manner.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase has two lines of input. The first line contains dimensions of the matrix A, n and m. The second line contains n*m elements separated by spaces.
Constraints:
1 <= T <= 100
1 <= n, m <= 30
0 <= A[i][j] <= 100For each testcase, in a new line, print the boundary traversal of the matrix A.Input:
4
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4
12 11 10 9 8 7 6 5 4 3 2 1
1 4
1 2 3 4
4 1
1 2 3 4
Output:
1 2 3 4 8 12 16 15 14 13 9 5
12 11 10 9 5 1 2 3 4 8
1 2 3 4
1 2 3 4
Explanation:
Testcase1: The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The boundary traversal is 1 2 3 4 8 12 16 15 14 13 9 5
Testcase 2: Boundary Traversal will be 12 11 10 9 5 1 2 3 4 8.
Testcase 3: Boundary Traversal will be 1 2 3 4.
Testcase 4: Boundary Traversal will be 1 2 3 4., I have written this Solution Code:
def boundaryTraversal(matrix, N, M): #N = 3, M = 4
start_row = 0
start_col = 0
count = 0
if N != 1 and M != 1:
total = 2 * (N - 1) + 2 * (M - 1)
else:
total = max(M, N)
#First row
for i in range(start_col, M, 1): #0,1, 2, 3
count += 1
print(matrix[start_row][i], end = " ")
if count == total:
return
start_row += 1
#Last column
for i in range(start_row, N, 1): # 1, 2
count += 1
print(matrix[i][M - 1], end = " ")
if count == total:
return
M -= 1
#Last Row
for i in range(M - 1, start_col - 1, -1): # 2, 1, 0
count += 1
print(matrix[N - 1][i], end = " ")
if count == total:
return
#First Column
N -= 1 # 2
for i in range(N - 1, start_row - 1, -1):
count += 1
print(matrix[i][start_col], end = " ")
start_col += 1
testcase = int(input().strip())
for test in range(testcase):
dim = input().strip().split(" ")
N = int(dim[0])
M = int(dim[1])
matrix = []
elements = input().strip().split(" ") #N * M
start = 0
for i in range(N):
matrix.append(elements[start : start + M]) # (0, M - 1) | (M, 2*m-1)
start = start + M
boundaryTraversal(matrix, N, M)
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix <b>A</b> of dimensions <b>n x m</b>. The task is to perform <b>boundary traversal</b> on the matrix in clockwise manner.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase has two lines of input. The first line contains dimensions of the matrix A, n and m. The second line contains n*m elements separated by spaces.
Constraints:
1 <= T <= 100
1 <= n, m <= 30
0 <= A[i][j] <= 100For each testcase, in a new line, print the boundary traversal of the matrix A.Input:
4
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4
12 11 10 9 8 7 6 5 4 3 2 1
1 4
1 2 3 4
4 1
1 2 3 4
Output:
1 2 3 4 8 12 16 15 14 13 9 5
12 11 10 9 5 1 2 3 4 8
1 2 3 4
1 2 3 4
Explanation:
Testcase1: The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The boundary traversal is 1 2 3 4 8 12 16 15 14 13 9 5
Testcase 2: Boundary Traversal will be 12 11 10 9 5 1 2 3 4 8.
Testcase 3: Boundary Traversal will be 1 2 3 4.
Testcase 4: Boundary Traversal will be 1 2 3 4., I have written this Solution Code: import java.io.*;
import java.lang.*;
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n1 = sc.nextInt();
int m1 = sc.nextInt();
int arr1[][] = new int[n1][m1];
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < m1; j++)
arr1[i][j] = sc.nextInt();
}
boundaryTraversal(n1, m1,arr1);
System.out.println();
}
}
static void boundaryTraversal( int n1, int m1, int arr1[][])
{
// base cases
if(n1 == 1)
{
int i = 0;
while(i < m1)
System.out.print(arr1[0][i++] + " ");
}
else if(m1 == 1)
{
int i = 0;
while(i < n1)
System.out.print(arr1[i++][0]+" ");
}
else
{
// traversing the first row
for(int j=0;j<m1;j++)
{
System.out.print(arr1[0][j]+" ");
}
// traversing the last column
for(int j=1;j<n1;j++)
{
System.out.print(arr1[j][m1-1]+ " ");
}
// traversing the last row
for(int j=m1-2;j>=0;j--)
{
System.out.print(arr1[n1-1][j]+" ");
}
// traversing the first column
for(int j=n1-2;j>=1;j--)
{
System.out.print(arr1[j][0]+" ");
}
}
}
}, 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 find the smallest prime divisor of the given number.<b>User Task</b>
Since this is a functional problem, you don't have to worry about the input. You just have to complete the function <i>SmallestPrime()</i> which contains the given number N.
<b>Constraints:</b>
2 <= N <= 10<sup>4</sup>
<b>Note:</b>
<i>But there is a catch here given user function has already code in it which may or may not be correct, now you need to figure out these and correct if it is required</i>Return the smallest prime factor of the given number.Sample Input:-
10
Sample Output:-
2
Sample Input:-
5
Sample Output:-
5, I have written this Solution Code: static int SmallestPrime(int N)
{
for(int i=2;i<=N;i++){
if(N%i==0){
return i;}
}
return N;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Some Data types are given below:-
Integer
Long
float
Double
char
Your task is to take input in the given format and print them in the same order.You don't have to worry about input, you just have to complete the function <b>printDataTypes()</b>Print each element in a new line in the same order as the input.
Note:- <b>Print float round off to two decimal places and double to 4 decimal places.</b>Sample Input:-
2
2312351235
1.21
543.1321
c
Sample Output:-
2
2312351235
1.21
543.1321
c, I have written this Solution Code: static void printDataTypes(int a, long b, float c, double d, char e)
{
System.out.println(a);
System.out.println(b);
System.out.printf("%.2f",c);
System.out.println();
System.out.printf("%.4f",d);
System.out.println();
System.out.println(e);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Some Data types are given below:-
Integer
Long
float
Double
char
Your task is to take input in the given format and print them in the same order.You don't have to worry about input, you just have to complete the function <b>printDataTypes()</b>Print each element in a new line in the same order as the input.
Note:- <b>Print float round off to two decimal places and double to 4 decimal places.</b>Sample Input:-
2
2312351235
1.21
543.1321
c
Sample Output:-
2
2312351235
1.21
543.1321
c, I have written this Solution Code: void printDataTypes(int a, long long b, float c, double d, char e){
cout<<a<<endl;
cout<<b<<endl;
cout <<fixed<< std::setprecision(2) << c << '\n';
cout <<fixed<< std::setprecision(4) << d << '\n';
cout<<e<<endl;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Some Data types are given below:-
Integer
Long
float
Double
char
Your task is to take input in the given format and print them in the same order.You don't have to worry about input, you just have to complete the function <b>printDataTypes()</b>Print each element in a new line in the same order as the input.
Note:- <b>Print float round off to two decimal places and double to 4 decimal places.</b>Sample Input:-
2
2312351235
1.21
543.1321
c
Sample Output:-
2
2312351235
1.21
543.1321
c, I have written this Solution Code: a=int(input())
b=int(input())
x=float(input())
g = "{:.2f}".format(x)
d=float(input())
e = "{:.4f}".format(d)
u=input()
print(a)
print(b)
print(g)
print(e)
print(u), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita likes a number if it is stored in an integer while Doraemon likes it when it is stored in a String. Your task is to write a code so that they can easily convert an integer to a string or a string to an integer whenever they want.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the following functions:-
<b>StringToInt()</b> that takes String S as parameter.
<b>IntToString()</b> that takes the integer N as parameter.
Constraints:-
1 <= (Given Number) <= 100Return an integer in <b>StringToInt()</b> while return a integer integer in <b>IntToString()</b>. The driver code will print "<b>Nice Job</b>" if your code is correct otherwise "<b>Wrong answer</b>".Sample Input:-
5
Sample Output:-
Nice Job
Sample Input:-
12
Sample Output:-
Nice Job, I have written this Solution Code: def StringToInt(a):
return int(a)
def IntToString(a):
return str(a)
if __name__ == "__main__":
n = input()
s = StringToInt(n)
if n == str(s):
a=1
# print("Nice Job")
else:
print("Wrong answer")
quit()
p = IntToString(s)
if s == int(p):
print("Nice Job")
else:
print("Wrong answer"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita likes a number if it is stored in an integer while Doraemon likes it when it is stored in a String. Your task is to write a code so that they can easily convert an integer to a string or a string to an integer whenever they want.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the following functions:-
<b>StringToInt()</b> that takes String S as parameter.
<b>IntToString()</b> that takes the integer N as parameter.
Constraints:-
1 <= (Given Number) <= 100Return an integer in <b>StringToInt()</b> while return a integer integer in <b>IntToString()</b>. The driver code will print "<b>Nice Job</b>" if your code is correct otherwise "<b>Wrong answer</b>".Sample Input:-
5
Sample Output:-
Nice Job
Sample Input:-
12
Sample Output:-
Nice Job, I have written this Solution Code: static int StringToInt(String S)
{
return Integer.parseInt(S);
}
static String IntToString(int N){
return String.valueOf(N);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(st.nextToken());
boolean vis[]=new boolean[n+1];
int max=n;
StringBuilder sb=new StringBuilder();
for(int i=0;i<n;i++)
{
vis[a[i]]=true;
if(vis[max])
{
while(vis[max])
{
sb.append(max).append(" ");
max--;
}
}
sb.append("\n");
}
System.out.println(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: def Solve(arr):
size = len(arr)
queue = {}
for i in arr:
queue[i] = True
placed = []
if i == size:
placed.append(i)
size -= 1
while size in queue:
placed.append(size)
size -= 1
yield placed
N = int(input())
arr = list(map(int, input().split()))
out_ = Solve(arr)
for i_out_ in out_:
print(' '.join(map(str, i_out_))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
vector<vector<int> > SolveTower (int N, vector<int> a) {
// Write your code here
vector<vector<int> > ans;
int done = N;
priority_queue<int> pq;
for(auto x: a){
pq.push(x);
vector<int> aux;
while(!pq.empty() && pq.top() == done){
aux.push_back(done);
pq.pop();
done--;
}
ans.push_back(aux);
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
assert(a.size() == N);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
assert(1 <= N && N <= 1e6);
vector<int> a(N);
for(int i_a = 0; i_a < N; i_a++)
{
cin >> a[i_a];
}
vector<vector<int> > out_;
out_ = SolveTower(N, a);
for(int i = 0; i < out_.size(); i++){
for(int j = 0; j < out_[i].size(); j++){
cout << out_[i][j] << " ";
}
cout << "\n";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita likes a number if it is stored in an integer while Doraemon likes it when it is stored in a String. Your task is to write a code so that they can easily convert an integer to a string or a string to an integer whenever they want.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the following functions:-
<b>StringToInt()</b> that takes String S as parameter.
<b>IntToString()</b> that takes the integer N as parameter.
Constraints:-
1 <= (Given Number) <= 100Return an integer in <b>StringToInt()</b> while return a integer integer in <b>IntToString()</b>. The driver code will print "<b>Nice Job</b>" if your code is correct otherwise "<b>Wrong answer</b>".Sample Input:-
5
Sample Output:-
Nice Job
Sample Input:-
12
Sample Output:-
Nice Job, I have written this Solution Code: def StringToInt(a):
return int(a)
def IntToString(a):
return str(a)
if __name__ == "__main__":
n = input()
s = StringToInt(n)
if n == str(s):
a=1
# print("Nice Job")
else:
print("Wrong answer")
quit()
p = IntToString(s)
if s == int(p):
print("Nice Job")
else:
print("Wrong answer"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita likes a number if it is stored in an integer while Doraemon likes it when it is stored in a String. Your task is to write a code so that they can easily convert an integer to a string or a string to an integer whenever they want.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the following functions:-
<b>StringToInt()</b> that takes String S as parameter.
<b>IntToString()</b> that takes the integer N as parameter.
Constraints:-
1 <= (Given Number) <= 100Return an integer in <b>StringToInt()</b> while return a integer integer in <b>IntToString()</b>. The driver code will print "<b>Nice Job</b>" if your code is correct otherwise "<b>Wrong answer</b>".Sample Input:-
5
Sample Output:-
Nice Job
Sample Input:-
12
Sample Output:-
Nice Job, I have written this Solution Code: static int StringToInt(String S)
{
return Integer.parseInt(S);
}
static String IntToString(int N){
return String.valueOf(N);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <i>Your Grace, I feel I've been remiss in my duties. I've given you meat and wine and music, but I haven’t shown you the hospitality you deserve. My King has married and I owe my new Queen a wedding gift. </i>
Roose Bolton thinks of attacking Robb Stark, but since Catelyn has realised his traumatising idea, she signals Robb by shouting two numbers A and B. For Robb to understand the signal, he needs to solve the following problem with the two numbers:
Given A and B, find whether A can be transformed to B if A can be converted to A + fact(A) any number of times, where fact(A) is any factor of A other than 1 and A itself.
For example: A = 9, B = 14 can follow the given steps during conversion
A = 9
A = 9 + 3 = 12 (3 is a factor of 9)
A = 12 + 2 = 14 = B (2 is a factor of 12)The first and the only line of input contains two numbers A and B.
Constraints
1 <= A, B <= 10<sup>12</sup>Output "Yes" (without quotes) if we can transform A to B, else output "No" (without quotes)Sample Input
9 14
Sample Output
Yes
Explanation: In the problem statement
Sample Input
33 35
Sample Output
No
Explanation
The minimum factor of 33 is 3, so we cannot transform 33 to 35., I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.StringTokenizer;
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 sc = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
long a=sc.nextLong();
long b=sc.nextLong();
long tmp=a;
res=new ArrayList<>();
factorize(a);
if(res.size()==1&&res.get(0)==a)
{
System.out.println("No");
return;
}
if(a==b)
{
System.out.println("Yes");
return;
}
for(long i=2;i <= (long) Math.sqrt(b);i++)
{
if(b%i==0&&(b-i)>=a)
{
for(long j:res)
{
if((b-i)%j==0)
{
System.out.println("Yes");
return;
}
}
}
}
System.out.println("No");
}
static ArrayList<Long> res;
static void factorize(long n) {
int count = 0;
while (!(n % 2 > 0)) {
n >>= 1;
count++;
}
if (count > 0) {
res.add(2l);
}
for (long i = 3; i <= (long) Math.sqrt(n); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n = n / i;
}
if (count > 0) {
res.add(i);
}
}
if (n > 2) {
res.add(n);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <i>Your Grace, I feel I've been remiss in my duties. I've given you meat and wine and music, but I haven’t shown you the hospitality you deserve. My King has married and I owe my new Queen a wedding gift. </i>
Roose Bolton thinks of attacking Robb Stark, but since Catelyn has realised his traumatising idea, she signals Robb by shouting two numbers A and B. For Robb to understand the signal, he needs to solve the following problem with the two numbers:
Given A and B, find whether A can be transformed to B if A can be converted to A + fact(A) any number of times, where fact(A) is any factor of A other than 1 and A itself.
For example: A = 9, B = 14 can follow the given steps during conversion
A = 9
A = 9 + 3 = 12 (3 is a factor of 9)
A = 12 + 2 = 14 = B (2 is a factor of 12)The first and the only line of input contains two numbers A and B.
Constraints
1 <= A, B <= 10<sup>12</sup>Output "Yes" (without quotes) if we can transform A to B, else output "No" (without quotes)Sample Input
9 14
Sample Output
Yes
Explanation: In the problem statement
Sample Input
33 35
Sample Output
No
Explanation
The minimum factor of 33 is 3, so we cannot transform 33 to 35., I have written this Solution Code: import math
arr=[int (x) for x in input().split()]
a,b=arr[0],arr[1]
if a>=b:
print("No")
else:
m=-1
i=2
while i**2<=a:
if a%i==0:
m=i
break
i+=1
if m==-1:
print("No")
elif math.gcd(a,b)!=1:
print("Yes")
else:
a=a+m
if a<b and math.gcd(a,b)!=1:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <i>Your Grace, I feel I've been remiss in my duties. I've given you meat and wine and music, but I haven’t shown you the hospitality you deserve. My King has married and I owe my new Queen a wedding gift. </i>
Roose Bolton thinks of attacking Robb Stark, but since Catelyn has realised his traumatising idea, she signals Robb by shouting two numbers A and B. For Robb to understand the signal, he needs to solve the following problem with the two numbers:
Given A and B, find whether A can be transformed to B if A can be converted to A + fact(A) any number of times, where fact(A) is any factor of A other than 1 and A itself.
For example: A = 9, B = 14 can follow the given steps during conversion
A = 9
A = 9 + 3 = 12 (3 is a factor of 9)
A = 12 + 2 = 14 = B (2 is a factor of 12)The first and the only line of input contains two numbers A and B.
Constraints
1 <= A, B <= 10<sup>12</sup>Output "Yes" (without quotes) if we can transform A to B, else output "No" (without quotes)Sample Input
9 14
Sample Output
Yes
Explanation: In the problem statement
Sample Input
33 35
Sample Output
No
Explanation
The minimum factor of 33 is 3, so we cannot transform 33 to 35., 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
int small_prime(int x) {
for(int i=2; i*i<=x; i++){
if(x%i==0){
return i;
}
}
return -1;
}
void solve(){
int a, b;
cin>>a>>b;
int aa = a, bb = b;
int d1 = small_prime(a);
int d2 = small_prime(b);
if (d1 == -1 || d2 == -1) {
cout<<"No";
return;
}
if ((a % 2) == 1) {
a += d1;
}
if ((b % 2) == 1) {
b -= d2;
}
if (a > b) {
cout<<"No";
return;
}
cout<<"Yes";
}
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: A number is said to be perfect if the sum of its digits is divisible by 5. Given a number N check if it is perfect 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>PerfectNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return 1 if the given number is perfect else return 0.Sample Input:-
122
Sample Output:-
1
Explanation:-
1 + 2 + 2 = 5 which is divisible by 5
Sample Input:-
123
Sample Output:-
0, I have written this Solution Code: int PerfectNumber(int N){
int ans=0;
while(N>0){
ans+=N%10;
N/=10;
}
if(ans%5==0){return 1;}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is said to be perfect if the sum of its digits is divisible by 5. Given a number N check if it is perfect 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>PerfectNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return 1 if the given number is perfect else return 0.Sample Input:-
122
Sample Output:-
1
Explanation:-
1 + 2 + 2 = 5 which is divisible by 5
Sample Input:-
123
Sample Output:-
0, I have written this Solution Code: int PerfectNumber(int N){
int ans=0;
while(N>0){
ans+=N%10;
N/=10;
}
if(ans%5==0){return 1;}
return 0;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is said to be perfect if the sum of its digits is divisible by 5. Given a number N check if it is perfect 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>PerfectNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return 1 if the given number is perfect else return 0.Sample Input:-
122
Sample Output:-
1
Explanation:-
1 + 2 + 2 = 5 which is divisible by 5
Sample Input:-
123
Sample Output:-
0, I have written this Solution Code: static int PerfectNumber(int N){
int ans=0;
while(N>0){
ans+=N%10;
N/=10;
}
if(ans%5==0){return 1;}
return 0;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is said to be perfect if the sum of its digits is divisible by 5. Given a number N check if it is perfect 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>PerfectNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return 1 if the given number is perfect else return 0.Sample Input:-
122
Sample Output:-
1
Explanation:-
1 + 2 + 2 = 5 which is divisible by 5
Sample Input:-
123
Sample Output:-
0, I have written this Solution Code: def PerfectNumber(N):
cnt=0
while N>0:
cnt=cnt+int(N%10)
N=int(N/10)
if (cnt%5==0):
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<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>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: function Charity(n,m) {
// write code here
// do no console.log the answer
// return the output using return keyword
const per = Math.floor(m / n)
return per > 1 ? per : -1
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<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>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: static int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<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>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<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>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: def Charity(N,M):
x = M//N
if x<=1:
return -1
return x
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<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>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the level in a binary tree which has the maximum number of nodes. The root is at level 0.
If multiple levels have the highest amount of nodes, print the lowest level.The first line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contain 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'
Constraints
1 <= N <= 100000Print the level number with maximum nodes.
If multiple levels have the highest amount of nodes, print the lowest level.Sample Input:
7
2 4
5 3
-1 -1
-1 7
6 -1
-1 -1
-1 -1
Sample output:
2
Explanation: Given binary tree
1
/ \
2 4
/ \ \
5 3 7 <-- Level 2
/
6
Level 0 has 1 node
Level 1 has 2 nodes
Level 2 has 3 nodes
Level 3 has 1 node, I have written this Solution Code: #include "bits/stdc++.h"
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 = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int l[N], r[N], p[N];
void dfs(int u, int dep){
p[dep]++;
if(l[u] != -1)
dfs(l[u], dep+1);
if(r[u] != -1)
dfs(r[u], dep+1);
}
void solve(){
int n;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> l[i] >> r[i];
dfs(1, 0);
int mx = 0, id = -1;
for(int i = 0; i <= n; i++){
if(p[i] > mx){
mx = p[i];
id = i;
}
}
cout << id;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: As always, Sid has come up with a new problem on Number Theory. Since it is too tricky to solve, can you help him. A bonus, you'll get a chapo for solving it right.
Given an integer n, let F(n) be the product of all positive integers i less or equal than n such that n and i are co-prime.
Now you're given an integer X, you need to find the sum of (F(i) modulo i) for all positive integers i less than or equal to X. If the answer is A, report the value of (A % 1000000007).The only line of input contains a single integer X.
Constraints
2 <= X <= 500000000Output a single integer, the required output for the problem.Sample Input
5
Sample Output
10
Sample Input
1000
Sample Output
128825, 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(ll 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 int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
#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
const int M = 1000000;
int Lower[M+1], Higher[M+1];
char Used[M+1];
bool prime[100001];
void sieve(){
memset(prime, true, sizeof(prime));
for(int i=2; i<=100000; i++){
if(!prime[i])
continue;
for(int j=i*i; j<=100000; j+=i)
prime[j]=false;
}
}
long long PrimePi(long long n)
{
long long v = sqrt(n+1e-9), p, temp, q, j, end, i, d, t;
for(int i=0;i<=M;i++)Used[i]=0;
Higher[1] = n - 1;
for(p = 2; p <= v; p++) {
Lower[p] = p - 1;
Higher[p] = (n/p) - 1;
}
for(p = 2; p <= v; p++) {
if(Lower[p] == Lower[p-1])
continue;
temp = Lower[p-1];
q = p * p;
Higher[1] -= Higher[p] - temp;
j = 1 + (p & 1);
end = (v <= n/q) ? v : n/q;
for(i = p + j; i <= 1 + end; i += j) {
if(Used[i])
continue;
d = i * p;
if(d <= v)
Higher[i] -= Higher[d] - temp;
else {
t = n/d;
Higher[i] -= Lower[t] - temp;
}
}
if(q <= v)
for(i = q; i <= end; i += p*j)
Used[i] = 1;
for(i = v; i >= q; i--) {
t = i/p;
Lower[i] -= Lower[t] - temp;
}
}
return Higher[1];
}
int psum(int n){
unordered_map<int, int> s;
vector<int> v;
int r = sqrt(n);
for(int i=1; i<=r; i++){
int x = n/i;
v.pb(x);
}
int y = n/r;
for(int i=y-1; i>0; i--){
v.pb(i);
}
for(int i: v){
s[i] = (i*(i+1))/2 - 1;
}
for(int p=2; p<=r; p++){
if(s[p] > s[p-1]){
int sp = s[p-1];
int p2 = p*p;
for(int i: v){
if(i<p2)
break;
s[i] -= (p * (s[i/p] - sp));
}
}
}
return s[n];
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int n; cin>>n;
int ans = psum(n)-2*PrimePi(n)+psum(n/2)*2-2*PrimePi(n/2)+n-1;
sieve();
for(int i=3; i<30000; i++){
if(!prime[i])
continue;
int x = i*i;
while(x <= n){
// trace(x);
ans += (x-2);
x *= i;
}
x = i*i*2;
while(x <= n){
// trace(x);
ans += (x-2);
x *= i;
}
ans %= MOD;
}
cout<<ans;
end_routine();
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each i (1 < = i < = N), you have to print the number except :-
For each multiple of 3, print "Newton" instead of the number.
For each multiple of 5, print "School" instead of the number.
For numbers that are multiples of both 3 and 5, print "NewtonSchool" instead of the number.The first line of the input contains N.
<b>Constraints</b>
1 < = N < = 1000
Print N space separated number or Newton School according to the condition.Sample Input:-
3
Sample Output:-
1 2 Newton
Sample Input:-
5
Sample Output:-
1 2 Newton 4 School, I have written this Solution Code: n=int(input())
for i in range(1,n+1):
if i%3==0 and i%5==0:
print("NewtonSchool",end=" ")
elif i%3==0:
print("Newton",end=" ")
elif i%5==0:
print("School",end=" ")
else:
print(i,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each i (1 < = i < = N), you have to print the number except :-
For each multiple of 3, print "Newton" instead of the number.
For each multiple of 5, print "School" instead of the number.
For numbers that are multiples of both 3 and 5, print "NewtonSchool" instead of the number.The first line of the input contains N.
<b>Constraints</b>
1 < = N < = 1000
Print N space separated number or Newton School according to the condition.Sample Input:-
3
Sample Output:-
1 2 Newton
Sample Input:-
5
Sample Output:-
1 2 Newton 4 School, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static void NewtonSchool(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.print("NewtonSchool ");}
else if(i%5==0){System.out.print("School ");}
else if(i%3==0){System.out.print("Newton ");}
else{System.out.print(i+" ");}
}
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int x= sc.nextInt();
NewtonSchool(x);
}
}, 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 find the smallest prime divisor of the given number.<b>User Task</b>
Since this is a functional problem, you don't have to worry about the input. You just have to complete the function <i>SmallestPrime()</i> which contains the given number N.
<b>Constraints:</b>
2 <= N <= 10<sup>4</sup>
<b>Note:</b>
<i>But there is a catch here given user function has already code in it which may or may not be correct, now you need to figure out these and correct if it is required</i>Return the smallest prime factor of the given number.Sample Input:-
10
Sample Output:-
2
Sample Input:-
5
Sample Output:-
5, I have written this Solution Code: static int SmallestPrime(int N)
{
for(int i=2;i<=N;i++){
if(N%i==0){
return i;}
}
return N;
}, In this Programming Language: Java, 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: A pair is called lucky if its sum is even and positive. Given three numbers find if there exists a lucky pair or not.The only line contains three integers a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>
<b>Constraints:</b>
-10<sup>9</sup> <= a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub> <= 10<sup>9</sup>Print "YES" without quotes if there exists a lucky pair otherwise print "NO" without quotes.Sample Input 1:
23 32 12
Sample Output 1:
YES
Sample Input 2:
1 -1 2
Sample Output 2:
NO, I have written this Solution Code: //HEADER FILES AND NAMESPACES
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target("popcnt")
using namespace std;
using namespace __gnu_pbds;
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_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// DEFINE STATEMENTS
const long long infty = 1e18;
#define num1 1000000007
#define num2 998244353
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define REPd(i,a,n) for(ll i=a; i>=n; i--)
#define pb push_back
#define pob pop_back
#define fr first
#define sc second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)
#define popcount __builtin_popcountll
#define fileio(x) freopen("input.txt", "r", stdin); freopen(x, "w", stdout);
#define out(x) cout << ((x) ? "Yes\n" : "No\n")
#define sz(x) x.size()
typedef long long ll;
typedef long long unsigned int llu;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;
void solve(){
vector<ll>a(3);
for(ll i = 0;i<3;i++)cin >> a[i];
for(ll i = 0;i<3;i++){
for(ll j = i+1;j<3;j++){
if((a[i]+a[j])>0 && (a[i]+a[j])%2 == 0){
cout << "YES\n";
return;
}
}
}
cout << "NO\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// cout.tie(NULL);
#ifdef LOCALFLAG
freopen("Input.txt", "r", stdin);
freopen("Output2.txt", "w", stdout);
#endif
ll t = 1;
//cin >> t;
while(t--){
solve();
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day(including), for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.
Explanation to the given example:
On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 2nd day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
Now on 3rd day we observe that stock price for last two consecutive days is less than or equal to current day i.e. (60, 70) thus count is 2
On 4th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 5th day we observe that stock price for last four consecutive days is less than or equal to current day i.e. (60, 70, 60, 75) thus count is 4.
On 6th day we observe that stock price for last six consecutive days is less than or equal to current day i.e. (80, 60, 70, 60, 75, 85) thus count is 6.The first line of each test case is N, N is the size of the array. The second line of each test case contains N input A[i].
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000For each test case, print the span values for all days.Input
7
100 80 60 70 60 75 85
Output
1 1 1 2 1 4 6
Input
6
10 4 5 90 120 80
Output
1 1 2 4 5 1
Explanation:
Test case 1:
On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 2nd day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
Now on 3rd day we observe that stock price for last two consecutive days is less than or equal to current day i.e. (60, 70) thus count is 2
On 4th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 5th day we observe that stock price for last four consecutive days is less than or equal to current day i.e. (60, 70, 60, 75) thus count is 4.
On 6th day we observe that stock price for last six consecutive days is less than or equal to current day i.e. (80, 60, 70, 60, 75, 85) thus count is 6., 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 size = Integer.parseInt(br.readLine());
String st = br.readLine();
String[] stArr = st.trim().split(" ");
int[] arr = new int[size];
for(int i=0; i<size; i++){
arr[i] = Integer.parseInt(stArr[i]);
}
Stack<Integer> stack = new Stack<>();
StringBuilder sb=new StringBuilder("");
for(int i=0; i<size; i++){
int count = 0;
if(stack.isEmpty()){
stack.push(i);
sb.append("1 ");
} else if(arr[stack.peek()] > arr[i]){
stack.push(i);
sb.append("1 ");
} else{
while(!stack.isEmpty() && arr[stack.peek()] <= arr[i]){
stack.pop();
}
if(stack.isEmpty()){
count = i+1;
} else{
count = i - stack.peek();
}
sb.append(count+" ");
stack.push(i);
}
}
System.out.print(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day(including), for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.
Explanation to the given example:
On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 2nd day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
Now on 3rd day we observe that stock price for last two consecutive days is less than or equal to current day i.e. (60, 70) thus count is 2
On 4th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 5th day we observe that stock price for last four consecutive days is less than or equal to current day i.e. (60, 70, 60, 75) thus count is 4.
On 6th day we observe that stock price for last six consecutive days is less than or equal to current day i.e. (80, 60, 70, 60, 75, 85) thus count is 6.The first line of each test case is N, N is the size of the array. The second line of each test case contains N input A[i].
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000For each test case, print the span values for all days.Input
7
100 80 60 70 60 75 85
Output
1 1 1 2 1 4 6
Input
6
10 4 5 90 120 80
Output
1 1 2 4 5 1
Explanation:
Test case 1:
On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 2nd day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
Now on 3rd day we observe that stock price for last two consecutive days is less than or equal to current day i.e. (60, 70) thus count is 2
On 4th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 5th day we observe that stock price for last four consecutive days is less than or equal to current day i.e. (60, 70, 60, 75) thus count is 4.
On 6th day we observe that stock price for last six consecutive days is less than or equal to current day i.e. (80, 60, 70, 60, 75, 85) thus count is 6., I have written this Solution Code: n = int(input())
lst = list(map(int, input().strip().split()))
S = [0]*n
st = []
st.append(0)
for i in range(n):
while( len(st) > 0 and lst[st[-1]] <= lst[i]):
st.pop()
S[i] = i + 1 if len(st) <= 0 else (i - st[-1])
st.append(i)
for i in range(0, n):
print (S[i], end =" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day(including), for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.
Explanation to the given example:
On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 2nd day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
Now on 3rd day we observe that stock price for last two consecutive days is less than or equal to current day i.e. (60, 70) thus count is 2
On 4th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 5th day we observe that stock price for last four consecutive days is less than or equal to current day i.e. (60, 70, 60, 75) thus count is 4.
On 6th day we observe that stock price for last six consecutive days is less than or equal to current day i.e. (80, 60, 70, 60, 75, 85) thus count is 6.The first line of each test case is N, N is the size of the array. The second line of each test case contains N input A[i].
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000For each test case, print the span values for all days.Input
7
100 80 60 70 60 75 85
Output
1 1 1 2 1 4 6
Input
6
10 4 5 90 120 80
Output
1 1 2 4 5 1
Explanation:
Test case 1:
On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 2nd day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
Now on 3rd day we observe that stock price for last two consecutive days is less than or equal to current day i.e. (60, 70) thus count is 2
On 4th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens.
On 5th day we observe that stock price for last four consecutive days is less than or equal to current day i.e. (60, 70, 60, 75) thus count is 4.
On 6th day we observe that stock price for last six consecutive days is less than or equal to current day i.e. (80, 60, 70, 60, 75, 85) thus count is 6., 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 a[N];
signed main() {
IOS;
int n; cin >> n;
stack<int> s;
a[0] = inf;
s.push(0);
for(int i = 1; i <= n; i++){
cin >> a[i];
while(!s.empty() && a[s.top()] <= a[i])
s.pop();
cout << i - s.top() << " ";
s.push(i);
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(st.nextToken());
boolean vis[]=new boolean[n+1];
int max=n;
StringBuilder sb=new StringBuilder();
for(int i=0;i<n;i++)
{
vis[a[i]]=true;
if(vis[max])
{
while(vis[max])
{
sb.append(max).append(" ");
max--;
}
}
sb.append("\n");
}
System.out.println(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: def Solve(arr):
size = len(arr)
queue = {}
for i in arr:
queue[i] = True
placed = []
if i == size:
placed.append(i)
size -= 1
while size in queue:
placed.append(size)
size -= 1
yield placed
N = int(input())
arr = list(map(int, input().split()))
out_ = Solve(arr)
for i_out_ in out_:
print(' '.join(map(str, i_out_))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
vector<vector<int> > SolveTower (int N, vector<int> a) {
// Write your code here
vector<vector<int> > ans;
int done = N;
priority_queue<int> pq;
for(auto x: a){
pq.push(x);
vector<int> aux;
while(!pq.empty() && pq.top() == done){
aux.push_back(done);
pq.pop();
done--;
}
ans.push_back(aux);
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
assert(a.size() == N);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
assert(1 <= N && N <= 1e6);
vector<int> a(N);
for(int i_a = 0; i_a < N; i_a++)
{
cin >> a[i_a];
}
vector<vector<int> > out_;
out_ = SolveTower(N, a);
for(int i = 0; i < out_.size(); i++){
for(int j = 0; j < out_[i].size(); j++){
cout << out_[i][j] << " ";
}
cout << "\n";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: n=int(input())
a=map(int,input().split())
b=[]
mx=-200000
cnt=0
for i in a:
if i>mx:
cnt+=1
mx=i
print(cnt), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: function numberOfRoofs(arr)
{
let count=1;
let max = arr[0];
for(let i=1;i<arrSize;i++)
{
if(arr[i] > max)
{
count++;
max = arr[i];
}
}
return count;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int count=1;
int max = a[0];
for(int i=1;i<n;i++)
{
if(a[i] > max)
{
count++;
max = a[i];
}
}
System.out.println(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to score well in his upcoming test, but he is not able to solve the simple division problems, seeing Nobita's determination Doraemon gives him a gadget that can do division problems easily but somehow Nobita deleted the internal program which calculates the division.
As an excellent coder, Nobita came to you for help. Help Nobita to write a code for his gadget.
You will be given two integers <b>D</b> and <b>Q</b>, you have to print the value of <b>D/Q</b> rounded down .The input contains two space- separated integers depicting the values of D and Q.
Constraints:-
0 <= D, Q <= 100Print the values of D/Q if the value can be calculated else print -1 if it is undefined.
Note:- Remember division by 0 is an undefined value that will give runtime error in your program.Sample Input:-
9 3
Sample Output:-
3
Sample Input:-
8 5
Sample Output:-
1
Explanation:-
8/5 = 1.6 = 1(floor), I have written this Solution Code: import java.io.*;
import java.util.*;
import java.lang.Math.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] st = bf.readLine().split(" ");
if(Integer.parseInt(st[1])==0)
System.out.print(-1);
else {
int f = (Integer.parseInt(st[0])/Integer.parseInt(st[1]));
System.out.print(f);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to score well in his upcoming test, but he is not able to solve the simple division problems, seeing Nobita's determination Doraemon gives him a gadget that can do division problems easily but somehow Nobita deleted the internal program which calculates the division.
As an excellent coder, Nobita came to you for help. Help Nobita to write a code for his gadget.
You will be given two integers <b>D</b> and <b>Q</b>, you have to print the value of <b>D/Q</b> rounded down .The input contains two space- separated integers depicting the values of D and Q.
Constraints:-
0 <= D, Q <= 100Print the values of D/Q if the value can be calculated else print -1 if it is undefined.
Note:- Remember division by 0 is an undefined value that will give runtime error in your program.Sample Input:-
9 3
Sample Output:-
3
Sample Input:-
8 5
Sample Output:-
1
Explanation:-
8/5 = 1.6 = 1(floor), I have written this Solution Code: D,Q = input().split()
D = int(D)
Q = int(Q)
if(0<=D and Q<=100 and Q >0):
print(int(D/Q))
else:
print('-1'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to score well in his upcoming test, but he is not able to solve the simple division problems, seeing Nobita's determination Doraemon gives him a gadget that can do division problems easily but somehow Nobita deleted the internal program which calculates the division.
As an excellent coder, Nobita came to you for help. Help Nobita to write a code for his gadget.
You will be given two integers <b>D</b> and <b>Q</b>, you have to print the value of <b>D/Q</b> rounded down .The input contains two space- separated integers depicting the values of D and Q.
Constraints:-
0 <= D, Q <= 100Print the values of D/Q if the value can be calculated else print -1 if it is undefined.
Note:- Remember division by 0 is an undefined value that will give runtime error in your program.Sample Input:-
9 3
Sample Output:-
3
Sample Input:-
8 5
Sample Output:-
1
Explanation:-
8/5 = 1.6 = 1(floor), I have written this Solution Code: #include <iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
if(m==0){cout<<-1;return 0;}
cout<<n/m;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string your task is to reverse the given string.The first line of the input contains a string.
Constraints:-
1 <= string length <= 100
String contains only lowercase english lettersThe output should contain reverse of the input string.Sample Input
abc
Sample Output
cba, I have written this Solution Code: s = input("")
print (s[::-1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string your task is to reverse the given string.The first line of the input contains a string.
Constraints:-
1 <= string length <= 100
String contains only lowercase english lettersThe output should contain reverse of the input string.Sample Input
abc
Sample Output
cba, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
reverse(s.begin(),s.end());
cout<<s;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string your task is to reverse the given string.The first line of the input contains a string.
Constraints:-
1 <= string length <= 100
String contains only lowercase english lettersThe output should contain reverse of the input string.Sample Input
abc
Sample Output
cba, 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 = s.length()-1;i>=0;i--){
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: Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximise the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i] amount of money present in it.The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each test case contains an integer n which denotes the number of houses. Next line contains space separated numbers denoting the amount of money in each house.
1 <= T <= 100
1 <= n <= 10^4
1 <= a[i] <= 10^4For each testcase, in a newline, print an integer which denotes the maximum amount he can take home.Sample Input:
2
6
5 5 10 100 10 5
3
1 2 3
Sample Output:
110
4
Explanation:
Testcase1:
5 + 100 + 5 = 110
Testcase2:
1 + 3 = 4, I have written this Solution Code: def maximize_loot(hval, n):
if n == 0:
return 0
value1 = hval[0]
if n == 1:
return value1
value2 = max(hval[0], hval[1])
if n == 2:
return value2
max_val = None
for i in range(2, n):
max_val = max(hval[i]+value1, value2)
value1 = value2
value2 = max_val
return max_val
t=int(input())
for l in range(0,t):
n=int(input())
arr=input().split()
one=0
two=0
for i in range(0,n):
arr[i]=int(arr[i])
print (maximize_loot(arr,n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximise the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i] amount of money present in it.The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each test case contains an integer n which denotes the number of houses. Next line contains space separated numbers denoting the amount of money in each house.
1 <= T <= 100
1 <= n <= 10^4
1 <= a[i] <= 10^4For each testcase, in a newline, print an integer which denotes the maximum amount he can take home.Sample Input:
2
6
5 5 10 100 10 5
3
1 2 3
Sample Output:
110
4
Explanation:
Testcase1:
5 + 100 + 5 = 110
Testcase2:
1 + 3 = 4, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define endl '\n'
#define pi pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define fi first
#define se 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 = 1e4 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], dp[N][2];
void solve(){
int n; cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
dp[i][0] = max(dp[i-1][1], dp[i-1][0]);
dp[i][1] = a[i] + dp[i-1][0];
}
cout << max(dp[n][1], dp[n][0]) << 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: Consider the integer sequence A = {1, 2, 3, ...., N} i.e. the first N natural numbers in order.
You are now given two integers, L and S. Determine whether there exists a subarray with length L and sum S after removing <b>at most one</b> element from A.
A <b>subarray</b> of an array is a non-empty sequence obtained by removing zero or more elements from the front of the array, and zero or more elements from the back of the array.The first line contains a single integer T, the number of test cases.
T lines follow. Each line describes a single test case and contains three integers: N, L, and S.
<b>Constraints:</b>
1 <= T <= 100
2 <= N <= 10<sup>9</sup>
1 <= L <= N-1
1 <= S <= 10<sup>18</sup> <b> (Note that S will not fit in a 32-bit integer) </b>For each testcase, print "YES" (without quotes) if a required subarray can exist, and "NO" (without quotes) otherwise.Sample Input:
3
5 3 11
5 3 5
5 3 6
Sample Output:
YES
NO
YES
Sample Explanation:
For the first test case, we can remove 3 from A to obtain A = {1, 2, 4, 5} where {2, 4, 5} is a required subarray of size 3 and sum 11., I have written this Solution Code: //HEADER FILES AND NAMESPACES
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include <sys/resource.h>
using namespace std;
using namespace __gnu_pbds;
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_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// DEFINE STATEMENTS
const long long INF = 1e18;
const int INFINT = INT_MAX/2;
#define num1 1000000007
#define num2 998244353
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define REPd(i,a,n) for(ll i=a; i>=n; i--)
#define pb push_back
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)
#define popcount __builtin_popcountll
#define fileio(x, y) freopen(x, "r", stdin); freopen(y, "w", stdout);
#define out(x) cout << ((x) ? "YES\n" : "NO\n")
#define CASE(x, y) cout << "Case #" << x << ":" << " \n"[y]
#define start_clock() auto start_time = std::chrono::high_resolution_clock::now(); auto end_time = start_time;
#define measure() end_time = std::chrono::high_resolution_clock::now(); cerr << (end_time - start_time)/std::chrono::milliseconds(1) << "ms" << endl;
#define reset_clock() start_time = std::chrono::high_resolution_clock::now();
typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;
// DEBUG FUNCTIONS
#ifdef LOCALY
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}"[it+1==a.end()];
}
template<typename T>
void __p(std::set<T> a) {
cout<<"{";
for(auto it=a.begin(); it!=a.end();){
__p(*it);
cout<<",}"[++it==a.end()];
}
}
template<typename T>
void __p(std::multiset<T> a) {
cout<<"{";
for(auto it=a.begin(); it!=a.end();){
__p(*it);
cout<<",}"[++it==a.end()];
}
}
template<typename T, typename F>
void __p(std::map<T,F> a) {
cout<<"{\n";
for(auto it=a.begin(); it!=a.end();++it)
{
__p(it->first);
cout << ": ";
__p(it->second);
cout<<"\n";
}
cout << "}\n";
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
// DEBUG FUNCTIONS END
// CUSTOM HASH TO SPEED UP UNORDERED MAP AND TO AVOID FORCED CLASHES
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // FOR RANDOM NUMBER GENERATION
ll mod_exp(ll a, ll b, ll c)
{
ll res=1; a=a%c;
while(b>0)
{
if(b%2==1)
res=(res*a)%c;
b/=2;
a=(a*a)%c;
}
return res;
}
ll norm(ll a,ll b)
{
return (((a = a%b) < 0) ? a + b : a);
}
ll gcdExtended(ll,ll,ll *,ll *);
ll modInverse(ll a, ll m)
{
ll x, y;
ll g = gcdExtended(a, m, &x, &y);
g++; g--; //this line was added just to remove compiler warning
ll res = (x%m + m) % m;
return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
ll x1, y1;
ll gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
struct Graph
{
vector<vector<int>> adj;
Graph(int n): adj(n+1) {}
void add_edge(int a, int b, bool directed = false)
{
adj[a].pb(b);
if(!directed) adj[b].pb(a);
}
};
struct LCA
{
vll tin, tout, level;
vector<vll> up;
ll timer;
void _dfs(vector<vector<int>> &adj, ll x, ll par=-1)
{
up[x][0] = par;
REP(i, 1, 20)
{
if(up[x][i-1] == -1) break;
up[x][i] = up[up[x][i-1]][i-1];
}
tin[x] = ++timer;
for(auto &p: adj[x])
{
if(p != par)
{
level[p] = level[x]+1; _dfs(adj, p, x);
}
}
tout[x] = ++timer;
}
LCA(Graph &G, ll root)
{
int n = G.adj.size();
tin.resize(n); tout.resize(n); up.resize(n, vll(20, -1)); level.resize(n, 0);
timer = 0;
//does not handle forests, easy to modify to handle
_dfs(G.adj, root);
}
ll find_kth(ll x, ll k)
{
ll cur = x;
REP(i, 0, 20)
{
if((k>>i)&1) cur = up[cur][i];
if(cur == -1) break;
}
return cur;
}
bool is_ancestor(ll x, ll y)
{
return tin[x]<=tin[y]&&tout[x]>=tout[y];
}
ll find_lca(ll x, ll y)
{
if(is_ancestor(x, y)) return x;
if(is_ancestor(y, x)) return y;
ll best = x;
REPd(i, 19, 0)
{
if(up[x][i] == -1) continue;
else if(is_ancestor(up[x][i], y)) best = up[x][i];
else x = up[x][i];
}
return best;
}
ll dist(ll a, ll b)
{
return level[a] + level[b] - 2*level[find_lca(a, b)];
}
};
long long ap_sum(long long st, long long len) {
//diff is always 1
long long en = st + len - 1;
return (len * (st + en))/2;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
for(int i=0; i<t; i++) {
long long N, L, S;
cin >> N >> L >> S;
if(S < ap_sum(1, L) || S > ap_sum(N - L + 1, L)) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
return 0;
}
/*
*/, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have N coins with either an integer (between 0-9) written on one side and an english letter (a- z) written on the other side.
The following statement must be true for all coins:
<b>If the coin has a vowel on one side, then it must have an even integer on other side. </b>
For example coin having 'b' and '3' is valid (since 'b' is not a vowel, other side can be anything), coin having 'a' and '4' is valid, but coin having 'a' and '5' is invalid.
Now you're given just one side of each coin, find the minimum number of coins you need to flip to check the authenticity of the statement.The first and only line of input contains a string S, where each character in S depicts a side of the coin.
<b>Constraints:</b>
1 ≤ |S| ≤ 50Output a single integer, the minimum number of coins you need to flip.Sample Input
ee
Sample Output
2
Explanation: You need to flip both the coins to make sure an even integer is there on the other side of coin.
Sample Input
0ay1
Sample Output
2, I have written this Solution Code: x = list(input())
c = 0
for i in x:
if i in ['a', 'e', 'i', 'o', 'u', '1', '3', '5', '7', '9']:
c += 1
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have N coins with either an integer (between 0-9) written on one side and an english letter (a- z) written on the other side.
The following statement must be true for all coins:
<b>If the coin has a vowel on one side, then it must have an even integer on other side. </b>
For example coin having 'b' and '3' is valid (since 'b' is not a vowel, other side can be anything), coin having 'a' and '4' is valid, but coin having 'a' and '5' is invalid.
Now you're given just one side of each coin, find the minimum number of coins you need to flip to check the authenticity of the statement.The first and only line of input contains a string S, where each character in S depicts a side of the coin.
<b>Constraints:</b>
1 ≤ |S| ≤ 50Output a single integer, the minimum number of coins you need to flip.Sample Input
ee
Sample Output
2
Explanation: You need to flip both the coins to make sure an even integer is there on the other side of coin.
Sample Input
0ay1
Sample Output
2, 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();
int n = s.length();
int cnt=0;
for(int i=0;i<n;i++){
if(s.charAt(i)>='a' && s.charAt(i)<='z'){
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='o' || s.charAt(i)=='i' ||s.charAt(i)=='u'){
cnt++;
}
}
else{
int x = s.charAt(i)-'0';
if(x%2==1){cnt++;}
}
}
System.out.print(cnt);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have N coins with either an integer (between 0-9) written on one side and an english letter (a- z) written on the other side.
The following statement must be true for all coins:
<b>If the coin has a vowel on one side, then it must have an even integer on other side. </b>
For example coin having 'b' and '3' is valid (since 'b' is not a vowel, other side can be anything), coin having 'a' and '4' is valid, but coin having 'a' and '5' is invalid.
Now you're given just one side of each coin, find the minimum number of coins you need to flip to check the authenticity of the statement.The first and only line of input contains a string S, where each character in S depicts a side of the coin.
<b>Constraints:</b>
1 ≤ |S| ≤ 50Output a single integer, the minimum number of coins you need to flip.Sample Input
ee
Sample Output
2
Explanation: You need to flip both the coins to make sure an even integer is there on the other side of coin.
Sample Input
0ay1
Sample Output
2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pb push_back
#define ll long long
#define mp make_pair
#define F first
#define S second
int main(){
string s;
cin>>s;
int ct=0;
for(int i=0; i<s.length(); i++){
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='1' || s[i]=='3' || s[i]=='5' || s[i]=='7' || s[i]=='9'){
ct++;
}
}
cout<<ct;
return 0;
}, 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: Swapnil is challenged to build a tower of a height greater than or equal to N. To build the tower he is given blocks of heights which are powers of 3. He can only use one type of block only once and is given infinite types of blocks. Help him make a tower of minimum height just greater than or equal to N.The first line of the input contains one integer T the number of tests. Then T test cases follow.
Each test case contains a single integer N.
<b>Constraints:-</b>
1 ≤ t ≤ 100
0 ≤ N ≤ 10<sup>18</sup>For each test case, print the height of the minimum possible tower greater than or equal to N.Sample Input
5
1
8
4
6
5
Sample Output
1
9
4
9
9
<b>Explanation:</b>
N=1 => use one brick of height 3<sup>0</sup>
N=8 => use one brick of height 3<sup>2</sup>
N=4 => use two bricks of heights 3<sup>0</sup> and 3<sup>1</sup>
N=6 => use one brick of height 3<sup>2</sup>. We can not use 2 bricks of the same type so can't use two bricks of height 3<sup>1</sup>., I have written this Solution Code: import math as m
def closestNum(n):
val = m.log(n, 3)
low = int(pow(3,int(val)))
high = int(pow(3,int(val)+1))
sumn = int((pow(3,int(val)+1)-1)//2)
if(n > sumn):
return high
elif(n - low == 0):
return low
else:
return low + closestNum(n-low);
t = int(input())
while(t > 0):
n = int(input())
print(closestNum(n))
t = t-1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Swapnil is challenged to build a tower of a height greater than or equal to N. To build the tower he is given blocks of heights which are powers of 3. He can only use one type of block only once and is given infinite types of blocks. Help him make a tower of minimum height just greater than or equal to N.The first line of the input contains one integer T the number of tests. Then T test cases follow.
Each test case contains a single integer N.
<b>Constraints:-</b>
1 ≤ t ≤ 100
0 ≤ N ≤ 10<sup>18</sup>For each test case, print the height of the minimum possible tower greater than or equal to N.Sample Input
5
1
8
4
6
5
Sample Output
1
9
4
9
9
<b>Explanation:</b>
N=1 => use one brick of height 3<sup>0</sup>
N=8 => use one brick of height 3<sup>2</sup>
N=4 => use two bricks of heights 3<sup>0</sup> and 3<sup>1</sup>
N=6 => use one brick of height 3<sup>2</sup>. We can not use 2 bricks of the same type so can't use two bricks of height 3<sup>1</sup>., 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 p[N], s[N];
int f(int x){
if(x <= 0)
return 0;
int ans = 0;
for(int i = 0; i <= 39; i++){
if(s[i] >= x){
ans = p[i] + f(x-p[i]);
break;
}
}
return ans;
}
signed main() {
IOS;
int t; cin >> t;
p[0] = s[0] = 1;
for(int i = 1; i <= 39; i++){
p[i] = 3*p[i-1];
s[i] = s[i-1] + p[i];
}
while(t--){
int n; cin >> n;
cout << f(n) << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Swapnil is challenged to build a tower of a height greater than or equal to N. To build the tower he is given blocks of heights which are powers of 3. He can only use one type of block only once and is given infinite types of blocks. Help him make a tower of minimum height just greater than or equal to N.The first line of the input contains one integer T the number of tests. Then T test cases follow.
Each test case contains a single integer N.
<b>Constraints:-</b>
1 ≤ t ≤ 100
0 ≤ N ≤ 10<sup>18</sup>For each test case, print the height of the minimum possible tower greater than or equal to N.Sample Input
5
1
8
4
6
5
Sample Output
1
9
4
9
9
<b>Explanation:</b>
N=1 => use one brick of height 3<sup>0</sup>
N=8 => use one brick of height 3<sup>2</sup>
N=4 => use two bricks of heights 3<sup>0</sup> and 3<sup>1</sup>
N=6 => use one brick of height 3<sup>2</sup>. We can not use 2 bricks of the same type so can't use two bricks of height 3<sup>1</sup>., I have written this Solution Code: import java.io.*;
import java.lang.*;
class Main
{
public static void main(String args[])throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int t = Integer.parseInt(br.readLine());
while(t-- >0)
{
long n = Long.parseLong(br.readLine());
if(n == 0)
{
System.out.println(1);
continue;
}
System.out.println(powerOfThree(n));
}
}
public static long powerOfThree(long n)
{
if(n<=0)
return 0;
long p = (long)(Math.log(n)/Math.log(3));
if(gpSum(p)>=n)
return power(3,p) + powerOfThree(n - power(3,p));
else
return power(3,p+1);
}
public static long gpSum(long p)
{
return (power(3,p+1)-1)/2;
}
public static long power(long base, long p)
{
if(p==0)
return 1;
else
return(base*power(base,p-1));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a side of a square, your task is to calculate and print its area.The first line of the input contains the side of the square.
<b>Constraints:</b>
1 <= side <=100You just have to print the area of a squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36, I have written this Solution Code: def area(side_of_square):
print(side_of_square*side_of_square)
def main():
N = int(input())
area(N)
if __name__ == '__main__':
main(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a side of a square, your task is to calculate and print its area.The first line of the input contains the side of the square.
<b>Constraints:</b>
1 <= side <=100You just have to print the area of a squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36, 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 side = Integer.parseInt(br.readLine());
System.out.print(side*side);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tom has a water bottle with the shape of a rectangular prism whose base is a square of side a cm and whose height is b cm. (The thickness of the bottle can be ignored)
We will pour x cubic cm of water into the bottle, and gradually tilt the bottle around one of the sides of the base. When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.Their is only one line of input which contain three integers a, b and x.
Constraints:-
1 ≤ a ≤ 100
1 ≤ b ≤ 100
1 ≤ x ≤ (a^2)*bPrint the maximum angle in which we can tilt the bottle without spilling any water, in degrees. Print 10 digits after decimal.Sample Input:
2 2 4
Sample Output
45.0000000000
Sample Input
12 21 10
Sample Output
89.7834636934, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.lang.Math;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader scan=new BufferedReader(new InputStreamReader(System.in));
String s[]=scan.readLine().split(" ");
int width=Integer.parseInt(s[0]);
int height=Integer.parseInt(s[1]);
int givenVolume=Integer.parseInt(s[2]);
double heightOfGivenVolume=(givenVolume)/(double)(width*width);
double angleInDegree=0.0;
if(heightOfGivenVolume==height)
{
angleInDegree=90.0;
}
else if(heightOfGivenVolume<=(height/2))
{
double tanTheta=(2.0*width*heightOfGivenVolume)/(double)(height*height);
double angleInRadian=Math.atan(tanTheta);
angleInDegree=90.0-Math.toDegrees(angleInRadian);
}
else
{
double tanTheta=width/(2.0*(height-heightOfGivenVolume));
double angleInRadian=Math.atan(tanTheta);
angleInDegree=90.0-Math.toDegrees(angleInRadian);
}
System.out.printf("%.10f",angleInDegree);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tom has a water bottle with the shape of a rectangular prism whose base is a square of side a cm and whose height is b cm. (The thickness of the bottle can be ignored)
We will pour x cubic cm of water into the bottle, and gradually tilt the bottle around one of the sides of the base. When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.Their is only one line of input which contain three integers a, b and x.
Constraints:-
1 ≤ a ≤ 100
1 ≤ b ≤ 100
1 ≤ x ≤ (a^2)*bPrint the maximum angle in which we can tilt the bottle without spilling any water, in degrees. Print 10 digits after decimal.Sample Input:
2 2 4
Sample Output
45.0000000000
Sample Input
12 21 10
Sample Output
89.7834636934, I have written this Solution Code: #include <bits/stdc++.h>
// #define ll long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
double a,b,x;
cin>>a>>b>>x;
double ans;
double pi = 2*acos(0.0);
if(x<(a*a*b)/2 )
{
double p= (a*b*b)/(2*x);
ans = atan(p);
ans= (ans*180)/pi;
}
else
{
double p = (2*((a*a*b) - x))/(a*a*a);
ans = atan(p);
ans= (ans*180)/pi;
}
cout <<std::fixed;
cout<<setprecision(10)<<ans;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<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>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: int DivisorProblem(int N){
int ans=0;
while(N>1){
int cnt=2;
while(N%cnt!=0){
cnt++;
}
N/=cnt;
ans++;
}
return ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<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>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: def DivisorProblem(N):
ans=0
while N>1:
cnt=2
while N%cnt!=0:
cnt=cnt+1
N = N//cnt
ans=ans+1
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<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>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: static int DivisorProblem(int N){
int ans=0;
while(N>1){
int cnt=2;
while(N%cnt!=0){
cnt++;
}
N/=cnt;
ans++;
}
return ans;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<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>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: int DivisorProblem(int N){
int ans=0;
while(N>1){
int cnt=2;
while(N%cnt!=0){
cnt++;
}
N/=cnt;
ans++;
}
return ans;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 0s and 1s are super cool.
You are given a binary string (string consisting of only zeros and ones). We need to modify the string such that no 0 is followed by a 1. For achieving this, we will find the leftmost occurrence of "01" substring in the string and remove it from the string. We will repeat this operation until there is no substring of the form "01" in the string.
For example, if the initial string is "011010", it will transform in the following manner:
<b>01</b>1010 -> 1<b>01</b>0 -> 10
Find the final remaining string. If the length of remaining string is 0, print -1 instead.The first and the only line of input contains the initial string, S.
Constraints
1 <= |S| <= 300000Output the remaining string. If the length of remaining string is 0, output -1.Sample Input
011010
Sample Output
10
Explanation: Available in the question text.
Sample Input
001101
Sample Output
-1
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
String s = st.nextToken();
int curzeroes = 0;
StringBuilder sb = new StringBuilder();
int len = s.length();
for(int i = 0;i<len;i++){
if(s.charAt(i) == '1'){
if(curzeroes == 0){
sb.append("1");
}
else{
curzeroes--;
}
}
else{
curzeroes++;
}
}
for(int i = 0;i<curzeroes;i++){
sb.append("0");
}
if(sb.length() == 0 && curzeroes == 0){
bw.write("-1\n");
}
else{
bw.write(sb.toString()+"\n");
}
bw.flush();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 0s and 1s are super cool.
You are given a binary string (string consisting of only zeros and ones). We need to modify the string such that no 0 is followed by a 1. For achieving this, we will find the leftmost occurrence of "01" substring in the string and remove it from the string. We will repeat this operation until there is no substring of the form "01" in the string.
For example, if the initial string is "011010", it will transform in the following manner:
<b>01</b>1010 -> 1<b>01</b>0 -> 10
Find the final remaining string. If the length of remaining string is 0, print -1 instead.The first and the only line of input contains the initial string, S.
Constraints
1 <= |S| <= 300000Output the remaining string. If the length of remaining string is 0, output -1.Sample Input
011010
Sample Output
10
Explanation: Available in the question text.
Sample Input
001101
Sample Output
-1
, I have written this Solution Code: arr = input()
c = 0
res = ""
n =len(arr)
for i in range(n):
if arr[i]=='0':
c+=1
else:
if c==0:
res+='1'
else:
c-=1
for i in range(c):
res+='0'
if len(res)==0:
print(-1)
else:
print(res), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 0s and 1s are super cool.
You are given a binary string (string consisting of only zeros and ones). We need to modify the string such that no 0 is followed by a 1. For achieving this, we will find the leftmost occurrence of "01" substring in the string and remove it from the string. We will repeat this operation until there is no substring of the form "01" in the string.
For example, if the initial string is "011010", it will transform in the following manner:
<b>01</b>1010 -> 1<b>01</b>0 -> 10
Find the final remaining string. If the length of remaining string is 0, print -1 instead.The first and the only line of input contains the initial string, S.
Constraints
1 <= |S| <= 300000Output the remaining string. If the length of remaining string is 0, output -1.Sample Input
011010
Sample Output
10
Explanation: Available in the question text.
Sample Input
001101
Sample Output
-1
, 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;
string s; cin >> s;
stack<int> st;
for(int i = 0; i < (int)s.length(); i++){
if(!st.empty() && s[st.top()] == '0' && s[i] == '1'){
st.pop();
}
else
st.push(i);
}
string res = "";
while(!st.empty()){
res += s[st.top()];
st.pop();
}
reverse(res.begin(), res.end());
if(res == "") res = "-1";
cout << res;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N +1 integers containing N different elements, your task is to print the array without the repeating element in it.First line of input contains a single integer N, the next line contains N +1 space separated integers containing values of Arr.
Constraints:-
1 < = N < = 1000
1 < = Arr[i] < = 10<sup>5</sup>Print the array without the duplicate element.Sample Input:-
5
1 2 3 4 5 1
Sample Output:-
1 2 3 4 5
Sample Input:-
5
2 4 5 5 9 8
Sample Output:-
2 4 5 9 8, I have written this Solution Code: N=int(input())
Arr=list(map(int, input().split()))
arr_unrepeated=[]
for i in Arr:
if i not in arr_unrepeated:
arr_unrepeated.append(i)
for j in arr_unrepeated:
print (j, end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N +1 integers containing N different elements, your task is to print the array without the repeating element in it.First line of input contains a single integer N, the next line contains N +1 space separated integers containing values of Arr.
Constraints:-
1 < = N < = 1000
1 < = Arr[i] < = 10<sup>5</sup>Print the array without the duplicate element.Sample Input:-
5
1 2 3 4 5 1
Sample Output:-
1 2 3 4 5
Sample Input:-
5
2 4 5 5 9 8
Sample Output:-
2 4 5 9 8, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long long a;
map<long long,int> m;
for(int i=0;i<n+1;i++){
cin>>a;
if(m.find(a)==m.end()){cout<<a<<" ";}
m[a]++;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N +1 integers containing N different elements, your task is to print the array without the repeating element in it.First line of input contains a single integer N, the next line contains N +1 space separated integers containing values of Arr.
Constraints:-
1 < = N < = 1000
1 < = Arr[i] < = 10<sup>5</sup>Print the array without the duplicate element.Sample Input:-
5
1 2 3 4 5 1
Sample Output:-
1 2 3 4 5
Sample Input:-
5
2 4 5 5 9 8
Sample Output:-
2 4 5 9 8, 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().trim());
String str[]= br.readLine().trim().split(" ");
HashSet <String> hs=new HashSet<>();
for(String s:str){
if(!hs.contains(s)){
hs.add(s);
System.out.print(s+" ");
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N integers. Solve the following problem for X from 1 to N :-
Find the number of ways to select a pair (i, j) such that i < j and i != X and j != X and Arr[i] = Arr[j].First line of input contains a single integer, N.
Second line of input contains N space separated integers, denoting array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers where ith integer is the answer when X = i.Sample Input
5
4 4 1 1 1
Sample Output
3 3 2 2 2
Explanation:
For X=1 we have (3, 4) (3, 5) (4, 5)
For X=2 we have (3, 4) (3, 5) (4, 5)
For X=3 we have (1, 2) (4, 5)
For X=4 we have (1, 2) (3, 5)
For X=5 we have (1, 2) (3, 4), I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(sc.readLine());
int a[]=new int[n];
int count[]=new int[n+1];
String[] temp1=sc.readLine().trim().split(" ");
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(temp1[i]);
count[a[i]]++;
}
int val=0,total=0;
for(int i=0;i<n;i++){
val=count[i];
total=total+(val)*(val-1)/2;
}
StringBuilder sb=new StringBuilder();
for(int j=0;j<n;j++){
int result=total-count[a[j]]+1;
sb.append(result+" ");
}
System.out.println(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N integers. Solve the following problem for X from 1 to N :-
Find the number of ways to select a pair (i, j) such that i < j and i != X and j != X and Arr[i] = Arr[j].First line of input contains a single integer, N.
Second line of input contains N space separated integers, denoting array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers where ith integer is the answer when X = i.Sample Input
5
4 4 1 1 1
Sample Output
3 3 2 2 2
Explanation:
For X=1 we have (3, 4) (3, 5) (4, 5)
For X=2 we have (3, 4) (3, 5) (4, 5)
For X=3 we have (1, 2) (4, 5)
For X=4 we have (1, 2) (3, 5)
For X=5 we have (1, 2) (3, 4), I have written this Solution Code: n=int(input())
arr=list(map(int,input().split()))
d=dict.fromkeys([i for i in arr],0)
for i in arr:
d[i]+=1
ans=0
for value in d.values():
if value>=2:
ans+=((value*(value-1))//2)
for i in range(n):
if arr[i] in d:
f1=(d[arr[i]]*(d[arr[i]]-1))//2
ans-=f1
f2=((d[arr[i]]-1)*(d[arr[i]]-2))//2
ans+=f2
print(ans,end=" ")
ans-=f2
ans+=f1
else:
print(0,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N integers. Solve the following problem for X from 1 to N :-
Find the number of ways to select a pair (i, j) such that i < j and i != X and j != X and Arr[i] = Arr[j].First line of input contains a single integer, N.
Second line of input contains N space separated integers, denoting array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers where ith integer is the answer when X = i.Sample Input
5
4 4 1 1 1
Sample Output
3 3 2 2 2
Explanation:
For X=1 we have (3, 4) (3, 5) (4, 5)
For X=2 we have (3, 4) (3, 5) (4, 5)
For X=3 we have (1, 2) (4, 5)
For X=4 we have (1, 2) (3, 5)
For X=5 we have (1, 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<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin >> n;
vector<int> a(n);
rep(i,n){
cin>>a[i];
}
map<int,int> b;
rep(i,n) b[a[i]]++;
ll ans=0,all=0;
for (auto p : b) {
ll v = p.second;
all+=v*(v-1)/2;
}
rep(i,n){
ans=all-b[a[i]]+1;
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: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: n=int(input())
a=map(int,input().split())
b=[]
mx=-200000
cnt=0
for i in a:
if i>mx:
cnt+=1
mx=i
print(cnt), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: function numberOfRoofs(arr)
{
let count=1;
let max = arr[0];
for(let i=1;i<arrSize;i++)
{
if(arr[i] > max)
{
count++;
max = arr[i];
}
}
return count;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int count=1;
int max = a[0];
for(int i=1;i<n;i++)
{
if(a[i] > max)
{
count++;
max = a[i];
}
}
System.out.println(count);
}
}, 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 and an integer K.
In one operation, you can choose any index i where 0 ≤ i < A.length and change A[i] to A[i] + x where x is an integer from the range [- K, K]. You can apply this operation at most once for each index i. The score of A is the difference between the maximum and minimum elements in A.
Return the minimum score of A after applying the mentioned operation at most once for each index in it.<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>smallestRangeI()</b> that takes the array A and integer K as a parameter.
<b>Constarints</b>
1 ≤ A.length ≤ 10<sup>3</sup>
0 ≤ A[i] ≤ 10<sup>4</sup>
0 ≤ K ≤ 10<sup>4</sup>Return the minimum score of A after applying the mentioned operation at most once for each index in it.Sample input:
3
1 3 6
3
Sample output:
0
<b>Explanation</b>
Change A to be [4, 4, 4]. The score is max(A) - min(A) = 4 - 4 = 0.
Sample Input:
4
1 5 2 4
2
Sample Output:
3
Explanation:
To minimize the difference between the maximum and minimum elements in the array, you can perform the following operations:
<ul>
<li>Add 2 to index 0: A = [3, 5, 2, 4]. Now the minimum value in the array is 2, and the maximum value is 5. The difference between them is 3.</li>
<li>Subtract 2 to index 1: A = [3, 3, 2, 4]. Now the minimum value in the array is 2, and the maximum value is 4. The difference between them is 1.</li>
<li>Add 1 to index 2: A = [3, 3, 3, 4]. Now the minimum value in the array is 3, and the maximum value is 4. The difference between them is 1.</li>
<li>Subtract 1 to index 3: A = [3, 3, 3, 3]. Now the minimum value in the array is 3, and the maximum value is 3. The difference between them is 0.</li>
</ul>
Therefore, the minimum score of the array is 0., I have written this Solution Code: class Solution {
public int smallestRangeI(int[] A, int K) {
int min = A[0], max = A[0];
for(int i = 1; i < A.length; i++) {
if(A[i] > max) max = A[i];
else if(A[i] < min) min = A[i];
}
if(max - min - 2*K <= 0) return 0;
return max - min - 2*K;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to become rich so he came up with an idea, So, he buys some gadgets from the future at a price of C and sells them at a price of S to his friends. Now Nobita wants to know how much he gains by selling all gadget. As we all know Nobita is weak in maths help him to find the profit he getsYou don't have to worry about the input, you just have to complete the function <b>Profit()</b>
<b>Constraints:-</b>
1 <= C <= S <= 1000Print the profit Nobita gets from selling one gadget.Sample Input:-
3 5
Sample Output:-
2
Sample Input:-
9 16
Sample Output:-
7, I have written this Solution Code: def profit(C, S):
print(S - C), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to become rich so he came up with an idea, So, he buys some gadgets from the future at a price of C and sells them at a price of S to his friends. Now Nobita wants to know how much he gains by selling all gadget. As we all know Nobita is weak in maths help him to find the profit he getsYou don't have to worry about the input, you just have to complete the function <b>Profit()</b>
<b>Constraints:-</b>
1 <= C <= S <= 1000Print the profit Nobita gets from selling one gadget.Sample Input:-
3 5
Sample Output:-
2
Sample Input:-
9 16
Sample Output:-
7, I have written this Solution Code: static void Profit(int C, int S){
System.out.println(S-C);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N. Print the value of PI with precision upto N decimal places.The only input line contains an integer N.
Constraints:
1 <= N <= 10Print the value of PI.Sample Input 1:
4
Output:
3.1416
Sample Input 2:
10
Output:
3.1415926536, I have written this Solution Code: import java.io.*;
import java.util.Scanner;
class Main {
public static void main (String[] args) {
int n;
Scanner s = new Scanner(System.in);
n = s.nextInt();
if(n==1)System.out.printf("%.1f",Math.PI);
else if(n==2)System.out.printf("%.2f",Math.PI);
else if(n==3)System.out.printf("%.3f",Math.PI);
else if(n==4)System.out.printf("%.4f",Math.PI);
else if(n==5)System.out.printf("%.5f",Math.PI);
else if(n==6)System.out.printf("%.6f",Math.PI);
else if(n==7)System.out.printf("%.7f",Math.PI);
else if(n==8)System.out.printf("%.8f",Math.PI);
else if(n==9)System.out.printf("%.9f",Math.PI);
else System.out.printf("%.10f",Math.PI);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N. Print the value of PI with precision upto N decimal places.The only input line contains an integer N.
Constraints:
1 <= N <= 10Print the value of PI.Sample Input 1:
4
Output:
3.1416
Sample Input 2:
10
Output:
3.1415926536, I have written this Solution Code: n = int(input())
pi = 3.1415926536
print(round(pi, n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Equality is eternal.
Given an integer n, find out whether we can divide 1, 2, 3..., n into two sets of equal sums.The only input line contains an integer n.
Constraints
1 <= n <= 1000000000Print "YES", if the division is possible, and "NO" otherwise.Sample Input
7
Sample Output
YES
Sample Input
6
Sample Output
NO
Explanation: In first sample, the division can be {1, 6, 7}, {2, 3, 4, 5}., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long n = Integer.parseInt(br.readLine());
long n1 = (n+1)/2;
if((n1&1)==0){
System.out.print("YES");
}else{
System.out.print("NO");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Equality is eternal.
Given an integer n, find out whether we can divide 1, 2, 3..., n into two sets of equal sums.The only input line contains an integer n.
Constraints
1 <= n <= 1000000000Print "YES", if the division is possible, and "NO" otherwise.Sample Input
7
Sample Output
YES
Sample Input
6
Sample Output
NO
Explanation: In first sample, the division can be {1, 6, 7}, {2, 3, 4, 5}., I have written this Solution Code: n=int(input())
x=(n*(n+1))//2
if(x%2==0):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Equality is eternal.
Given an integer n, find out whether we can divide 1, 2, 3..., n into two sets of equal sums.The only input line contains an integer n.
Constraints
1 <= n <= 1000000000Print "YES", if the division is possible, and "NO" otherwise.Sample Input
7
Sample Output
YES
Sample Input
6
Sample Output
NO
Explanation: In first sample, the division can be {1, 6, 7}, {2, 3, 4, 5}., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
long long n,x;
cin>>n;
x=n*(n+1);
x=x/2;
if(x&1){cout<<"NO";}
else{cout<<"YES";}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array of integers. Consider absolute difference between all the pairs of the the elements. You need to find Kth smallest absolute difference. If the size of the array is N then value of K will be less than N and more than or equal to 1.The first line of input contains number of test cases T.
The first line of each test case contains a two integers N and K denoting the number of elements in the array A and difference you need to output. The second line of each test case contains N space separated integers denoting the elements of the array A
Constraints:
1<= T <= 10
2 <= N <= 100000
1 <= K < N < 100000
0 <= A[i] <= 100000For each test case, output Kth smallest absolute difference.Input :
1
6 2
1 3 4 1 3 8
Output :
0
Explanation :
Test case 1: First smallest difference is 0, between the pair (1, 1) and second smallest absolute difference difference is also 0 between the pairs (3, 3)., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine().trim());
while (t-- > 0) {
String str[] = read.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int arr[] = new int[n];
str = read.readLine().trim().split(" ");
for (int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
System.out.println(Math.abs(small(arr, k)));
}
}
public static int small(int arr[], int k) {
Arrays.sort(arr);
int l = 0, r = arr[arr.length - 1] - arr[0];
while (r > l) {
int mid = l + (r - l) / 2;
if (count(arr, mid) < k) {
l = mid + 1;
} else {
r = mid;
}
}
return r;
}
public static int count(int arr[], int mid) {
int ans = 0, j = 0;
for (int i = 1; i < arr.length; ++i) {
while (j < i && arr[i] - arr[j] > mid) {
++j;
}
ans += i - j;
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list containing N nodes such that each nodes contains an additional random pointer, your task is to clone the given linked list into a new list and return the new head.<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>CloneList()</b> that takes the head of given list as parameter.
Constraints:
1 < = N < = 1000
1 < = Node.val < = 1000
<b> Custom Input:-</b>
First line of input should contains the number of Nodes N. Second line of input should contains N space separated integers depicting the data at each node. Last line of input should N space separated integers contains the number of the node the random pointer is pointing to.
For eg:-
5
2 7 8 8 3
1 2 2 3 2 You don't need to print anything you just need to return the head of the new list. You will get 1 if the cloning is done properly else you will get 0.Sample Input:-
4
1 2 3 4
1 4 1 1
Input Explanation:
1 - > 2 - > 3 - > 4
↓ ↓ ↓ ↓
1 4 1 1
Sample Output:-
1, I have written this Solution Code:
public static Node CloneList(Node head) {
if (head == null)
return null;
Node p = head;
// copy every node and insert to list
while (p != null) {
Node copy = new Node(p.val);
copy.next = p.next;
p.next = copy;
p = copy.next;
}
// copy random pointer for each new node
p = head;
while (p != null) {
if (p.random != null)
p.next.random = p.random.next;
p = p.next.next;
}
// break list to two
p = head;
Node newHead = head.next;
while (p != null) {
Node temp = p.next;
p.next = temp.next;
if (temp.next != null)
temp.next = temp.next.next;
p = p.next;
}
return newHead;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array, your task is to sort the array using merge sort.<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>implementMergeSort()</b> that takes 3 arguments.
arr: input array
start: starting index which is 0
end: ending index of array
Constraints
1 <= T <= 100
1 <= N <= 10<sup>6</sup>
0 <= Arr[i] <= 10<sup>9</sup>
Sum of 'N' over all test cases does not exceed 10<sup>6</sup>You need to return the sorted array. The driver code will print the array in sorted form.Sample Input:
2
3
3 1 2
3
4 5 6
Sample Output:
1 2 3
4 5 6, I have written this Solution Code:
public static int[] implementMergeSort(int arr[], int start, int end)
{
if (start < end)
{
// Find the middle point
int mid = (start+end)/2;
// Sort first and second halves
implementMergeSort(arr, start, mid);
implementMergeSort(arr , mid+1, end);
// Merge the sorted halves
merge(arr, start, mid, end);
}
return arr;
}
public static void merge(int arr[], int start, int mid, int end)
{
// Find sizes of two subarrays to be merged
int n1 = mid - start + 1;
int n2 = end - mid;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[start + i];
for (int j=0; j<n2; ++j)
R[j] = arr[mid + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
, 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.