Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code:
char Race(int A, int B, int C){
if(abs(C-A)==abs(C-B)){return 'D';}
if(abs(C-A)>abs(C-B)){return 'S';}
else{
return 'N';}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code: def Race(A,B,C):
if abs(C-A) ==abs(C-B):
return 'D'
if abs(C-A)>abs(C-B):
return 'S'
return 'N'
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code:
char Race(int A, int B, int C){
if(abs(C-A)==abs(C-B)){return 'D';}
if(abs(C-A)>abs(C-B)){return 'S';}
else{
return 'N';}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code: static char Race(int A,int B,int C){
if(Math.abs(C-A)==Math.abs(C-B)){return 'D';}
if(Math.abs(C-A)>Math.abs(C-B)){return 'S';}
else{
return 'N';}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a door at Newton School that can be used only by one person at a time i. e either a person can enter from the door or exit but no two people can do it simultaneously. If two people going in the opposite direction arrived at the door at the same time then these 3 cases should be considered:-
1. If the door was not used before or it was not used in the previous second then the person who wants to exit goes first.
2. If the door has been used in the previous second for entering, then the person who wants to enter goes first.
3. If the door has been used in the previous second for exiting, then the person who wants to exist goes first.
If two people arrive at the same time and go in the same direction then the person whose name in the given list comes first will go first.
<b>Note:-</b> To cross the door, it will take exactly one second for each person.The first line of input contains a single integer N containing the number of people The second line of input contains N space-separated integers depicting the arrival time of the ith person. The last line of input containing N space-separated integers which are either 0 or 1. 0 indicates that the person wants to enter and 1 indicates he wants to exit.
<b>Constraints:-</b>
1 ≤ N ≤ 50000
0 ≤ Arrival[i] ≤ Arrival [i+1] ≤ 10<sup>9</sup>Print N space-separated integers denoting the time at which the ith person will cross the door.Sample Input 1:-
4
0 0 1 5
0 1 1 0
Sample Output 1:-
2 0 1 5
Sample Input 2:-
5
0 1 1 3 3
0 1 0 0 1
Sample Output 2:-
0 2 1 4 3
<b>Explanation:-</b>
At t = 0:- the first and the second person wants to enter and exit. As per the case, 1 2nd person will go first.
At t=1, the first and the 3rd person wants to enter and exit. As per case 3, the 3rd person goes first.
At t= 2, only 1st is the only person standing so he goes.
At t = 3, and 4 no one wants to cross
At t = 5, the 4th person is the only one who wants to cross., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int[] getTimes(int [] time, int [] direction) {
Queue <Integer> enters = new LinkedList<Integer>();
Queue <Integer> exits = new LinkedList<Integer>();
int n = time.length;
int i=0;
for(i=0; i<n; i++) {
Queue <Integer> q = direction[i] == 1 ? exits : enters;
q.offer(i);
}
int [] result = new int[n];
int lastTime = -2;
Queue <Integer> lastQ = exits;
while(enters.size() > 0 && exits.size() > 0) {
int currentTime = lastTime + 1;
int peekEnterTime = time[enters.peek()];
int peekExitTime = time[exits.peek()];
Queue<Integer> q;
if(currentTime < peekEnterTime && currentTime < peekExitTime) {
q = (peekEnterTime < peekExitTime) ? enters : exits;
int personidx = q.poll();
result[personidx] = time[personidx];
lastTime=time[personidx];
lastQ = q;
} else {
if(currentTime >= peekEnterTime && currentTime >= peekExitTime) {
q = lastQ;
} else {
q = currentTime >= peekEnterTime ? enters : exits;
}
int personidx = q.poll();
result[personidx] = currentTime;
lastTime = currentTime;
lastQ = q;
}
}
Queue <Integer> q = enters.size() > 0 ? enters : exits;
while(q.size() > 0) {
int currentTime = lastTime + 1;
int personidx = q.poll();
if(currentTime < time[personidx]) {
currentTime = time[personidx];
}
result[personidx] = currentTime;
lastTime = currentTime;
}
return result;
}
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int [] Time = new int[n];
int i=0;
for(i=0; i<n; i++) {
Time[i] = input.nextInt();
}
int [] direction = new int[n];
for(i=0; i<n; i++) {
direction[i] = input.nextInt();
}
int [] res = new int[n];
res = getTimes(Time, direction);
for(i=0; i<n; i++) {
System.out.print(res[i] + " ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a door at Newton School that can be used only by one person at a time i. e either a person can enter from the door or exit but no two people can do it simultaneously. If two people going in the opposite direction arrived at the door at the same time then these 3 cases should be considered:-
1. If the door was not used before or it was not used in the previous second then the person who wants to exit goes first.
2. If the door has been used in the previous second for entering, then the person who wants to enter goes first.
3. If the door has been used in the previous second for exiting, then the person who wants to exist goes first.
If two people arrive at the same time and go in the same direction then the person whose name in the given list comes first will go first.
<b>Note:-</b> To cross the door, it will take exactly one second for each person.The first line of input contains a single integer N containing the number of people The second line of input contains N space-separated integers depicting the arrival time of the ith person. The last line of input containing N space-separated integers which are either 0 or 1. 0 indicates that the person wants to enter and 1 indicates he wants to exit.
<b>Constraints:-</b>
1 ≤ N ≤ 50000
0 ≤ Arrival[i] ≤ Arrival [i+1] ≤ 10<sup>9</sup>Print N space-separated integers denoting the time at which the ith person will cross the door.Sample Input 1:-
4
0 0 1 5
0 1 1 0
Sample Output 1:-
2 0 1 5
Sample Input 2:-
5
0 1 1 3 3
0 1 0 0 1
Sample Output 2:-
0 2 1 4 3
<b>Explanation:-</b>
At t = 0:- the first and the second person wants to enter and exit. As per the case, 1 2nd person will go first.
At t=1, the first and the 3rd person wants to enter and exit. As per case 3, the 3rd person goes first.
At t= 2, only 1st is the only person standing so he goes.
At t = 3, and 4 no one wants to cross
At t = 5, the 4th person is the only one who wants to cross., I have written this Solution Code: from collections import defaultdict, Counter,deque
from math import sqrt, log10, log, floor, factorial,gcd,ceil,log2
from bisect import bisect_left, bisect_right,insort
from itertools import permutations,combinations
from heapq import heapify,heappop,heappush
import sys, io, os
input = sys.stdin.readline
inf = float('inf')
mod = 10 ** 9 + 7
def yn(a): print("YES" if a else "NO")
cl = lambda a, b: (a + b - 1) // b
for i in range(1):
n=int(input())
arrival_time=[int(i) for i in input().split()]
enter_or_exit=[int(i) for i in input().split()]
entry=[[arrival_time[i],i] for i in range(n) if enter_or_exit[i]==0]
exit=[[arrival_time[i],i] for i in range(n) if enter_or_exit[i]==1]
ans=[-1 for i in range(n)]
entryiter=0
exititer=0
previous_use=None
previous_use_time=-100
while entryiter<len(entry) and exititer<len(exit):
if previous_use=="Entry" and entry[entryiter][0]<=previous_use_time+1:
previous_use_time+=1
ans[entry[entryiter][1]]=previous_use_time
entryiter+=1
elif previous_use=="Exit" and exit[exititer][0]<=previous_use_time+1:
previous_use_time+=1
ans[exit[exititer][1]]=previous_use_time
exititer+=1
elif entry[entryiter][0]<exit[exititer][0]:
previous_use="Entry"
previous_use_time=max(previous_use_time+1, entry[entryiter][0])
ans[entry[entryiter][1]]=previous_use_time
entryiter+=1
else:
previous_use="Exit"
previous_use_time=max(previous_use_time+1,exit[exititer][0])
ans[exit[exititer][1]]=previous_use_time
exititer+=1
while entryiter<len(entry):
previous_use_time =max(previous_use_time+1, entry[entryiter][0])
ans[entry[entryiter][1]] = previous_use_time
entryiter += 1
while exititer<len(exit):
previous_use_time = max(previous_use_time+1,exit[exititer][0])
ans[exit[exititer][1]] = previous_use_time
exititer += 1
print(*ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a door at Newton School that can be used only by one person at a time i. e either a person can enter from the door or exit but no two people can do it simultaneously. If two people going in the opposite direction arrived at the door at the same time then these 3 cases should be considered:-
1. If the door was not used before or it was not used in the previous second then the person who wants to exit goes first.
2. If the door has been used in the previous second for entering, then the person who wants to enter goes first.
3. If the door has been used in the previous second for exiting, then the person who wants to exist goes first.
If two people arrive at the same time and go in the same direction then the person whose name in the given list comes first will go first.
<b>Note:-</b> To cross the door, it will take exactly one second for each person.The first line of input contains a single integer N containing the number of people The second line of input contains N space-separated integers depicting the arrival time of the ith person. The last line of input containing N space-separated integers which are either 0 or 1. 0 indicates that the person wants to enter and 1 indicates he wants to exit.
<b>Constraints:-</b>
1 ≤ N ≤ 50000
0 ≤ Arrival[i] ≤ Arrival [i+1] ≤ 10<sup>9</sup>Print N space-separated integers denoting the time at which the ith person will cross the door.Sample Input 1:-
4
0 0 1 5
0 1 1 0
Sample Output 1:-
2 0 1 5
Sample Input 2:-
5
0 1 1 3 3
0 1 0 0 1
Sample Output 2:-
0 2 1 4 3
<b>Explanation:-</b>
At t = 0:- the first and the second person wants to enter and exit. As per the case, 1 2nd person will go first.
At t=1, the first and the 3rd person wants to enter and exit. As per case 3, the 3rd person goes first.
At t= 2, only 1st is the only person standing so he goes.
At t = 3, and 4 no one wants to cross
At t = 5, the 4th person is the only one who wants to cross., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
fast();
int n;
cin>>n;
int a[n],b[n];
FOR(i,n){
cin>>a[i];}
FOR(i,n){
cin>>b[i];}
vector<pair<int,int>> p,q;
FOR(i,n){
if(b[i]==0){
p.EB(make_pair(a[i],i));
}
else{
q.EB(make_pair(a[i],i));
}
}
sort(p.begin(),p.end());
sort(q.begin(),q.end());
int j=0,k=0;
int t=0;
int x=1;
int ans[n];
int pp,qq;
FOR(i,n){
ans[i]=-1;}
while(q.size()!=k || p.size()!=j){
// FOR(i,n){
// out1(ans[i]);}
//out1(t);
if(p.size()==j){
pp=LLONG_MAX;
}
else{
pp=p[j].first;}
if(q.size()==k){
qq=LLONG_MAX;
}
else{
qq=q[k].first;}
if(pp<t){pp=t;}
if(qq<t){qq=t;}
// out1(pp);out1(qq);END;
if(pp==qq){
if(pp==t){
if(x==1){
ans[q[k].second]=qq;
k++;
t=qq+1;
}
else{
ans[p[j].second]=pp;
j++;
t=pp+1;
}
}
else{
ans[q[k].second]=qq;
k++;
t=qq+1;
x=1;
}}
else{
if(pp<qq){
x=0;
ans[p[j].second]=pp;
t=pp+1;j++;
}
else{
x=1;
ans[q[k].second]=qq;
t=qq+1;
k++;
}
}
}
FOR(i,n){
cout<<ans[i]<<" ";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer A and a floating point number B, upto exactly two decimal places. Compute their product, truncate its fractional part, and print the result as an integer.Since this will be a functional problem, you don't have to take input. You just have to complete the function solve() that takes an integer and a float as arguments.
<b>Constraints</b>
0<=A<=2 x 10<sup>15</sup>
0<=B<sub>i</sub><=10Return the product after truncating the fractional part.Sample Input:
155
2.41
Sample Output:
373
The result is 373.55, after truncating the fractional part, we get 373., I have written this Solution Code: class Solution {
long solve(long a, double b) {
b = b*100 ;
return (a*(long)b)/100 ;
}
}
, 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 (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code: n = int(input())
all_no = input().split(' ')
i = 0
joined_str = ''
while(i < n-1):
if(i == 0):
joined_str = str(int(all_no[i]) + int(all_no[i+1]))
else:
joined_str = joined_str + ' ' + str(int(all_no[i]) + int(all_no[i+1]))
i = i + 2
print(joined_str), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, 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];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int t;
for(int i=0;i<n;i+=2){
System.out.print(a[i]+a[i+1]+" ");
}
}
}, 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 (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i+=2){
cout<<a[i]+a[i+1]<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 ≤ A, B, C ≤ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
String a = in.readLine();
String[] nums = a.split(" ");
long[] l = new long[3];
for(int i=0; i<3; i++){
l[i] = Long.parseLong(nums[i]);
}
Arrays.sort(l);
System.out.print(l[1]);
}
catch(Exception e){
System.out.println(e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 ≤ A, B, C ≤ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code:
//#define ASC
//#define DBG_LOCAL
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#define int long long
// #define int __int128
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define endl '\n'
#define fi first
#define se second
// const int mod = 1e9 + 7;
const int mod=998'244'353;
const long long INF = 2e18 + 10;
// const int INF=1e9+10;
#define readv(x, n) \
vector<int> x(n); \
for (auto &i : x) \
cin >> i;
template <typename T>
using v = vector<T>;
template <typename T>
using vv = vector<vector<T>>;
template <typename T>
using vvv = vector<vector<vector<T>>>;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<vector<double>> vvd;
typedef pair<int, int> pii;
int multiply(int a, int b, int in_mod) { return (int)(1LL * a * b % in_mod); }
int mult_identity(int a) { return 1; }
const double PI = acosl(-1);
auto power(auto a, auto b, const int in_mod)
{
auto prod = mult_identity(a);
auto mult = a % 2;
while (b != 0)
{
if (b % 2)
{
prod = multiply(prod, mult, in_mod);
}
if(b/2)
mult = multiply(mult, mult, in_mod);
b /= 2;
}
return prod;
}
auto mod_inv(auto q, const int in_mod)
{
return power(q, in_mod - 2, in_mod);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define stp cout << fixed << setprecision(20);
void solv()
{
int A ,B, C;
cin>>A>>B>>C;
vector<int> values;
values.push_back(A);
values.push_back(B);
values.push_back(C);
sort(all(values));
cout<<values[1]<<endl;
}
void solve()
{
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++)
{
// cout<<"Case #"<<i<<": ";
solv();
}
}
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);
}
#else
#ifdef ASC
namespace fs = std::filesystem;
std::string path = "./";
string filename;
for (const auto & entry : fs::directory_iterator(path)){
if( entry.path().extension().string() == ".in"){
filename = entry.path().filename().stem().string();
}
}
if(filename != ""){
string input_file = filename +".in";
string output_file = filename +".out";
if (fopen(input_file.c_str(), "r"))
{
freopen(input_file.c_str(), "r", stdin);
freopen(output_file.c_str(), "w", stdout);
}
}
#endif
#endif
// auto clk = clock();
// -------------------------------------Code starts here---------------------------------------------------------------------
signed t = 1;
// cin >> t;
for (signed test = 1; test <= t; test++)
{
// cout<<"Case #"<<test<<": ";
// cout<<endl;
solve();
}
// -------------------------------------Code ends here------------------------------------------------------------------
// clk = clock() - clk;
#ifndef ONLINE_JUDGE
// cerr << fixed << setprecision(6) << "\nTime: " << ((float)clk) / CLOCKS_PER_SEC << "\n";
#endif
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 ≤ A, B, C ≤ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code: lst = list(map(int, input().split()))
lst.sort()
print(lst[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three numbers A, B, C. The triplet is said to be a "good triplet" if the sum of any two numbers is equal to the third number.
Print 1 if the triplet is good, else print 0.Three integers are given as input. a, b, c.
<b>Constraints</b>
1 ≤ a, b, c ≤ 10<sup>4</sup>Print 1 if three numbers form a good triplet else print 0.Sample Input:
2 5 3
Sample Output:
1
Explanation:
sum of (2, 3) is 5., I have written this Solution Code: import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int s=0;
int sum=(a+b+c);
int maxi=Math.max(Math.max(a,b),c);
int mini=Math.min(Math.min(a,b),c);
int middle=sum-(maxi+mini);
if(middle+mini==maxi)
s=1;
System.out.print(s);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a grid having N*M elements. Find the index of the row (1-base indexing) which has the maximum sum of elements.
Note: if more than two rows have the same sum of elements, then return the smallest index.The first line of the input contains two integers N and M.
The next N lines each contain M space separated integers.
<b>Constraints:</b>
1 <= N, M <= 10<sup>3</sup>
1 <= A<sub>i, j</sub> <= 10<sup>9Print the index of the row (1-base indexing) which has the maximum sum of elements.Sample Input:
4 3
3 4 2
5 1 7
2 8 1
2 3 3
Sample Output:
2
<b>Explaination:</b>
Row number 2 has sum = 5 + 1 + 7 = 13 which is the maximum among all rows., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) {
FastReader sc=new FastReader();
int n=sc.nextInt();
int m=sc.nextInt();
int arr[][]=new int[n][m];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
arr[i][j]= sc.nextInt();
}
}
long maxsum=0;
int index=0;
for(int i=0; i<n; i++)
{
long sum=0;
for(int j=0; j<m; j++)
{
sum += arr[i][j];
}
if(sum > maxsum)
{
maxsum=sum;
index=i+1;
}
}
System.out.print(index);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a grid having N*M elements. Find the index of the row (1-base indexing) which has the maximum sum of elements.
Note: if more than two rows have the same sum of elements, then return the smallest index.The first line of the input contains two integers N and M.
The next N lines each contain M space separated integers.
<b>Constraints:</b>
1 <= N, M <= 10<sup>3</sup>
1 <= A<sub>i, j</sub> <= 10<sup>9Print the index of the row (1-base indexing) which has the maximum sum of elements.Sample Input:
4 3
3 4 2
5 1 7
2 8 1
2 3 3
Sample Output:
2
<b>Explaination:</b>
Row number 2 has sum = 5 + 1 + 7 = 13 which is the maximum among all rows., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int> (m));
vector<int> sum(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
sum[i] += a[i][j];
}
}
int mx = 0, ans = 0;
for(int i = 0; i < n; i++){
if(mx < sum[i]){
mx = sum[i];
ans = i;
}
}
cout << ans + 1;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A conveyor belt has parcels that must be shipped from one point to another within D days.
The i-th parcel on the conveyor belt has a weight of weights[i]. Each day, we load the ship with parcels on the conveyor belt (in the order given). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the parcels on the conveyor belt being shipped within D days.
Note:- the cargo must be shipped in the order givenThe input line contains T, denoting the number of testcases. Each testcase contains two lines. First line N ,contains size of the conveyor belt and D days separated by space. Second line contains weights of parcels.
Constraints:
1 <= T <= 100
1 <= D <= N <= 5*10^4
1 <= weights[i] <= 500
Sum if N over all test cases is <= 1000000For each testcase you need to print the least weight capacity of the ship that will result in all the parcels on the conveyor belt being shipped within D days.Sample Input:
2
10 5
1 2 3 4 5 6 7 8 9 10
6 3
3 2 2 4 1 4
Sample Output:
15
6
Explanation:
Testcase 1:
A ship capacity of 15 is the minimum to ship all the parcels in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the parcels into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed., I have written this Solution Code: def isValid(weight, n, D, mx):
st = 1
sum = 0
for i in range(n):
sum += weight[i]
if (sum > mx):
st += 1
sum = weight[i]
if (st > D):
return False
return True
def shipWithinDays(weight, D, n):
sum = 0
for i in range(n):
sum += weight[i]
s = weight[0]
for i in range(1, n):
s = max(s, weight[i])
e = sum
res = -1
while (s <= e):
mid = s + (e - s) // 2
if (isValid(weight, n, D, mid)):
res = mid
e = mid - 1
else:
s = mid + 1
print(res)
if __name__ == '__main__':
n=int(input())
l=[]
for r in range(n):
leng,D=input().split()
D=int(D)
weight=input().split()
weight = [ int(i) for i in weight ]
N = len(weight)
l.append(shipWithinDays(weight, D, N))
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A conveyor belt has parcels that must be shipped from one point to another within D days.
The i-th parcel on the conveyor belt has a weight of weights[i]. Each day, we load the ship with parcels on the conveyor belt (in the order given). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the parcels on the conveyor belt being shipped within D days.
Note:- the cargo must be shipped in the order givenThe input line contains T, denoting the number of testcases. Each testcase contains two lines. First line N ,contains size of the conveyor belt and D days separated by space. Second line contains weights of parcels.
Constraints:
1 <= T <= 100
1 <= D <= N <= 5*10^4
1 <= weights[i] <= 500
Sum if N over all test cases is <= 1000000For each testcase you need to print the least weight capacity of the ship that will result in all the parcels on the conveyor belt being shipped within D days.Sample Input:
2
10 5
1 2 3 4 5 6 7 8 9 10
6 3
3 2 2 4 1 4
Sample Output:
15
6
Explanation:
Testcase 1:
A ship capacity of 15 is the minimum to ship all the parcels in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the parcels into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
int N = Integer.parseInt(str[0]);
int D = Integer.parseInt(str[1]);
int arr[] = new int[N];
str = read.readLine().trim().split(" ");
for(int i = 0; i < N; i++)
arr[i] = Integer.parseInt(str[i]);
int res = shipWithinDays(arr, D);
//print(res);
System.out.println(res);
}
}
static void print(int list[])
{
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
public static int shipWithinDays(int[] weights, int D) {
// set lower/upper bound for binary search
int low = 0, high = 0;
for(int weight: weights) {
low = Math.max(low, weight);
high += weight;
}
while(low < high) {
int mid = low + (high - low)/2;
if(calDays(mid, weights) > D) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
public static int calDays(int w, int[] weights) {
int days = 0;
int accuSum = 0;
for(int i=0;i<weights.length;i++) {
if(accuSum + weights[i] <= w) {
accuSum += weights[i];
} else {
accuSum = weights[i];
days++;
}
}
if(accuSum>0) {
days++;
}
return days;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A conveyor belt has parcels that must be shipped from one point to another within D days.
The i-th parcel on the conveyor belt has a weight of weights[i]. Each day, we load the ship with parcels on the conveyor belt (in the order given). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the parcels on the conveyor belt being shipped within D days.
Note:- the cargo must be shipped in the order givenThe input line contains T, denoting the number of testcases. Each testcase contains two lines. First line N ,contains size of the conveyor belt and D days separated by space. Second line contains weights of parcels.
Constraints:
1 <= T <= 100
1 <= D <= N <= 5*10^4
1 <= weights[i] <= 500
Sum if N over all test cases is <= 1000000For each testcase you need to print the least weight capacity of the ship that will result in all the parcels on the conveyor belt being shipped within D days.Sample Input:
2
10 5
1 2 3 4 5 6 7 8 9 10
6 3
3 2 2 4 1 4
Sample Output:
15
6
Explanation:
Testcase 1:
A ship capacity of 15 is the minimum to ship all the parcels in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the parcels into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,d;
cin>>n>>d;
long sum=0;
long ma=0;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
ma=max(ma,a[i]);
}
long low=ma;
long high=sum;
int mid;
while(low<high){
mid=low+(high-low)/2;
int ans=0;
int i=0;
int cnt=1;
while(i<n){
ans+=a[i];
if(ans>mid){
ans=a[i];
cnt++;
}
i++;
}
if(cnt<=d){high=mid;}
else
{
low=mid+1;
}
}
cout<<low<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a binary string S of length N. Suppose there is an integer K (1 <= K <= N). You can perform the following operation on the string any number of times (possible zero):
Select a substring in S whose size is <b>at least</b> K i. e, R − L + 1 >= K must be followed and flip this substring. Flipping here means if S<sub>i</sub> is equal to '1', make it equal to '0' and if S<sub>i</sub> is equal to '0', make it equal to '1'.
Find the maximum value of K such that you can make string S a null string. A null string is defined as a binary string in which all the characters are '0'.The first line of the input contains a single integer N.
The second line of the input contains a binary string of lenth N.
Constraints:
1 <= N <= 10<sup>5</sup>Print the maximum value of K such that you can make string S a null string.Sample Input:
3
010
Sample Output:
2
Explaination:
We can make S a null string by the following operations:
<ul>
<li>First, we pick the segment S[1,3] with length 3. S is now 101.</li>
<li>Next, we pick the segment S[1,2] with length 2. S is now 011.</li>
<li>And finally we pick the segment S[2,3] with length 2. S is now 000.</li>
</ul>, 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().split(" ");
int m=Integer.parseInt(str[0]);
String input=br.readLine();
int n=input.length();
int ans=n;
for(int i=1; i<n; i++)
{
if(input.charAt(i-1) != input.charAt(i))
{
ans=Math.min(Math.max(i,n-i), ans);
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a binary string S of length N. Suppose there is an integer K (1 <= K <= N). You can perform the following operation on the string any number of times (possible zero):
Select a substring in S whose size is <b>at least</b> K i. e, R − L + 1 >= K must be followed and flip this substring. Flipping here means if S<sub>i</sub> is equal to '1', make it equal to '0' and if S<sub>i</sub> is equal to '0', make it equal to '1'.
Find the maximum value of K such that you can make string S a null string. A null string is defined as a binary string in which all the characters are '0'.The first line of the input contains a single integer N.
The second line of the input contains a binary string of lenth N.
Constraints:
1 <= N <= 10<sup>5</sup>Print the maximum value of K such that you can make string S a null string.Sample Input:
3
010
Sample Output:
2
Explaination:
We can make S a null string by the following operations:
<ul>
<li>First, we pick the segment S[1,3] with length 3. S is now 101.</li>
<li>Next, we pick the segment S[1,2] with length 2. S is now 011.</li>
<li>And finally we pick the segment S[2,3] with length 2. S is now 000.</li>
</ul>, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
bool solve(int k, string &s){
bool a = true, b = true;
for(int i = 0; i < s.size(); i++){
if (i >= k || i + k < s.size()) continue;
a &= (s[i] == '0');
b &= (s[i] == '1');
}
return a||b;
}
signed main(){
int n;
cin >> n;
string s;
cin >> s;
int l = 1, r = n, ans = 1;
while(r >= l){
int k = (l + r)/2;
if(solve(k, s)){
ans = k;
l = k + 1;
}
else r = k - 1;
}
cout << ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: void magicTrick(int a, int b){
cout<<a+b/2;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: static void magicTrick(int a, int b){
System.out.println(a + b/2);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: A,B = map(int,input().split(' '))
C = A+B//2
print(C)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We have N+M balls, each of which has an integer written on it.
It is known that:
1) The numbers written on the N of the balls are even.
2) The numbers written on the M of the balls are odd.
Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.The first and the only line of the input contains 2 space separated integers, N and M
<b>Constraints:</b>
1) 0 ≤ N, M ≤ 100
2) 2 ≤ N + MPrint the answer<b>Sample Input 1:</b>
2 1
<b>Sample Output 1:</b>
1
<b>Sample Input 2:</b>
4 3
<b>Sample Output 2:</b>
9
<b>Sample Input 3:</b>
13 3
<b>Sample Output 3:</b>
81, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define pi 3.14159265358979
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define ci(x) cin >> (x)
#define cii(x) int (x);cin >> (x)
#define cci(x,y) int (x),(y);cin >> (x) >>(y)
#define co(x) cout << (x) << endl
int main() {
cci(n, m);
co((n*(n-1))/2 + (m*(m-1))/2);
return 0;
}, 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, and you are also given a sum. You need to find if two numbers in A exists such that their sum is equal to the given sum. If yes, print 1, else print 0.The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array. The third line contains element sum.
Constraints:
1 <= N <= 100000
1 <= A[i] <= 1000000Print 1, if there is an occurrence of the sum, else print 0.Sample Input
10 14
1 2 3 4 5 6 7 8 9 10
Sample Output
1
Explanation
10 + 4 = 14, so pair exists
Sample Input
5 9
1 2 3 4 5
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 [] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int target = Integer.parseInt(input[1]);
input = br.readLine().split(" ");
int [] arr = new int[n];
for(int i =0; i<n; i++){
arr[i] = Integer.parseInt(input[i]);
}
Arrays.sort(arr);
int pointerLeft = 0;
int pointerRight = n-1;
boolean value = false;
while(pointerLeft<pointerRight){
if(arr[pointerLeft] + arr[pointerRight] == target){
System.out.println("1");
value = true;
break;
}
else if(arr[pointerLeft]+arr[pointerRight]<target){
pointerLeft++;
}
else{
pointerRight--;
}
}
if(value == false){
System.out.println("0");
}
}
}, 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, and you are also given a sum. You need to find if two numbers in A exists such that their sum is equal to the given sum. If yes, print 1, else print 0.The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array. The third line contains element sum.
Constraints:
1 <= N <= 100000
1 <= A[i] <= 1000000Print 1, if there is an occurrence of the sum, else print 0.Sample Input
10 14
1 2 3 4 5 6 7 8 9 10
Sample Output
1
Explanation
10 + 4 = 14, so pair exists
Sample Input
5 9
1 2 3 4 5
Sample Output
1
, I have written this Solution Code: st = set()
n, s = map(int, input().split())
l = list(map(int, input().split()))
stt = set(l)
found = False
for i in l:
if s - i in stt:
print(1)
found = True
break
if not found:
print(0), 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 of size N, and you are also given a sum. You need to find if two numbers in A exists such that their sum is equal to the given sum. If yes, print 1, else print 0.The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array. The third line contains element sum.
Constraints:
1 <= N <= 100000
1 <= A[i] <= 1000000Print 1, if there is an occurrence of the sum, else print 0.Sample Input
10 14
1 2 3 4 5 6 7 8 9 10
Sample Output
1
Explanation
10 + 4 = 14, so pair exists
Sample Input
5 9
1 2 3 4 5
Sample Output
1
, 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
int dp[sz];
signed main()
{
int n,k;
cin>>n>>k;
int A[n];
set<int> ss;
int ch=0;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
A[i]=a;
int p=k-a;
if(ss.find(p)!=ss.end()) ch=1;
ss.insert(a);
}
cout<<ch<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have to complete <code>printName</code> function
<ol>
<li>Takes 2 arguments which are EventEmitter object , event name </li>
<li>Using the <code>EventEmitter</code> object you need to register an eventListener for <code>personEvent</code> event which takes a callback function. </li>
<li>The callback function itself takes an argument (which would be emitted by test runner) which is of type string. Using that argument print to console.
`My name is ${argument}`.
</li>
</ol>Function will take two arguments
1) 1st argument will be an object of EventEmitter class
2) 2nd argument will be the event name (string)
Function returns a registered Event using an object of EventEmitter class (which then is used to print the name)<pre>
<code>
const emitter=EventEmitter object
const personEvent="event"
function printName(emitter,personEvent)//registers event with event Name "event"
emitter.emit("event","Dev")//emits the event and give the output "My name is Dev"
</code>
</pre>
, I have written this Solution Code: function printName(emitter,personEvent) {
emitter.on(personEvent,(arg)=>{
console.log('My name is ',arg);
});
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>floor</code>, which should take a number which can be a float(decimal)
and return its result as an integer with floor function applied to it (Use JS In built functions)Function will take a float as inputFunction will return a numberconsole.log(floor(1.99)) // prints 1
console.log(floor(2.1)) // prints 2
console.log(floor(-0.8)) // prints -1, I have written this Solution Code: function floor(num){
// write code here
// return the output , do not use console.log here
return Math.floor(num)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>floor</code>, which should take a number which can be a float(decimal)
and return its result as an integer with floor function applied to it (Use JS In built functions)Function will take a float as inputFunction will return a numberconsole.log(floor(1.99)) // prints 1
console.log(floor(2.1)) // prints 2
console.log(floor(-0.8)) // prints -1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
System.out.print((int)Math.floor(a));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*1).<b>User Task</b>
Since this is a functional problem, you don't have to worry about the input. You just have to complete the function <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: def factorial(n):
if(n == 1):
return 1
return n * factorial(n-1)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*1).<b>User Task</b>
Since this is a functional problem, you don't have to worry about the input. You just have to complete the function <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: static int Factorial(int N)
{
if(N==0){
return 1;}
return N*Factorial(N-1);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*1).<b>User Task</b>
Since this is a functional problem, you don't have to worry about the input. You just have to complete the function <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: // n is the input number
function factorial(n) {
// write code here
// do not console.log
// return the answer as a number
if (n == 1 ) return 1;
return n * factorial(n-1)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., 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 s1 = br.readLine();
String s2 = br.readLine();
boolean flag = true;
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for(int i=0; i<s1.length(); i++){
arr1[s1.charAt(i)-97]++;
}
for(int i=0; i<s2.length(); i++){
arr2[s2.charAt(i)-97]++;
}
for(int i=0; i<25; i++){
if(arr1[i]!=arr2[i]){
flag = false;
break;
}
}
if(flag==true)
System.out.print("YES");
else System.out.print("NO");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: s1 = input().strip()
s2 = input().strip()
dict1 = dict()
dict2 = dict()
for i in s1:
dict1[i] = dict1.get(i, 0) + 1
for j in s2:
dict2[j] = dict2.get(j, 0) + 1
print(("NO", "YES")[dict1 == dict2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., 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
int A[26],B[26];
signed main()
{
string s,p;
cin>>s>>p;
for(int i=0;i<s.size();i++)
{
int y=s[i]-'a';
A[y]++;
}
for(int i=0;i<p.size();i++)
{
int y=p[i]-'a';
B[y]++;
}int ch=1;
for(int i=0;i<26;i++)
{
if(B[i]!=A[i])ch=0;
}
if(ch==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: // str1 and str2 are the two input strings
function isAnagram(str1,str2){
// Get lengths of both strings
let n1 = str1.length;
let n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return "NO";
str1 = str1.split('')
str2 = str2.split('')
// Sort both strings
str1.sort();
str2.sort()
// Compare sorted strings
for (let i = 0; i < n1; i++)
if (str1[i] != str2[i])
return "NO";
return "YES";
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: def OccurenceOfX(N,X):
cnt=0
for i in range(1, N+1):
if(X%i==0 and X/i<=N):
cnt=cnt+1
return cnt, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
int main()
{
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: public static int OccurenceOfX(int N,int X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code: def equationSum(N) :
re=2*N*(N+1)
re=re-3*N
return re, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code: static long equationSum(long N)
{
return 2*N*(N+1) - 3*N;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code:
long int equationSum( long N){
long ans = (2*N*(N+1)) - 3*N;
return ans;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code:
long int equationSum( long N){
long ans = (2*N*(N+1)) - 3*N;
return ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you need to count the integers which are multiple of N between 1 to 100. Return the count.<b>User task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>countMultiples()</b> which contains N as a parameter.
<b>Constraints:</b>
1 <= N <= 100You need to return the count.Sample Input:
3
Sample Output:
33
Sample Input:
4
Sample Output:
25, I have written this Solution Code: def countMultiples(N):
return int(100/N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
System.out.println("Yes");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: string = str(input())
print("Yes"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string s; cin>>s;
cout<<"Yes";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., 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 n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(minValue(arr, n, k));
}
static int minValue(int arr[], int N, int k)
{
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(arr, m, N) <= k)
h = m;
else
l = m;
}
return h;
}
static int f(int a[], int x, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum += Math.max(a[i]-x, 0);
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: def minCut(arr,k):
mini = 1
maxi = k
mid = 0
while mini<=maxi:
mid = mini + int((maxi - mini)/2)
wood = 0
for j in range(n):
if(arr[j]-mid>=0):
wood += arr[j] - mid
if wood == k:
break;
elif wood > k:
mini = mid+1
else:
maxi = mid-1
print(mini)
n,k = list(map(int, input().split()))
arr = list(map(int, input().split()))
minCut(arr,k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], n, k;
int f(int x){
int sum = 0;
for(int i = 1; i <= n; i++)
sum += max(a[i]-x, 0);
return sum;
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(m) <= k)
h = m;
else
l = m;
}
cout << h;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
testcases();
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four numbers A, B, C, D. Find the maximum number of pairs that can be made.
(each pair consist of two distinct numbers). Each number can be used only once.Four integers are given as input. a, b, c, d
<b>Constraints</b>
1 ≤ a, b, c, d ≤ 10<sup>4</sup>Output should print the maximum number of pairs.Sample Input:
2 3 5 5
Sample Output:
2
Explanation:
Two pairs can be formed.
(2, 5) and (3, 5) are two pairs, I have written this Solution Code: import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
int s=2;
if(a==b && b==c && c==d)
s=0;
else if(a==b && b==c)
s=1;
else if(b==c && c==d)
s=1;
else if(a==c && c==d)
s=1;
System.out.print(s);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Quadratic probing is a collision handling technique in hashing. Quadratic probing says that whenever a collision occurs, search for i^2 position.
In this question, we'll learn how to fill up the hash table using Quadratic probing technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr[] of size N. You need to fill up the hash table using Quadratic probing and print the resultant hash table.
Note:
1. All the positions that are unoccupied are denoted by -1 in the hash table.
2. An empty slot can only be found if load factor < 0.5 and hash table size is a prime number.
3. The given testcases satisfy the above condition so you can assume that an empty slot is always reachable.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and size of array. The third line contains elements of the array.
Constraints:
1 <= T <= 100
2 <= hashSize (prime) <= 97
1 <= sizeOfArray < hashSize*0.5
0 <= Array[i] <= 10^5For each testcase, in a new line, print the hash table.Input:
2
11 4
21 10 32 43
11 4
880 995 647 172
Output:
10 -1 -1 32 -1 -1 -1 -1 43 -1 21
880 -1 -1 -1 -1 995 -1 172 -1 647 -1
Explanation:
Testcase1: 21%11=10 so 21 goes into hashTable[10] position. 10%11=10. hashTable[10] is already filled so we try for (10+1)%11=0 position. hashTable[0] is empty so we put 10 there. 32%11=10. hashTable[10] is filled. We try (32+1)%11=0. But hashTable[0] is also already filled. We try (32+4)%11=3. hashTable[3] is empty so we put 32 in hashTable[3] position. 43 uses (43+9)%11=8. We put it in hashTable[8].
Testcase2: Using the similar approach as used in above explanation we will get the output like 880 -1 -1 -1 -1 995 -1 172 -1 647 -1 ., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
int t = Integer.parseInt(br.readLine().trim());
while(t-- > 0){
String str[] = br.readLine().trim().split(" ");
int capacity = Integer.parseInt(str[0]);
int n = Integer.parseInt(str[1]);
str = br.readLine().trim().split(" ");
ArrayList<Integer> ht = new ArrayList<>(capacity);
for(int i = 0; i < capacity; i++){
ht.add(-1);
}
for(int i = 0; i < n; i++){
int key = Integer.parseInt(str[i]);
int count =0;
int hashKey = key % capacity;
if (ht.get(hashKey)==-1){
ht.set(hashKey, key);
}
else
{
for(int j=0; j<capacity;j++){
hashKey = (key + j*j) %capacity;
if(ht.get(hashKey)==-1){
ht.set(hashKey, key);
break;
}
}
}
}
for(int i = 0; i < capacity; i++){
System.out.print(ht.get(i)+ " ");
}
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Quadratic probing is a collision handling technique in hashing. Quadratic probing says that whenever a collision occurs, search for i^2 position.
In this question, we'll learn how to fill up the hash table using Quadratic probing technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr[] of size N. You need to fill up the hash table using Quadratic probing and print the resultant hash table.
Note:
1. All the positions that are unoccupied are denoted by -1 in the hash table.
2. An empty slot can only be found if load factor < 0.5 and hash table size is a prime number.
3. The given testcases satisfy the above condition so you can assume that an empty slot is always reachable.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and size of array. The third line contains elements of the array.
Constraints:
1 <= T <= 100
2 <= hashSize (prime) <= 97
1 <= sizeOfArray < hashSize*0.5
0 <= Array[i] <= 10^5For each testcase, in a new line, print the hash table.Input:
2
11 4
21 10 32 43
11 4
880 995 647 172
Output:
10 -1 -1 32 -1 -1 -1 -1 43 -1 21
880 -1 -1 -1 -1 995 -1 172 -1 647 -1
Explanation:
Testcase1: 21%11=10 so 21 goes into hashTable[10] position. 10%11=10. hashTable[10] is already filled so we try for (10+1)%11=0 position. hashTable[0] is empty so we put 10 there. 32%11=10. hashTable[10] is filled. We try (32+1)%11=0. But hashTable[0] is also already filled. We try (32+4)%11=3. hashTable[3] is empty so we put 32 in hashTable[3] position. 43 uses (43+9)%11=8. We put it in hashTable[8].
Testcase2: Using the similar approach as used in above explanation we will get the output like 880 -1 -1 -1 -1 995 -1 172 -1 647 -1 ., I have written this Solution Code: for _ in range(int(input())):
hs,n = map(int,input().split())
l = list(map(int,input().split()))
ht = [-1]*hs
lf = 0
for i in l:
if(lf/hs >= 0.5):
break
if(ht[i%hs] == -1):
ht[i%hs] = i
else:
k = 1
j = (i%hs + k**2) % hs
while(ht[j] != -1):
k += 1
j = (i%hs + k**2) % hs
ht[j] = i
lf += 1
for i in ht:
print(i,end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Quadratic probing is a collision handling technique in hashing. Quadratic probing says that whenever a collision occurs, search for i^2 position.
In this question, we'll learn how to fill up the hash table using Quadratic probing technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr[] of size N. You need to fill up the hash table using Quadratic probing and print the resultant hash table.
Note:
1. All the positions that are unoccupied are denoted by -1 in the hash table.
2. An empty slot can only be found if load factor < 0.5 and hash table size is a prime number.
3. The given testcases satisfy the above condition so you can assume that an empty slot is always reachable.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and size of array. The third line contains elements of the array.
Constraints:
1 <= T <= 100
2 <= hashSize (prime) <= 97
1 <= sizeOfArray < hashSize*0.5
0 <= Array[i] <= 10^5For each testcase, in a new line, print the hash table.Input:
2
11 4
21 10 32 43
11 4
880 995 647 172
Output:
10 -1 -1 32 -1 -1 -1 -1 43 -1 21
880 -1 -1 -1 -1 995 -1 172 -1 647 -1
Explanation:
Testcase1: 21%11=10 so 21 goes into hashTable[10] position. 10%11=10. hashTable[10] is already filled so we try for (10+1)%11=0 position. hashTable[0] is empty so we put 10 there. 32%11=10. hashTable[10] is filled. We try (32+1)%11=0. But hashTable[0] is also already filled. We try (32+4)%11=3. hashTable[3] is empty so we put 32 in hashTable[3] position. 43 uses (43+9)%11=8. We put it in hashTable[8].
Testcase2: Using the similar approach as used in above explanation we will get the output like 880 -1 -1 -1 -1 995 -1 172 -1 647 -1 ., I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 1000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
bool isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
int main(){
int t;
cin>>t;
while(t--){
int n,p;
cin>>p>>n;
int a[n];
li sum;
ll cur=0;
int b[p];
FOR(i,p){
b[i]=-1;}
unordered_map<ll,int> m;
ll cnt=0;
for(int i=0;i<n;i++){
cin>>a[i];
cur=a[i]%p;
int j=0;
while(b[(cur+j*j)%p]!=-1){
j++;
}
b[(cur+j*j)%p]=a[i];
}
FOR(i,p){
out1(b[i]);
}
END;
}
}
/*
.$ $.
/:; :;\
: $ $ ;
;:$ $;:
: $: ________ ;$ ;
; $;; _..gg$$SSP^^^^T$S$$pp.._ ::$ :
: :$;| .g$$$$$$SSP" "TS$$$$$$$p. |:$; ;
; :$;:.d$$$$$$$SSS SS$$$$$$$$b.;:$; :
: :$$$$$$$$$$$$SSS SS$$$$$$$$$$$$$; ;
; $$$$$$$$$$$$$$SSb. .dS$$$$$$$$$$$$$$; :
: :S$$$$$$$$$$$$$$SSSSppggSSS$$$$$$$$$$$$$$$; ;
| :SS$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$; :
| :SS$$$$$$$$$$$$$$$$$^^^^^^^^^$$$$$$$$$$$$$$ :
; SS$$$$$$$$$$P^" "^T$$$$$$$ :
: :SS$$$$$$$$$ T$$$$$; :
| SSS$$$$$$$; T$$$$ :
| :SSS$$$$$$; :$$$; :
; SSS$$$$$$; :S$$; :
; :SS$$P"^P S$$; :
; ..d$$$P ` S$$$ :
; T$$$P dS T$$$b. :
; :$$$$. . dSS; $$$$$b.:
; :$$$$$b Tb. . .dSS$$b.d$$$$$$$:
: $$$$$$$b TSb Tb..g._, :$$SS$$$$$$SSS$$$:
: :$$$$$$$$b SSb T$SS$P "^TS$$$$$$P"TS$$:
: $$$$$$$$$$b._.dS$$b _ T$$P _ TSS$SSP :SS$:
: :$$$$SSS$$$$$$$$$P" d$b. _.d$P TSSP" SS$:
: :P"TSSP"^T$$$$$$P :$$$$$$$$P d$$b $S$;
: :b.dS^^-. ""^^" $b T d$$$s$$$$b __..--""$ $;
: :$$$S ""^^..ggSS$$$$$$$$$$$$$$P^^"" .$ $;
: $$$$$pp..__ `j$$$$$$^$$$$$b. d....ggppTSSS$$;
: $$$$SP """t :$$$$ $$$$$$$b. d$b `TSS$;
: \:$$SP _.gd$$P_d$$$$$ $$$$$$$$$bd$P' .dSPd;
: \"^S "^T$$$$$$$$$$ $$$$$SS$$$$b. dSS'd$;
$ $. "-.__.gd$$$$$$$SP:S$$P TSS$$$$$bssS^".d$$:
:$ $$b. ""^^T$$$$SP' :S$P TSSSP^^"" .d$$$$:
:$ :$$$P "^SP' :S; .^"`. $$$$$$$:
$; :$$$ "-. :S; .-" \ :$$$$$$:
.$ : $$$; : `.:S;.' ; $$$$$$:;
.P :S $$$ ; `^' :$$$$$:;
.P S;.d$$; : -' $$$$$:;
$ :SS$$$$ ; __....---- --...____ :$$$ :S
$ $SSS$$; : ; d$$$$$$$pppqqqq$$$$$$L; :$$$ SS
: :SSSSS$$ ; : \ "^T$$$$$$$$$$$$$P' .': $PT$ SS;
$SP^"^TSP\ : \ "-. """"""""""" .-" ; / $ SSSb.
:S S \ "--...___..--" / : / :gSSSSSb.
T bug T \ `. _____ / ; /
` \ : "==="""""""""==="" : /
`: ;'
"-. .-"
""--.. ..--""
*/
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find out whether it is divisible by 3.The first and the only line of input contains the number N.
<b>Constraints</b>
1 <= N <= 10^100000 (N may consist of 100001 digits).
<b>No usual datatype will be able to input such large number.</b>Output "Yes" if the number is divisible by 3, else output "No".Sample Input 1
14
Sample Output 1
No
Sample Input 2
1234567890123456789012345678901234567890
Sample Output 2
Yes
Explanation: In the first sample case, the number is not divisible by 3, while in the second sample case, it is divisible by 3. (We know how weird this explanation is, but ok)., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string s; cin>>s;
int sum=0;
For(i, 0, s.length()){
sum += (s[i]-'0');
}
if(sum%3==0)
cout<<"Yes";
else
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 number N, find out whether it is divisible by 3.The first and the only line of input contains the number N.
<b>Constraints</b>
1 <= N <= 10^100000 (N may consist of 100001 digits).
<b>No usual datatype will be able to input such large number.</b>Output "Yes" if the number is divisible by 3, else output "No".Sample Input 1
14
Sample Output 1
No
Sample Input 2
1234567890123456789012345678901234567890
Sample Output 2
Yes
Explanation: In the first sample case, the number is not divisible by 3, while in the second sample case, it is divisible by 3. (We know how weird this explanation is, but ok)., 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 sum=0;
for(int i=0;i<S.length();i++){
sum+=S.charAt(i)-'0';
}
if(sum%3==0){
System.out.print("Yes");
}
else{
System.out.print("No");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find out whether it is divisible by 3.The first and the only line of input contains the number N.
<b>Constraints</b>
1 <= N <= 10^100000 (N may consist of 100001 digits).
<b>No usual datatype will be able to input such large number.</b>Output "Yes" if the number is divisible by 3, else output "No".Sample Input 1
14
Sample Output 1
No
Sample Input 2
1234567890123456789012345678901234567890
Sample Output 2
Yes
Explanation: In the first sample case, the number is not divisible by 3, while in the second sample case, it is divisible by 3. (We know how weird this explanation is, but ok)., I have written this Solution Code: n = int(input())
if n%3==0:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 1e9+7.The first line contains a single space separated integer N.
The second line contains N space-separated integers a[i].
Constraints
1 <= n <= 1000
1 <= a[i] <= n
Print the number of all possible permutations.
Sample Input 1:
3
2 1 3
Sample Output 1:
1
Explanation:
Only single permutations of nums can produce similar BST.
Sample Input 2:
5
3 4 5 1 2
Sample Output 2:
5
Explanation:
3 1 2 4 5
3 1 4 2 5
3 1 4 5 2
3 4 1 2 5
3 4 1 5 2
There are 5 possible permutations of nums that can generate the same binary search tree., I have written this Solution Code: nan, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 1e9+7.The first line contains a single space separated integer N.
The second line contains N space-separated integers a[i].
Constraints
1 <= n <= 1000
1 <= a[i] <= n
Print the number of all possible permutations.
Sample Input 1:
3
2 1 3
Sample Output 1:
1
Explanation:
Only single permutations of nums can produce similar BST.
Sample Input 2:
5
3 4 5 1 2
Sample Output 2:
5
Explanation:
3 1 2 4 5
3 1 4 2 5
3 1 4 5 2
3 4 1 2 5
3 4 1 5 2
There are 5 possible permutations of nums that can generate the same binary search tree., I have written this Solution Code: /**
* author: tourist1256
* created: 2022-06-14 14:26:47
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
#define int long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
const int mod = 1e9 + 7;
vector<int> fac, sz, dp;
vector<vector<int>> adj;
struct Node {
int data;
Node *left = NULL, *right = NULL;
Node(int val) : data(val) {}
};
int power(int x, int y) {
x %= mod;
if (!x) return x;
int res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
y >>= 1;
x = (x * x) % mod;
}
return res;
}
int inverse(int x) {
return power(x, mod - 2);
}
Node *insert(Node *root, int val) {
if (!root) {
Node *tmp = new Node(val);
return tmp;
}
if (root->data < val)
root->right = insert(root->right, val);
else
root->left = insert(root->left, val);
return root;
}
void inorder(Node *root, Node *par) {
if (!root)
return;
inorder(root->left, root);
inorder(root->right, root);
if (par != NULL) {
sz[par->data] += sz[root->data];
}
int val = fac[sz[root->data] - 1];
if (root->left != NULL) {
val = (val * inverse(fac[sz[root->left->data]])) % mod;
dp[root->data] = (dp[root->data] * dp[root->left->data]) % mod;
}
if (root->right != NULL) {
val = (val * inverse(fac[sz[root->right->data]])) % mod;
dp[root->data] = (dp[root->data] * dp[root->right->data]) % mod;
}
dp[root->data] = (dp[root->data] * val) % mod;
}
int numOfWays(vector<int> &nums) {
int n = nums.size();
fac.assign(n + 10, 1);
for (int i = 2; i <= n; i++)
fac[i] = (fac[i - 1] * i) % mod;
sz.assign(n + 10, 1);
dp.assign(n + 10, 1);
adj.assign(n + 1, vector<int>());
Node *root = new Node(nums[0]);
for (int i = 1; i < n; i++)
insert(root, nums[i]);
inorder(root, NULL);
dp[nums[0]]--;
dp[nums[0]] += mod;
dp[nums[0]] %= mod;
return dp[nums[0]];
}
int32_t main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
debug(a);
cout << numOfWays(a) << "\n";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given Q queries. Each query consists of a single number N. You can perform any one of the 2 operations in a single move:
1: If we take 2 integers a and b where, N=a*b, (a and b are not equal to 1), then we can change N=max(a,b)
2: Decrease the value of N by 1 .
For each query determine the minimum number of moves required to reduce the value of N to 0.The first line contains the integer Q.
The next Q lines each contain an integer, N.
1 <= Q <= 1000
1 <= N <= 1000000Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0.Sample Input
2
3
4
Sample Output
3
3
Explanation
For test case 1, We only have one option that gives the minimum number of moves.
Follow 3-> 2 -> 1 ->0 . Hence, 3 moves.
For the case 2, we can either go 4 -> 3 -> 2 -> 1 -> 0 or 4-> 2 -> 1 ->0 . The 2nd option is more optimal. Hence, 3 moves., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
int n = 1000000;
int dp[] = new int[n+1];
for(int i=0; i<n; i++){
dp[i] = Integer.MAX_VALUE;
}
dp[0] = 0;
dp[1] = 1;
for(int i=2; i<n; i++){
dp[i] = Math.min(dp[i], 1+dp[i-1]);
for(int j=2; j<=i && i*j<n; j++){
dp[i*j] = Math.min(dp[i*j], 1+dp[i]);
}
}
while(q-- > 0){
int x = sc.nextInt();
System.out.println(dp[x]);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given Q queries. Each query consists of a single number N. You can perform any one of the 2 operations in a single move:
1: If we take 2 integers a and b where, N=a*b, (a and b are not equal to 1), then we can change N=max(a,b)
2: Decrease the value of N by 1 .
For each query determine the minimum number of moves required to reduce the value of N to 0.The first line contains the integer Q.
The next Q lines each contain an integer, N.
1 <= Q <= 1000
1 <= N <= 1000000Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0.Sample Input
2
3
4
Sample Output
3
3
Explanation
For test case 1, We only have one option that gives the minimum number of moves.
Follow 3-> 2 -> 1 ->0 . Hence, 3 moves.
For the case 2, we can either go 4 -> 3 -> 2 -> 1 -> 0 or 4-> 2 -> 1 ->0 . The 2nd option is more optimal. Hence, 3 moves., 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 = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
vector<int> v[N];
int dp[N];
void solve(){
for(int i = 1; i < N; i++)
dp[i] = inf;
dp[1] = 1;
for(int i = 2; i < N; i++){
dp[i] = min(dp[i], 1 + dp[i-1]);
for(int j = 1; j <= i && j*i < N; j++)
dp[i*j] = min(dp[i*j], 1 + dp[i]);
}
int q; cin >> q;
while(q--){
int n; cin >> n;
cout << dp[n] << endl;
}
}
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: An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings, like in a dictionary.Input contains 5 strings of length 2 separated by spaces.
String contains only uppercase English letters.Print the sorted array.INPUT :
AS KF ER DD JK
OUTPUT :
AS DD ER JK KF, I have written this Solution Code: function easySorting(arr)
{
for(let i = 1; i < 5; i++)
{
let str = arr[i];
let j = i-1;
while(j >= 0 && (arr[j].toString().localeCompare(str)) > 0 )
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = str;
}
return arr;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings, like in a dictionary.Input contains 5 strings of length 2 separated by spaces.
String contains only uppercase English letters.Print the sorted array.INPUT :
AS KF ER DD JK
OUTPUT :
AS DD ER JK KF, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
map<string,int> m;
string s;
for(int i=0;i<5;i++){
cin>>s;
m[s]++;
}
for(auto it=m.begin();it!=m.end();it++){
while(it->second>0){
cout<<it->first<<" ";
it->second--;}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings, like in a dictionary.Input contains 5 strings of length 2 separated by spaces.
String contains only uppercase English letters.Print the sorted array.INPUT :
AS KF ER DD JK
OUTPUT :
AS DD ER JK KF, I have written this Solution Code: inp = input("").split(" ")
print(" ".join(sorted(inp))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings, like in a dictionary.Input contains 5 strings of length 2 separated by spaces.
String contains only uppercase English letters.Print the sorted array.INPUT :
AS KF ER DD JK
OUTPUT :
AS DD ER JK KF, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static void printArray(String str[])
{
for (String string : str)
System.out.print(string + " ");
}
public static void main (String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int len = 5;
String[] str = new String[len];
str = br.readLine().split(" ");
Arrays.sort(str, String.CASE_INSENSITIVE_ORDER);
printArray(str);
}
}, 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 non-negative integers. You have to move all 0's to the end of array while maintaining the relative order of the non-zero elements.
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.The first line of input line contains T, denoting the number of testcases. Each test cases contains 2 lines. First-line contains N, size of array. Second-line contains elements of array separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to print the updated array.Sample Input:
2
5
0 1 0 3 12
8
0 0 0 0 1 2 3 4
Sample Output:
1 3 12 0 0
1 2 3 4 0 0 0 0, 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 t;
t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int j=-1;
for(int i=0;i<n;i++){
if(a[i]==0 && j==-1){
j=i;
}
if(j!=-1 && a[i]!=0){
a[j]=a[i];
a[i]=0;
j++;
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N containing non-negative integers. You have to move all 0's to the end of array while maintaining the relative order of the non-zero elements.
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.The first line of input line contains T, denoting the number of testcases. Each test cases contains 2 lines. First-line contains N, size of array. Second-line contains elements of array separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to print the updated array.Sample Input:
2
5
0 1 0 3 12
8
0 0 0 0 1 2 3 4
Sample Output:
1 3 12 0 0
1 2 3 4 0 0 0 0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n;
int a[n];
int cnt=0;
for(int i=0;i<n;i++){
cin>>a[i];\
if(a[i]==0){cnt++;}
}
for(int i=0;i<n;i++){
if(a[i]!=0){
cout<<a[i]<<" ";
}
}
while(cnt--){
cout<<0<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Myra has x red colour balls and y blue colour balls. She wants to distribute these balls into identical groups without any balls left. What is the largest number of groups she can make?
Identical groups mean there are equal number of same coloured balls in each group.A single line containing two integers x and y.
Constraints:
1<=x, y<=10000000Return the largest number of groups that can be made.Sample Input
3 9
Sample Output
3
Explanation:-
1 red ball and 3 blue ball will be in each group.
Sample Input:
4 9
Sample Output:
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int gcd(int x,int y)
{
if(x%y==0)
return y;
if(y%x==0)
return x;
int gc=1;
int min=x;
if(min>y)
min=y;
for(int i=2;i<=Math.sqrt(min);i++)
{
if(x%i==0 )
{
if(x%i==0 && y%i==0)
{
if(i>gc)
gc=i;
}
int a=x/i;
if(x%a==0 && y%a==0)
{
gc=a;
break;
}
}
}
return gc;
}
public static void main (String[] args) throws IOException {
BufferedReader bf =new BufferedReader(new InputStreamReader(System.in));
String str[]=bf.readLine().split(" ");
int x=Integer.parseInt(str[0]);
int y=Integer.parseInt(str[1]);
System.out.println(gcd(x,y));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Myra has x red colour balls and y blue colour balls. She wants to distribute these balls into identical groups without any balls left. What is the largest number of groups she can make?
Identical groups mean there are equal number of same coloured balls in each group.A single line containing two integers x and y.
Constraints:
1<=x, y<=10000000Return the largest number of groups that can be made.Sample Input
3 9
Sample Output
3
Explanation:-
1 red ball and 3 blue ball will be in each group.
Sample Input:
4 9
Sample Output:
1, I have written this Solution Code: def gcd(a, b):
result = min(a, b)
while result:
if a % result == 0 and b % result == 0:
break
result -= 1
return result
one,two = map(int,input().split())
print(gcd(one,two)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Myra has x red colour balls and y blue colour balls. She wants to distribute these balls into identical groups without any balls left. What is the largest number of groups she can make?
Identical groups mean there are equal number of same coloured balls in each group.A single line containing two integers x and y.
Constraints:
1<=x, y<=10000000Return the largest number of groups that can be made.Sample Input
3 9
Sample Output
3
Explanation:-
1 red ball and 3 blue ball will be in each group.
Sample Input:
4 9
Sample Output:
1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
long x,y;
cin>>x>>y;
cout<<__gcd(x,y);
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You must be familiar with various types of operators. One of the most commonly used operators in any language are increment and decrement operators. Given two numbers X and Y. Your task is to print the value of X decremented by 1 and value of Y after incremented by 1.The first line of the input contains two integers X and Y.
<b>Constraints:</b>
1 ≤ X, Y ≤ 10000You need to perform the task as mentioned in the question, and finally, print the result separated by a space.Sample Input 1:
4 5
Sample Output 1:
3 6
Sample Input 2:
5 6
Sample Output 2:
4 7, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
x--;
y++;
System.out.print(x);
System.out.print(" ");
System.out.print(y);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You must be familiar with various types of operators. One of the most commonly used operators in any language are increment and decrement operators. Given two numbers X and Y. Your task is to print the value of X decremented by 1 and value of Y after incremented by 1.The first line of the input contains two integers X and Y.
<b>Constraints:</b>
1 ≤ X, Y ≤ 10000You need to perform the task as mentioned in the question, and finally, print the result separated by a space.Sample Input 1:
4 5
Sample Output 1:
3 6
Sample Input 2:
5 6
Sample Output 2:
4 7, I have written this Solution Code: def incremental_decremental(x, y):
x -= 1
y += 1
print(x, y, end =' ')
def main():
input1 = input().split()
x = int(input1[0])
y = int(input1[1])
#z = int(input1[2])
incremental_decremental(x, y)
if __name__ == '__main__':
main(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: static int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: def Probability(A,B):
x=1
for i in range (2,A+1):
if (A%i==0) and (B%i==0):
x=i
A=A//x
B=B//x
return A+B
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: def OccurenceOfX(N,X):
cnt=0
for i in range(1, N+1):
if(X%i==0 and X/i<=N):
cnt=cnt+1
return cnt, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
int main()
{
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: public static int OccurenceOfX(int N,int X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies.
Each person i makes a claim of the form: “the number of truth-tellers in this circle is between a<sub>i</sub> and b<sub>i</sub>, inclusive.”
Compute the maximum number of people who could be telling the truth.The first line contains a single integer n (1 ≤ n ≤ 10^3).
Each of the next n lines contains two space-separated integers a<sub>i</sub> and b<sub>i</sub>
(0 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ n).Print, on a single line, the maximum number of people who could be telling the truth. If the given
set of statements are inconsistent, print -1 instead.Sample input
3
1 1
2 3
2 2
Sample output
2
Sample input
8
0 1
1 7
4 8
3 7
1 2
4 5
3 7
1 8
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 read=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(read.readLine());
int count=0;
int cnt[]=new int[n+2];
for(int i=0;i<n;i++){
StringTokenizer st=new StringTokenizer(read.readLine()," ");
int ai=Integer.parseInt(st.nextToken());
int bi=Integer.parseInt(st.nextToken());
for(int j=ai;j<=bi;j++)
{
cnt[j]++;
}
}
int ans=-1;
for(int i=0;i<=n;i++){
if(cnt[i]==i)
{
ans=i;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies.
Each person i makes a claim of the form: “the number of truth-tellers in this circle is between a<sub>i</sub> and b<sub>i</sub>, inclusive.”
Compute the maximum number of people who could be telling the truth.The first line contains a single integer n (1 ≤ n ≤ 10^3).
Each of the next n lines contains two space-separated integers a<sub>i</sub> and b<sub>i</sub>
(0 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ n).Print, on a single line, the maximum number of people who could be telling the truth. If the given
set of statements are inconsistent, print -1 instead.Sample input
3
1 1
2 3
2 2
Sample output
2
Sample input
8
0 1
1 7
4 8
3 7
1 2
4 5
3 7
1 8
Sample output
-1, I have written this Solution Code: n=int(input())
a=[0]*(n+1)
m=0
for i in range(n):
b=[int(k) for k in input().split()]
for j in range(b[0],b[1]+1):
a[j]+=1;
for i in range(n,0,-1):
if a[i]==i:
print(i)
exit()
print(-1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies.
Each person i makes a claim of the form: “the number of truth-tellers in this circle is between a<sub>i</sub> and b<sub>i</sub>, inclusive.”
Compute the maximum number of people who could be telling the truth.The first line contains a single integer n (1 ≤ n ≤ 10^3).
Each of the next n lines contains two space-separated integers a<sub>i</sub> and b<sub>i</sub>
(0 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ n).Print, on a single line, the maximum number of people who could be telling the truth. If the given
set of statements are inconsistent, print -1 instead.Sample input
3
1 1
2 3
2 2
Sample output
2
Sample input
8
0 1
1 7
4 8
3 7
1 2
4 5
3 7
1 8
Sample output
-1, I have written this Solution Code: #include<stdio.h>
#define maxn 1100
struct node{
int l,r;
}a[maxn];
int main(){
int n,i,cnt,j,ans=-1;
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r);
for (i=n;i>=0;i--) {
cnt=0;
for (j=1;j<=n;j++) if(a[j].l<=i&&i<=a[j].r) cnt++;
if (cnt==i) {ans=i;break;}
}
printf("%d\n",ans);
return 0;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. You need to find the count of digits in n.The first line of input contains T denoting the number of test cases. Each testcase contains one line of input containing n.
For Python, Language Just Completes the function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ n ≤ 10<sup>8</sup>For each testcase, in a newline, print the count of digits in n.Input:
2
1
99999
Output:
1
5
<b>Explanation:</b>
Testcase 1: The number of digits in 1 is 1.
Testcase 2: The number of digits in 99999 is 5, 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 n;
cin>>n;
int cnt=0;
while(n>0)
{
cnt++;
n/=10LL;
}
cout<<cnt<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. You need to find the count of digits in n.The first line of input contains T denoting the number of test cases. Each testcase contains one line of input containing n.
For Python, Language Just Completes the function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ n ≤ 10<sup>8</sup>For each testcase, in a newline, print the count of digits in n.Input:
2
1
99999
Output:
1
5
<b>Explanation:</b>
Testcase 1: The number of digits in 1 is 1.
Testcase 2: The number of digits in 99999 is 5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static int countDigit(int a)
{
if(a==0)
{
return 0;
}
else
{
return 1 + countDigit(a/10);
}
}
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i=0;i<T;i++)
{
int n = sc.nextInt();
System.out.println(countDigit(n));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. You need to find the count of digits in n.The first line of input contains T denoting the number of test cases. Each testcase contains one line of input containing n.
For Python, Language Just Completes the function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ n ≤ 10<sup>8</sup>For each testcase, in a newline, print the count of digits in n.Input:
2
1
99999
Output:
1
5
<b>Explanation:</b>
Testcase 1: The number of digits in 1 is 1.
Testcase 2: The number of digits in 99999 is 5, I have written this Solution Code: def totalDigits(n):
cnt=0
while(n>0):
cnt +=1
n//=10
return cnt, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number, you task is to reverse its digits. If the reversed number contains 0s in the beginning, you must remove them as well.First line consists of a single integer denoting n.
Constraint:-
1<=size of n<=100Output is a single line containing reversed number n.Sample Input
123445
Sample Output
544321
Sample Input
16724368
Sample Output
86342761, 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();
int i=str.length()-1;
if(i==0){
int number=Integer.parseInt(str);
System.out.println(number);
}else{
while(str.charAt(i)=='0'){
i--;
}
for(int j=i;j>=0;j--){
System.out.print(str.charAt(j));
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number, you task is to reverse its digits. If the reversed number contains 0s in the beginning, you must remove them as well.First line consists of a single integer denoting n.
Constraint:-
1<=size of n<=100Output is a single line containing reversed number n.Sample Input
123445
Sample Output
544321
Sample Input
16724368
Sample Output
86342761, I have written this Solution Code: n=int(input())
def reverse(n):
return int(str(n)[::-1])
print(reverse(n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number, you task is to reverse its digits. If the reversed number contains 0s in the beginning, you must remove them as well.First line consists of a single integer denoting n.
Constraint:-
1<=size of n<=100Output is a single line containing reversed number n.Sample Input
123445
Sample Output
544321
Sample Input
16724368
Sample Output
86342761, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
reverse(s.begin(),s.end());
int I;
for( I=0;I<s.length();I++){
if(s[I]!='0'){break;}
}
if(I==s.length()){cout<<0;return 0;}
for(int j=I;j<s.length();j++){
cout<<s[j];}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main{
public static void swap(int[] arr,int i, int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static int partition(int[] arr,int l,int r){
int pivot=arr[r];
int i=l-1;
for(int j=l;j<r;j++){
if(arr[j]>pivot){
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,r);
return i+1;
}
public static void quickSort(int[] arr,int l,int r){
if(l<r){
int pivot=partition(arr,l,r);
quickSort(arr,l,pivot-1);
quickSort(arr,pivot+1,r);
}
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int target=sc.nextInt();
quickSort(arr,0,n-1);
int i=0,j=n-1;
while(i<j){
if((arr[i]+arr[j])==target){
System.out.print("Pair found ("+arr[i]+", "+arr[j]+")");
return;
}
else if((arr[i]+arr[j])<target){
j--;
}
else{
i++;
}
}
System.out.print("Pair not found");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code:
N=int(input())
arr=list(map(int,input().split()))
target=int(input())
arr.sort(reverse=True)
'''
ans='Pair not found'
for i in range(N):
for j in range(i+1,N):
if arr[i]+arr[j]==target:
ans='Pair found ({}, {})'.format(arr[i],arr[j])
print(ans)
'''
i=0
j=N-1
ans='Pair not found'
while i<j:
if arr[i]+arr[j]<target:
j-=1
elif arr[i]+arr[j]>target:
i+=1
j+=1
else:
ans='Pair found ({}, {})'.format(arr[i],arr[j])
break
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code: #include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
// Function to find a pair in an array with a given sum using sorting
void findPair(vector<int> &nums, int n, int target)
{
// sort the array in ascending order
sort(nums.begin(), nums.end());
// maintain two indices pointing to endpoints of the array
int low = 0;
int high = n - 1;
// reduce the search space `nums[low…high]` at each iteration of the loop
// loop till the search space is exhausted
while (low < high)
{
// sum found
if (nums[low] + nums[high] == target)
{
if (nums[low] < nums[high])
swap(nums[low], nums[high]);
cout << "Pair found (" << nums[low] << ", " << nums[high] << ")\n";
return;
}
// increment `low` index if the total is less than the desired sum;
// decrement `high` index if the total is more than the desired sum
(nums[low] + nums[high] < target) ? low++ : high--;
}
// we reach here if the pair is not found
cout << "Pair not found";
}
int main()
{
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++)
cin >> nums[i];
int target;
cin >> target;
findPair(nums, n, target);
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: static int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: def Probability(A,B):
x=1
for i in range (2,A+1):
if (A%i==0) and (B%i==0):
x=i
A=A//x
B=B//x
return A+B
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.