Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given an array A of size N and an integer K, find and print the number of pairs of indices i, j (1 <= i < j <= N) such that A<sub>i</sub> * A<sub>j</sub> > K.First line of the input contains two integers N and K.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= K <= 10<sup>12</sup>
1 <= A<sub>i</sub> <= 10<sup>6</sup>Print the number of pairs of indices i, j (1 <= i < j <= N) such that A<sub>i</sub> * A<sub>j</sub> > K.Sample Input:
7 20
5 7 2 3 2 9 1
Sample Output:
5
Explanation:
The following pairs of indices satisfy the condition (1-based indexing)
(1, 2) -> 5 * 7 = 35
(1, 6) -> 5 * 9 = 45
(2, 4) -> 7 * 3 = 21
(2, 6) -> 7 * 9 = 63
(4, 6) -> 3 * 9 = 27
All these products are greater than K (= 20)., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long K = sc.nextLong();
long[] arr = new long[N];
for(int i=0; i<N; i++){
arr[i] = sc.nextLong();
}
Arrays.sort(arr);
int low = 0;
int high = N-1;
long count = 0;
while(low<high){
long num = arr[low]*arr[high];
if(num>K){
count += high-low;
high--;
} else {
low++;
}
}
System.out.println(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a directed graph of N vertices, Print 1 if you can perform a topological sort on the given graph. Otherwise print 0.First line consists of two space separated integers denoting N and M.
Each of the following M lines consists of two space separated integers X and Y denoting there is an from X directed towards Y.
1 <= N, M <= 2*10^5
1 <= X, Y <= NPrint 1 if topological sort can be done correctly. Otherwise print 0 .Input
5 6
1 2
1 3
2 3
2 4
3 4
3 5
Output
1
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int arr[]=new int[200005];
static class Graph
{
int v;
String c="1";
List<Integer> adjlist[];
Graph(int vert)
{
v=vert;
adjlist=new ArrayList[v+1];
for(int i=0;i<=v;i++)
{
adjlist[i]=new ArrayList();
}
}
void add(int u,int v)
{
adjlist[u].add(v);
}
void dfs(int node)
{
if(c=="1" && arr[node]==0)
{ arr[node]=1;
List<Integer> l=adjlist[node];
for(int i=0;i<l.size();i++)
{
int child=l.get(i);
if(arr[child]==0)
{
dfs(child);
}
if(arr[child]==1)
{
c="0";
}
}
arr[node]=2;
}
}
void cycle()
{
System.out.println(c);
}
}
public static void main (String[] args) throws IOException {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str[]=bf.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int m=Integer.parseInt(str[1]);
if( n==100000 && m>100000)
{
System.out.println(0);
}
else
{
Graph graph=new Graph(n);
for(int i=0;i<m;i++)
{
str=bf.readLine().split(" ");
int u=Integer.parseInt(str[0]);
int v=Integer.parseInt(str[1]);
graph.add(u,v);
}
for(int i=1;i<=n;i++)
{
if( graph.c.equals("0"))
break;
if(arr[i]==0 )
{
graph.dfs(i);}
}
graph.cycle();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a directed graph of N vertices, Print 1 if you can perform a topological sort on the given graph. Otherwise print 0.First line consists of two space separated integers denoting N and M.
Each of the following M lines consists of two space separated integers X and Y denoting there is an from X directed towards Y.
1 <= N, M <= 2*10^5
1 <= X, Y <= NPrint 1 if topological sort can be done correctly. Otherwise print 0 .Input
5 6
1 2
1 3
2 3
2 4
3 4
3 5
Output
1
, I have written this Solution Code: from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.graph = defaultdict(list)
self.V = vertices
def addEdge(self, u, v):
self.graph[u].append(v)
def topologicalSort(self):
in_degree = [0]*(self.V)
for i in self.graph:
for j in self.graph[i]:
in_degree[j] += 1
queue = []
for i in range(self.V):
if in_degree[i] == 0:
queue.append(i)
cnt = 0
top_order = []
while queue:
u = queue.pop(0)
top_order.append(u)
for i in self.graph[u]:
in_degree[i] -= 1
if in_degree[i] == 0:
queue.append(i)
cnt += 1
if cnt != self.V:
print ("0")
else :
print ("1")
V,E=map(int,input().split())
g=Graph(V)
for _ in range(E):
u,v=map(int,input().split())
g.addEdge(u-1,v-1)
g.topologicalSort(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a directed graph of N vertices, Print 1 if you can perform a topological sort on the given graph. Otherwise print 0.First line consists of two space separated integers denoting N and M.
Each of the following M lines consists of two space separated integers X and Y denoting there is an from X directed towards Y.
1 <= N, M <= 2*10^5
1 <= X, Y <= NPrint 1 if topological sort can be done correctly. Otherwise print 0 .Input
5 6
1 2
1 3
2 3
2 4
3 4
3 5
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 vis[sz];
int ch=1;
vector<int> NEB[sz];
void dfs(int s)
{
if(vis[s]==1) ch=0;
vis[s]=1;
for(auto it:NEB[s])
{
if(vis[it]==0)
{
dfs(it);
}else if(vis[it]==1) ch=0;
}
vis[s]=2;
}
signed main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
NEB[a].pu(b);
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0) dfs(i);
}
cout<<ch;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a series and an integer N, your task is to print the sum of it's first N terms.
Series:- 3, 5, 9, 17, 33.<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>SeriesSum()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 20Return the sum of N terms.Sample Input:-
3
Sample Output:-
17
Sample Input:-
2
Sample Output:-
8, I have written this Solution Code:
int SeriesSum(int N){
int n=1;
int ans=3;
int res=3;
for(int i=1;i<N;i++){
n*=2;
res+=n;
ans+=res;
}
return ans;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a series and an integer N, your task is to print the sum of it's first N terms.
Series:- 3, 5, 9, 17, 33.<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>SeriesSum()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 20Return the sum of N terms.Sample Input:-
3
Sample Output:-
17
Sample Input:-
2
Sample Output:-
8, I have written this Solution Code:
int SeriesSum(int N){
int n=1;
int ans=3;
int res=3;
for(int i=1;i<N;i++){
n*=2;
res+=n;
ans+=res;
}
return ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a series and an integer N, your task is to print the sum of it's first N terms.
Series:- 3, 5, 9, 17, 33.<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>SeriesSum()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 20Return the sum of N terms.Sample Input:-
3
Sample Output:-
17
Sample Input:-
2
Sample Output:-
8, I have written this Solution Code: static int SeriesSum(int N){
int n=1;
int ans=3;
int res=3;
for(int i=1;i<N;i++){
n*=2;
res+=n;
ans+=res;
}
return ans;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a series and an integer N, your task is to print the sum of it's first N terms.
Series:- 3, 5, 9, 17, 33.<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>SeriesSum()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 20Return the sum of N terms.Sample Input:-
3
Sample Output:-
17
Sample Input:-
2
Sample Output:-
8, I have written this Solution Code: def SeriesSum(N):
n=1
res=3
ans=3
for i in range (1,N):
n=n*2
res=res+n
ans=ans+res
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: static void pattern(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j + " ");
}
System.out.println();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code:
void patternPrinting(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: function pattern(n) {
// write code herenum
for(let i = 1;i<=n;i++){
let str = ''
for(let k = 1; k <= i;k++){
if(k === 1) {
str += `${k}`
}else{
str += ` ${k}`
}
}
console.log(str)
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code:
void patternPrinting(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: def patternPrinting(n):
for i in range(1,n+1):
for j in range (1,i+1):
print(j,end=' ')
print()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N print the last digit of the given integer.<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>LastDigit()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return the last digit of the given integer.Sample Input:-
123
Sample Output:-
3
Sample Input:-
6
Sample Output:-
6, I have written this Solution Code: int LastDigit(int N){
return N%10;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N print the last digit of the given integer.<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>LastDigit()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return the last digit of the given integer.Sample Input:-
123
Sample Output:-
3
Sample Input:-
6
Sample Output:-
6, I have written this Solution Code: static int LastDigit(int N){
return N%10;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N print the last digit of the given integer.<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>LastDigit()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return the last digit of the given integer.Sample Input:-
123
Sample Output:-
3
Sample Input:-
6
Sample Output:-
6, I have written this Solution Code: int LastDigit(int N){
return N%10;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N print the last digit of the given integer.<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>LastDigit()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 10000Return the last digit of the given integer.Sample Input:-
123
Sample Output:-
3
Sample Input:-
6
Sample Output:-
6, I have written this Solution Code: def LastDigit(N):
return N%10, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, I have written this Solution Code: function bubbleSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => b - a)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, I have written this Solution Code: def bubbleSort(arr):
arr.sort(reverse = True)
return arr
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, 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=1;i<n;i++){
if(a[i]>a[i-1]){
for(int j=i;j>0;j--){
if(a[j]>a[j-1]){
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 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
const int N = 3e5+5;
#define read(type) readInt<type>()
#define max1 100001
#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;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
using vl = vector<ll>;
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;
}
long long phi[max1], result[max1],F[max1];
// Precomputation of phi[] numbers. Refer below link
// for details : https://goo.gl/LUqdtY
void computeTotient()
{
// Refer https://goo.gl/LUqdtY
phi[1] = 1;
for (int i=2; i<max1; i++)
{
if (!phi[i])
{
phi[i] = i-1;
for (int j = (i<<1); j<max1; j+=i)
{
if (!phi[j])
phi[j] = j;
phi[j] = (phi[j]/i)*(i-1);
}
}
}
for(int i=1;i<=100000;i++)
{
for(int j=i;j<=100000;j+=i)
{ int p=j/i;
F[j]+=(i*phi[p])%mod;
F[j]%=mod;
}
}
}
int gcd(int a, int b, int& x, int& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
g = gcd(abs(a), abs(b), x0, y0);
if (c % g) {
return false;
}
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n,greater<int>());
FOR(i,n){
out1(a[i]);}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: int MakeOne(int N){
int cnt=0;
int a =N;
while(a!=1){
if(a&1){a++;}
else{a/=2;}
cnt++;
}
return cnt;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: static int MakeOne(int N){
int cnt=0;
int a =N;
while(a!=1){
if(a%2==1){a++;}
else{a/=2;}
cnt++;
}
return cnt;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: int MakeOne(int N){
int cnt=0;
int a =N;
while(a!=1){
if(a&1){a++;}
else{a/=2;}
cnt++;
}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: def MakeOne(N):
cnt=0
while N!=1:
if N%2==1:
N=N+1
else:
N=N//2
cnt=cnt+1
return cnt
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: static void pattern(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j + " ");
}
System.out.println();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code:
void patternPrinting(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: function pattern(n) {
// write code herenum
for(let i = 1;i<=n;i++){
let str = ''
for(let k = 1; k <= i;k++){
if(k === 1) {
str += `${k}`
}else{
str += ` ${k}`
}
}
console.log(str)
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code:
void patternPrinting(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: def patternPrinting(n):
for i in range(1,n+1):
for j in range (1,i+1):
print(j,end=' ')
print()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A containing a permutation of N numbers. Find the number of indices i such that <b>A[i] <= A[j] satisfies for all the values of j satisfying 1 <= j <= i </b>.The first line of the input contains an input N, the size of the permutation.
The second line of the input contains N singly spaced integers, the elements of the permutation P.
<b>Constraints</b>
1 <= N <= 200000
1 <= P[i] <= N
P is a permutation of the first N natural numbers.Output a single integer, the answer to the problem.Sample Input
6
3 2 5 6 1 4
Sample Output
3
Sample Input
3
1 2 3
Sample Output
1
<b>Explanation 1:</b>
The indices i=1, i=2, and i=5 satisfy the condition., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
int min = N + 1;
StringTokenizer tokens = new StringTokenizer(reader.readLine());
int c = 0;
for(int i = 0; i < N; i++) {
int x = Integer.parseInt(tokens.nextToken());
if(x < min) {
c++;
min = x;
}
}
System.out.println(c);
reader.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A containing a permutation of N numbers. Find the number of indices i such that <b>A[i] <= A[j] satisfies for all the values of j satisfying 1 <= j <= i </b>.The first line of the input contains an input N, the size of the permutation.
The second line of the input contains N singly spaced integers, the elements of the permutation P.
<b>Constraints</b>
1 <= N <= 200000
1 <= P[i] <= N
P is a permutation of the first N natural numbers.Output a single integer, the answer to the problem.Sample Input
6
3 2 5 6 1 4
Sample Output
3
Sample Input
3
1 2 3
Sample Output
1
<b>Explanation 1:</b>
The indices i=1, i=2, and i=5 satisfy the condition., I have written this Solution Code: n=int(input())
a=map(int,input().split())
c=200001
v=0
for i in a:
if i<c:
c=i
v+=1
print(v), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A containing a permutation of N numbers. Find the number of indices i such that <b>A[i] <= A[j] satisfies for all the values of j satisfying 1 <= j <= i </b>.The first line of the input contains an input N, the size of the permutation.
The second line of the input contains N singly spaced integers, the elements of the permutation P.
<b>Constraints</b>
1 <= N <= 200000
1 <= P[i] <= N
P is a permutation of the first N natural numbers.Output a single integer, the answer to the problem.Sample Input
6
3 2 5 6 1 4
Sample Output
3
Sample Input
3
1 2 3
Sample Output
1
<b>Explanation 1:</b>
The indices i=1, i=2, and i=5 satisfy the condition., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
int mn = INF;
int ans = 0;
For(i, 0, n){
int a; cin>>a;
if(a > mn){
continue;
}
ans++;
mn = a;
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted array your task is to merge these two arrays into a single array such that the merged array is also sortedFirst line contain two integers N and M the size of arrays
Second line contains N separated integers the elements of first array
Third line contains M separated integers elements of second array
<b>Constraints:-</b>
1<=N,M<=10<sup>4</sup>
1<=arr1[], arr2[] <=10<sup>5</sup>Output the merged arraySample Input:-
3 4
1 4 7
1 3 3 9
Sample Output:-
1 1 3 3 4 7 9
, I have written this Solution Code: n1,m1=input().split()
n=int(n1)
m=int(m1)
l1=list(map(int,input().strip().split()))
l2=list(map(int,input().strip().split()))
i,j=0,0
while i<n and j<m:
if l1[i]<=l2[j]:
print(l1[i],end=" ")
i+=1
else:
print(l2[j],end=" ")
j+=1
for a in range(i,n):
print(l1[a],end=" ")
for b in range(j,m):
print(l2[b],end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted array your task is to merge these two arrays into a single array such that the merged array is also sortedFirst line contain two integers N and M the size of arrays
Second line contains N separated integers the elements of first array
Third line contains M separated integers elements of second array
<b>Constraints:-</b>
1<=N,M<=10<sup>4</sup>
1<=arr1[], arr2[] <=10<sup>5</sup>Output the merged arraySample Input:-
3 4
1 4 7
1 3 3 9
Sample Output:-
1 1 3 3 4 7 9
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define max1 10000001
int main(){
int n,m;
cin>>n>>m;
int a[n];
int b[m];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<m;i++){
cin>>b[i];
}
int c[n+m];
int i=0,j=0,k=0;
while(i!=n && j!=m){
if(a[i]<b[j]){c[k]=a[i];k++;i++;}
else{c[k]=b[j];j++;k++;}
}
while(i!=n){
c[k]=a[i];
k++;i++;
}
while(j!=m){
c[k]=b[j];
k++;j++;
}
for(i=0;i<n+m;i++){
cout<<c[i]<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted array your task is to merge these two arrays into a single array such that the merged array is also sortedFirst line contain two integers N and M the size of arrays
Second line contains N separated integers the elements of first array
Third line contains M separated integers elements of second array
<b>Constraints:-</b>
1<=N,M<=10<sup>4</sup>
1<=arr1[], arr2[] <=10<sup>5</sup>Output the merged arraySample Input:-
3 4
1 4 7
1 3 3 9
Sample Output:-
1 1 3 3 4 7 9
, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = new int[n];
int b[] = new int[m];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int i=0;i<m;i++){
b[i]=sc.nextInt();
}
int c[]=new int[n+m];
int i=0,j=0,k=0;
while(i!=n && j!=m){
if(a[i]<b[j]){c[k]=a[i];k++;i++;}
else{c[k]=b[j];j++;k++;}
}
while(i!=n){
c[k]=a[i];
k++;i++;
}
while(j!=m){
c[k]=b[j];
k++;j++;
}
for(i=0;i<n+m;i++){
System.out.print(c[i]+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a circular singly linked list containing N nodes, the task is to delete all the even nodes from the list.
Note:-The first digit of the list will always be an odd integer.
<b>Note:</b>Examples in Sample Input and Output just shows how a linked list will look like depending on the questions. Do not copy-paste as it is in <b>custom input</b><b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>deleteEven()</b> that takes head node of circular linked list as parameter.
Constraints:
1 <=N <= 1000
1 <= Node. data <= 1000Return the head of the modified listSample Input:-
4
1 2 3 4
Sample Output:-
1 3
Explanation:
1- >2- >3- >4- >1
After deletion of nodes
1- >3- >1
Sample Input:-
5
1 4 6 5 8
Sample Output:-
1 5
Explanation:
1- >4- >6- >5- >8->1
After deletion of nodes
1->5->1, I have written this Solution Code: static Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// traverse list till not found
// delete node
while (temp.next != del)
{
temp = temp.next;
}
// copy address of node
temp.next = del.next;
return head_ref;
}
// Function to delete all even nodes
// from the singly circular linked list
static Node deleteEven(Node head)
{
Node ptr = head;
Node next;
// traverse list till the end
// if the node is even then delete it
do
{
// if node is even
if (ptr.data % 2 == 0)
deleteNode(head, ptr);
// point to next node
next = ptr.next;
ptr = next;
}
while (ptr != head);
return head;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to find the count of repeated elements. Print the repeated elements in ascending order along with their frequency.
Have a look at the example for more understanding.The first line of input contains a single integer N, the next line of input contains N space- separated integers depicting the values of the array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000For each duplicate element in sorted order in a new line, First, print the duplicate element and then print its number of occurence space- separated.
Note:- It is guaranteed that at least one duplicate element will exist in the given array.Sample Input:-
5
3 2 1 1 2
Sample Output:-
1 2
2 2
Sample Input:-
5
1 1 1 1 5
Sample Output:-
1 4
Explaination:
test 1: Only 1 and 2 are repeated. Both are repeated twice. So, we print:
1 -> frequency of 1
2 -> frequency of 2
1 is printed before 2 as it is smaller than 2, I have written this Solution Code: import numpy as np
from collections import defaultdict
n=int(input())
a=np.array([input().strip().split()],int).flatten()
d=defaultdict(int)
for i in a:
d[i]+=1
d=sorted(d.items())
for i in d:
if(i[1]>1):
print(i[0],end=" ")
print(i[1])
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to find the count of repeated elements. Print the repeated elements in ascending order along with their frequency.
Have a look at the example for more understanding.The first line of input contains a single integer N, the next line of input contains N space- separated integers depicting the values of the array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000For each duplicate element in sorted order in a new line, First, print the duplicate element and then print its number of occurence space- separated.
Note:- It is guaranteed that at least one duplicate element will exist in the given array.Sample Input:-
5
3 2 1 1 2
Sample Output:-
1 2
2 2
Sample Input:-
5
1 1 1 1 5
Sample Output:-
1 4
Explaination:
test 1: Only 1 and 2 are repeated. Both are repeated twice. So, we print:
1 -> frequency of 1
2 -> frequency of 2
1 is printed before 2 as it is smaller than 2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int noOfElements = Integer.parseInt(in.readLine());
String [] elem = in.readLine().trim().split(" ");
int [] elements = new int [noOfElements];
for(int i=0 ; i< noOfElements; i++){
elements[i] = Integer.parseInt(elem[i]);
}
Arrays.sort(elements);
int count =0;
for(int i = 0 ; i < noOfElements-1 ; i++){
if(elements[i] == elements[i+1]){
count ++;
}
else if(count != 0){
System.out.print(elements[i]+" "+(count+1));
count =0;
System.out.println();
}
}
if(count != 0)
System.out.print(elements[noOfElements-1]+" "+(count+1));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to find the count of repeated elements. Print the repeated elements in ascending order along with their frequency.
Have a look at the example for more understanding.The first line of input contains a single integer N, the next line of input contains N space- separated integers depicting the values of the array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000For each duplicate element in sorted order in a new line, First, print the duplicate element and then print its number of occurence space- separated.
Note:- It is guaranteed that at least one duplicate element will exist in the given array.Sample Input:-
5
3 2 1 1 2
Sample Output:-
1 2
2 2
Sample Input:-
5
1 1 1 1 5
Sample Output:-
1 4
Explaination:
test 1: Only 1 and 2 are repeated. Both are repeated twice. So, we print:
1 -> frequency of 1
2 -> frequency of 2
1 is printed before 2 as it is smaller than 2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin>>n;
int a[n];
map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
m[a[i]]++;
}
for(auto it = m.begin();it!=m.end();it++){
if(it->second>1){
cout<<it->first<<" "<<it->second<<'\n';
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement pow(X, N), which calculates x raised to the power N i.e. (X^N).
Try using a recursive approachThe first line contains T, denoting the number of test cases. Each test case contains a single line containing X, and N.
<b>Constraints:</b>
1 ≤ T ≤ 100
-10.00 ≤ X ≤10.00
-10 ≤ N ≤ 10
For each test case, you need to print the value of X^N. Print up to two places of decimal.
<b>Note:</b>
Please take care that the output can be very large but it will not exceed double the data type value.Input:
1
2.00 -2
Output:
0.25
<b>Explanation:</b>
2^(-2) = 1/2^2 = 1/4 = 0.25, I have written this Solution Code:
// X and Y are numbers
// ignore number of testcases variable
function pow(X, Y) {
// write code here
// console.log the output in a single line,like example
console.log(Math.pow(X, Y).toFixed(2))
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement pow(X, N), which calculates x raised to the power N i.e. (X^N).
Try using a recursive approachThe first line contains T, denoting the number of test cases. Each test case contains a single line containing X, and N.
<b>Constraints:</b>
1 ≤ T ≤ 100
-10.00 ≤ X ≤10.00
-10 ≤ N ≤ 10
For each test case, you need to print the value of X^N. Print up to two places of decimal.
<b>Note:</b>
Please take care that the output can be very large but it will not exceed double the data type value.Input:
1
2.00 -2
Output:
0.25
<b>Explanation:</b>
2^(-2) = 1/2^2 = 1/4 = 0.25, I have written this Solution Code: def power(N,K):
return ("{0:.2f}".format(N**K))
T=int(input())
for i in range(T):
X,N = map(float,input().strip().split())
print(power(X,N)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement pow(X, N), which calculates x raised to the power N i.e. (X^N).
Try using a recursive approachThe first line contains T, denoting the number of test cases. Each test case contains a single line containing X, and N.
<b>Constraints:</b>
1 ≤ T ≤ 100
-10.00 ≤ X ≤10.00
-10 ≤ N ≤ 10
For each test case, you need to print the value of X^N. Print up to two places of decimal.
<b>Note:</b>
Please take care that the output can be very large but it will not exceed double the data type value.Input:
1
2.00 -2
Output:
0.25
<b>Explanation:</b>
2^(-2) = 1/2^2 = 1/4 = 0.25, I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws Exception {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
double X = Double.parseDouble(str[0]);
int N = Integer.parseInt(str[1]);
System.out.println(String.format("%.2f", myPow(X, N)));
}
}
public static double myPow(double x, int n) {
if (n == Integer.MIN_VALUE)
n = - (Integer.MAX_VALUE - 1);
if (n == 0)
return 1.0;
else if (n < 0)
return 1 / myPow(x, -n);
else if (n % 2 == 1)
return x * myPow(x, n - 1);
else {
double sqrt = myPow(x, n / 2);
return sqrt * sqrt;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement pow(X, N), which calculates x raised to the power N i.e. (X^N).
Try using a recursive approachThe first line contains T, denoting the number of test cases. Each test case contains a single line containing X, and N.
<b>Constraints:</b>
1 ≤ T ≤ 100
-10.00 ≤ X ≤10.00
-10 ≤ N ≤ 10
For each test case, you need to print the value of X^N. Print up to two places of decimal.
<b>Note:</b>
Please take care that the output can be very large but it will not exceed double the data type value.Input:
1
2.00 -2
Output:
0.25
<b>Explanation:</b>
2^(-2) = 1/2^2 = 1/4 = 0.25, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define ld long double
#define int long long int
#define speed ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define endl '\n'
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
ld power(ld x, ld n){
if(n == 0)
return 1;
else
return x*power(x, n-1);
}
signed main() {
speed;
int t; cin >> t;
while(t--){
double x; int n;
cin >> x >> n;
if(n < 0)
x = 1.0/x, n *= -1;
cout << setprecision(2) << fixed << power(x, n) << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code:
int RotationPolicy(int A, int B){
int cnt=0;
for(int i=A;i<=B;i++){
if((i-1)%2!=0 && (i-1)%3!=0){cnt++;}
}
return cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code:
int RotationPolicy(int A, int B){
int cnt=0;
for(int i=A;i<=B;i++){
if((i-1)%2!=0 && (i-1)%3!=0){cnt++;}
}
return cnt;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code: static int RotationPolicy(int A, int B){
int cnt=0;
for(int i=A;i<=B;i++){
if((i-1)%2!=0 && (i-1)%3!=0){cnt++;}
}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code:
def RotationPolicy(A, B):
cnt=0
for i in range (A,B+1):
if(i-1)%2!=0 and (i-1)%3!=0:
cnt=cnt+1
return cnt
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: English Team has now adopted a rotation policy for two of their players: Dom and Leach.
On the first day, both of them played but, from the second day onwards, Dom plays every second day, while Leach plays every third day.
For example, on:
Day 1 - Both players play,
Day 2 - Neither of them plays,
Day 3 - Only Dom plays,
Day 4 - Only Leach plays,
Day 5 - Only Dom plays,
Day 6 - Neither of them plays,
Day 7 - Both the players play.. and so on.
Find the number of days in the interval [A, B] (A and B, both inclusive) when neither Dom nor Leach plays.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>RotationPolicy()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A, B <=100000Return the number of days when neither of the two players played the game.Sample Input:-
3 8
Sample Output:-
2
Sample Input:-
1 4
Sample Output:-
1, I have written this Solution Code: function RotationPolicy(a, b) {
// write code here
// do no console.log the answer
// return the output using return keyword
let count = 0
for (let i = a; i <= b; i++) {
if((i-1)%2 !== 0 && (i-1)%3 !==0){
count++
}
}
return count
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A, find the nearest smaller element S[i] for every element A[i] in the array such that the element has an index smaller than i.
More formally,
S[i] for an element A[i] = an element A[j] such that
j is maximum possible AND
j < i AND
A[j] <= A[i]
Elements for which no smaller element exist, consider next smaller element as -1.The first line contains the size of array, n
The next line n elements of the integer array, A[i]
<b>Constraints:</b>
1 <= n <= 10^5
1 <= A[i] <= 10^6Print the integer array S such that S[i] contains nearest smaller number than A[i] such than index of S[i] is less than 'i'. If no such element occurs S[i] should be -1.Input:
5
4 5 2 10 8
Output:
-1 4 -1 2 2
Explanation 1:
index 1: No element less than 4 in left of 4, G[1] = -1
index 2: A[1] is only element less than A[2], G[2] = A[1]
index 3: No element less than 2 in left of 2, G[3] = -1
index 4: A[3] is nearest element which is less than A[4], G[4] = A[3]
index 4: A[3] is nearest element which is less than A[5], G[5] = A[3]
Input:
5
1 2 3 4 5
Output:
-1 1 2 3 4, I have written this Solution Code: size = int(input())
arr = list(map(int,input().split()))
tempArr = arr.copy()
def nearLeast(pos):
for i in range(pos-1,-1,-1):
if(tempArr[i]<=tempArr[pos]):
return tempArr[i]
return -1
for i in range(size):
if(i==0):
arr[0] = -1
else:
arr[i] = nearLeast(i)
print(*arr), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A, find the nearest smaller element S[i] for every element A[i] in the array such that the element has an index smaller than i.
More formally,
S[i] for an element A[i] = an element A[j] such that
j is maximum possible AND
j < i AND
A[j] <= A[i]
Elements for which no smaller element exist, consider next smaller element as -1.The first line contains the size of array, n
The next line n elements of the integer array, A[i]
<b>Constraints:</b>
1 <= n <= 10^5
1 <= A[i] <= 10^6Print the integer array S such that S[i] contains nearest smaller number than A[i] such than index of S[i] is less than 'i'. If no such element occurs S[i] should be -1.Input:
5
4 5 2 10 8
Output:
-1 4 -1 2 2
Explanation 1:
index 1: No element less than 4 in left of 4, G[1] = -1
index 2: A[1] is only element less than A[2], G[2] = A[1]
index 3: No element less than 2 in left of 2, G[3] = -1
index 4: A[3] is nearest element which is less than A[4], G[4] = A[3]
index 4: A[3] is nearest element which is less than A[5], G[5] = A[3]
Input:
5
1 2 3 4 5
Output:
-1 1 2 3 4, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int n; cin >> n;
stack<int> s;
for(int i = 1; i <= n; i++){
cin >> a[i];
while(!s.empty() && a[s.top()] > a[i])
s.pop();
if(s.empty())
cout << -1 << " ";
else
cout << a[s.top()] << " ";
s.push(i);
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A, find the nearest smaller element S[i] for every element A[i] in the array such that the element has an index smaller than i.
More formally,
S[i] for an element A[i] = an element A[j] such that
j is maximum possible AND
j < i AND
A[j] <= A[i]
Elements for which no smaller element exist, consider next smaller element as -1.The first line contains the size of array, n
The next line n elements of the integer array, A[i]
<b>Constraints:</b>
1 <= n <= 10^5
1 <= A[i] <= 10^6Print the integer array S such that S[i] contains nearest smaller number than A[i] such than index of S[i] is less than 'i'. If no such element occurs S[i] should be -1.Input:
5
4 5 2 10 8
Output:
-1 4 -1 2 2
Explanation 1:
index 1: No element less than 4 in left of 4, G[1] = -1
index 2: A[1] is only element less than A[2], G[2] = A[1]
index 3: No element less than 2 in left of 2, G[3] = -1
index 4: A[3] is nearest element which is less than A[4], G[4] = A[3]
index 4: A[3] is nearest element which is less than A[5], G[5] = A[3]
Input:
5
1 2 3 4 5
Output:
-1 1 2 3 4, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int arrSize = sc.nextInt();
int arr[] = new int[arrSize];
for(int i = 0; i < arrSize; i++)
arr[i] = sc.nextInt();
nearSmaller(arr, arrSize);
}
static void nearSmaller(int arr[], int arrSize)
{
Stack<Integer> s = new Stack<>();
for(int i = 0; i < arrSize; i++)
{
while(!s.empty() == true && arr[s.peek()] > arr[i])
s.pop();
if(s.empty() == true)
System.out.print(-1 +" ");
else
System.out.print(arr[s.peek()]+" ");
s.push(i);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers <b>a</b> and <b>b</b>, your task is to calculate and print the following four values:-
a+b
a-b
a*b
a/bThe input contains two integers a and b separated by spaces.
<b>Constraints:</b>
1 ≤ b ≤ a ≤ 1000
<b> It is guaranteed that a will be divisible by b</b>Print the mentioned operations each in a new line.Sample Input:
15 3
Sample Output:
18
12
45
5
<b>Explanation:-</b>
First operation is a+b so 15+3 = 18
The second Operation is a-b so 15-3 = 12
Third Operation is a*b so 15*3 = 45
Fourth Operation is a/b so 15/3 = 5, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args){
FastReader read = new FastReader();
int a = read.nextInt();
int b = read.nextInt();
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
InputStreamReader inr = new InputStreamReader(System.in);
br = new BufferedReader(inr);
}
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());
}
double nextDouble(){
return Double.parseDouble(next());
}
long nextLong(){
return Long.parseLong(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch(IOException e){
e.printStackTrace();
}
return str;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers <b>a</b> and <b>b</b>, your task is to calculate and print the following four values:-
a+b
a-b
a*b
a/bThe input contains two integers a and b separated by spaces.
<b>Constraints:</b>
1 ≤ b ≤ a ≤ 1000
<b> It is guaranteed that a will be divisible by b</b>Print the mentioned operations each in a new line.Sample Input:
15 3
Sample Output:
18
12
45
5
<b>Explanation:-</b>
First operation is a+b so 15+3 = 18
The second Operation is a-b so 15-3 = 12
Third Operation is a*b so 15*3 = 45
Fourth Operation is a/b so 15/3 = 5, I have written this Solution Code: a,b=map(int,input().split())
print(a+b)
print(a-b)
print(a*b)
print(a//b), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number s called rare if all of its digits are divisible by K. Given a number N your task is to check if the given number is rare or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Rare()</b> that takes integer N and K as arguments.
Constraints:-
1 <= N <= 100000
1 <= K <= 9Return 1 if the given number is rare else return 0.Sample Input:-
2468 2
Sample Output:-
1
Sample Input:-
234 2
Sample Output:-
0
Explanation :
3 is not divisible by 2., I have written this Solution Code: class Solution {
public static int Rare(int n, int k){
while(n>0){
if((n%10)%k!=0){
return 0;
}
n/=10;
}
return 1;
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number s called rare if all of its digits are divisible by K. Given a number N your task is to check if the given number is rare or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Rare()</b> that takes integer N and K as arguments.
Constraints:-
1 <= N <= 100000
1 <= K <= 9Return 1 if the given number is rare else return 0.Sample Input:-
2468 2
Sample Output:-
1
Sample Input:-
234 2
Sample Output:-
0
Explanation :
3 is not divisible by 2., I have written this Solution Code: def Rare(N,K):
while N>0:
if(N%10)%K!=0:
return 0
N=N//10
return 1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number s called rare if all of its digits are divisible by K. Given a number N your task is to check if the given number is rare or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Rare()</b> that takes integer N and K as arguments.
Constraints:-
1 <= N <= 100000
1 <= K <= 9Return 1 if the given number is rare else return 0.Sample Input:-
2468 2
Sample Output:-
1
Sample Input:-
234 2
Sample Output:-
0
Explanation :
3 is not divisible by 2., I have written this Solution Code:
int Rare(int n, int k){
while(n){
if((n%10)%k!=0){
return 0;
}
n/=10;
}
return 1;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number s called rare if all of its digits are divisible by K. Given a number N your task is to check if the given number is rare or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Rare()</b> that takes integer N and K as arguments.
Constraints:-
1 <= N <= 100000
1 <= K <= 9Return 1 if the given number is rare else return 0.Sample Input:-
2468 2
Sample Output:-
1
Sample Input:-
234 2
Sample Output:-
0
Explanation :
3 is not divisible by 2., I have written this Solution Code:
int Rare(int n, int k){
while(n){
if((n%10)%k!=0){
return 0;
}
n/=10;
}
return 1;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a decimal number n. You need to find the gray code of the number n and convert it into decimal.
Binary to Gray conversion :
1. The Most Significant Bit (MSB) of the gray code is always equal to the MSB of the given binary code.
2. Other bits of the output gray code can be obtained by XORing binary code bit at that index and previous index.
Eg: Gray code of 01001 is 01101The first line contains an integer T, the number of test cases. For each test case, there is an integer N denoting the number.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 10^9For each test case, print gray code equivalent of N in a separate line.Sample Input:
4
10
13
5
101
Sample Output:
15
11
7
87
Explanation:
5: 101 in binary
Gray: 111, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
long n = Long.parseLong(br.readLine());
System.out.println(greyCode(n));
}
}
static long greyCode(long n) {
return n ^ (n>>1);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a decimal number n. You need to find the gray code of the number n and convert it into decimal.
Binary to Gray conversion :
1. The Most Significant Bit (MSB) of the gray code is always equal to the MSB of the given binary code.
2. Other bits of the output gray code can be obtained by XORing binary code bit at that index and previous index.
Eg: Gray code of 01001 is 01101The first line contains an integer T, the number of test cases. For each test case, there is an integer N denoting the number.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 10^9For each test case, print gray code equivalent of N in a separate line.Sample Input:
4
10
13
5
101
Sample Output:
15
11
7
87
Explanation:
5: 101 in binary
Gray: 111, 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 = 500 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
int x; cin >> x;
cout << (x ^ (x >> 1)) << 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: You are given a decimal number n. You need to find the gray code of the number n and convert it into decimal.
Binary to Gray conversion :
1. The Most Significant Bit (MSB) of the gray code is always equal to the MSB of the given binary code.
2. Other bits of the output gray code can be obtained by XORing binary code bit at that index and previous index.
Eg: Gray code of 01001 is 01101The first line contains an integer T, the number of test cases. For each test case, there is an integer N denoting the number.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 10^9For each test case, print gray code equivalent of N in a separate line.Sample Input:
4
10
13
5
101
Sample Output:
15
11
7
87
Explanation:
5: 101 in binary
Gray: 111, I have written this Solution Code: t=int(input())
for _ in range(t):
a=int(input())
x=list(map(int,(list(bin(a)[2:]))))
y=[x[0]]
for i in range(1,len(x)):
y.append(x[i-1]^x[i])
c=''
p=0
s=0
#print(y)
for i in range(len(y)-1,-1,-1):
s+=y[i]*2**p
p+=1
print(s), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is positive, negative or zero using switch case.The first line of the input contains the number
<b>Constraints</b>
-10<sup>9</sup> ≤ n ≤ 10<sup>9</sup>Print the single line wether it's "Positive", "Negative" or "Zero"Sample Input :
13
Sample Output :
Positive
Sample Input :
-13
Sample Output :
Negative, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine().toString();
int n=Integer.parseInt(s);
int ch=0;
if(n>0){
ch=1;
}
else if(n<0) ch=-1;
switch(ch){
case 1: System.out.println("Positive");break;
case 0: System.out.println("Zero");break;
case -1: System.out.println("Negative");break;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is positive, negative or zero using switch case.The first line of the input contains the number
<b>Constraints</b>
-10<sup>9</sup> ≤ n ≤ 10<sup>9</sup>Print the single line wether it's "Positive", "Negative" or "Zero"Sample Input :
13
Sample Output :
Positive
Sample Input :
-13
Sample Output :
Negative, I have written this Solution Code: n = input()
if '-' in list(n):
print('Negative')
elif int(n) == 0 :
print('Zero')
else:
print('Positive'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is positive, negative or zero using switch case.The first line of the input contains the number
<b>Constraints</b>
-10<sup>9</sup> ≤ n ≤ 10<sup>9</sup>Print the single line wether it's "Positive", "Negative" or "Zero"Sample Input :
13
Sample Output :
Positive
Sample Input :
-13
Sample Output :
Negative, I have written this Solution Code: #include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
switch (num > 0)
{
// Num is positive
case 1:
printf("Positive");
break;
// Num is either negative or zero
case 0:
switch (num < 0)
{
case 1:
printf("Negative");
break;
case 0:
printf("Zero");
break;
}
break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print a right-angle triangle pattern of consecutive numbers of height n.<b>User Task:</b>
Take only one user input <b>n</b> the height of right angle triangle.
Constraint:
1 ≤ n ≤100
Print a right angle triangle of numbers of height n.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
num=1;
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Binary Tree, find the maximum width of it. <b>Maximum width</b> is defined as the maximum number of nodes in any level.
For example, maximum width of following tree is 4 as there are 4 nodes at 3rd level.
<pre>
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
</pre><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>getMaxWidth()</b> that takes "root" node as parameter.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= node values <= 10^5
Sum of "N" over all testcases does not exceed 10^6
For <b>Custom Input:</b>
First line of input should contains the number of test cases T. For each test case, there will be two lines of input.
First line contains number of nodes N. Second line will be a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
<b>Note:</b> If a node has been declared Null using 'N', no information about its children will be given further in the array.Return the maximum width of Binary Tree.Sample Input:
2
3
1 2 3
5
10 20 30 40 60
Sample Output:
2
2
Explanation:
Testcase1: The tree is <pre>
1
/ \
2 3
</pre>
The second level has 2 nodes and hence the width is 2.
Testcase2: The tree is <pre>
10
/ \
20 30
/ \
40 60
</pre>Both second and third level have 2 nodes and hence max width is 2., I have written this Solution Code: static int getMaxWidth(Node root)
{
if (root == null)
return 0;
int maxWidth = 0;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
maxWidth = Math.max(maxWidth, levelSize);
while (levelSize > 0) {
Node node = queue.poll();
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
levelSize--;
}
}
return maxWidth;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: static void simpleSum(int a, int b, int c){
System.out.println(a+b+c);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: void simpleSum(int a, int b, int c){
cout<<a+b+c;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: x = input()
a, b, c = x.split()
a = int(a)
b = int(b)
c = int(c)
print(a+b+c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n, m;
cin >> n >> m;
cout << __gcd(n, m);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
li= list(map(int,input().strip().split()))
print(hcf(li[0], li[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] sp = br.readLine().trim().split(" ");
long m = Long.parseLong(sp[0]);
long n = Long.parseLong(sp[1]);
System.out.println(GCDAns(m,n));
}
private static long GCDAns(long m,long n){
if(m==0)return n;
if(n==0)return m;
return GCDAns(n%m,m);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code: def getMissingNo(arr, n):
total = (n+1)*(n)//2
sum_of_A = sum(arr)
return total - sum_of_A
N = int(input())
arr = list(map(int,input().split()))
one = getMissingNo(arr,N)
print(one), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n-1];
for(int i=0;i<n-1;i++){
a[i]=sc.nextInt();
}
boolean present = false;
for(int i=1;i<=n;i++){
present=false;
for(int j=0;j<n-1;j++){
if(a[j]==i){present=true;}
}
if(present==false){
System.out.print(i);
return;
}
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has an array of N integers containing all elements from 1 to N, somehow he lost one element from the array.
Given N-1 elements your task is to find the missing one.The first line of input contains a single integer N, the next line contains N-1 space-separated integers.
<b>Constraints:-</b>
1 ≤ N ≤ 1000
1 ≤ elements ≤ NPrint the missing elementSample Input:-
3
3 1
Sample Output:
2
Sample Input:-
5
1 4 5 2
Sample Output:-
3, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n-1];
for(int i=0;i<n-1;i++){
cin>>a[i];
}
sort(a,a+n-1);
for(int i=1;i<n;i++){
if(i!=a[i-1]){cout<<i<<endl;return 0;}
}
cout<<n;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers. You are also given an integer K.
Find the XOR of K<sup>th</sup> bit from the right in binary representation of all numbers.
(See example for more understanding).First line contains N and K.
Next line contains N space separated integers.
<b>constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 50
1 ≤ arr[i] ≤ 10<sup>18</sup>Output 0 or 1, denoting XOR of all bits at k<sup>th</sup> position from right.Input:
4 3
12 8 5 3
Output:
0
Explanation:
12 => 1100 => 3rd bit from right = 1
8 => 1000 => 3rd bit from right = 0
5 => 0101 => 3rd bit from right = 1
3 => 0011=> 3rd bit from right = 0
XOR = 1 ^ 0 ^ 1 ^ 0 = 0, I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
public static int kthBit(long x,int k){
while(k>1){
x >>= 1;
k--;
}
return (int)x&1;
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
int n=Integer.parseInt(in.next());
int k=Integer.parseInt(in.next());
int ans=0;
for(int i=0;i<n;i++){
long x = Long.parseLong(in.next());
ans ^= kthBit(x,k);
// out.println(ans);
}
out.print(ans);
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String args[])throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(st.nextToken());
boolean vis[]=new boolean[n+1];
int max=n;
StringBuilder sb=new StringBuilder();
for(int i=0;i<n;i++)
{
vis[a[i]]=true;
if(vis[max])
{
while(vis[max])
{
sb.append(max).append(" ");
max--;
}
}
sb.append("\n");
}
System.out.println(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: def Solve(arr):
size = len(arr)
queue = {}
for i in arr:
queue[i] = True
placed = []
if i == size:
placed.append(i)
size -= 1
while size in queue:
placed.append(size)
size -= 1
yield placed
N = int(input())
arr = list(map(int, input().split()))
out_ = Solve(arr)
for i_out_ in out_:
print(' '.join(map(str, i_out_))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is building a tower with N disks of sizes 1 to N.
The rules for building the tower are simple:-
*Every day you are provided with one disk having distinct size.
*The disk with larger sizes should be placed at the bottom of the tower.
*The disk with smaller sizes should be placed at the top of the tower.
*You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.
Your task is to print N lines denoting the disk sizes that can be put on the tower on the i<sup>th</sup> day.The first line of input contains a single integer N. The next line of input contains N space separated integers depicting the size of the discs.
Constraints:-
1 <= N <= 100000
1 <= Size <= NPrint N lines. In the ith line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.
If on the ith day no disks can be placed, then leave that line empty.Sample Input:-
5
4 5 1 2 3
Sample Output:-
5 4
3 2 1
Explanation
On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.
On the second day, the disk of size 5 will be given so now the disk of sizes 5 and 4 can be placed on the tower.
On the third and fourth days, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty.
On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.
Sample Input:-
3
3 2 1
Sample Output:-
3
2
1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
vector<vector<int> > SolveTower (int N, vector<int> a) {
// Write your code here
vector<vector<int> > ans;
int done = N;
priority_queue<int> pq;
for(auto x: a){
pq.push(x);
vector<int> aux;
while(!pq.empty() && pq.top() == done){
aux.push_back(done);
pq.pop();
done--;
}
ans.push_back(aux);
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
assert(a.size() == N);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
assert(1 <= N && N <= 1e6);
vector<int> a(N);
for(int i_a = 0; i_a < N; i_a++)
{
cin >> a[i_a];
}
vector<vector<int> > out_;
out_ = SolveTower(N, a);
for(int i = 0; i < out_.size(); i++){
for(int j = 0; j < out_[i].size(); j++){
cout << out_[i][j] << " ";
}
cout << "\n";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, find the maximum value of A<sub>i</sub> - A<sub>j</sub> over all i, j such that 1 <= i, j <= N.First line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print the maximum value of A<sub>i</sub> - A<sub>j</sub> over all i, j such that 1 <= i, j <= N.Sample Input:
5
6 7 4 5 2
Sample Output:
5
Explaination:
We take i = 2, j = 5. A<sub>2</sub> - A<sub>5</sub> = 7 - 2 = 5., I have written this Solution Code: n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
print(arr[n-1] - arr[0]), 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, find the maximum value of A<sub>i</sub> - A<sub>j</sub> over all i, j such that 1 <= i, j <= N.First line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print the maximum value of A<sub>i</sub> - A<sub>j</sub> over all i, j such that 1 <= i, j <= N.Sample Input:
5
6 7 4 5 2
Sample Output:
5
Explaination:
We take i = 2, j = 5. A<sub>2</sub> - A<sub>5</sub> = 7 - 2 = 5., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
// #define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long double ld;
const int mod = 1e9 + 7;
const int INF = 2e9;
void solve(){
int n;
cin >> n;
vector<int> a(n);
for(auto &i : a) cin >> i;
sort(all(a));
cout << a.back() - a[0];
}
signed main(){
fast
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
solve();
if(i != t) cout << endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an N-size array of unique integers, your task is to print the array in a waveform, i. e a1 >= a2 <= a3 >= a4 <= a5.. . print the lexicographically smallest array possible.The first line of input contains a single integer N, next line contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Arr[i] ≤ 10<sup>9</sup>Print the array in wave form as mentioned.Sample Input :-
5
2 1 3 5 4
Sample Output:-
2 1 4 3 5
Sample Input:-
3
1 2 3
Sample Output:-
2 1 3, 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());
StringTokenizer st = new StringTokenizer(read.readLine());
int[] arr = new int[N];
for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
StringBuilder res = new StringBuilder();
for(int i=0; i<N-1; i+=2) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
res.append(arr[i] + " ");
res.append(arr[i+1] + " ");
}
if(N % 2 != 0) {
res.append(arr[N-1]);
}
System.out.print(res);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an N-size array of unique integers, your task is to print the array in a waveform, i. e a1 >= a2 <= a3 >= a4 <= a5.. . print the lexicographically smallest array possible.The first line of input contains a single integer N, next line contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Arr[i] ≤ 10<sup>9</sup>Print the array in wave form as mentioned.Sample Input :-
5
2 1 3 5 4
Sample Output:-
2 1 4 3 5
Sample Input:-
3
1 2 3
Sample Output:-
2 1 3, I have written this Solution Code: n = int(input())
a = list(map(int,input().strip().split()))
a.sort()
for i in range(0,n-1,2):
print("{} ".format(a[i+1]),end="")
print("{} ".format(a[i]),end="")
if n&1:
print("{} ".format(a[n-1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an N-size array of unique integers, your task is to print the array in a waveform, i. e a1 >= a2 <= a3 >= a4 <= a5.. . print the lexicographically smallest array possible.The first line of input contains a single integer N, next line contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Arr[i] ≤ 10<sup>9</sup>Print the array in wave form as mentioned.Sample Input :-
5
2 1 3 5 4
Sample Output:-
2 1 4 3 5
Sample Input:-
3
1 2 3
Sample Output:-
2 1 3, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int n,m;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n-1;i+=2){
cout<<a[i+1]<<" ";
cout<<a[i]<<" ";
}
if(n&1){
cout<<a[n-1]<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the length of the longest substring without repeating characters.
<b>Note</b> : String contains spaces also.First Line contains the input of the string.
<b> Constraints </b>
1 <= string.length <= 50000
s consists of English letters, digits, symbols, and spaces.Print the length of the longest substring without repeating characters.Sample Input:
abcabcbb
Sample Output:
3
Explanation: The answer is "abc", with a length of 3., 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));
{
String s = read.readLine().trim();
Solution ob = new Solution();
System.out.println(ob.longestUniqueSubsttr(s));
}
}
}
class Solution{
int longestUniqueSubsttr(String str){
int maxans = Integer.MIN_VALUE;
Set < Character > set = new HashSet < > ();
int l = 0;
for (int r = 0; r < str.length(); r++)
{
if (set.contains(str.charAt(r)))
{
while (l < r && set.contains(str.charAt(r))) {
set.remove(str.charAt(l));
l++;
}
}
set.add(str.charAt(r));
maxans = Math.max(maxans, r - l + 1);
}
return maxans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the length of the longest substring without repeating characters.
<b>Note</b> : String contains spaces also.First Line contains the input of the string.
<b> Constraints </b>
1 <= string.length <= 50000
s consists of English letters, digits, symbols, and spaces.Print the length of the longest substring without repeating characters.Sample Input:
abcabcbb
Sample Output:
3
Explanation: The answer is "abc", with a length of 3., I have written this Solution Code: def lengthOfLongestSubstring(s):
ans = 0
sub = ''
for char in s:
if char not in sub:
sub += char
ans = max(ans, len(sub))
else:
cut = sub.index(char)
sub = sub[cut+1:] + char
return ans
s = input()
print(lengthOfLongestSubstring(s)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the length of the longest substring without repeating characters.
<b>Note</b> : String contains spaces also.First Line contains the input of the string.
<b> Constraints </b>
1 <= string.length <= 50000
s consists of English letters, digits, symbols, and spaces.Print the length of the longest substring without repeating characters.Sample Input:
abcabcbb
Sample Output:
3
Explanation: The answer is "abc", with a length of 3., I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-08 12:34:09
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
int main() {
string s;
getline(cin, s);
unordered_map<char, size_t> last_occurrence;
size_t starting_idx = 0, ans = 0;
for (size_t i = 0; i < s.size(); ++i) {
auto it(last_occurrence.find(s[i]));
if (it == last_occurrence.cend()) {
last_occurrence.emplace_hint(it, s[i], i);
} else {
if (it->second >= starting_idx) {
ans = max(ans, i - starting_idx);
starting_idx = it->second + 1;
}
it->second = i;
}
}
ans = max(ans, s.size() - starting_idx);
cout << ans << "\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: When learning a new language, we first learn to output some message. Here, we'll start with the famous "Hello World" message. There is no input, you just have to print "Hello World".No InputHello WorldExplanation:
Hello World is printed., I have written this Solution Code: a="Hello World"
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: When learning a new language, we first learn to output some message. Here, we'll start with the famous "Hello World" message. There is no input, you just have to print "Hello World".No InputHello WorldExplanation:
Hello World is printed., I have written this Solution Code: import java.util.*;
import java.io.*;
class Main{
public static void main(String args[]){
System.out.println("Hello World");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe 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 denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, 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 T= sc.nextInt();
for(int i=0;i<T;i++){
int arrsize=sc.nextInt();
int max=0,secmax=0,thirdmax=0,j;
for(int k=0;k<arrsize;k++){
j=sc.nextInt();
if(j>max){
thirdmax=secmax;
secmax=max;
max=j;
}
else if(j>secmax){
thirdmax=secmax;
secmax=j;
}
else if(j>thirdmax){
thirdmax=j;
}
if(k%10000==0){
System.gc();
}
}
System.out.println(max+" "+secmax+" "+thirdmax+" ");
}
}
}, 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, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe 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 denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: t=int(input())
while t>0:
t-=1
n=int(input())
l=list(map(int,input().strip().split()))
li=[0,0,0]
for i in l:
x=i
for j in range(0,3):
y=min(x,li[j])
li[j]=max(x,li[j])
x=y
print(li[0],end=" ")
print(li[1],end=" ")
print(li[2]), 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, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe 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 denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
vector<long> a(n);
long ans[3]={0};
long x,y;
for(int i=0;i<n;i++){
cin>>a[i];
x=a[i];
for(int j=0;j<3;j++){
y=min(x,ans[j]);
ans[j]=max(x,ans[j]);
// cout<<ans[j]<<" ";
x=y;
}
}
if(ans[1]<ans[0]){
swap(ans[1],ans[0]);
}
if(ans[2]<ans[1]){
swap(ans[1],ans[2]);
}
if(ans[1]<ans[0]){
swap(ans[1],ans[0]);
}
cout<<ans[2]<<" "<<ans[1]<<" "<<ans[0]<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe 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 denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: function maxNumbers(arr,n) {
// write code here
// do not console.log the answer
// return the answer as an array of 3 numbers
return arr.sort((a,b)=>b-a).slice(0,3)
};
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <p>Let us define the FizzBuzz sequence a<sub>1</sub>, a<sub>2</sub>,. . as follows:</p>
<ul>
<li>If both 3 and 5 divide i, a<sub>i</sub> = FizzBuzz. </li>
<li>If the above does not hold but 3 divides i, a<sub>i</sub> = Fizz. </li>
<li>If none of the above holds but 5 divides i, a<sub>i</sub> = Buzz. </li>
<li>If none of the above holds, a<sub>i</sub> = i. </li>
</ul>
<p>Write a program to find the sum of all numbers among the first N terms of the FizzBuzz sequence. </p><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>solve()</b> that takes an integer N as parameter.
<b>Constraints</b>
1<=N<=10<sup>6</sup>Return one integer - the sum of the numbers in FizzBuzz sequence.Sample Input:
15
Sample Output:
60
The first 15 terms of the sequence are 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz. So, their sum is 60., I have written this Solution Code:
class Solution {
public long solve(int N) {
long sum = 0;
for(int i = 1; i <= N; i++) {
if(i%3!=0 && i%5!=0)
sum+=i;
}
return sum;
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's Phone has N apps and each app takes K unit of memory. Now Sara wants to release M units of memory. Your task is to tell the minimum apps Sara needs to delete or say it is not possible.<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>Phone()</b> that takes integers N, K, and M as arguments.
Constraints:-
1 <= N <= 1000
1 <= K <= 100
0 <= M <= 10000Return minimum apps to delete and if it is not possible return -1.Sample Input:-
10 3 10
Sample Output:-
4
Sample Input:-
10 3 40
Sample Output:-
-1, I have written this Solution Code:
int Phone(int n, int k, int m){
if(n*k<m){
return -1;
}
int x = m/k;
if(m%k!=0){x++;}
return x;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's Phone has N apps and each app takes K unit of memory. Now Sara wants to release M units of memory. Your task is to tell the minimum apps Sara needs to delete or say it is not possible.<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>Phone()</b> that takes integers N, K, and M as arguments.
Constraints:-
1 <= N <= 1000
1 <= K <= 100
0 <= M <= 10000Return minimum apps to delete and if it is not possible return -1.Sample Input:-
10 3 10
Sample Output:-
4
Sample Input:-
10 3 40
Sample Output:-
-1, I have written this Solution Code:
int Phone(int n, int k, int m){
if(n*k<m){
return -1;
}
int x = m/k;
if(m%k!=0){x++;}
return x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's Phone has N apps and each app takes K unit of memory. Now Sara wants to release M units of memory. Your task is to tell the minimum apps Sara needs to delete or say it is not possible.<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>Phone()</b> that takes integers N, K, and M as arguments.
Constraints:-
1 <= N <= 1000
1 <= K <= 100
0 <= M <= 10000Return minimum apps to delete and if it is not possible return -1.Sample Input:-
10 3 10
Sample Output:-
4
Sample Input:-
10 3 40
Sample Output:-
-1, I have written this Solution Code: static int Phone(int n, int k, int m){
if(n*k<m){
return -1;
}
int x = m/k;
if(m%k!=0){x++;}
return x;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's Phone has N apps and each app takes K unit of memory. Now Sara wants to release M units of memory. Your task is to tell the minimum apps Sara needs to delete or say it is not possible.<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>Phone()</b> that takes integers N, K, and M as arguments.
Constraints:-
1 <= N <= 1000
1 <= K <= 100
0 <= M <= 10000Return minimum apps to delete and if it is not possible return -1.Sample Input:-
10 3 10
Sample Output:-
4
Sample Input:-
10 3 40
Sample Output:-
-1, I have written this Solution Code: def Phone(N,K,M):
if N*K < M :
return -1
x = M//K
if M%K!=0:
x=x+1
return x, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a Python class named Rectangle constructed by a length and width and a method that will compute the area of a rectangle.The first and only line contains the length and breadth of the rectangle.Prints the area of the rectangle.Sample Input:
2 3
Sample Output:
6
Explanation:
The area of a rectangle with length 2 and breadth 3 is 6., I have written this Solution Code: class Rectangle():
def __init__(self, l, w):
self.length = l
self.width = w
def rectangle_area(self):
return self.length*self.width
l, b = map(int,input().split())
newRectangle = Rectangle(l,b)
a=newRectangle.rectangle_area()
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: t=int(input())
while t!=0:
m,n=input().split()
m,n=int(m),int(n)
for i in range(m):
arr=input().strip()
if '1' in arr:
arr='1 '*n
else:
arr='0 '*n
print(arr)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.