Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Harsh is thinking of starting his own business. He has decided to start with the hotel business. He has been given a list that contains the arrival time and the duration of stay of each guest at his hotel. He can give 1 room to only 1 guest. Since he does not want to disappoint his guests he wants to find the minimum number of rooms he needs to build so that he could accommodate all the guests. Help him in finding this answer.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You have to complete the function <b>rooms()</b> that takes an integer n denoting the number of guests and two arrays A and B denoting the arrival time and duration of stay of each guest respectively as parameters.
<b>Constraints:</b>
1 ≤ T ≤ 5
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ A[i], B[i] ≤ 10<sup>9</sup>For each test case, print in a new line the minimum number of rooms he needs to build.Sample Input:-
1
3
1 2 3
3 3 3
Sample Output:-
3
Sample Input:-
1
5
1 2 3 4 5
2 3 4 5 6
Sample Output:-
3, I have written this Solution Code: class Solution {
public void rooms(int n,int []a,int []b) {
for (int i = 0; i < n ; i++)
b[i] += a[i];
Arrays.sort(a);
Arrays.sort(b);
int i = 1, j = 0, count = 1;
int result = 0;
while (i < n)
{
if (a[i] < b[j])
{
count++;
i++;
}
else if (a[i] >= b[j])
{
count--;
j++;
}
result = Math.max(result, count);
}
System.out.println(result);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice wants to go to Bob's house. The location of their houses is given on a 2-D coordinate system. There are a total of 8 directions:
North - Directly above
South - Directly below
East - To the right
West - To the left
North East - In between north and east
North West - In between north and west
South East - In between south and east
South West - In between south and west
Find the direction in which Alice must go to reach Bob's house.There are two lines of input. The first line contains the x and y coordinate of Alice's house. The second line contains x and y coordinate of Bob's house. It is given that these two locations are different.
-100 <= Coordinates <= 100Print a single string denoting the direction in which Alice must move to reach Bob's house.Sample Input 1:
2 5
11 25
Sample Output 1:
North East
Sample Input 2:
23 12
-85 12
Sample Output 2:
West, I have written this Solution Code: x0,y0 =map(int,input().split(" "))
x1,y1 = map(int,input().split(" "))
N = "North " if y0<y1 else "South " if y0> y1 else ""
E = "East " if x0<x1 else "West " if x0> x1 else ""
print(N+E), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice wants to go to Bob's house. The location of their houses is given on a 2-D coordinate system. There are a total of 8 directions:
North - Directly above
South - Directly below
East - To the right
West - To the left
North East - In between north and east
North West - In between north and west
South East - In between south and east
South West - In between south and west
Find the direction in which Alice must go to reach Bob's house.There are two lines of input. The first line contains the x and y coordinate of Alice's house. The second line contains x and y coordinate of Bob's house. It is given that these two locations are different.
-100 <= Coordinates <= 100Print a single string denoting the direction in which Alice must move to reach Bob's house.Sample Input 1:
2 5
11 25
Sample Output 1:
North East
Sample Input 2:
23 12
-85 12
Sample Output 2:
West, 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 = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if(x1 == x2){
if(y1 < y2)
cout << "North";
else
cout << "South";
}
else if(y1 == y2){
if(x1 < x2)
cout << "East";
else
cout << "West";
}
else if(x1 < x2 && y1 < y2)
cout << "North East";
else if(x1 < x2 && y1 > y2)
cout << "South East";
else if(x1 > x2 && y1 < y2)
cout << "North West";
else
cout << "South West";
}
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: Alice wants to go to Bob's house. The location of their houses is given on a 2-D coordinate system. There are a total of 8 directions:
North - Directly above
South - Directly below
East - To the right
West - To the left
North East - In between north and east
North West - In between north and west
South East - In between south and east
South West - In between south and west
Find the direction in which Alice must go to reach Bob's house.There are two lines of input. The first line contains the x and y coordinate of Alice's house. The second line contains x and y coordinate of Bob's house. It is given that these two locations are different.
-100 <= Coordinates <= 100Print a single string denoting the direction in which Alice must move to reach Bob's house.Sample Input 1:
2 5
11 25
Sample Output 1:
North East
Sample Input 2:
23 12
-85 12
Sample Output 2:
West, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int x1, y1, x2, y2;
x1=sc.nextInt();
y1=sc.nextInt();
x2=sc.nextInt();
y2=sc.nextInt();
if(x1 == x2){
if(y1 < y2)
System.out.print("North");
else
System.out.print("South");
}
else if(y1 == y2){
if(x1 < x2)
System.out.print("East");
else
System.out.print("West");
}
else if(x1 < x2 && y1 < y2)
System.out.print("North East");
else if(x1 < x2 && y1 > y2)
System.out.print("South East");
else if(x1 > x2 && y1 < y2)
System.out.print("North West");
else
System.out.print("South West");
}
}, 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: 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: 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: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import math
n = int(input())
for i in range(n):
x = int(input())
count = 0
for i in range(1, int(math.sqrt(x))+1):
if x % i == 0:
if (i%2 == 0):
count+=1
if ((x/i) %2 == 0):
count+=1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int count=0;
for(int i=1;i<=Math.sqrt(n);i++){
if(n%i == 0)
{
if(i%2==0) {
count++;
}
if(i*i != n && (n/i)%2==0) {
count++;
}
}
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
if(n&1){cout<<0<<endl;continue;}
long x=sqrt(n);
int cnt=0;
for(long long i=1;i<=x;i++){
if(!(n%i)){
if(!(i%2)){cnt++;}
if(i*i!=n){
if(!((n/i)%2)){cnt++;}
}
}
}
cout<<cnt<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements and a number K. The task is to arrange array elements according to the absolute difference with K, i. e., element having minimum difference comes first and so on.
Note : If two or more elements are at equal distance arrange them in same sequence as in the given array.First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of test case contains two space separated integers N and K. Second line of each test case contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= K <= 10^4
1 <= A[i] <= 10^4
Sum of N over all test cases does not exceed 2*10^5For each test case print the given array in the order as described above.Input:
3
5 7
10 5 3 9 2
5 6
1 2 3 4 5
4 5
2 6 8 3
Output:
5 9 10 3 2
5 4 3 2 1
6 3 2 8
Explanation:
Testcase 1: Sorting the numbers accoding to the absolute difference with 7, we have array elements as 5, 9, 10, 3, 2.
Testcase 2: Sorting the numbers according to the absolute difference with 6, we have array elements as 5 4 3 2 1.
Testcase 3: Sorting the numbers according to the absolute difference with 5, we have array elements as 6 3 2 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 t = Integer.parseInt(br.readLine().trim());
while(t-->0){
String inputLine[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(inputLine[0]);
int x = Integer.parseInt(inputLine[1]);
int arr[] = new int[n];
inputLine = br.readLine().trim().split(" ");
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine[i]);
}
new Solution().sortABS(arr,n, x);
StringBuilder sb = new StringBuilder();
for(int y : arr)
sb.append(y+ " ");
System.out.println(sb.toString());
}
}
}
class Solution
{
static void sortABS(int arr[], int n,int k)
{
List<Integer> aux=new ArrayList<>();
Sort obj=new Sort();
obj.k=k;
int j=0;
for(int i:arr)
aux.add(i);
Collections.sort(aux, obj);
for(int i:aux)
arr[j++]=i;
}
}
class Sort implements Comparator<Integer> {
Integer k;
public int compare(Integer a, Integer b)
{
return Math.abs(a-k) - Math.abs(b-k);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements and a number K. The task is to arrange array elements according to the absolute difference with K, i. e., element having minimum difference comes first and so on.
Note : If two or more elements are at equal distance arrange them in same sequence as in the given array.First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of test case contains two space separated integers N and K. Second line of each test case contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= K <= 10^4
1 <= A[i] <= 10^4
Sum of N over all test cases does not exceed 2*10^5For each test case print the given array in the order as described above.Input:
3
5 7
10 5 3 9 2
5 6
1 2 3 4 5
4 5
2 6 8 3
Output:
5 9 10 3 2
5 4 3 2 1
6 3 2 8
Explanation:
Testcase 1: Sorting the numbers accoding to the absolute difference with 7, we have array elements as 5, 9, 10, 3, 2.
Testcase 2: Sorting the numbers according to the absolute difference with 6, we have array elements as 5 4 3 2 1.
Testcase 3: Sorting the numbers according to the absolute difference with 5, we have array elements as 6 3 2 8., I have written this Solution Code: def merge(arr,l,m,h,x):
n1=m-l+1
n2=h-m
L=arr[l:m+1]
R=arr[m+1:h+1]
i=j=0
k=l
while(i<n1 and j<n2):
if abs(x-L[i])<=abs(x-R[j]):
arr[k]=L[i]
i+=1
else:
arr[k]=R[j]
j+=1
k+=1
while(i<n1):
arr[k]=L[i]
i+=1
k+=1
while(j<n2):
arr[k]=R[j]
j+=1
k+=1
def mergeSort(arr,l,h,k):
if l<h:
m=l+(h-l)//2
mergeSort(arr,l,m,k)
mergeSort(arr,m+1,h,k)
merge(arr,l,m,h,k)
if __name__=="__main__":
t=int(input())
while t>0:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
mergeSort(arr,0,len(arr)-1,k)
for i in arr:
print(i,end=" ")
print()
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements and a number K. The task is to arrange array elements according to the absolute difference with K, i. e., element having minimum difference comes first and so on.
Note : If two or more elements are at equal distance arrange them in same sequence as in the given array.First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of test case contains two space separated integers N and K. Second line of each test case contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= K <= 10^4
1 <= A[i] <= 10^4
Sum of N over all test cases does not exceed 2*10^5For each test case print the given array in the order as described above.Input:
3
5 7
10 5 3 9 2
5 6
1 2 3 4 5
4 5
2 6 8 3
Output:
5 9 10 3 2
5 4 3 2 1
6 3 2 8
Explanation:
Testcase 1: Sorting the numbers accoding to the absolute difference with 7, we have array elements as 5, 9, 10, 3, 2.
Testcase 2: Sorting the numbers according to the absolute difference with 6, we have array elements as 5 4 3 2 1.
Testcase 3: Sorting the numbers according to the absolute difference with 5, we have array elements as 6 3 2 8., 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 k;
bool cmp(pi a, pi b){
int x = abs(a.fi-k), y = abs(b.fi-k);
if(x == y)
return a.se < b.se;
return x < y;
}
pi a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i].fi;
a[i].se = i;
}
sort(a + 1, a + n + 1, cmp);
for(int i = 1; i <= n; i++)
cout << a[i].fi << " ";
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Consider a sequence of integers from 1 to N (1, 2, 3,. .. . N). Your task is to divide this sequence into two sets such that the absolute difference of the sum of these sets is minimum i.e if we divide the sequence in set A and B then |Sum(A) - Sum(B)| should be minimum.The input contains a single integer N.
Constraints:-
1 <= N <= 100000Print the minimum absolute difference possible.Sample Input:-
5
Sample Output:-
1
Explanation:-
Set A:- 1, 2, 5
Set B:- 3. 4
Sample Input:-
8
Sample Output:-
0
Explanation:-
Set A:- 1, 2, 3, 5, 7
Set B;- 4, 6, 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));
String str=br.readLine().trim();
long n=Integer.parseInt(str);
long sum=(n*(n+1)/2)/2;
int sum1=0,sum2=0;
for(long i=n;i>=1;i--){
if(sum-i>=0)
{
sum-=i;
sum1+=i;
}
else{
sum2+=i;
}
}
System.out.println(Math.abs(sum1-sum2));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Consider a sequence of integers from 1 to N (1, 2, 3,. .. . N). Your task is to divide this sequence into two sets such that the absolute difference of the sum of these sets is minimum i.e if we divide the sequence in set A and B then |Sum(A) - Sum(B)| should be minimum.The input contains a single integer N.
Constraints:-
1 <= N <= 100000Print the minimum absolute difference possible.Sample Input:-
5
Sample Output:-
1
Explanation:-
Set A:- 1, 2, 5
Set B:- 3. 4
Sample Input:-
8
Sample Output:-
0
Explanation:-
Set A:- 1, 2, 3, 5, 7
Set B;- 4, 6, 8, I have written this Solution Code: n=int(input())
if(n%4==1 or n%4==2):
print(1)
else:
print(0)
'''if sum1%2==0:
print(0)
else:
print(1)''', In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Consider a sequence of integers from 1 to N (1, 2, 3,. .. . N). Your task is to divide this sequence into two sets such that the absolute difference of the sum of these sets is minimum i.e if we divide the sequence in set A and B then |Sum(A) - Sum(B)| should be minimum.The input contains a single integer N.
Constraints:-
1 <= N <= 100000Print the minimum absolute difference possible.Sample Input:-
5
Sample Output:-
1
Explanation:-
Set A:- 1, 2, 5
Set B:- 3. 4
Sample Input:-
8
Sample Output:-
0
Explanation:-
Set A:- 1, 2, 3, 5, 7
Set B;- 4, 6, 8, I have written this Solution Code: #include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
if(n%4==1 || n%4==2){cout<<1;}
else {
cout<<0;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In an 8X8 chessboard. Given the positions of the Queen as (X, Y) and the King as (P, Q) .
Your task is to check whether the queen can attack the king in one move or not.
The queen is the most powerful piece in the game of chess. It can move any number of squares vertically, horizontally or diagonally .<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>QueenAttack()</b> that takes integers X, Y, P, and Q as arguments.
Constraints:-
1 <= X, Y, P, Q <= 8
Note:- King and Queen can not be in the same positionReturn 1 if the king is in the check position else return 0.Sample Input:-
1 1 5 5
Sample Output:-
1
Sample Input:-
3 4 6 6
Sample Output:-
0, I have written this Solution Code: def QueenAttack(X, Y, P, Q):
if X==P or Y==Q or abs(X-P)==abs(Y-Q):
return 1
return 0, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In an 8X8 chessboard. Given the positions of the Queen as (X, Y) and the King as (P, Q) .
Your task is to check whether the queen can attack the king in one move or not.
The queen is the most powerful piece in the game of chess. It can move any number of squares vertically, horizontally or diagonally .<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>QueenAttack()</b> that takes integers X, Y, P, and Q as arguments.
Constraints:-
1 <= X, Y, P, Q <= 8
Note:- King and Queen can not be in the same positionReturn 1 if the king is in the check position else return 0.Sample Input:-
1 1 5 5
Sample Output:-
1
Sample Input:-
3 4 6 6
Sample Output:-
0, I have written this Solution Code: int QueenAttack(int X, int Y, int P, int Q){
if(X==P || Y==Q || abs(X-P)==abs(Y-Q) ){
return 1;
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In an 8X8 chessboard. Given the positions of the Queen as (X, Y) and the King as (P, Q) .
Your task is to check whether the queen can attack the king in one move or not.
The queen is the most powerful piece in the game of chess. It can move any number of squares vertically, horizontally or diagonally .<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>QueenAttack()</b> that takes integers X, Y, P, and Q as arguments.
Constraints:-
1 <= X, Y, P, Q <= 8
Note:- King and Queen can not be in the same positionReturn 1 if the king is in the check position else return 0.Sample Input:-
1 1 5 5
Sample Output:-
1
Sample Input:-
3 4 6 6
Sample Output:-
0, I have written this Solution Code: static int QueenAttack(int X, int Y, int P, int Q){
if(X==P || Y==Q || Math.abs(X-P)==Math.abs(Y-Q) ){
return 1;
}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In an 8X8 chessboard. Given the positions of the Queen as (X, Y) and the King as (P, Q) .
Your task is to check whether the queen can attack the king in one move or not.
The queen is the most powerful piece in the game of chess. It can move any number of squares vertically, horizontally or diagonally .<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>QueenAttack()</b> that takes integers X, Y, P, and Q as arguments.
Constraints:-
1 <= X, Y, P, Q <= 8
Note:- King and Queen can not be in the same positionReturn 1 if the king is in the check position else return 0.Sample Input:-
1 1 5 5
Sample Output:-
1
Sample Input:-
3 4 6 6
Sample Output:-
0, I have written this Solution Code: int QueenAttack(int X, int Y, int P, int Q){
if(X==P || Y==Q || abs(X-P)==abs(Y-Q) ){
return 1;
}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: When learning a new language, we first learn to output some message. Here, we'll start with the famous "Hello World" message. There is no input, you just have to print "Hello World".No InputHello WorldExplanation:
Hello World is printed., I have written this Solution Code: a="Hello World"
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: When learning a new language, we first learn to output some message. Here, we'll start with the famous "Hello World" message. There is no input, you just have to print "Hello World".No InputHello WorldExplanation:
Hello World is printed., I have written this Solution Code: import java.util.*;
import java.io.*;
class Main{
public static void main(String args[]){
System.out.println("Hello World");
}
}, In this Programming Language: Java, 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: Given an integer K, find a positive integer x such that <b>K = x<sup>2</sup> + 3*x</b>. If no such positive integer x exists, print -1.First and the only line of the input contains an integer K.
Constraints:
1 <= K <= 10<sup>18</sup>Print a positive integer x such that the above equation satisfies. If no such integer x exists, print -1.Sample Input:
28
Sample Output:
4
Explaination:
4<sup>2</sup> + 3*4 = 28
There is no other positive integer that will give such result., I have written this Solution Code: def FindIt(n):
sqrt_n = int(K**0.5)
check = -1
for i in range(sqrt_n - 1, sqrt_n - 2, -1):
check = i**2 + (i * 3)
if check == n:
return i
return -1
K = int(input())
print(FindIt(K)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K, find a positive integer x such that <b>K = x<sup>2</sup> + 3*x</b>. If no such positive integer x exists, print -1.First and the only line of the input contains an integer K.
Constraints:
1 <= K <= 10<sup>18</sup>Print a positive integer x such that the above equation satisfies. If no such integer x exists, print -1.Sample Input:
28
Sample Output:
4
Explaination:
4<sup>2</sup> + 3*4 = 28
There is no other positive integer that will give such result., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long double ld;
const int mod = 1e9 + 7;
const int INF = 1e9;
void solve(){
int n;
cin >> n;
int l = 1, r = 1e9, ans = -1;
while(l <= r){
int m = (l + r)/2;
int val = m*m + 3*m;
if(val == n){
ans = m;
break;
}
if(val < n){
l = m + 1;
}
else r = m - 1;
}
cout << ans;
}
signed main(){
fast
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
solve();
if(i != t) cout << endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K, find a positive integer x such that <b>K = x<sup>2</sup> + 3*x</b>. If no such positive integer x exists, print -1.First and the only line of the input contains an integer K.
Constraints:
1 <= K <= 10<sup>18</sup>Print a positive integer x such that the above equation satisfies. If no such integer x exists, print -1.Sample Input:
28
Sample Output:
4
Explaination:
4<sup>2</sup> + 3*4 = 28
There is no other positive integer that will give such result., 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));
long K = Long.parseLong(br.readLine());
long ans = -1;
for(long x =0;((x*x)+(3*x))<=K;x++){
if(K==((x*x)+(3*x))){
ans = x;
break;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take an integer as input and print it.The first line contains integer as input.
<b>Constraints</b>
1 <= N <= 10Print the input integer in a single lineSample Input:-
2
Sample Output:-
2
Sample Input:-
4
Sample Output:-
4, I have written this Solution Code: /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Main
{
public static void printVariable(int variable){
System.out.println(variable);
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
printVariable(num);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, check if the given string contains any white space or not.Input contains a single string S.
Constraints:-
1 < = |S| < = 20
Note:- String will only contain lowercase english letters.Print "Yes" if the given string contains a whitespace else print "No"Sample Input:-
newton school
Sample Output:-
Yes
Sample Input:-
newtonschool
Sample Output:-
No, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if(str.contains(" ")) {
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, check if the given string contains any white space or not.Input contains a single string S.
Constraints:-
1 < = |S| < = 20
Note:- String will only contain lowercase english letters.Print "Yes" if the given string contains a whitespace else print "No"Sample Input:-
newton school
Sample Output:-
Yes
Sample Input:-
newtonschool
Sample Output:-
No, I have written this Solution Code: s = input()
if " " in s:
print("Yes")
else:
print("No")
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, check if the given string contains any white space or not.Input contains a single string S.
Constraints:-
1 < = |S| < = 20
Note:- String will only contain lowercase english letters.Print "Yes" if the given string contains a whitespace else print "No"Sample Input:-
newton school
Sample Output:-
Yes
Sample Input:-
newtonschool
Sample Output:-
No, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin,s);
for(int i=0;i<s.length();i++){
if(s[i]==' '){cout<<"Yes";return 0;}
}
cout<<"No";return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, your task is to print the string S.You don't have to worry about taking input, you just have to complete the function <b>printString</b>Print the string S.Sample Input 1:-
NewtonSchool
Sample Output 1:-
NewtonSchool
Sample Input 2:-
Hello
Sample Output 2:-
Hello, I have written this Solution Code: static void printString(String stringVariable){
System.out.println(stringVariable);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, your task is to print the string S.You don't have to worry about taking input, you just have to complete the function <b>printString</b>Print the string S.Sample Input 1:-
NewtonSchool
Sample Output 1:-
NewtonSchool
Sample Input 2:-
Hello
Sample Output 2:-
Hello, I have written this Solution Code: S=input()
print(S), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, your task is to print the string S.You don't have to worry about taking input, you just have to complete the function <b>printString</b>Print the string S.Sample Input 1:-
NewtonSchool
Sample Output 1:-
NewtonSchool
Sample Input 2:-
Hello
Sample Output 2:-
Hello, I have written this Solution Code: void printString(string s){
cout<<s;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b, your task is to check following conditions:-
1. If a <= 10 and b >= 10 (Logical AND).
2. Atleast one from a or b will be even (Logical OR).
3. if a is not equal to b (Logical NOT).The first line of the input contains 2 integers a and b.
<b>Constraints:</b>
1 <= a, b <= 100Print the string <b>"true"</b> if the condition holds in each function else <b>"false"</b> .
Sample Input:-
3 12
Sample Output:-
true true true
Explanation
So a = 3 and b = 12, so a<=10 and b>=10 hence first condition true, a is not even but b is even so atleast one of them is even hence true, third a != b which is also true hence the final output comes true true true.
Sample Input:-
10 10
Sample Output:-
true true false
, I have written this Solution Code: a, b = list(map(int, input().split(" ")))
print(str(a <= 10 and b >= 10).lower(), end=' ')
print(str(a % 2 == 0 or b % 2 == 0).lower(), end=' ')
print(str(not a == b).lower()), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b, your task is to check following conditions:-
1. If a <= 10 and b >= 10 (Logical AND).
2. Atleast one from a or b will be even (Logical OR).
3. if a is not equal to b (Logical NOT).The first line of the input contains 2 integers a and b.
<b>Constraints:</b>
1 <= a, b <= 100Print the string <b>"true"</b> if the condition holds in each function else <b>"false"</b> .
Sample Input:-
3 12
Sample Output:-
true true true
Explanation
So a = 3 and b = 12, so a<=10 and b>=10 hence first condition true, a is not even but b is even so atleast one of them is even hence true, third a != b which is also true hence the final output comes true true true.
Sample Input:-
10 10
Sample Output:-
true true false
, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Main {
static boolean Logical_AND(int a, int b){
if(a<=10 && b>=10){
return true;}
return false;}
static boolean Logical_OR(int a, int b){
if(a%2==0 || b%2==0){
return true;}
return false;}
static boolean Logical_NOT(int a, int b){
if(a!=b){
return true;}
return false;}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.print(Logical_AND(a, b)+" ");
System.out.print(Logical_OR(a,b)+" ");
System.out.print(Logical_NOT(a,b)+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b, your task is to check following conditions:-
1. If a <= 10 and b >= 10 (Logical AND).
2. Atleast one from a or b will be even (Logical OR).
3. if a is not equal to b (Logical NOT).The first line of the input contains 2 integers a and b.
<b>Constraints:</b>
1 <= a, b <= 100Print the string <b>"true"</b> if the condition holds in each function else <b>"false"</b> .
Sample Input:-
3 12
Sample Output:-
true true true
Explanation
So a = 3 and b = 12, so a<=10 and b>=10 hence first condition true, a is not even but b is even so atleast one of them is even hence true, third a != b which is also true hence the final output comes true true true.
Sample Input:-
10 10
Sample Output:-
true true false
, I have written this Solution Code: a, b = list(map(int, input().split(" ")))
print(str(a <= 10 and b >= 10).lower(), end=' ')
print(str(a % 2 == 0 or b % 2 == 0).lower(), end=' ')
print(str(not a == b).lower()), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b, your task is to check following conditions:-
1. If a <= 10 and b >= 10 (Logical AND).
2. Atleast one from a or b will be even (Logical OR).
3. if a is not equal to b (Logical NOT).The first line of the input contains 2 integers a and b.
<b>Constraints:</b>
1 <= a, b <= 100Print the string <b>"true"</b> if the condition holds in each function else <b>"false"</b> .
Sample Input:-
3 12
Sample Output:-
true true true
Explanation
So a = 3 and b = 12, so a<=10 and b>=10 hence first condition true, a is not even but b is even so atleast one of them is even hence true, third a != b which is also true hence the final output comes true true true.
Sample Input:-
10 10
Sample Output:-
true true false
, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Main {
static boolean Logical_AND(int a, int b){
if(a<=10 && b>=10){
return true;}
return false;}
static boolean Logical_OR(int a, int b){
if(a%2==0 || b%2==0){
return true;}
return false;}
static boolean Logical_NOT(int a, int b){
if(a!=b){
return true;}
return false;}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.print(Logical_AND(a, b)+" ");
System.out.print(Logical_OR(a,b)+" ");
System.out.print(Logical_NOT(a,b)+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N, with all values as non-negative integers. Bodega asked you whether it is possible to partition the array into exactly K non-empty subarrays such that the sum of values of each subarray is equal. If it is possible print "Yes", otherwise print "No".
Note:
Every element of the array should belong to exactly one subarray in the partition.The first line consists of two space-separated integers N and K respectively.
The second line consists of N space-separated integers β A<sub>1</sub>, A<sub>2</sub>, ... A<sub>N</sub>.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 20
0 ≤ A<sub>i</sub> ≤ 10<sup>6</sup>
There exists at least one non-zero element in array A.Print a single word "Yes" if the array can be partitioned into K equal sum parts, otherwise print "No".
Note:
The quotation marks are only for clarity, you should not print them in output.
The output is case-sensitive.Sample Input 1:
5 2
2 2 1 2 2
Sample Output 1:
No
Sample Input 2:
10 3
3 0 1 2 0 0 6 0 1 5
Sample Output 2:
Yes
Explanation:
The array can be partitioned as {3, 0, 1, 2, 0, 0}, {6}, {0, 1, 5}., I have written this Solution Code: import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve(int TC) {
int n = ni(), k = ni();
long sum = 0L, tempSum = 0;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
sum += a[i];
}
if (sum % k != 0) {
pn("No");
return;
}
long req = sum / (long) k;
for (int i : a) {
tempSum += i;
if (tempSum == req) {
tempSum = 0;
} else if (tempSum > req) {
pn("No");
return;
}
}
pn(tempSum == 0 ? "Yes" : "No");
}
boolean TestCases = false;
public static void main(String[] args) throws Exception {
new Main().run();
}
void hold(boolean b) throws Exception {
if (!b) throw new Exception("Hold right there, Sparky!");
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
int T = TestCases ? ni() : 1;
for (int t = 1; t <= T; t++) solve(t);
out.flush();
if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
double nd() {
return Double.parseDouble(ns());
}
char nc() {
return (char) skip();
}
int BUF_SIZE = 1024 * 8;
byte[] inbuf = new byte[BUF_SIZE];
int lenbuf = 0, ptrbuf = 0;
int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
void tr(Object... o) {
if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N, with all values as non-negative integers. Bodega asked you whether it is possible to partition the array into exactly K non-empty subarrays such that the sum of values of each subarray is equal. If it is possible print "Yes", otherwise print "No".
Note:
Every element of the array should belong to exactly one subarray in the partition.The first line consists of two space-separated integers N and K respectively.
The second line consists of N space-separated integers β A<sub>1</sub>, A<sub>2</sub>, ... A<sub>N</sub>.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 20
0 ≤ A<sub>i</sub> ≤ 10<sup>6</sup>
There exists at least one non-zero element in array A.Print a single word "Yes" if the array can be partitioned into K equal sum parts, otherwise print "No".
Note:
The quotation marks are only for clarity, you should not print them in output.
The output is case-sensitive.Sample Input 1:
5 2
2 2 1 2 2
Sample Output 1:
No
Sample Input 2:
10 3
3 0 1 2 0 0 6 0 1 5
Sample Output 2:
Yes
Explanation:
The array can be partitioned as {3, 0, 1, 2, 0, 0}, {6}, {0, 1, 5}., I have written this Solution Code:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %mod;
a = a * a %mod;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("input.txt","r"))
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
#endif
int n,k;
cin>>n>>k;
int a[n];
int sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
if(sum%k)
cout<<"No";
else
{
int tot=0;
int cur=0;
sum/=k;
for(int i=0;i<n;i++)
{
cur+=a[i];
if(cur==sum)
{
tot++;
cur=0;
}
}
if(cur==0 && tot==k)
cout<<"Yes";
else
cout<<"No";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N, with all values as non-negative integers. Bodega asked you whether it is possible to partition the array into exactly K non-empty subarrays such that the sum of values of each subarray is equal. If it is possible print "Yes", otherwise print "No".
Note:
Every element of the array should belong to exactly one subarray in the partition.The first line consists of two space-separated integers N and K respectively.
The second line consists of N space-separated integers β A<sub>1</sub>, A<sub>2</sub>, ... A<sub>N</sub>.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 20
0 ≤ A<sub>i</sub> ≤ 10<sup>6</sup>
There exists at least one non-zero element in array A.Print a single word "Yes" if the array can be partitioned into K equal sum parts, otherwise print "No".
Note:
The quotation marks are only for clarity, you should not print them in output.
The output is case-sensitive.Sample Input 1:
5 2
2 2 1 2 2
Sample Output 1:
No
Sample Input 2:
10 3
3 0 1 2 0 0 6 0 1 5
Sample Output 2:
Yes
Explanation:
The array can be partitioned as {3, 0, 1, 2, 0, 0}, {6}, {0, 1, 5}., I have written this Solution Code: N,K=map(int,input().split())
S=0
a=input().split()
for i in a:
S+=int(i)
if S%K!=0:
print('No')
else:
su=0
count=0
asd=S/K
for i in range(N):
su+=int(a[i])
if su==asd:
count+=1
su=0
if count==K:
print('Yes')
else:
print('No'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A (distinct integers) of size N, and you are also given a sum. You need to find if two numbers in A exists that have sum equal to the given sum.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
1 <= sum <= 10^5
1 <= A[i] <= 10^4For each testcase, in a new line, print "1"(without quotes) if any pair found, othwerwise print "0"(without quotes) if not found.Sample Input
2
10 14
1 2 3 4 5 6 7 8 9 10
2 10
2 5
Sample Output
1
0
Explanation:
Testcase 1: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and sum = 14. There is a pair {4, 10} with sum 14.
Testcase 2: arr[] = {2, 5} and sum = 10. There is no pair with sum 10., I have written this Solution Code: import numpy as np
from collections import defaultdict
t=int(input())
def solve():
d=defaultdict(int)
n,s=input().strip().split()
s=int(s)
a=np.array([input().strip().split()],int).flatten()
for i in a:
d[i]+=1
c=0
for i in a:
if(d[s-i]>0 and (s-i)!=i):
c=1
break
print(c)
while(t>0):
solve()
t-=1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A (distinct integers) of size N, and you are also given a sum. You need to find if two numbers in A exists that have sum equal to the given sum.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
1 <= sum <= 10^5
1 <= A[i] <= 10^4For each testcase, in a new line, print "1"(without quotes) if any pair found, othwerwise print "0"(without quotes) if not found.Sample Input
2
10 14
1 2 3 4 5 6 7 8 9 10
2 10
2 5
Sample Output
1
0
Explanation:
Testcase 1: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and sum = 14. There is a pair {4, 10} with sum 14.
Testcase 2: arr[] = {2, 5} and sum = 10. There is no pair with sum 10., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#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 sz 200000
signed main()
{
int t;
cin>>t;
while(t>0)
{ t--;
int ch=0;
int n,sum;
cin>>n>>sum;
int A[n];
set<int> ss;
for(int i=0;i<n;i++)
{
cin>>A[i];
if(ss.find(sum-A[i])!=ss.end()) ch=1;
ss.insert(A[i]);
}
cout<<ch<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A (distinct integers) of size N, and you are also given a sum. You need to find if two numbers in A exists that have sum equal to the given sum.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
1 <= sum <= 10^5
1 <= A[i] <= 10^4For each testcase, in a new line, print "1"(without quotes) if any pair found, othwerwise print "0"(without quotes) if not found.Sample Input
2
10 14
1 2 3 4 5 6 7 8 9 10
2 10
2 5
Sample Output
1
0
Explanation:
Testcase 1: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and sum = 14. There is a pair {4, 10} with sum 14.
Testcase 2: arr[] = {2, 5} and sum = 10. There is no pair with sum 10., I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for(int i = 1; i <= testCases; i++)
{
int arrSize = sc.nextInt();
int sum = sc.nextInt();
int arr[] = new int[arrSize];
for(int j = 0; j < arrSize; j++)
arr[j] = sc.nextInt();
System.out.println(pairFound(arr, arrSize, sum));
}
}
static int pairFound(int arr[], int arrSize, int sum)
{
HashSet<Integer> hSet = new HashSet<>();
for(int i = 0; i < arrSize; i++)
{
if(hSet.contains(sum-arr[i]) == true)
return 1;
hSet.add(arr[i]);
}
return 0;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code: def getMissingNo(arr, n):
total = (n+1)*(n)//2
sum_of_A = sum(arr)
return total - sum_of_A
N = int(input())
arr = list(map(int,input().split()))
one = getMissingNo(arr,N)
print(one), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n-1];
for(int i=0;i<n-1;i++){
a[i]=sc.nextInt();
}
boolean present = false;
for(int i=1;i<=n;i++){
present=false;
for(int j=0;j<n-1;j++){
if(a[j]==i){present=true;}
}
if(present==false){
System.out.print(i);
return;
}
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n-1];
for(int i=0;i<n-1;i++){
cin>>a[i];
}
sort(a,a+n-1);
for(int i=1;i<n;i++){
if(i!=a[i-1]){cout<<i<<endl;return 0;}
}
cout<<n;
}
, In this Programming Language: C++, 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 |
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: for _ in range(int(input())):
n = int(input())
print(*sorted(list(map(int,input().split())))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings 'str1' and 'str2', check whether both of these strings are beautiful or not.
Two strings str1 and str2 are called beautiful if there is a one-to-one mapping possible for every character of str1 to every character of str2 while preserving the order.
<b>Note</b>: All occurrences of every character in str1 should map to the same character in str2First line contain a string str1. Second line contain a string str2.
<b>Constraints:</b>
1 <= |str1|, |str2| <= 2*10^4Print "Yes" if str1 and str2 are beautiful otherwise print "No".Sample Input 1:
ppqrr
ffghh
Sample Output 1:
Yes
Explanation:
Given strings are Beautiful among each other.
There a three different strings in str1 and str2 with equal frequency.
str1
p : 2
q : 1
r : 2
Similarly str2 having
f : 2
g : 1
h : 2, I have written this Solution Code: import numpy as np
from collections import defaultdict
str1=input()
str2=input()
str1_d=defaultdict(int)
str2_d=defaultdict(int)
for i in str1:
str1_d[i]+=1
for i in str2:
str2_d[i]+=1
c=1
for i in range(len(str1)):
if(str1_d[str1[i]]!=str2_d[str2[i]]):
c=0
break
if(c==1):
print("Yes",end="")
else:
print("No",end=""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings 'str1' and 'str2', check whether both of these strings are beautiful or not.
Two strings str1 and str2 are called beautiful if there is a one-to-one mapping possible for every character of str1 to every character of str2 while preserving the order.
<b>Note</b>: All occurrences of every character in str1 should map to the same character in str2First line contain a string str1. Second line contain a string str2.
<b>Constraints:</b>
1 <= |str1|, |str2| <= 2*10^4Print "Yes" if str1 and str2 are beautiful otherwise print "No".Sample Input 1:
ppqrr
ffghh
Sample Output 1:
Yes
Explanation:
Given strings are Beautiful among each other.
There a three different strings in str1 and str2 with equal frequency.
str1
p : 2
q : 1
r : 2
Similarly str2 having
f : 2
g : 1
h : 2, I have written this Solution Code: import java.io.IOException;
import java.util.Arrays;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
Scanner sc=new Scanner(System.in);
int tc =1;
while(tc-->0)
{
String s1 = sc.nextLine();
String s2 = sc.nextLine();
Solution obj = new Solution();
boolean a = obj.areBeautiful(s1,s2);
if(a)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
// } Driver Code Ends
class Solution
{
//Function to check if two strings are isomorphic.
public static boolean areBeautiful(String str1,String str2)
{
// Your code here
if(str1.length() != str2.length()){
return false;
}
HashMap<Character,Character> map = new HashMap<>();
for (int i = 0; i < str1.length(); i++) {
if(!map.containsKey(str1.charAt(i))){
if(map.containsValue(str2.charAt(i))){
return false;
}
map.put(str1.charAt(i),str2.charAt(i));
}
else {
char val = map.get(str1.charAt(i));
if(val != str2.charAt(i)){
return false;
}
}
}
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N containing 0s and 1s only. The task is to count the subarrays having an equal number of 0s and 1s.The first line of the input contains an integer N denoting the size of the array and the second line contains N space-separated 0s and 1s.
Constraints:-
1 <= N <= 10^6
0 <= A[i] <= 1For each test case, print the count of required sub-arrays in new line.Sample Input
7
1 0 0 1 0 1 1
Sample Output
8
The index range for the 8 sub-arrays are:
(0, 1), (2, 3), (0, 3), (3, 4), (4, 5)
(2, 5), (0, 5), (1, 6), I have written this Solution Code: size = int(input())
givenList = list(map(int,input().split()))
hs = {}
sm = 0
ct = 0
for i in givenList:
if i == 0:
i = -1
sm = sm + i
if sm == 0:
ct += 1
if sm not in hs.keys():
hs[sm] = 1
else:
freq = hs[sm]
ct = ct +freq
hs[sm] = freq + 1
print(ct), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N containing 0s and 1s only. The task is to count the subarrays having an equal number of 0s and 1s.The first line of the input contains an integer N denoting the size of the array and the second line contains N space-separated 0s and 1s.
Constraints:-
1 <= N <= 10^6
0 <= A[i] <= 1For each test case, print the count of required sub-arrays in new line.Sample Input
7
1 0 0 1 0 1 1
Sample Output
8
The index range for the 8 sub-arrays are:
(0, 1), (2, 3), (0, 3), (3, 4), (4, 5)
(2, 5), (0, 5), (1, 6), I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define max1 1000001
int a[max1];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==0){a[i]=-1;}
}
long sum=0;
unordered_map<long,int> m;
long cnt=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==0){cnt++;}
cnt+=m[sum];
m[sum]++;
}
cout<<cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N containing 0s and 1s only. The task is to count the subarrays having an equal number of 0s and 1s.The first line of the input contains an integer N denoting the size of the array and the second line contains N space-separated 0s and 1s.
Constraints:-
1 <= N <= 10^6
0 <= A[i] <= 1For each test case, print the count of required sub-arrays in new line.Sample Input
7
1 0 0 1 0 1 1
Sample Output
8
The index range for the 8 sub-arrays are:
(0, 1), (2, 3), (0, 3), (3, 4), (4, 5)
(2, 5), (0, 5), (1, 6), I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int arrSize = sc.nextInt();
long arr[] = new long[arrSize];
for(int i = 0; i < arrSize; i++)
arr[i] = sc.nextInt();
System.out.println(countSubarrays(arr, arrSize));
}
static long countSubarrays(long arr[], int arrSize)
{
for(int i = 0; i < arrSize; i++)
{
if(arr[i] == 0)
arr[i] = -1;
}
long ans = 0;
long sum = 0;
HashMap<Long, Integer> hash = new HashMap<>();
for(int i = 0; i < arrSize; i++)
{
sum += arr[i];
if(sum == 0)
ans++;
if(hash.containsKey(sum) == true)
{
ans += hash.get(sum);
int freq = hash.get(sum);
hash.put(sum, freq+1);
}
else hash.put(sum, 1);
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N, containing positive integers. You need to sort the elements of array using Insertion sort algorithm.First line of the input denotes number of test cases T. First line of the testcase is the size of array N and second line consists of array elements separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^3
1 <= A[i] <= 10^3For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
Explanation:
Testcase 1: The array after perfoming insertion sort: 1 3 4 7 9.
Testcase 2: The array after performing insertion sort: 1 2 3 4 5 6 7 8 9 10., I have written this Solution Code: // arr is unsorted array
// n is the number of elements in the array
function insertionSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => a - b)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N, containing positive integers. You need to sort the elements of array using Insertion sort algorithm.First line of the input denotes number of test cases T. First line of the testcase is the size of array N and second line consists of array elements separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^3
1 <= A[i] <= 10^3For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
Explanation:
Testcase 1: The array after perfoming insertion sort: 1 3 4 7 9.
Testcase 2: The array after performing insertion sort: 1 2 3 4 5 6 7 8 9 10., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void insertionSort(int[] arr){
for(int i = 0; i < arr.length-1; i++){
for(int j = i+1; j < arr.length; j++){
if(arr[i] > arr[j]){
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
while(T > 0){
int n = scan.nextInt();
int arr[] = new int[n];
for(int i = 0; i<n; i++){
arr[i] = scan.nextInt();
}
insertionSort(arr);
for(int i = 0; i<n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
T--;
System.gc();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N, containing positive integers. You need to sort the elements of array using Insertion sort algorithm.First line of the input denotes number of test cases T. First line of the testcase is the size of array N and second line consists of array elements separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^3
1 <= A[i] <= 10^3For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
Explanation:
Testcase 1: The array after perfoming insertion sort: 1 3 4 7 9.
Testcase 2: The array after performing insertion sort: 1 2 3 4 5 6 7 8 9 10., 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 t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
cout << a[i] << " ";
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N, containing positive integers. You need to sort the elements of array using Insertion sort algorithm.First line of the input denotes number of test cases T. First line of the testcase is the size of array N and second line consists of array elements separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^3
1 <= A[i] <= 10^3For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
Explanation:
Testcase 1: The array after perfoming insertion sort: 1 3 4 7 9.
Testcase 2: The array after performing insertion sort: 1 2 3 4 5 6 7 8 9 10., I have written this Solution Code: def InsertionSort(arr):
arr.sort()
return arr, 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: 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: Your school has N students, and each student has a strength denoted by an integer. You have to make a team by picking some students such that gcd of strengths of those students is not 1, otherwise they tend to fight all the time, and there wil be no team spirit .
What is the maximum number of students you can pick ?The input consists of two lines.
The first line contains an integer n, the number of Students in the school
The next line contains n space separated integers, where the i-th of them denotes s[i], the strength of the i-th Student.
Constraints:-
1<=n<=10^5
1<=s<=10^5Print single integer β the maximum number of Students you can take.Input:
5
2 3 4 6 7
Output:
3
Input:
8
45 23 12 3 4 62 2 4
Output:
5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String ar[] = br.readLine().split(" ");
int a[] = new int[n];
int max = Integer.MIN_VALUE;
for(int i=0;i<n;i++){
a[i] = Integer.parseInt(ar[i]);
max = Math.max(max,a[i]);
}
int ans = 1;
for(int i=2;i<=Math.sqrt(max);i++)
{ int count=0,k=0;
for(int j=0;j<n;j++)
{
if(a[j]%i ==0)
count++;
if(i>a[j])
k++;
}
ans = Math.max(ans,count);
if(count>=n-k)
break;
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Your school has N students, and each student has a strength denoted by an integer. You have to make a team by picking some students such that gcd of strengths of those students is not 1, otherwise they tend to fight all the time, and there wil be no team spirit .
What is the maximum number of students you can pick ?The input consists of two lines.
The first line contains an integer n, the number of Students in the school
The next line contains n space separated integers, where the i-th of them denotes s[i], the strength of the i-th Student.
Constraints:-
1<=n<=10^5
1<=s<=10^5Print single integer β the maximum number of Students you can take.Input:
5
2 3 4 6 7
Output:
3
Input:
8
45 23 12 3 4 62 2 4
Output:
5, I have written this Solution Code: import math
def SieveOfEratosthenes(n,s):
p=2
prime = [True for i in range(n+1)]
M = 0
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, int(n**(1/2)+1)):
if prime[p]:
count = 0
for j in s:
if j%p == 0:
count += 1
if count>M:
M=count
print(M)
n = int(input())
s = list(map(int,input().split()))
s.sort()
SieveOfEratosthenes(s[-1],s), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Your school has N students, and each student has a strength denoted by an integer. You have to make a team by picking some students such that gcd of strengths of those students is not 1, otherwise they tend to fight all the time, and there wil be no team spirit .
What is the maximum number of students you can pick ?The input consists of two lines.
The first line contains an integer n, the number of Students in the school
The next line contains n space separated integers, where the i-th of them denotes s[i], the strength of the i-th Student.
Constraints:-
1<=n<=10^5
1<=s<=10^5Print single integer β the maximum number of Students you can take.Input:
5
2 3 4 6 7
Output:
3
Input:
8
45 23 12 3 4 62 2 4
Output:
5, 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 n;
cin>>n;
int a[n];
unordered_map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
int x=sqrt(a[i]);
m[a[i]]++;
for(int j=2;j<=x;j++){
if(a[i]%j==0){
m[j]++;
if(j*j!=a[i]){m[a[i]/j]++;}
}
}
}
int ans=0;
for(auto it=m.begin();it!=m.end();it++){
ans=max(it->second,ans);
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<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>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code:
int RotationPolicy(int A, int B){
int cnt=0;
for(int i=A;i<=B;i++){
if((i-1)%2!=0 && (i-1)%3!=0){cnt++;}
}
return cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<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>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code:
int RotationPolicy(int A, int B){
int cnt=0;
for(int i=A;i<=B;i++){
if((i-1)%2!=0 && (i-1)%3!=0){cnt++;}
}
return cnt;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<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>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code: static int RotationPolicy(int A, int B){
int cnt=0;
for(int i=A;i<=B;i++){
if((i-1)%2!=0 && (i-1)%3!=0){cnt++;}
}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<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>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code:
def RotationPolicy(A, B):
cnt=0
for i in range (A,B+1):
if(i-1)%2!=0 and (i-1)%3!=0:
cnt=cnt+1
return cnt
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<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>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code: function RotationPolicy(a, b) {
// write code here
// do no console.log the answer
// return the output using return keyword
let count = 0
for (let i = a; i <= b; i++) {
if((i-1)%2 !== 0 && (i-1)%3 !==0){
count++
}
}
return count
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given is a positive integer N, where none of the digits is 0. Let k be the number of digits in N. We want to make a multiple of 3 by erasing at least 0 and at most kβ1 digits from N and concatenating the remaining digits without changing the order. Determine whether it is possible to make a multiple of 3 in this way. If it is possible, find the minimum number of digits that must be erased to make such a number.The input consists of a single integer N.
<b>Constraints</b>
1≤N≤10^18
None of the digits in N is 0.If it is impossible to make a multiple of 3, print -1; otherwise, print the minimum number of digits that must be erased to make such a number.<b>Sample Input 1</b>
35
<b>Sample Output 1</b>
1
<b>Sample Input 2</b>
369
<b>Sample Output 2</b>
0
<b>Sample Input 3</b>
6227384
<b>Sample Output 3</b>
1
<b>Sample Input 4</b>
11
<b>Sample Output 4</b>
-1, I have written this Solution Code: #include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main() {
int64_t n;
scanf("%" SCNd64, &n);
int cnt[3] = { 0 };
while (n) {
cnt[n % 10 % 3]++;
n /= 10;
}
int cur = (cnt[1] + 2 * cnt[2]) % 3;
int k = cnt[0] + cnt[1] + cnt[2];
int res;
if (!cur) res = 0;
else if (cur == 1) {
if (cnt[1]) {
if (k == 1) res = -1;
else res = 1;
} else {
if (k == 2) res = -1;
else res = 2;
}
} else {
if (cnt[2]) {
if (k == 1) res = -1;
else res = 1;
} else {
if (k == 2) res = -1;
else res = 2;
}
}
printf("%d\n", res);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let us start our journey by creating a 'PROFILE' table for storing the data of the users of our platform. Create a table profile with 3 fields ( USERNAME VARCHAR (24), FULL_NAME VARCHAR (72), HEADLINE VARCHAR (72) ).
<schema>[{'name': 'PROFILE', 'columns': [{'name': 'USERNAME', 'type': 'VARCHAR (24)'}, {'name': 'FULL_NAME', 'type': 'VARCHAR (72)'}, {'name': 'HEADLINE', 'type': 'VARCHAR (72)'}]}]</schema>NA - User input does not work for this question.The output will be generated in a 2-D array format consisting of rows and columns.nan, I have written this Solution Code: CREATE TABLE PROFILE (
USERNAME VARCHAR(24),
FULL_NAME VARCHAR(72),
HEADLINE VARCHAR(72)
);
, In this Programming Language: SQL, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function <code>getValue()</code> which takes one argument which is a number.
The function is a recursive function means it calls itself if a certain condition is met.
The function can take any positive whole number and will return a single-digit positive whole number from their recursive sum.
For example, if the input number is 7893
then 7+8+9+3=27(27 is not single digit number so we will add its digits again)
then 2+7=9
The function will return 9 as output.The function will take a positive whole number as input.The output will be the repeated sum of digits until there is only a single digit in the numberconst answer= getValue(456);
console.log(answer);
//prints 6 (4+5+6=15, then 1+5=6)
, I have written this Solution Code: const getValue = (num) => {
let numberX = num;
num = 0;
while (numberX > 0) {
num = num + (numberX % 10 | 0);
numberX = numberX / 10;
}
if (num < 10) {
return num;
} else {
return getValue(num);
}
};
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number n find the number of prime numbers less than equal to that number.There is only one integer containing value of n.
Constraints:-
1 <= n <= 10000000Return number of primes less than or equal to nSample Input
5
Sample Output
3
Explanation:-
2 3 and 5 are the required primes.
Sample Input
5000
Sample Output
669, I have written this Solution Code: #include <bits/stdc++.h>
// #define ll long long
using namespace std;
#define ma 10000001
bool a[ma];
int main()
{
int n;
cin>>n;
for(int i=0;i<=n;i++){
a[i]=false;
}
for(int i=2;i<=n;i++){
if(a[i]==false){
for(int j=i+i;j<=n;j+=i){
a[j]=true;
}
}
}
int cnt=0;
for(int i=2;i<=n;i++){
if(a[i]==false){cnt++;}
}
cout<<cnt;
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 find the number of prime numbers less than equal to that number.There is only one integer containing value of n.
Constraints:-
1 <= n <= 10000000Return number of primes less than or equal to nSample Input
5
Sample Output
3
Explanation:-
2 3 and 5 are the required primes.
Sample Input
5000
Sample Output
669, 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 br = new BufferedReader(new InputStreamReader(System.in));
long n = Integer.parseInt(br.readLine());
long i=2,j,count,noOfPrime=0;
if(n<=1)
System.out.println("0");
else{
while(i<=n)
{
count=0;
for(j=2; j<=Math.sqrt(i); j++)
{
if( i%j == 0 ){
count++;
break;
}
}
if(count==0){
noOfPrime++;
}
i++;
}
System.out.println(noOfPrime);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number n find the number of prime numbers less than equal to that number.There is only one integer containing value of n.
Constraints:-
1 <= n <= 10000000Return number of primes less than or equal to nSample Input
5
Sample Output
3
Explanation:-
2 3 and 5 are the required primes.
Sample Input
5000
Sample Output
669, I have written this Solution Code: function numberOfPrimes(N)
{
let arr = new Array(N+1);
for(let i = 0; i <= N; i++)
arr[i] = 0;
for(let i=2; i<= N/2; i++)
{
if(arr[i] === -1)
{
continue;
}
let p = i;
for(let j=2; p*j<= N; j++)
{
arr[p*j] = -1;
}
}
//console.log(arr);
let count = 0;
for(let i=2; i<= N; i++)
{
if(arr[i] === 0)
{
count++;
}
}
//console.log(arr);
return count;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number n find the number of prime numbers less than equal to that number.There is only one integer containing value of n.
Constraints:-
1 <= n <= 10000000Return number of primes less than or equal to nSample Input
5
Sample Output
3
Explanation:-
2 3 and 5 are the required primes.
Sample Input
5000
Sample Output
669, I have written this Solution Code: import math
n = int(input())
n=n+1
if n<3:
print(0)
else:
primes=[1]*(n//2)
for i in range(3,int(math.sqrt(n))+1,2):
if primes[i//2]:primes[i*i//2::i]=[0]*((n-i*i-1)//(2*i)+1)
print(sum(primes)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
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;
int n, m;
cin >> n >> m;
cout << __gcd(n, m);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
li= list(map(int,input().strip().split()))
print(hcf(li[0], li[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] sp = br.readLine().trim().split(" ");
long m = Long.parseLong(sp[0]);
long n = Long.parseLong(sp[1]);
System.out.println(GCDAns(m,n));
}
private static long GCDAns(long m,long n){
if(m==0)return n;
if(n==0)return m;
return GCDAns(n%m,m);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two binary trees, the task is to find if both of them are identical or not.
Note: Here identical means exactly same.User task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function <b>isIdentical()</b> that takes the root of both the trees as input.
Constraints:
1 <= T <= 10
1 <= N <= 100
1 <= Data of Nodes <= 100
For <b>Custom Input:</b>
The first line of input should contain the number of test cases T. For each test case, there will be two lines of input.
The first line contains a number of nodes N. Second line will be a string representing the tree as described below:
The values in the string are such that for every parent you need to mention its child and state whether its is left or right like- 1 2 L 1 3 RFor each test case you need to return true or false based on whether they are identical or not. If the trees are identical then driver code will print 1 otherwise 0Sample Input:
1
/ \
2 3
1
/ \
2 3
Sample Output:
1
Sample Input:-
1
/ \
2 3
1
/ \
3 2
Sample Output:-
0
Explanation:
Test case 1: There are two trees both having 3 nodes and 2 edges, both trees are identical having the root as 1, left child of 1 is 2 and right child of 1 is 3.
Test case 2: There are two trees both having 3 nodes and 2 edges, but both trees are not identical, I have written this Solution Code: static boolean isIdentical(Node root1, Node root2)
{
if (root1 == null && root2 == null) {
return true;
}
if (root1 == null || root2 == null) {
return false;
}
if (root1.data != root2.data) {
return false;
}
return isIdentical(root1.left, root2.left) && isIdentical(root1.right, root2.right);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram is studying in Class V and has four subjects, each subject carry 100 marks. He passed with flying colors in his exam, but when his neighbour asked how much percentage did he got in exam, he got stuck in calculation. Ram is a good student but he forgot how to calculate percentage. Help Ram to get him out of this problem.
Given four numbers a , b , c and d denoting the marks in four subjects . Calculate the overall percentage (floor value ) Ram got in exam .First line contains four variables a, b, c and d.
<b>Constraints</b>
1<= a, b, c, d <= 100
Print single line containing the percentage.Sample Input 1:
25 25 25 25
Sample Output 1:
25
Sample Input 2:
75 25 75 25
Sample Output 2:
50, 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));
String str[]=br.readLine().split(" ");
int a[]=new int[str.length];
int sum=0;
for(int i=0;i<str.length;i++)
{
a[i]=Integer.parseInt(str[i]);
sum=sum+a[i];
}
System.out.println(sum/4);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram is studying in Class V and has four subjects, each subject carry 100 marks. He passed with flying colors in his exam, but when his neighbour asked how much percentage did he got in exam, he got stuck in calculation. Ram is a good student but he forgot how to calculate percentage. Help Ram to get him out of this problem.
Given four numbers a , b , c and d denoting the marks in four subjects . Calculate the overall percentage (floor value ) Ram got in exam .First line contains four variables a, b, c and d.
<b>Constraints</b>
1<= a, b, c, d <= 100
Print single line containing the percentage.Sample Input 1:
25 25 25 25
Sample Output 1:
25
Sample Input 2:
75 25 75 25
Sample Output 2:
50, I have written this Solution Code: a,b,c,d = map(int,input().split())
print((a+b+c+d)*100//400), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has A number of Pokeballs with her and there are B pokemons in front of Sara. Considering each pokemon takes one Pokeball, your task is to tell Sara if she can catch all the pokemons or not.
Sara can catch a pokemon if she is having at least one pokeball for that pokemon.<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>PokemonMaster()</b> that takes integers A and B as arguments.
Constraints:-
1 <= A, B <= 8Return 1 if Sara can catch all the pokemon else return 0.Sample Input:-
4 3
Sample Output:-
1
Sample Input:-
4 6
Sample Output:-
0, I have written this Solution Code: function PokemonMaster(a,b) {
// write code here
// do no console.log the answer
// return the output using return keyword
const ans = (a - b >= 0) ? 1 : 0
return ans
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has A number of Pokeballs with her and there are B pokemons in front of Sara. Considering each pokemon takes one Pokeball, your task is to tell Sara if she can catch all the pokemons or not.
Sara can catch a pokemon if she is having at least one pokeball for that pokemon.<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>PokemonMaster()</b> that takes integers A and B as arguments.
Constraints:-
1 <= A, B <= 8Return 1 if Sara can catch all the pokemon else return 0.Sample Input:-
4 3
Sample Output:-
1
Sample Input:-
4 6
Sample Output:-
0, I have written this Solution Code: def PokemonMaster(A,B):
if(A>=B):
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has A number of Pokeballs with her and there are B pokemons in front of Sara. Considering each pokemon takes one Pokeball, your task is to tell Sara if she can catch all the pokemons or not.
Sara can catch a pokemon if she is having at least one pokeball for that pokemon.<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>PokemonMaster()</b> that takes integers A and B as arguments.
Constraints:-
1 <= A, B <= 8Return 1 if Sara can catch all the pokemon else return 0.Sample Input:-
4 3
Sample Output:-
1
Sample Input:-
4 6
Sample Output:-
0, I have written this Solution Code: int PokemonMaster(int A, int B){
if(A>=B){return 1;}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has A number of Pokeballs with her and there are B pokemons in front of Sara. Considering each pokemon takes one Pokeball, your task is to tell Sara if she can catch all the pokemons or not.
Sara can catch a pokemon if she is having at least one pokeball for that pokemon.<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>PokemonMaster()</b> that takes integers A and B as arguments.
Constraints:-
1 <= A, B <= 8Return 1 if Sara can catch all the pokemon else return 0.Sample Input:-
4 3
Sample Output:-
1
Sample Input:-
4 6
Sample Output:-
0, I have written this Solution Code: int PokemonMaster(int A, int B){
if(A>=B){return 1;}
return 0;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has A number of Pokeballs with her and there are B pokemons in front of Sara. Considering each pokemon takes one Pokeball, your task is to tell Sara if she can catch all the pokemons or not.
Sara can catch a pokemon if she is having at least one pokeball for that pokemon.<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>PokemonMaster()</b> that takes integers A and B as arguments.
Constraints:-
1 <= A, B <= 8Return 1 if Sara can catch all the pokemon else return 0.Sample Input:-
4 3
Sample Output:-
1
Sample Input:-
4 6
Sample Output:-
0, I have written this Solution Code: static int PokemonMaster(int A, int B){
if(A>=B){return 1;}
return 0;
}, 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: 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: Daemon has 2 dragon eggs, one is made of A grams of Gold and the other is made of B grams of Silver.
We know that Gold costs G coins per gram and Silver costs S coins per gram. You need to determine which of the two dragon eggs are more valuable.The input contains 4 space-separated integers G, S, A, B.
<b> Constraints: </b>
1 β€ G, S, A, B β€ 10<sup>4</sup>Print "Gold" (without quotes) if the Gold egg costs <b>equal to or more than</b> the silver egg. Otherwise, print "Silver" (without quotes).
Note that the output is case-sensitive.Sample Input 1:
5 4 4 5
Sample Output 1:
Gold
Sample Input 2:
1 1 2 3
Sample Output 2:
Silver, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int [] num = new int[4];
String[] str = bi.readLine().split(" ");
for(int i = 0; i < str.length; i++)
num[i] = Integer.parseInt(str[i]);
if((num[0] * num[2]) >= (num[1] * num[3]))
System.out.print("Gold");
else
System.out.print("Silver");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Daemon has 2 dragon eggs, one is made of A grams of Gold and the other is made of B grams of Silver.
We know that Gold costs G coins per gram and Silver costs S coins per gram. You need to determine which of the two dragon eggs are more valuable.The input contains 4 space-separated integers G, S, A, B.
<b> Constraints: </b>
1 β€ G, S, A, B β€ 10<sup>4</sup>Print "Gold" (without quotes) if the Gold egg costs <b>equal to or more than</b> the silver egg. Otherwise, print "Silver" (without quotes).
Note that the output is case-sensitive.Sample Input 1:
5 4 4 5
Sample Output 1:
Gold
Sample Input 2:
1 1 2 3
Sample Output 2:
Silver, I have written this Solution Code:
G, S, A, B = input().split()
g = int(G)
s = int(S)
a = int(A)
b = int(B)
gold = (g*a)
silver = (s*b)
if(gold>=silver):
print("Gold")
else:
print("Silver"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Daemon has 2 dragon eggs, one is made of A grams of Gold and the other is made of B grams of Silver.
We know that Gold costs G coins per gram and Silver costs S coins per gram. You need to determine which of the two dragon eggs are more valuable.The input contains 4 space-separated integers G, S, A, B.
<b> Constraints: </b>
1 β€ G, S, A, B β€ 10<sup>4</sup>Print "Gold" (without quotes) if the Gold egg costs <b>equal to or more than</b> the silver egg. Otherwise, print "Silver" (without quotes).
Note that the output is case-sensitive.Sample Input 1:
5 4 4 5
Sample Output 1:
Gold
Sample Input 2:
1 1 2 3
Sample Output 2:
Silver, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main() {
int g, s, a, b;
cin >> g >> s >> a >> b;
cout << ((g * a >= s * b) ? "Gold" : "Silver");
return 0;
}, In this Programming Language: C++, 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.