Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given a Binary Tree, find diameter of it. The <b>diameter</b> of a tree is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).<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 getDiameter() that takes "root" node as parameter.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^4
1 <= node values <= 10^3
<b>Sum of "N" over all testcases does not exceed 10^5</b>
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 diameter of the tree.Sample Input:
2
3
1 2 3
5
10 20 30 40 60
Sample Output:
3
4
Explanation:
Test Case1: The tree is
1
/ \
2 3
The diameter is of 3 length.
Test Case2: The tree is
10
/ \
20 30
/ \
40 60
The diameter is of 4 length., I have written this Solution Code: static int dia = 0;
public static int util(Node root) {
if (root == null) return 0;
int l = util(root.left); // height of left subtree
int r = util(root.right); // height of right subtree
if (l + r + 1 > dia) dia = l + r + 1; // l+r+1 is a possible max dia
return 1 + Math.max(l, r); // returning height of subtree
}
public static int getDiameter(Node root) {
dia = 0;
util(root);
return dia;
}, 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:
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 a side of a square, your task is to calculate and print its area.The first line of the input contains the side of the square.
<b>Constraints:</b>
1 <= side <=100You just have to print the area of a squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36, I have written this Solution Code: def area(side_of_square):
print(side_of_square*side_of_square)
def main():
N = int(input())
area(N)
if __name__ == '__main__':
main(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a side of a square, your task is to calculate and print its area.The first line of the input contains the side of the square.
<b>Constraints:</b>
1 <= side <=100You just have to print the area of a squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int side = Integer.parseInt(br.readLine());
System.out.print(side*side);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: you have given two integer arrays A and B of length N sorted in non-decreasing order. Calculate the minimum possible difference between elements of A and B.
Solve this problem in O(N) complexity.first- line contains a single integer N
second and third lines contain N space- separated integer A[i] and B[i].Print the minimum absolute difference between two elements from A and B.Sample Input:
6
12 15 16 19 21 29
1 2 3 58 61 65
Sample Output:
9
Explanation : minimum absolute difference can be found between first element of first array and third element of second array., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N];
int[] B = new int[N];
for(int i = 0 ; i < N ; i++){
A[i] = sc.nextInt();
}
for(int i = 0 ; i < N ; i++){
B[i] = sc.nextInt();
}
int i = 0;
int j = 0;
int minPosDiff = Integer.MAX_VALUE;
while(i < N && j < N){
int diff = Math.abs(A[i] - B[j]);
minPosDiff = Math.min(minPosDiff , diff);
if(A[i] < B[j]){
i++;
}else{
j++;
}
}
System.out.println(minPosDiff);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: you have given two integer arrays A and B of length N sorted in non-decreasing order. Calculate the minimum possible difference between elements of A and B.
Solve this problem in O(N) complexity.first- line contains a single integer N
second and third lines contain N space- separated integer A[i] and B[i].Print the minimum absolute difference between two elements from A and B.Sample Input:
6
12 15 16 19 21 29
1 2 3 58 61 65
Sample Output:
9
Explanation : minimum absolute difference can be found between first element of first array and third element of second array., I have written this Solution Code: N = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
my_list = []
for i in range(N):
for j in range(len(l2)):
a = abs(l1[i] - l2[j])
my_list.append(a)
print(min(my_list)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: you have given two integer arrays A and B of length N sorted in non-decreasing order. Calculate the minimum possible difference between elements of A and B.
Solve this problem in O(N) complexity.first- line contains a single integer N
second and third lines contain N space- separated integer A[i] and B[i].Print the minimum absolute difference between two elements from A and B.Sample Input:
6
12 15 16 19 21 29
1 2 3 58 61 65
Sample Output:
9
Explanation : minimum absolute difference can be found between first element of first array and third element of second array., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int A[n],B[n],ans=INT_MAX;
for(int &i:A)cin>>i;
for(int &i:B)cin>>i;
int i=0,j=0;
while(i<n && j<n){
ans=min(ans,abs(A[i]-B[j]));
if(A[i]<B[j])i++;
else j++;
}
cout<<ans<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: int DivisorProblem(int N){
int ans=0;
while(N>1){
int cnt=2;
while(N%cnt!=0){
cnt++;
}
N/=cnt;
ans++;
}
return ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: def DivisorProblem(N):
ans=0
while N>1:
cnt=2
while N%cnt!=0:
cnt=cnt+1
N = N//cnt
ans=ans+1
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: static int DivisorProblem(int N){
int ans=0;
while(N>1){
int cnt=2;
while(N%cnt!=0){
cnt++;
}
N/=cnt;
ans++;
}
return ans;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is solving a math problem in which she has been given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DivisorProblem()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations required.Sample Input:-
100
Sample Output:-
4
Explanation:-
100 - > 50
50 - > 25
25 - > 5
5 - > 1
Sample Input:-
10
Sample Output:-
2, I have written this Solution Code: int DivisorProblem(int N){
int ans=0;
while(N>1){
int cnt=2;
while(N%cnt!=0){
cnt++;
}
N/=cnt;
ans++;
}
return ans;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a natural number N, your task is to print all the digits of the number in English words. The words have to separate by space and in lowercase English letters.<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>Print_Digit()</b> that takes integer N as a parameter.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>7</sup>Print the digits of the number as shown in the example.
<b>Note:-</b>
Print all digits in lowercase English lettersSample Input:-
1024
Sample Output:-
one zero two four
Sample Input:-
2
Sample Output:-
two, I have written this Solution Code: def Print_Digit(n):
dc = {1: "one", 2: "two", 3: "three", 4: "four",
5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 0: "zero"}
final_list = []
while (n > 0):
final_list.append(dc[int(n%10)])
n = int(n / 10)
for val in final_list[::-1]:
print(val, end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a natural number N, your task is to print all the digits of the number in English words. The words have to separate by space and in lowercase English letters.<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>Print_Digit()</b> that takes integer N as a parameter.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>7</sup>Print the digits of the number as shown in the example.
<b>Note:-</b>
Print all digits in lowercase English lettersSample Input:-
1024
Sample Output:-
one zero two four
Sample Input:-
2
Sample Output:-
two, I have written this Solution Code: class Solution {
public static void Print_Digits(int N){
if(N==0){return;}
Print_Digits(N/10);
int x=N%10;
if(x==1){System.out.print("one ");}
else if(x==2){System.out.print("two ");}
else if(x==3){System.out.print("three ");}
else if(x==4){System.out.print("four ");}
else if(x==5){System.out.print("five ");}
else if(x==6){System.out.print("six ");}
else if(x==7){System.out.print("seven ");}
else if(x==8){System.out.print("eight ");}
else if(x==9){System.out.print("nine ");}
else if(x==0){System.out.print("zero ");}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given a positive integer K and your task is to print the number of elements in array a[] that are smaller than or equal to K.<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>smallerElements()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N.
The second line of input contains N space- separated integers depicting the values of the array.
The third line of input contains a single integer Q, the number of queries.
Each of the next Q lines of input contain a single integer, the value of K.
<b>Constraints:-</b>
1 <= N <= 10<sup>5</sup>
1 <= K, Arr[i] <= 10<sup>12</sup>
1 <= Q <= 10<sup>4</sup>Return the count of elements smaller than or equal to K.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
1
1
3
0
5, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n;
cin>>n;
vector<int> v(n);
FOR(i,n){
cin>>v[i];}
int q;
cin>>q;
int x;
while(q--){
cin>>x;
auto it = upper_bound(v.begin(),v.end(),x);
out(it-v.begin());
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given a positive integer K and your task is to print the number of elements in array a[] that are smaller than or equal to K.<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>smallerElements()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N.
The second line of input contains N space- separated integers depicting the values of the array.
The third line of input contains a single integer Q, the number of queries.
Each of the next Q lines of input contain a single integer, the value of K.
<b>Constraints:-</b>
1 <= N <= 10<sup>5</sup>
1 <= K, Arr[i] <= 10<sup>12</sup>
1 <= Q <= 10<sup>4</sup>Return the count of elements smaller than or equal to K.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
1
1
3
0
5, I have written this Solution Code: static int smallerElements(int a[], int n, int k){
int l=0;
int h=n-1;
int m;
int ans=n;
while(l<=h){
m=l+h;
m/=2;
if(a[m]<=k){
l=m+1;
}
else{
h=m-1;
ans=m;
}
}
return ans;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculate inversion count of array of integers.
Inversion count of an array is quantisation of how much unsorted an array is. A sorted array has inversion count 0, while an unsorted array has maximum inversion count.
Formally speaking inversion count = number of pairs i, j such that i < j and a[i] > a[j].The first line contain integers N.
The second line of the input contains N singly spaces integers.
1 <= N <= 100000
1 <= A[i] <= 1000000000Output one integer the inversion count.Sample Input
5
1 1 3 2 2
Sample Output
2
Sample Input
5
5 4 3 2 1
Sample Output
10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static long count(int[] arr, int l, int h, int[] aux) {
if (l >= h) return 0;
int mid = (l +h) / 2;
long count = 0;
count += count(aux, l, mid, arr);
count += count(aux, mid + 1, h, arr);
count += merge(arr, l, mid, h, aux);
return count;
}
static long merge(int[] arr, int l, int mid, int h, int[] aux) {
long count = 0;
int i = l, j = mid + 1, k = l;
while (i <= mid || j <= h) {
if (i > mid) {
arr[k++] = aux[j++];
} else if (j > h) {
arr[k++] = aux[i++];
} else if (aux[i] <= aux[j]) {
arr[k++] = aux[i++];
} else {
arr[k++] = aux[j++];
count += mid + 1 - i;
}
}
return count;
}
public static void main (String[] args)throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String str[];
str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
str = br.readLine().split(" ");
int arr[] =new int[n];
for (int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(str[j]);
}
int[] aux = arr.clone();
System.out.print(count(arr, 0, n - 1, aux));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculate inversion count of array of integers.
Inversion count of an array is quantisation of how much unsorted an array is. A sorted array has inversion count 0, while an unsorted array has maximum inversion count.
Formally speaking inversion count = number of pairs i, j such that i < j and a[i] > a[j].The first line contain integers N.
The second line of the input contains N singly spaces integers.
1 <= N <= 100000
1 <= A[i] <= 1000000000Output one integer the inversion count.Sample Input
5
1 1 3 2 2
Sample Output
2
Sample Input
5
5 4 3 2 1
Sample Output
10, I have written this Solution Code: count=0
def implementMergeSort(arr,s,e):
global count
if e-s==1:
return
mid=(s+e)//2
implementMergeSort(arr,s,mid)
implementMergeSort(arr,mid,e)
count+=merge_sort_place(arr,s,mid,e)
return count
def merge_sort_place(arr,s,mid,e):
arr3=[]
i=s
j=mid
count=0
while i<mid and j<e:
if arr[i]>arr[j]:
arr3.append(arr[j])
j+=1
count+=(mid-i)
else:
arr3.append(arr[i])
i+=1
while (i<mid):
arr3.append(arr[i])
i+=1
while (j<e):
arr3.append(arr[j])
j+=1
for x in range(len(arr3)):
arr[s+x]=arr3[x]
return count
n=int(input())
arr=list(map(int,input().split()[:n]))
c=implementMergeSort(arr,0,len(arr))
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculate inversion count of array of integers.
Inversion count of an array is quantisation of how much unsorted an array is. A sorted array has inversion count 0, while an unsorted array has maximum inversion count.
Formally speaking inversion count = number of pairs i, j such that i < j and a[i] > a[j].The first line contain integers N.
The second line of the input contains N singly spaces integers.
1 <= N <= 100000
1 <= A[i] <= 1000000000Output one integer the inversion count.Sample Input
5
1 1 3 2 2
Sample Output
2
Sample Input
5
5 4 3 2 1
Sample Output
10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
long long _mergeSort(long long arr[], int temp[], int left, int right);
long long merge(long long arr[], int temp[], int left, int mid, int right);
/* This function sorts the input array and returns the
number of inversions in the array */
long long mergeSort(long long arr[], int array_size)
{
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function that sorts the input array and
returns the number of inversions in the array. */
long long _mergeSort(long long arr[], int temp[], int left, int right)
{
long long mid, inv_count = 0;
if (right > left) {
/* Divide the array into two parts and
call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of
inversions in left-part, right-part
and number of inversions in merging */
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left, mid + 1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
long long merge(long long arr[], int temp[], int left,
int mid, int right)
{
int i, j, k;
long long inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
/* this is tricky -- see above
explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;}
int main(){
int n;
cin>>n;
long long a[n];
for(int i=0;i<n;i++){
cin>>a[i];}
long long ans = mergeSort(a, n);
cout << ans; }
, In this Programming Language: C++, 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 a temperature F in Farenheit, your task is to convert it into Celsius using the following equation:-
T(°c) = (T(°f) - 32)*5/9You don't have to worry about the input, you just have to complete the function <b>fahrenheitToCelsius</b>
Constraints:-
-10^3 <= F <= 10^3
<b>Note:-</b> It is guaranteed that F - 32 will be a multiple of 9.Print an integer containing converted temperature in Fahrenheit.Sample Input 1:
77
Sample Output 1:
25
Sample Input 2:-
-40
Sample Output 2:-
-40
<b>Explanation 1</b>:
T(°c) = (T(°f) - 32)*5/9
T(°c) = (77-32)*5/9
T(°c) =25, I have written this Solution Code: void farhenheitToCelsius(int n){
n-=32;
n/=9;
n*=5;
cout<<n;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a temperature F in Farenheit, your task is to convert it into Celsius using the following equation:-
T(°c) = (T(°f) - 32)*5/9You don't have to worry about the input, you just have to complete the function <b>fahrenheitToCelsius</b>
Constraints:-
-10^3 <= F <= 10^3
<b>Note:-</b> It is guaranteed that F - 32 will be a multiple of 9.Print an integer containing converted temperature in Fahrenheit.Sample Input 1:
77
Sample Output 1:
25
Sample Input 2:-
-40
Sample Output 2:-
-40
<b>Explanation 1</b>:
T(°c) = (T(°f) - 32)*5/9
T(°c) = (77-32)*5/9
T(°c) =25, I have written this Solution Code: Fahrenheit= int(input())
Celsius = int(((Fahrenheit-32)*5)/9 )
print(Celsius), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a temperature F in Farenheit, your task is to convert it into Celsius using the following equation:-
T(°c) = (T(°f) - 32)*5/9You don't have to worry about the input, you just have to complete the function <b>fahrenheitToCelsius</b>
Constraints:-
-10^3 <= F <= 10^3
<b>Note:-</b> It is guaranteed that F - 32 will be a multiple of 9.Print an integer containing converted temperature in Fahrenheit.Sample Input 1:
77
Sample Output 1:
25
Sample Input 2:-
-40
Sample Output 2:-
-40
<b>Explanation 1</b>:
T(°c) = (T(°f) - 32)*5/9
T(°c) = (77-32)*5/9
T(°c) =25, I have written this Solution Code: static void farhrenheitToCelsius(int farhrenheit)
{
int celsius = ((farhrenheit-32)*5)/9;
System.out.println(celsius);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here, given user function has already code in it which may or may not be correct, now you need to figure out these and correct them if it is required</i>Print all the even numbers from 1 to n. (print all the numbers in the same line, space-separated)Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: def For_Loop(n):
string = ""
for i in range(1, n+1):
if i % 2 == 0:
string += "%s " % i
return string
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here, given user function has already code in it which may or may not be correct, now you need to figure out these and correct them if it is required</i>Print all the even numbers from 1 to n. (print all the numbers in the same line, space-separated)Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: public static void For_Loop(int n){
for(int i=2;i<=n;i+=2){
System.out.print(i+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two numbers m and n, multiply them using only "addition" operations.<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>Multiply()</b> that takes the integer M and N as a parameter.
Constraints:
1 ≤ T ≤ 100
0 ≤ M, N ≤ 100Return the product of N and M.Sample Input
2
2 3
3 4
Sample Output
6
12, I have written this Solution Code: static int Multiply(int n, int m)
{
if(n==0 || m==0){return 0;}
if (m == 1)
{ return n;}
return n + Multiply(n,m-1);
} , In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two numbers m and n, multiply them using only "addition" operations.<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>Multiply()</b> that takes the integer M and N as a parameter.
Constraints:
1 ≤ T ≤ 100
0 ≤ M, N ≤ 100Return the product of N and M.Sample Input
2
2 3
3 4
Sample Output
6
12, I have written this Solution Code: def multiply_by_recursion(n,m):
# Base Case
if n==0 or m==0:
return 0
# Recursive Case
if m==1:
return n
return n + multiply_by_recursion(n,m-1)
# Driver Code
t=int(input())
while t>0:
l=list(map(int,input().strip().split()))
n=l[0]
m=l[1]
print(multiply_by_recursion(n,m))
t=t-1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcase = Integer.parseInt(br.readLine());
for(int t=0;t<testcase;t++){
int num = Integer.parseInt(br.readLine().trim());
if(num==1)
System.out.println("No");
else if(num<=3)
System.out.println("Yes");
else{
if((num%2==0)||(num%3==0))
System.out.println("No");
else{
int flag=0;
for(int i=5;i*i<=num;i+=6){
if(((num%i)==0)||(num%(i+2)==0)){
System.out.println("No");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Yes");
}
}
}
}catch (Exception e) {
System.out.println("I caught: " + e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: t=int(input())
for i in range(t):
number = int(input())
if number > 1:
i=2
while i*i<=number:
if (number % i) == 0:
print("No")
break
i+=1
else:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, print whether that integer is a prime number or not.First line of input contains an integer T, showing the number of test cases. Every test case is a single integer A.
Constraints
1 <= T <= 100
1 <= A <= 10^8If the given integer is prime, print 'Yes', else print 'No'.Sample Input
3
5
9
13
Output
Yes
No
Yes, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n,k;
cin>>n;
long x=sqrt(n);
int cnt=0;
vector<int> v;
for(long long i=2;i<=x;i++){
if(n%i==0){
cout<<"No"<<endl;
goto f;
}}
cout<<"Yes"<<endl;
f:;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given a positive integer K and your task is to print the number of elements in array a[] that are smaller than or equal to K.<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>smallerElements()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N.
The second line of input contains N space- separated integers depicting the values of the array.
The third line of input contains a single integer Q, the number of queries.
Each of the next Q lines of input contain a single integer, the value of K.
<b>Constraints:-</b>
1 <= N <= 10<sup>5</sup>
1 <= K, Arr[i] <= 10<sup>12</sup>
1 <= Q <= 10<sup>4</sup>Return the count of elements smaller than or equal to K.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
1
1
3
0
5, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n;
cin>>n;
vector<int> v(n);
FOR(i,n){
cin>>v[i];}
int q;
cin>>q;
int x;
while(q--){
cin>>x;
auto it = upper_bound(v.begin(),v.end(),x);
out(it-v.begin());
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given a positive integer K and your task is to print the number of elements in array a[] that are smaller than or equal to K.<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>smallerElements()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N.
The second line of input contains N space- separated integers depicting the values of the array.
The third line of input contains a single integer Q, the number of queries.
Each of the next Q lines of input contain a single integer, the value of K.
<b>Constraints:-</b>
1 <= N <= 10<sup>5</sup>
1 <= K, Arr[i] <= 10<sup>12</sup>
1 <= Q <= 10<sup>4</sup>Return the count of elements smaller than or equal to K.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
1
1
3
0
5, I have written this Solution Code: static int smallerElements(int a[], int n, int k){
int l=0;
int h=n-1;
int m;
int ans=n;
while(l<=h){
m=l+h;
m/=2;
if(a[m]<=k){
l=m+1;
}
else{
h=m-1;
ans=m;
}
}
return ans;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given head which is a reference node to a doubly- linked list of characters. Complete the function <b> isPalin</b> which returns true if the doubly linked list of characters is palindrome otherwise return false.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b> isPalin</b> that takes the head of the linked list as parameter.
Constraints:
0 <= Number of nodes <= 10^5.Return true if the doubly linked list of characters is palindrome otherwise return false.Sample Input 1:-
6
098890
Sample Output 1:-
YES
Explanation
It's a palindrome as the first character is equal to last character and 2nd character is equal to second last character and so on.
Sample Input 2:-
2
10
Sample Output 2:-
NO
Explanation:
"10" is not a palindrome, I have written this Solution Code: a=int(input())
b=input()
if(b==b[::-1]):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given head which is a reference node to a doubly- linked list of characters. Complete the function <b> isPalin</b> which returns true if the doubly linked list of characters is palindrome otherwise return false.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b> isPalin</b> that takes the head of the linked list as parameter.
Constraints:
0 <= Number of nodes <= 10^5.Return true if the doubly linked list of characters is palindrome otherwise return false.Sample Input 1:-
6
098890
Sample Output 1:-
YES
Explanation
It's a palindrome as the first character is equal to last character and 2nd character is equal to second last character and so on.
Sample Input 2:-
2
10
Sample Output 2:-
NO
Explanation:
"10" is not a palindrome, I have written this Solution Code: static boolean isPalin( Node left)
{
if (left == null)
return true;
Node right = left;
while (right.next != null)
right = right.next;
while (left != right)
{
if (left.data != right.data)
return false;
left = left.next;
right = right.prev;
}
return true;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We are given a list Arr[] of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [freq, val] = [Arr[2*i], Arr[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a subarray. Concatenate all the subarray from left to right to generate the decompressed array.
Print the decompressed array.The input line contains T, denoting the number of testcases. Each testcase contains 2 lines. The first line contains size of array. Second line contains elements of array separated by space.
Note: size of input array is even
Constraints:
1 <= T <= 100
2 <= N <= 100
0 <= Arr[i] <= 100For each testcase you need to print the decompressed array in a new line.Sample Input:
2
4
1 2 3 4
4
1 1 2 3
Sample Output:
2 4 4 4
1 3 3
Explanation:
Testcase 1: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
At the end the concatenation [2] + [4,4,4] is [2,4,4,4]., I have written this Solution Code: n = int(input())
for _ in range(n):
nt = int(input())
l = [int(x) for x in input().split()];
for i in range(0,nt,2):
print((str(l[i+1]) + " ") * l[i],end="")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We are given a list Arr[] of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [freq, val] = [Arr[2*i], Arr[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a subarray. Concatenate all the subarray from left to right to generate the decompressed array.
Print the decompressed array.The input line contains T, denoting the number of testcases. Each testcase contains 2 lines. The first line contains size of array. Second line contains elements of array separated by space.
Note: size of input array is even
Constraints:
1 <= T <= 100
2 <= N <= 100
0 <= Arr[i] <= 100For each testcase you need to print the decompressed array in a new line.Sample Input:
2
4
1 2 3 4
4
1 1 2 3
Sample Output:
2 4 4 4
1 3 3
Explanation:
Testcase 1: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
At the end the concatenation [2] + [4,4,4] is [2,4,4,4]., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
int N = Integer.parseInt(read.readLine());
String str[] = read.readLine().trim().split(" ");
int arr[] = new int[N];
for(int i = 0; i < N; i++)
arr[i] = Integer.parseInt(str[i]);
int res[] = new int[decompress_EncodedArray(arr, N).length];
res = decompress_EncodedArray(arr, N);
//Collections.addAll(list, );
print(res);
System.out.println();
}
}
static void print(int list[])
{
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
public static int [] decompress_EncodedArray(int[] nums, int n) {
int[] intArr=null;
int opArrSize=0;
int opArrIndex=0;
for(int i=0; i<nums.length; i += 2)
{
opArrSize += nums[i];
}
intArr= new int[opArrSize];
for(int j=0; j<nums.length; j += 2)
{
int freq= nums[j];
int val = nums[j+1];
while(freq>0)
{
intArr[opArrIndex] = val;
freq--;
opArrIndex++;
}
}
return intArr;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We are given a list Arr[] of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [freq, val] = [Arr[2*i], Arr[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a subarray. Concatenate all the subarray from left to right to generate the decompressed array.
Print the decompressed array.The input line contains T, denoting the number of testcases. Each testcase contains 2 lines. The first line contains size of array. Second line contains elements of array separated by space.
Note: size of input array is even
Constraints:
1 <= T <= 100
2 <= N <= 100
0 <= Arr[i] <= 100For each testcase you need to print the decompressed array in a new line.Sample Input:
2
4
1 2 3 4
4
1 1 2 3
Sample Output:
2 4 4 4
1 3 3
Explanation:
Testcase 1: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
At the end the concatenation [2] + [4,4,4] is [2,4,4,4]., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i+=2){
while(a[i]>0){
cout<<a[i+1]<<" ";
a[i]--;
}
}
cout<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a rectangular tile of dimensions N*M, you have to divide the tile into three rectangular pieces such that the three pieces can be combined (without breaking any piece) to form the original tile. Let the area of the three pieces be a, b and c. You have to find the minimum value of (max(a, b, c) - min(a, b, c)).First Line of input contains two integers N and M.
Constraints
1 <= N, M <= 100000Output a single integer which is the minimum possible value of (max(a, b, c) - min(a, b, c)).Sample Input 1
3 5
Sample Output 1
0
Explaination : we can break tile into (1*5) (1*5) (1*5)
Sample Input 2
4 5
Sample Output 2
2
Explaination : we can break tile into (4*2) (2*3) (2*3), I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
if((n%3==0)||(m%3==0)) {
System.out.print(0);
} else {
int res[]=new int[3];
int max = Math.max(n,m);
int min = Math.min(n,m);
int lt;
if(max%3==2) {
lt=max/3+1;
} else {
lt=max/3;
}
res[2]=lt*min;
if((max-lt)%2==0||min%2==0) {
res[0]=((max-lt)*min)/2;
res[1]=((max-lt)*min)/2;
} else {
int tmax = Math.max(min,(max-lt));
int tmin = Math.min(min,(max-lt));
int tt=(tmax/2)+1;
res[0]=tt*tmin;
res[1]=(tmax-tt)*tmin;
}
Arrays.sort(res);
System.out.print(res[2]-res[0]);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a rectangular tile of dimensions N*M, you have to divide the tile into three rectangular pieces such that the three pieces can be combined (without breaking any piece) to form the original tile. Let the area of the three pieces be a, b and c. You have to find the minimum value of (max(a, b, c) - min(a, b, c)).First Line of input contains two integers N and M.
Constraints
1 <= N, M <= 100000Output a single integer which is the minimum possible value of (max(a, b, c) - min(a, b, c)).Sample Input 1
3 5
Sample Output 1
0
Explaination : we can break tile into (1*5) (1*5) (1*5)
Sample Input 2
4 5
Sample Output 2
2
Explaination : we can break tile into (4*2) (2*3) (2*3), I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define se second
#define fi first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
long long H, W;
cin >> H >> W;
long long ans1, ans2, ans3, ans4, ans5, ans6;
ans1=H*((W+2)/3-W/3);
ans2=W*((H+2)/3-H/3);
ans3 = max({(H+2)/3*W,(H-(H+2)/3)*(W/2),(H-(H+2)/3)*((W+1)/2)})
- min({(H+2)/3*W,(H-(H+2)/3)*(W/2),(H-(H+2)/3)*((W+1)/2)});
ans4 = max({H/3*W,(H-H/3)*(W/2),(H-H/3)*((W+1)/2)})
- min({H/3*W,(H-H/3)*(W/2),(H-H/3)*((W+1)/2)});
ans5 = max({(W+2)/3*H,(W-(W+2)/3)*(H/2),(W-(W+2)/3)*((H+1)/2)})
- min({(W+2)/3*H,(W-(W+2)/3)*(H/2),(W-(W+2)/3)*((H+1)/2)});
ans6 = max({W/3*H,(W-W/3)*(H/2),(W-W/3)*((H+1)/2)})
- min({W/3*H,(W-W/3)*(H/2),(W-W/3)*((H+1)/2)});
cout << min({ans1,ans2,ans3,ans4,ans5,ans6}) << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a year(an integer variable) as input, find if it is a leap year or not.
Note: Leap year is the year that is multiple of 4. But, multiples of 100 which are not multiples of 400 are not leap years.The input contains a single integer N
<b>Constraint:</b>
1 <= n <= 10<sup>4</sup>Print "YES" if the year is a leap year else print "NO".Sample Input:
2000
Sample Output:
YES
Sample Input:
2003
Sample Output:
NO
Sample Input:
1900
Sample Output:
NO, I have written this Solution Code:
function LeapYear(year){
// write code here
// return the output using return keyword
// do not use console.log here
if ((0 != year % 4) || ((0 == year % 100) && (0 != year % 400))) {
return 0;
} else {
return 1
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a year(an integer variable) as input, find if it is a leap year or not.
Note: Leap year is the year that is multiple of 4. But, multiples of 100 which are not multiples of 400 are not leap years.The input contains a single integer N
<b>Constraint:</b>
1 <= n <= 10<sup>4</sup>Print "YES" if the year is a leap year else print "NO".Sample Input:
2000
Sample Output:
YES
Sample Input:
2003
Sample Output:
NO
Sample Input:
1900
Sample Output:
NO, I have written this Solution Code: year = int(input())
if year % 4 == 0 and not year % 100 == 0 or year % 400 == 0:
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a year(an integer variable) as input, find if it is a leap year or not.
Note: Leap year is the year that is multiple of 4. But, multiples of 100 which are not multiples of 400 are not leap years.The input contains a single integer N
<b>Constraint:</b>
1 <= n <= 10<sup>4</sup>Print "YES" if the year is a leap year else print "NO".Sample Input:
2000
Sample Output:
YES
Sample Input:
2003
Sample Output:
NO
Sample Input:
1900
Sample Output:
NO, I have written this Solution Code: import java.util.Scanner;
class Main {
public static void main (String[] args)
{
//Capture the user's input
Scanner scanner = new Scanner(System.in);
//Storing the captured value in a variable
int side = scanner.nextInt();
int area = LeapYear(side);
if(area==1){
System.out.println("YES");}
else{
System.out.println("NO");}
}
static int LeapYear(int year){
if(year%400==0){return 1;}
if(year%100 != 0 && year%4==0){return 1;}
else {
return 0;}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Today is Newton School's first class of this year. Nutan, a student at Newton School, has received his first assignment. He will be given a string as input. His task is to print "Gravity'' if the input is "Apple''; otherwise, he will have to print "Space''.
Can you help Nutan in solving his first assignment? Note that the quotation marks are just for clarity. They are not part of the input string, and should not be a part of your output string.The input consists of a single line that contains a string S (1 ≤ length of S ≤ 10). The string only consists of lowercase and uppercase letters.Print "Gravity'' or "Space'' according to the input.Sample Input 1:
Apple
Sample Output 1:
Gravity
Sample Input 2:
Mango
Sample Output 2:
Space
Sample Input 3:
AppLE
Sample Output 3:
Space, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) {
FastReader sc= new FastReader();
String str= sc.nextLine();
String a="Apple";
if(a.equals(str)){
System.out.println("Gravity");
}
else{
System.out.println("Space");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Today is Newton School's first class of this year. Nutan, a student at Newton School, has received his first assignment. He will be given a string as input. His task is to print "Gravity'' if the input is "Apple''; otherwise, he will have to print "Space''.
Can you help Nutan in solving his first assignment? Note that the quotation marks are just for clarity. They are not part of the input string, and should not be a part of your output string.The input consists of a single line that contains a string S (1 ≤ length of S ≤ 10). The string only consists of lowercase and uppercase letters.Print "Gravity'' or "Space'' according to the input.Sample Input 1:
Apple
Sample Output 1:
Gravity
Sample Input 2:
Mango
Sample Output 2:
Space
Sample Input 3:
AppLE
Sample Output 3:
Space, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
//Work
int main()
{
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen ("INPUT.txt" , "r" , stdin);
//freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
//-----------------------------------------------------------------------------------------------------------//
string S;
cin>>S;
if(S=="Apple")
{
cout<<"Gravity"<<endl;
}
else
{
cout<<"Space"<<endl;
}
//-----------------------------------------------------------------------------------------------------------//
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Today is Newton School's first class of this year. Nutan, a student at Newton School, has received his first assignment. He will be given a string as input. His task is to print "Gravity'' if the input is "Apple''; otherwise, he will have to print "Space''.
Can you help Nutan in solving his first assignment? Note that the quotation marks are just for clarity. They are not part of the input string, and should not be a part of your output string.The input consists of a single line that contains a string S (1 ≤ length of S ≤ 10). The string only consists of lowercase and uppercase letters.Print "Gravity'' or "Space'' according to the input.Sample Input 1:
Apple
Sample Output 1:
Gravity
Sample Input 2:
Mango
Sample Output 2:
Space
Sample Input 3:
AppLE
Sample Output 3:
Space, I have written this Solution Code: n=input()
if n=='Apple':print('Gravity')
else:print('Space'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print out the integer N.You don't have to worry about the input, you just have to complete the function <b>printIntger()</b>
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>9</sup>Print the integer N.Sample Input:-
3
Sample Output:
3
Sample Input:
56
Sample Output:
56, I have written this Solution Code: static void printInteger(int N){
System.out.println(N);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print out the integer N.You don't have to worry about the input, you just have to complete the function <b>printIntger()</b>
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>9</sup>Print the integer N.Sample Input:-
3
Sample Output:
3
Sample Input:
56
Sample Output:
56, I have written this Solution Code: void printInteger(int x){
printf("%d", x);
}, 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 out the integer N.You don't have to worry about the input, you just have to complete the function <b>printIntger()</b>
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>9</sup>Print the integer N.Sample Input:-
3
Sample Output:
3
Sample Input:
56
Sample Output:
56, I have written this Solution Code: void printIntger(int n)
{
cout<<n;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print out the integer N.You don't have to worry about the input, you just have to complete the function <b>printIntger()</b>
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>9</sup>Print the integer N.Sample Input:-
3
Sample Output:
3
Sample Input:
56
Sample Output:
56, I have written this Solution Code: n=int(input())
print (n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a Singly linked list and an integer K. Your task is to insert the integer K at the head of the given linked list<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>addElement()</b> that takes head node and the element K as a parameter.
Constraints:
1 <=N<= 1000
1 <=K, value<= 1000Return the head of the modified linked listSample Input:-
5 2
1 2 3 4 5
Sample Output:
2 1 2 3 4 5
, I have written this Solution Code: public static Node addElement(Node head,int k) {
Node temp =new Node(k);
temp.next=head;
return temp;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono has gathered all the FRIENDS for a grand chapo event. For winning the chapo, she has asked the FRIENDS for the solution to a simple problem.
Our magical Tono has A apple candies and O orange candies. In one turn, she chooses an apple or an orange candy with equal probability, and turns it into the other type. Now, she asks the FRIENDS the expected number of turns she will follow this process until either the number of apple or the orange candies become 0.The first and the only line of input contains two integers A and O.
Constraints
1 <= A, O <= 10<sup>9</sup>Output the single line corresponding to the expected number of turns Tono needs to complete the process.
The answer is of the form p/q. Print it as (p*r)%1000000007 where r is modulo inverse of q with respect to 1000000007.Sample Input
1 2
Sample Output
2
Sample Input
0 5
Sample Output
0, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
static long mod = 1000000007;
public static void main(String[] args) throws IOException {
FastScanner s=new FastScanner();
int t1 =1;
while(t1-->0){
long a = s.nextLong();
long o = s.nextLong();
if(a==0 || o==0) System.out.println(0);
else System.out.println(a*(long)o%mod);
}
}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono has gathered all the FRIENDS for a grand chapo event. For winning the chapo, she has asked the FRIENDS for the solution to a simple problem.
Our magical Tono has A apple candies and O orange candies. In one turn, she chooses an apple or an orange candy with equal probability, and turns it into the other type. Now, she asks the FRIENDS the expected number of turns she will follow this process until either the number of apple or the orange candies become 0.The first and the only line of input contains two integers A and O.
Constraints
1 <= A, O <= 10<sup>9</sup>Output the single line corresponding to the expected number of turns Tono needs to complete the process.
The answer is of the form p/q. Print it as (p*r)%1000000007 where r is modulo inverse of q with respect to 1000000007.Sample Input
1 2
Sample Output
2
Sample Input
0 5
Sample Output
0, I have written this Solution Code: a,b =map(int, input().split())
print((a*b)%1000000007), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono has gathered all the FRIENDS for a grand chapo event. For winning the chapo, she has asked the FRIENDS for the solution to a simple problem.
Our magical Tono has A apple candies and O orange candies. In one turn, she chooses an apple or an orange candy with equal probability, and turns it into the other type. Now, she asks the FRIENDS the expected number of turns she will follow this process until either the number of apple or the orange candies become 0.The first and the only line of input contains two integers A and O.
Constraints
1 <= A, O <= 10<sup>9</sup>Output the single line corresponding to the expected number of turns Tono needs to complete the process.
The answer is of the form p/q. Print it as (p*r)%1000000007 where r is modulo inverse of q with respect to 1000000007.Sample Input
1 2
Sample Output
2
Sample Input
0 5
Sample Output
0, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
template<class C> void mini(C&a4, C b4){a4=min(a4,b4);}
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define mod 1000000007ll
#define pii pair<int,int>
/////////////
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int mo=1000000007;
int a,b;
cin>>a>>b;
cout<<(a*b)%mo;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<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 integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: def Pattern(N):
print('*')
for i in range (0,N-2):
print('*',end='')
for j in range (0,i+1):
print('^',end='')
print('*')
for i in range (0,N+1):
print('*',end='')
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<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 integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: void Pattern(int N){
cout<<'*'<<endl;
for(int i=0;i<N-2;i++){
cout<<'*';
for(int j=0;j<=i;j++){
cout<<'^';
}
cout<<'*'<<endl;
}
for(int i=0;i<=N;i++){
cout<<'*';
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<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 integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: static void Pattern(int N){
System.out.println('*');
for(int i=0;i<N-2;i++){
System.out.print('*');
for(int j=0;j<=i;j++){
System.out.print('^');
}System.out.println('*');
}
for(int i=0;i<=N;i++){
System.out.print('*');
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<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 integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: void Pattern(int N){
printf("*\n");
for(int i=0;i<N-2;i++){
printf("*");
for(int j=0;j<=i;j++){
printf("^");}printf("*\n");
}
for(int i=0;i<=N;i++){
printf("*");
}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: void pattern_making(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
for(int j=i-1;j>=1;j--){
cout<<j<<" ";
}
cout<<endl;
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
for(int j=i-1;j>=1;j--){
cout<<j<<" ";
}
cout<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code:
void pattern_making(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
for(int j=i-1;j>=1;j--){
printf("%d ",j);
}
printf("\n");
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
for(int j=i-1;j>=1;j--){
printf("%d ",j);
}
printf("\n");
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: public static void pattern_making(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
for(int j=i-1;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
for(int j=i-1;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: function patternMaking(N)
{
for(let j=1; j <= 2*N - 1; j++) {
let k;
if(j<= N) {
k = j;
} else {
k = 2*N - j;
}
let res = "";
for(let i=1; i<=2*k-1; i++) {
if(i<=k) {
res += i + " ";
} else {
res += 2*k - i + " ";
}
}
console.log(res);
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, your task is to print the pattern as shown in example:-
For n=5, the pattern is:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>pattern_making()</b> that takes the integer n as parameter.
Constraints:-
1 <= n <= 100Print the pattern as shown.Sample Input:-
5
Sample output:-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Sample Input:-
2
Sample Output:-
1
1 2 1
1, I have written this Solution Code: def pattern_making(n):
for i in range(1, n+1):
for j in range(1, i+1):
print(j,end=" ")
for j in range (1,i):
print(i-j,end=" ")
print("\r")
i=n-1
while i>=1 :
for j in range(1, i+1):
print(j,end=" ")
for j in range (1,i):
print(i-j,end=" ")
print("\r")
i=i-1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take an integer as input and print it.The first line contains integer as input.
<b>Constraints</b>
1 <= N <= 10Print the input integer in a single lineSample Input:-
2
Sample Output:-
2
Sample Input:-
4
Sample Output:-
4, I have written this Solution Code: /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Main
{
public static void printVariable(int variable){
System.out.println(variable);
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
printVariable(num);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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: You are given a cubic dice with 6 faces. All the individual faces have a numbers printed on them. The numbers are in the range of 1 to 6, like any ordinary dice.
Given a number on one face of the dice , you need to print the number on the opposite face .
NOTE : In a normal dice sum of numbers on opposite faces is 7 .The first line of the input contains a single integer T, denoting the number of test cases. Then T test case follows. Each test case contains a single line of the input containing a positive integer N.
Constraints:
1 <= T <= 100
1 <= N <= 6For each testcase, print the number that is on the opposite side of the given face.Input:
2
6
2
Output:
1
5
Explanation:
Testcase 1: For dice facing number 6 opposite face will have the number 1., I have written this Solution Code: def get_opposite_face(n):
return 7-n
t = int(input())
for n in range(t):
print(get_opposite_face(int(input()))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have a numbers printed on them. The numbers are in the range of 1 to 6, like any ordinary dice.
Given a number on one face of the dice , you need to print the number on the opposite face .
NOTE : In a normal dice sum of numbers on opposite faces is 7 .The first line of the input contains a single integer T, denoting the number of test cases. Then T test case follows. Each test case contains a single line of the input containing a positive integer N.
Constraints:
1 <= T <= 100
1 <= N <= 6For each testcase, print the number that is on the opposite side of the given face.Input:
2
6
2
Output:
1
5
Explanation:
Testcase 1: For dice facing number 6 opposite face will have the number 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 t; cin >> t;
while(t--){
int n; cin >> n;
cout << 7-n << 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 cubic dice with 6 faces. All the individual faces have a numbers printed on them. The numbers are in the range of 1 to 6, like any ordinary dice.
Given a number on one face of the dice , you need to print the number on the opposite face .
NOTE : In a normal dice sum of numbers on opposite faces is 7 .The first line of the input contains a single integer T, denoting the number of test cases. Then T test case follows. Each test case contains a single line of the input containing a positive integer N.
Constraints:
1 <= T <= 100
1 <= N <= 6For each testcase, print the number that is on the opposite side of the given face.Input:
2
6
2
Output:
1
5
Explanation:
Testcase 1: For dice facing number 6 opposite face will have the number 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));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
System.out.println(6-n+1);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Loki is one mischievous guy. He would always find different ways to make things difficult for everyone. After spending hours sorting a coded array of size N (in increasing order), you realise it’s been tampered with by none other than Loki. Like a clock, he has moved the array thus tampering the data. The task is to find the <b>minimum</b> element in it.The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
<b>Sum of "N" over all testcases does not exceed 10^5</b>Corresponding to each test case, in a new line, print the minimum element in the array.Input:
3
10
2 3 4 5 6 7 8 9 10 1
5
3 4 5 1 2
8
10 20 30 45 50 60 4 6
Output:
1
1
4
Explanation:
Testcase 1: The array is rotated once anti-clockwise. So minium element is at last index (n-1) which is 1.
Testcase 2: The array is rotated and the minimum element present is at index (n-2) which is 1.
Testcase 3: The array is rotated and the minimum element present is 4., I have written this Solution Code: def findMin(arr, low, high):
if high < low:
return arr[0]
if high == low:
return arr[low]
mid = int((low + high)/2)
if mid < high and arr[mid+1] < arr[mid]:
return arr[mid+1]
if mid > low and arr[mid] < arr[mid - 1]:
return arr[mid]
if arr[high] > arr[mid]:
return findMin(arr, low, mid-1)
return findMin(arr, mid+1, high)
T =int(input())
for i in range(T):
N = int(input())
arr = list(input().split())
for k in range(len(arr)):
arr[k] = int(arr[k])
print(findMin(arr,0,N-1)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Loki is one mischievous guy. He would always find different ways to make things difficult for everyone. After spending hours sorting a coded array of size N (in increasing order), you realise it’s been tampered with by none other than Loki. Like a clock, he has moved the array thus tampering the data. The task is to find the <b>minimum</b> element in it.The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
<b>Sum of "N" over all testcases does not exceed 10^5</b>Corresponding to each test case, in a new line, print the minimum element in the array.Input:
3
10
2 3 4 5 6 7 8 9 10 1
5
3 4 5 1 2
8
10 20 30 45 50 60 4 6
Output:
1
1
4
Explanation:
Testcase 1: The array is rotated once anti-clockwise. So minium element is at last index (n-1) which is 1.
Testcase 2: The array is rotated and the minimum element present is at index (n-2) which is 1.
Testcase 3: The array is rotated and the minimum element present is 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 = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = n+1;
if(a[1] < a[n]){
cout << a[1] << endl;
continue;
}
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] >= a[1])
l = m;
else
h = m;
}
cout << a[h] << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Loki is one mischievous guy. He would always find different ways to make things difficult for everyone. After spending hours sorting a coded array of size N (in increasing order), you realise it’s been tampered with by none other than Loki. Like a clock, he has moved the array thus tampering the data. The task is to find the <b>minimum</b> element in it.The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
<b>Sum of "N" over all testcases does not exceed 10^5</b>Corresponding to each test case, in a new line, print the minimum element in the array.Input:
3
10
2 3 4 5 6 7 8 9 10 1
5
3 4 5 1 2
8
10 20 30 45 50 60 4 6
Output:
1
1
4
Explanation:
Testcase 1: The array is rotated once anti-clockwise. So minium element is at last index (n-1) which is 1.
Testcase 2: The array is rotated and the minimum element present is at index (n-2) which is 1.
Testcase 3: The array is rotated and the minimum element present is 4., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main{
public static void main (String[] args) {
//code
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int j=0;j<t;j++){
int al = s.nextInt();
int a[] = new int[al];
for(int i=0;i<al;i++){
a[i] = s.nextInt();
}
binSearchSmallest(a);
}
}
public static void binSearchSmallest(int a[]) {
int s=0;
int e = a.length - 1;
int mid = 0;
while(s<=e){
mid = (s+e)/2;
if(a[s]<a[e]){
System.out.println(a[s]);
return;
}
if(a[mid]>=a[s]){
s=mid+1;
}
else{
e=mid;
}
if(s == e){
System.out.println(a[s]);
return;
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Loki is one mischievous guy. He would always find different ways to make things difficult for everyone. After spending hours sorting a coded array of size N (in increasing order), you realise it’s been tampered with by none other than Loki. Like a clock, he has moved the array thus tampering the data. The task is to find the <b>minimum</b> element in it.The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
<b>Sum of "N" over all testcases does not exceed 10^5</b>Corresponding to each test case, in a new line, print the minimum element in the array.Input:
3
10
2 3 4 5 6 7 8 9 10 1
5
3 4 5 1 2
8
10 20 30 45 50 60 4 6
Output:
1
1
4
Explanation:
Testcase 1: The array is rotated once anti-clockwise. So minium element is at last index (n-1) which is 1.
Testcase 2: The array is rotated and the minimum element present is at index (n-2) which is 1.
Testcase 3: The array is rotated and the minimum element present is 4., I have written this Solution Code: // arr is the input array
function findMin(arr,n) {
// write code here
// do not console.log
// return the number
let min = arr[0]
for(let i=1;i<arr.length;i++){
if(min > arr[i]){
min = arr[i]
}
}
return min
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. The array contains almost distinct elements with some duplicated. You have to print the elements in sorted order which appears more than once.The first line of input contains T, denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line of input contains size of array N. The second line contains N integers separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= A<sub>i</sub> <= 100000
Sum of N over all test cases do not exceed 100000For each test case, in a new line, print the required answer in separate line. If there are no duplicates print -1.Input:
1
9
3 4 5 7 8 1 2 1 3
Output:
1 3
Explanation : both 1,3 appeared 2 times
Input
1
5
1 2 3 4 5
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));
int t=Integer.parseInt(br.readLine());
String nums[];
int n;
TreeMap<Integer,Integer> tm=null;
int m,p;
boolean flag;
for(int a=0;a<t;++a)
{
n=Integer.parseInt(br.readLine());
nums=br.readLine().split("\\s");
tm=new TreeMap<>();
for(int i=0;i<n;++i)
{
p=Integer.parseInt(nums[i]);
tm.put(p,tm.getOrDefault(p,0)+1);
}
flag=false;
for(Integer i:tm.keySet())
{
m=tm.get(i);
if(m>1)
{
System.out.print(i+" ");
flag=true;
}
}
if(!flag)
{
System.out.println(-1);
}
else System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. The array contains almost distinct elements with some duplicated. You have to print the elements in sorted order which appears more than once.The first line of input contains T, denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line of input contains size of array N. The second line contains N integers separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= A<sub>i</sub> <= 100000
Sum of N over all test cases do not exceed 100000For each test case, in a new line, print the required answer in separate line. If there are no duplicates print -1.Input:
1
9
3 4 5 7 8 1 2 1 3
Output:
1 3
Explanation : both 1,3 appeared 2 times
Input
1
5
1 2 3 4 5
Output
-1
, I have written this Solution Code: import numpy as np
from collections import defaultdict
t=int(input())
def solve():
d=defaultdict(int)
n=int(input())
a=np.array([input().strip().split()],int).flatten()
for i in a:
d[i]+=1
d=sorted(d.items())
c=0
for i in d:
if(i[1]>1):
c=1
print(i[0],end=" ")
if(c==0):
print(-1,end=" ")
while(t>0):
solve()
print()
t-=1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. The array contains almost distinct elements with some duplicated. You have to print the elements in sorted order which appears more than once.The first line of input contains T, denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line of input contains size of array N. The second line contains N integers separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= A<sub>i</sub> <= 100000
Sum of N over all test cases do not exceed 100000For each test case, in a new line, print the required answer in separate line. If there are no duplicates print -1.Input:
1
9
3 4 5 7 8 1 2 1 3
Output:
1 3
Explanation : both 1,3 appeared 2 times
Input
1
5
1 2 3 4 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
signed main()
{
int t;
cin>>t;
while(t>0)
{
t--;
int n,m;
cin>>n;
map<int,int> A;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
A[a]++;
}
int ch=1;
int cnt=0;
for(auto it:A)
{
if(it.se>1){ cout<<it.fi<<" "; cnt++;}
}
if(cnt==0) cout<<-1;
cout<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<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>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code: def Average(A,B,C):
return (A+B+C)//3
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<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>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code:
int Average(int A,int B, int C){
return (A+B+C)/3;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<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>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code:
static int Average(int A,int B, int C){
return (A+B+C)/3;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<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>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code:
int Average(int A,int B, int C){
return (A+B+C)/3;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A string is called AB string if it's of length at least 2 and all it's characters are A except the last character which is B.
You have an empty string s, you can do the following operation any number of times.
Choose any position of s and insert any AB string in that position.
You are given a string t of some length containing only A and B. Find out if it is possible to convert s to t after some operations.First line contains N denoting the length of t.
Next line contains t.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
t contains only A and B.Output "YES" or "NO".Input:
5
AAABB
Output:
YES
Explanation :
"" => "AAB" => "AA "AB" B" => "AAABB"
Input:
3
ABB
Output:
NO
, I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
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());
String s=in.next();
int cnt=0;
int f=0;
for(int i=n-1;i>=0;i--){
if(s.charAt(i) == 'A'){
if(cnt == 0){
if(f == 0){
f=-1;
break;
}
}
else cnt--;
}
else{
cnt++;
f=1;
}
}
if(f == -1 || cnt != 0){
out.print("NO");
}
else out.print("YES");
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: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String arr[][]=new String[n][n];
String transpose[][]=new String[n][n];
int row;
int cols;
for(row=0;row<n;row++)
{
String rowNum=br.readLine();
String rowVals[]=rowNum.split(" ");
for(cols=0; cols<n;cols++)
{
arr[row][cols]=rowVals[cols];
}
}
for(row=0;row<n;row++)
{
for(cols=0; cols<n;cols++)
{
transpose[row][cols]=arr[cols][row];
System.out.print(transpose[row][cols]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: x=int(input())
l1=[]
for i in range(x):
a1=list(map(int,input().split()))
l1.append(a1)
l4=[]
for j in range(x):
l3=[]
for i in range(x):
l3.append(l1[i][j])
l4.append(l3)
for i in range(x):
for j in range(x):
print(l4[i][j], end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[j][i];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of length N in which you can swap two elements if their sum is odd i.e for every i (1 to N) and j (1 to N) if (Arr[i] + Arr[j]) is odd then you can swap these elements.
What is the lexicographically smallest array you can obtain?First line of input contains a single integer N. Next line contains N space separated integers depicting the elements of the array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print N space separated elements i. e the array which is the lexicographically smallest possibleSample Input:-
3
4 1 7
Sample Output:-
1 4 7
Explanation:-
Swap numbers at indices 1 and 2 as their sum 4 + 1 = 5 is odd
Sample Input:-
2
2 4
Sample Output:-
2 4
Sample Input:-
2
5 3
Sample Output:-
5 3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String array[] = br.readLine().trim().split(" ");
StringBuilder sb = new StringBuilder("");
int[] arr = new int[n];
int oddCount = 0,
evenCount = 0;
for(int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(array[i]);
if(arr[i] % 2 == 0)
++evenCount;
else
++oddCount;
}
if(evenCount > 0 && oddCount > 0)
Arrays.sort(arr);
for(int i = 0; i < n; i++)
sb.append(arr[i] + " ");
System.out.println(sb.substring(0, sb.length() - 1));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of length N in which you can swap two elements if their sum is odd i.e for every i (1 to N) and j (1 to N) if (Arr[i] + Arr[j]) is odd then you can swap these elements.
What is the lexicographically smallest array you can obtain?First line of input contains a single integer N. Next line contains N space separated integers depicting the elements of the array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print N space separated elements i. e the array which is the lexicographically smallest possibleSample Input:-
3
4 1 7
Sample Output:-
1 4 7
Explanation:-
Swap numbers at indices 1 and 2 as their sum 4 + 1 = 5 is odd
Sample Input:-
2
2 4
Sample Output:-
2 4
Sample Input:-
2
5 3
Sample Output:-
5 3, I have written this Solution Code: n=int(input())
p=[int(x) for x in input().split()[:n]]
o=0;e=0
for i in range(n):
if (p[i] % 2 == 1):
o += 1;
else:
e += 1;
if (o > 0 and e > 0):
p.sort();
for i in range(n):
print(p[i], end = " ");, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of length N in which you can swap two elements if their sum is odd i.e for every i (1 to N) and j (1 to N) if (Arr[i] + Arr[j]) is odd then you can swap these elements.
What is the lexicographically smallest array you can obtain?First line of input contains a single integer N. Next line contains N space separated integers depicting the elements of the array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print N space separated elements i. e the array which is the lexicographically smallest possibleSample Input:-
3
4 1 7
Sample Output:-
1 4 7
Explanation:-
Swap numbers at indices 1 and 2 as their sum 4 + 1 = 5 is odd
Sample Input:-
2
2 4
Sample Output:-
2 4
Sample Input:-
2
5 3
Sample Output:-
5 3, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
long long a[n];
bool win=false,win1=false;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]&1){win=true;}
if(a[i]%2==0){win1=true;}
}
if(win==true && win1==true){sort(a,a+n);}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
, 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 all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here, given user function has already code in it which may or may not be correct, now you need to figure out these and correct them if it is required</i>Print all the even numbers from 1 to n. (print all the numbers in the same line, space-separated)Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: def For_Loop(n):
string = ""
for i in range(1, n+1):
if i % 2 == 0:
string += "%s " % i
return string
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here, given user function has already code in it which may or may not be correct, now you need to figure out these and correct them if it is required</i>Print all the even numbers from 1 to n. (print all the numbers in the same line, space-separated)Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: public static void For_Loop(int n){
for(int i=2;i<=n;i+=2){
System.out.print(i+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have been given a number N you have to print the number from 1 to N except for the following conditions:-
If the number is a multiple of 3 print "Coca" instead of the number
If the number is a multiple of 5 print "Cola" instead of the number
If the number is a multiple of both 3 and 5 print "CocaCola" instead of the number
<b>Example</b>
A number 5 is given to you. From 1 to 5, it contains 3 and 5 which are multiple of 3 and 5 respectively so they are printed as Coca and Cola respectively. Rest all the numbers i.e 1,2 and 4 are printed as it is.
Hence the output will be:
1 2 Coca 4 ColaThe input contains a single integer N.
<b>Constraints:- </b>
1 ≤ N ≤ 1000Print N space-separated number or Coca Cola according to the condition.Sample Input:-
3
Sample Output:-
1 2 Coca
Sample Input:-
5
Sample Output:-
1 2 Coca 4 Cola, 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 x= sc.nextInt();
cocacola(x);
}
static void cocacola(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.print("CocaCola ");}
else if(i%5==0){System.out.print("Cola ");}
else if(i%3==0){System.out.print("Coca ");}
else{System.out.print(i+" ");}
}
}
}, 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.
There are no self-loops or multiple edges.The first line contains two integers N and E, denoting the number of nodes and number of edges in the graph respectively. Next E lines contain two integers u and v, denoting an edge from u to v
Constraints
1 <= N, E <= 1000
0 <= u, v <= N-1
u != vPrint 1 if topological sort can be done correctly. Otherwise, print 0.Sample Input 1:
4 5
0 1
1 2
2 3
3 0
0 2
Sample Output 1:
0
Sample Input 2:
4 3
0 1
1 2
2 3
Sample Output 2:
1
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static ArrayList<ArrayList<Integer>> adj;
static void graph(int v){
adj =new ArrayList<>();
for(int i=0;i<=v;i++) adj.add(new ArrayList<Integer>());
}
static void addedge(int u, int v){
adj.get(u).add(v);
}
static boolean dfs(int i, boolean [] vis, int parent[]){
vis[i] = true; parent[i] = 1;
for(Integer it: adj.get(i)){
if(vis[it]==false) {
if(dfs(it, vis, parent)==true) return true;
}
else if(parent[it]==1) return true;
}
parent[i] = 0;
return false;
}
static boolean helper(int n){
boolean vis[] = new boolean [n+1];
int parent[] = new int[n+1];
int i=1;
if(vis[i]==false){
if(dfs(i, vis, parent)) return true;
}
return false;
}
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String t[] = br.readLine().split(" ");
int n = Integer.parseInt(t[0]);
graph(n);
int e = Integer.parseInt(t[1]);
for(int i=0;i<e;i++) {
String num[] = br.readLine().split(" ");
int u = Integer.parseInt(num[0]);
int v = Integer.parseInt(num[1]);
addedge(u, v);
}
if(helper(n)) System.out.println("0");
else System.out.println("1");
}
}, 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.
There are no self-loops or multiple edges.The first line contains two integers N and E, denoting the number of nodes and number of edges in the graph respectively. Next E lines contain two integers u and v, denoting an edge from u to v
Constraints
1 <= N, E <= 1000
0 <= u, v <= N-1
u != vPrint 1 if topological sort can be done correctly. Otherwise, print 0.Sample Input 1:
4 5
0 1
1 2
2 3
3 0
0 2
Sample Output 1:
0
Sample Input 2:
4 3
0 1
1 2
2 3
Sample Output 2:
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;
vector<int> g[N];
int vis[N];
bool flag = 0;
void dfs(int u, int p){
vis[u] = 1;
for(auto i: g[u]){
if(i == p) continue;
if(vis[i] == 1)
flag = 1;
if(vis[i] == 0) dfs(i, u);
}
vis[u] = 2;
}
signed main() {
IOS;
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
}
for(int i = 0; i < n; i++){
if(vis[i]) continue;
dfs(i, n);
}
if(!flag)
cout << 1;
else
cout << 0;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tom has N sticks that are distinguishable from each other. The length of the i-th stick is L[i]. He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:
a < b+c
b < c+a
c < a+b
How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.The first line of input contains the number of sticks N, the next line contain N space separated integer where the ith integer shows the length of ith stick.
Constraints:-
3 ≤ N ≤ 1000
1 ≤ L[i] ≤ 1000Print the number of different triangles that can be formed.Sample Input 1
4
3 4 2 1
Sample Output 1
1
Sample Input 2
3
1 1000 1
Sample Output 2
0
Explanation:
For sample test case 1 the only triangle that can be formed is 2 3 4
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
long cnt=0;
for(int i=0;i<n-2;i++){
int j=i+1;
int k=i+2;
while(k!=n){
if(a[j]+a[i]<=a[k]){if(j>=k-1){k++;}
else{j++;}
}
else{
cnt+=(k-j);
k++;
}
}
}
cout<<cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tom has N sticks that are distinguishable from each other. The length of the i-th stick is L[i]. He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:
a < b+c
b < c+a
c < a+b
How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.The first line of input contains the number of sticks N, the next line contain N space separated integer where the ith integer shows the length of ith stick.
Constraints:-
3 ≤ N ≤ 1000
1 ≤ L[i] ≤ 1000Print the number of different triangles that can be formed.Sample Input 1
4
3 4 2 1
Sample Output 1
1
Sample Input 2
3
1 1000 1
Sample Output 2
0
Explanation:
For sample test case 1 the only triangle that can be formed is 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();
System.out.println(countTri(arr, arrSize));
}
static long countTri(int arr[], int arrSize)
{
Arrays.sort(arr);
long cnt=0;
for(int i=0;i<arrSize-2;i++)
{
int j=i+1;
int k=i+2;
while(k!=arrSize){
if(arr[j]+arr[i]<=arr[k]){if(j>=k-1){k++;}
else{j++;}
}
else{
cnt+=(k-j);
k++;
}
}
}
return cnt;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N containing 0s and 1s only. The task is to count the subarrays having an equal number of 0s and 1s.The first line of the input contains an integer N denoting the size of the array and the second line contains N space-separated 0s and 1s.
Constraints:-
1 <= N <= 10^6
0 <= A[i] <= 1For each test case, print the count of required sub-arrays in new line.Sample Input
7
1 0 0 1 0 1 1
Sample Output
8
The index range for the 8 sub-arrays are:
(0, 1), (2, 3), (0, 3), (3, 4), (4, 5)
(2, 5), (0, 5), (1, 6), I have written this Solution Code: size = int(input())
givenList = list(map(int,input().split()))
hs = {}
sm = 0
ct = 0
for i in givenList:
if i == 0:
i = -1
sm = sm + i
if sm == 0:
ct += 1
if sm not in hs.keys():
hs[sm] = 1
else:
freq = hs[sm]
ct = ct +freq
hs[sm] = freq + 1
print(ct), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.