Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
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: Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
Try to solve with and without reduce function.An array containing numbersSum of all the odd numbers in that arraySample Input:-
1 3 5 4
Sample output:-
9
Explanation-
1+3+5=9, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static int odd_ball( ArrayList<Integer> myNumbers){
int res=0;
for (int i : myNumbers){
if(i%2!=0){
res+=i;
}
}
return res;
}
public static void main (String[] args) {
Scanner sc= new Scanner(System.in);
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
while (sc.hasNextInt()) {
int i = sc.nextInt();
myNumbers.add(i);
}
System.out.print( odd_ball(myNumbers));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
Try to solve with and without reduce function.An array containing numbersSum of all the odd numbers in that arraySample Input:-
1 3 5 4
Sample output:-
9
Explanation-
1+3+5=9, I have written this Solution Code:
arr=list(map(int,input().split()))
Sum=0
for i in range(len(arr)):
if arr[i]%2!=0:
Sum+=arr[i]
print(Sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
Try to solve with and without reduce function.An array containing numbersSum of all the odd numbers in that arraySample Input:-
1 3 5 4
Sample output:-
9
Explanation-
1+3+5=9, I have written this Solution Code: function oddball_sum(nums) {
// final count of all odd numbers added up
let final_count = 0;
// loop through entire list
for (let i = 0; i < nums.length; i++) {
// we divide by 2, and if there is a remainder then
// the number must be odd so we add it to final_count
if (nums[i] % 2 === 1) {
final_count += nums[i]
}
}
console.log(final_count);
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a sequence of numbers of size N. You have to find if there is a way to insert + or - operator in between the numbers so that the result equals K.The first line of input contains two integers N and K. The next line of input contains N space- separated integers depicting the values of the sequence.
Constraints:-
1 <= N <= 20
-10^15 <= K <= 10^15
0 <= Numbers <=10^13Print YES if possible else print NO.Sample Input:-
4 4
1 2 3 4
Sample Output:-
YES
Sample Input:-
4 1
1 2 3 4
Sample Output:-
NO, I have written this Solution Code:
import java.io.*;
import java.util.*;
class Main {
public static boolean isArrangementPossible(long arr[],int n,long sum){
if(n==1){
if(arr[0]==sum)
return true;
else
return false;
}
return(isArrangementPossible(arr,n-1,sum-arr[n-1]) || isArrangementPossible(arr,n-1,sum+arr[n-1]));
}
public static void main (String[] args) throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str1[]=br.readLine().trim().split(" ");
int n=Integer.parseInt(str1[0]);
long sum=Long.parseLong(str1[1]);
String str[]=br.readLine().trim().split(" ");
long arr[]=new long[n];
for(int i=0;i<n;i++){
arr[i]=Long.parseLong(str[i]);
}
if(isArrangementPossible(arr,n,sum)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a sequence of numbers of size N. You have to find if there is a way to insert + or - operator in between the numbers so that the result equals K.The first line of input contains two integers N and K. The next line of input contains N space- separated integers depicting the values of the sequence.
Constraints:-
1 <= N <= 20
-10^15 <= K <= 10^15
0 <= Numbers <=10^13Print YES if possible else print NO.Sample Input:-
4 4
1 2 3 4
Sample Output:-
YES
Sample Input:-
4 1
1 2 3 4
Sample Output:-
NO, I have written this Solution Code: def checkIfGivenTargetIsPossible(nums,currSum,i,targetSum):
if i == len(nums):
if currSum == targetSum:
return 1
return 0
if(checkIfGivenTargetIsPossible(nums,currSum + nums[i],i+1,targetSum)):
return 1
return checkIfGivenTargetIsPossible(nums,currSum - nums[i], i+1,targetSum)
n,k = map(int,input().split())
nums = list(map(int,input().split()))
if(checkIfGivenTargetIsPossible(nums,0,0,k)):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a sequence of numbers of size N. You have to find if there is a way to insert + or - operator in between the numbers so that the result equals K.The first line of input contains two integers N and K. The next line of input contains N space- separated integers depicting the values of the sequence.
Constraints:-
1 <= N <= 20
-10^15 <= K <= 10^15
0 <= Numbers <=10^13Print YES if possible else print NO.Sample Input:-
4 4
1 2 3 4
Sample Output:-
YES
Sample Input:-
4 1
1 2 3 4
Sample Output:-
NO, I have written this Solution Code: #include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define int long long
int k;
using namespace std;
int solve(int n, int a[], int i, int curr ){
if(i==n){
if(curr==k){return 1;}
return 0;
}
if(solve(n,a,i+1,curr+a[i])==1){return 1;}
return solve(n,a,i+1,curr-a[i]);
}
signed main() {
int n;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
if(solve(n,a,1,a[0])){
cout<<"YES";}
else{
cout<<"NO";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to become rich so he came up with an idea, So, he buys some gadgets from the future at a price of C and sells them at a price of S to his friends. Now Nobita wants to know how much he gains by selling all gadget. As we all know Nobita is weak in maths help him to find the profit he getsYou don't have to worry about the input, you just have to complete the function <b>Profit()</b>
<b>Constraints:-</b>
1 <= C <= S <= 1000Print the profit Nobita gets from selling one gadget.Sample Input:-
3 5
Sample Output:-
2
Sample Input:-
9 16
Sample Output:-
7, I have written this Solution Code: def profit(C, S):
print(S - C), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to become rich so he came up with an idea, So, he buys some gadgets from the future at a price of C and sells them at a price of S to his friends. Now Nobita wants to know how much he gains by selling all gadget. As we all know Nobita is weak in maths help him to find the profit he getsYou don't have to worry about the input, you just have to complete the function <b>Profit()</b>
<b>Constraints:-</b>
1 <= C <= S <= 1000Print the profit Nobita gets from selling one gadget.Sample Input:-
3 5
Sample Output:-
2
Sample Input:-
9 16
Sample Output:-
7, I have written this Solution Code: static void Profit(int C, int S){
System.out.println(S-C);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Internet download speed is often expressed in bit per second whereas file size is expressed in Bytes. It is known that 1 Byte = 8 bits. Given file size in megabytes(MB) and internet speed in megabits per seconds(Mbps), find the time taken in seconds to download the file.The only line of input contains two integers denoting the file size in MB and download speed in Mbps.
1 <= file size <= 1000
1 <= download speed <= 1000Print a single integer denoting the time taken to download the file in seconds.
It is guaranteed that the result will be an integer.Sample Input:
10 16
Sample Output:
5, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
int f, s;
cin >> f >> s;
cout << (8*f)/s << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Internet download speed is often expressed in bit per second whereas file size is expressed in Bytes. It is known that 1 Byte = 8 bits. Given file size in megabytes(MB) and internet speed in megabits per seconds(Mbps), find the time taken in seconds to download the file.The only line of input contains two integers denoting the file size in MB and download speed in Mbps.
1 <= file size <= 1000
1 <= download speed <= 1000Print a single integer denoting the time taken to download the file in seconds.
It is guaranteed that the result will be an integer.Sample Input:
10 16
Sample Output:
5, 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 f = sc.nextInt();
int s = sc.nextInt();
int ans = (8*f)/s;
System.out.print(ans);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Internet download speed is often expressed in bit per second whereas file size is expressed in Bytes. It is known that 1 Byte = 8 bits. Given file size in megabytes(MB) and internet speed in megabits per seconds(Mbps), find the time taken in seconds to download the file.The only line of input contains two integers denoting the file size in MB and download speed in Mbps.
1 <= file size <= 1000
1 <= download speed <= 1000Print a single integer denoting the time taken to download the file in seconds.
It is guaranteed that the result will be an integer.Sample Input:
10 16
Sample Output:
5, I have written this Solution Code: a = list(map(int,input().strip().split()))[:2]
print(int((a[0]*8)/a[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list consisting of N nodes and an integer K, your task is to delete the Kth node from the end of the 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>deleteElement()</b> that takes head node and K as parameter.
Constraints:
1 <=K<=N<= 1000
1 <=Node.data<= 1000Return the head of the modified linked listInput 1:
5 3
1 2 3 4 5
Output 1:
1 2 4 5
Explanation:
After deleting 3rd node from the end of the linked list, 3 will be deleted and the list will be as 1, 2, 4, 5.
Input 2:-
5 5
8 1 8 3 6
Output 2:-
1 8 3 6 , I have written this Solution Code: public static Node deleteElement(Node head,int k) {
int cnt=0;
Node temp=head;
while(temp!=null){
cnt++;
temp=temp.next;
}
temp=head;
k=cnt-k;
if(k==0){head=head.next; return head;}
temp=head;
cnt=0;
while(cnt!=k-1){
temp=temp.next;
cnt++;
}
temp.next=temp.next.next;
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: function Charity(n,m) {
// write code here
// do no console.log the answer
// return the output using return keyword
const per = Math.floor(m / n)
return per > 1 ? per : -1
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: static int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: def Charity(N,M):
x = M//N
if x<=1:
return -1
return x
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After the decimation, the world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.The first line contains n, the total number of people donating. The next line contains n space-separated integers denoting the amount of money paid by the people. The amounts are mentioned in the order in which the people paid.
<b>Constraints:-</b>
1 <= n <= 100000
0 <= money <= 100000The first line contains the extra money that each student has to pay after their teacher applied the rule. The second line contains the total amount collected by the teacher at the end.Sample Input 1:-
10
1 2 3 2 4 3 6 6 7 6
Sample Output 1:-
0 0 0 1 0 1 0 0 0 1
43
Sample Input 2:-
7
10 20 30 40 30 20 10
Sample Output 2:-
0 0 0 0 10 20 30
220, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int n = Integer.parseInt(in.readLine());
int []donationList = new int[n];
int[] defaulterList = new int[n];
long totalDonations = 0L;
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i=0 ; i<n ; i++){
donationList[i] = Integer.parseInt(st.nextToken());
}
int max = Integer.MIN_VALUE;
for(int i=0; i<n; i++){
totalDonations += donationList[i];
max = Math.max(max,donationList[i]);
if(i>0){
if(donationList[i] >= max)
defaulterList[i] = 0;
else
defaulterList[i] = max - donationList[i];
}
totalDonations += defaulterList[i];
}
for(int i=0; i<n ;i++){
System.out.print(defaulterList[i]+" ");
}
System.out.println();
System.out.print(totalDonations);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After the decimation, the world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.The first line contains n, the total number of people donating. The next line contains n space-separated integers denoting the amount of money paid by the people. The amounts are mentioned in the order in which the people paid.
<b>Constraints:-</b>
1 <= n <= 100000
0 <= money <= 100000The first line contains the extra money that each student has to pay after their teacher applied the rule. The second line contains the total amount collected by the teacher at the end.Sample Input 1:-
10
1 2 3 2 4 3 6 6 7 6
Sample Output 1:-
0 0 0 1 0 1 0 0 0 1
43
Sample Input 2:-
7
10 20 30 40 30 20 10
Sample Output 2:-
0 0 0 0 10 20 30
220, I have written this Solution Code: n = int(input())
a = input().split()
b = int(a[0])
sum = 0
for i in a:
if int(i)<b:
print(b-int(i),end=' ')
else:
b = int(i)
print(0,end=' ')
sum = sum+b
print()
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After the decimation, the world went into chaos. People had to rebuild the planet so Shield came up with a donation strategy. They feel all the rich guys need to donate more than the poor guys. So, they make a rule. They would make a donation list in which the donation of each person would be shown. But the rule is that a person can’t pay less than what has already been paid before them. Find the extra amount each person will pay, and also, tell shield the total amount of donation.The first line contains n, the total number of people donating. The next line contains n space-separated integers denoting the amount of money paid by the people. The amounts are mentioned in the order in which the people paid.
<b>Constraints:-</b>
1 <= n <= 100000
0 <= money <= 100000The first line contains the extra money that each student has to pay after their teacher applied the rule. The second line contains the total amount collected by the teacher at the end.Sample Input 1:-
10
1 2 3 2 4 3 6 6 7 6
Sample Output 1:-
0 0 0 1 0 1 0 0 0 1
43
Sample Input 2:-
7
10 20 30 40 30 20 10
Sample Output 2:-
0 0 0 0 10 20 30
220, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n;
cin>>n;
int a[n];
int ma=0;
int cnt=0;
//map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
ma=max(ma,a[i]);
cout<<ma-a[i]<<" ";
cnt+=ma-a[i];
cnt+=a[i];
//m[a[i]]++;
}
cout<<endl;
cout<<cnt<<endl;
}
signed main(){
int t;
t=1;
while(t--){
solve();}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<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>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code: static int King(int X, int Y){
int a[] = {0,0,1,1,1,-1,-1,-1};
int b[] = {-1,1,1,-1,0,1,-1,0};
int cnt=0;
for(int i=0;i<8;i++){
if(X+a[i]<=8 && X+a[i]>=1 && Y+b[i]>=1 && Y+b[i]<=8){cnt++;}
}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<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>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code:
int King(int X, int Y){
int a[] = {0,0,1,1,1,-1,-1,-1};
int b[] = {-1,1,1,-1,0,1,-1,0};
int cnt=0;
for(int i=0;i<8;i++){
if(X+a[i]<=8 && X+a[i]>=1 && Y+b[i]>=1 && Y+b[i]<=8){cnt++;}
}
return cnt;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<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>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code: import array as arr
def King(X, Y):
a = arr.array('i', [0,0,1,1,1,-1,-1,-1])
b = arr.array('i', [-1,1,1,-1,0,1,-1,0])
cnt=0
for i in range (0,8):
if(X+a[i]<=8 and X+a[i]>=1 and Y+b[i]>=1 and Y+b[i]<=8):
cnt=cnt+1
return cnt
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<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>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code:
int King(int X, int Y){
int a[] = {0,0,1,1,1,-1,-1,-1};
int b[] = {-1,1,1,-1,0,1,-1,0};
int cnt=0;
for(int i=0;i<8;i++){
if(X+a[i]<=8 && X+a[i]>=1 && Y+b[i]>=1 && Y+b[i]<=8){cnt++;}
}
return cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a big number in form of a string A of characters from 0 to 9.
Check whether the given number is divisible by 30 .The first argument is the string A.
<b>Constraints</b>
1 ≤ |A| ≤ 10<sup>5</sup>Return "Yes" if it is divisible by 30 and "No" otherwise. Sample Input :
3033330
Sample Output:
Yes, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String a= br.readLine();
int sum=0;
int n=a.length();
int s=0;
for(int i=0; i<n; i++){
sum=sum+ (a.charAt(i) - '0');
if(i == n-1){
s= (a.charAt(i) - '0');
}
}
if(sum%3==0 && s==0){
System.out.print("Yes");
}else{
System.out.print("No");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a big number in form of a string A of characters from 0 to 9.
Check whether the given number is divisible by 30 .The first argument is the string A.
<b>Constraints</b>
1 ≤ |A| ≤ 10<sup>5</sup>Return "Yes" if it is divisible by 30 and "No" otherwise. Sample Input :
3033330
Sample Output:
Yes, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-02-10 11:06:49
**/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
int solve(string a) {
int n = a.size();
int sum = 0;
if (a[n - 1] != '0') {
return 0;
}
for (auto &it : a) {
sum += (it - '0');
}
debug(sum % 3);
if (sum % 3 == 0) {
return 1;
}
return 0;
}
int32_t main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
string str;
cin >> str;
int res = solve(str);
if (res) {
cout << "Yes\n";
} else {
cout << "No\n";
}
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 big number in form of a string A of characters from 0 to 9.
Check whether the given number is divisible by 30 .The first argument is the string A.
<b>Constraints</b>
1 ≤ |A| ≤ 10<sup>5</sup>Return "Yes" if it is divisible by 30 and "No" otherwise. Sample Input :
3033330
Sample Output:
Yes, I have written this Solution Code: A=int(input())
if A%30==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: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: t=int(input())
while t!=0:
m,n=input().split()
m,n=int(m),int(n)
for i in range(m):
arr=input().strip()
if '1' in arr:
arr='1 '*n
else:
arr='0 '*n
print(arr)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define N 1000
int a[N][N];
// Driver code
int main()
{
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
bool b[n];
for(int i=0;i<n;i++){
b[i]=false;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]==1){
b[i]=true;
}
}
}
for(int i=0;i<n;i++){
if(b[i]){
for(int j=0;j<m;j++){
cout<<1<<" ";
}}
else{
for(int j=0;j<m;j++){
cout<<0<<" ";
}
}
cout<<endl;
}
}}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1.
Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.The first line of input contains T denoting the number of test cases. T test cases follow.
The first line of each test case contains m and n denotes the number of rows and a number of columns.
Then next m lines contain n elements denoting the elements of the matrix.
Constraints:
1 ≤ T ≤ 20
1 ≤ m, n ≤ 700
Mat[I][j] ∈ {0,1}For each testcase, in a new line, print the modified matrix.Input:
1
5 4
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Output:
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1
Explanation:
Rows = 5 and columns = 4
The given matrix is
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too.
The final matrix is
1 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(isr);
int t = Integer.parseInt(bf.readLine());
while (t-- > 0){
String inputs[] = bf.readLine().split(" ");
int m = Integer.parseInt(inputs[0]);
int n = Integer.parseInt(inputs[1]);
String[] matrix = new String[m];
for(int i=0; i<m; i++){
matrix[i] = bf.readLine();
}
StringBuffer ones = new StringBuffer("");
StringBuffer zeros = new StringBuffer("");
for(int i=0; i<n; i++){
ones.append("1 ");
zeros.append("0 ");
}
for(int i=0; i<m; i++){
if(matrix[i].contains("1")){
System.out.println(ones);
}else{
System.out.println(zeros);
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N and an integer K. Print the count of distinct numbers in all subarrays of size k in the array A[].The first line of input contains two integers N and K. The next line contains N space separated values of the array A[].
Constraints:-
1 <= N, K <= 100000
1 <= A[] <= 1000000000Print the space separated values denoting counts of distinct numbers in all subarrays of size k in the array A[].Sample Input :
7 4
1 2 1 3 4 2 3
Sample Output:
3 4 4 3
Explanation:-`
First window's elements is:- 1 2 1 3 which contains 3 different elements.
Second window's elements is:- 2 1 3 4 which contains 4 different elements.
Third window's elements is:- 1 3 4 2 which contains 4 different elements.
Fourth window's elements is:- 3 4 2 3 which contains 3 different elements., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
str = br.readLine().trim().split(" ");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(str[i]);
}
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i < k; i++) {
if (hm.containsKey(arr[i])) {
int temp = hm.get(arr[i]);
hm.put(arr[i], temp+1);
}
else {
hm.put(arr[i], 1);
}
}
System.out.print(hm.size() +" ");
for (int i = k; i < n; i++) {
int temp = hm.get(arr[i-k]);
if (temp == 1){
hm.remove(arr[i-k]);
}
else {
hm.put(arr[i-k], temp-1);
}
if (hm.containsKey(arr[i])) {
temp = hm.get(arr[i]);
hm.put(arr[i], temp+1);
}
else {
hm.put(arr[i], 1);
}
System.out.print(hm.size() +" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N and an integer K. Print the count of distinct numbers in all subarrays of size k in the array A[].The first line of input contains two integers N and K. The next line contains N space separated values of the array A[].
Constraints:-
1 <= N, K <= 100000
1 <= A[] <= 1000000000Print the space separated values denoting counts of distinct numbers in all subarrays of size k in the array A[].Sample Input :
7 4
1 2 1 3 4 2 3
Sample Output:
3 4 4 3
Explanation:-`
First window's elements is:- 1 2 1 3 which contains 3 different elements.
Second window's elements is:- 2 1 3 4 which contains 4 different elements.
Third window's elements is:- 1 3 4 2 which contains 4 different elements.
Fourth window's elements is:- 3 4 2 3 which contains 3 different elements., I have written this Solution Code: n,k=input().split()
n=int(n)
k=int(k)
arr=input().split()
for i in range(0,len(arr)):
arr[i]=int(arr[i])
hs={}
for i in range(0,k):
if arr[i] not in hs:
hs[arr[i]]=1
else:
hs[arr[i]]+=1
print (len(hs),end=' ')
for i in range(k,n):
if(hs[arr[i-k]]==1):
del hs[arr[i-k]]
else:
hs[arr[i-k]]-=1
if arr[i] not in hs:
hs[arr[i]]=1
else:
hs[arr[i]]+=1
print (len(hs),end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N and an integer K. Print the count of distinct numbers in all subarrays of size k in the array A[].The first line of input contains two integers N and K. The next line contains N space separated values of the array A[].
Constraints:-
1 <= N, K <= 100000
1 <= A[] <= 1000000000Print the space separated values denoting counts of distinct numbers in all subarrays of size k in the array A[].Sample Input :
7 4
1 2 1 3 4 2 3
Sample Output:
3 4 4 3
Explanation:-`
First window's elements is:- 1 2 1 3 which contains 3 different elements.
Second window's elements is:- 2 1 3 4 which contains 4 different elements.
Third window's elements is:- 1 3 4 2 which contains 4 different elements.
Fourth window's elements is:- 3 4 2 3 which contains 3 different elements., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unordered_map<long,int> m;
int n,k;
cin>>n>>k;
long a[n];
for(int i=0;i<n;i++){cin>>a[i];}
for(int i=0;i<k;i++){
m[a[i]]++;
}
cout<<m.size()<<" ";
int ans=m.size();
for(int i=k;i<n;i++){
m[a[i-k]]--;
if(m[a[i-k]]==0){ans--;}
m[a[i]]++;
if(m[a[i]]==1){ans++;}
cout<<ans<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: static int candies(int X, int Y){
if(X<=Y){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: int candies(int X, int Y){
if(X<=Y){return 1;}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: def candies(X,Y):
if(X<=Y):
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: According to the latest government guidelines, the mass gathering of more than or equal to X people are not allowed but Sara organizes a party in her home and invited Y of her friends. As per the government guidelines your task is to tell Sara if she is in trouble or not.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>candies()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X <= 1000
1 <= Y <= 1000Return 1 if Sara is in trouble else return 0.Sample Input:-
X = 3, Y = 5
Sample Output:-
1
Explanation:-
Total people in Sara's home = 5, no. of people allowed = 3
Sample Input:-
X = 4, Y = 2
Sample Output:-
0, I have written this Solution Code: int candies(int X, int Y){
if(X<=Y){return 1;}
return 0;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc =new Scanner(System.in);
int T= sc.nextInt();
for(int i=0;i<T;i++){
int arrsize=sc.nextInt();
int max=0,secmax=0,thirdmax=0,j;
for(int k=0;k<arrsize;k++){
j=sc.nextInt();
if(j>max){
thirdmax=secmax;
secmax=max;
max=j;
}
else if(j>secmax){
thirdmax=secmax;
secmax=j;
}
else if(j>thirdmax){
thirdmax=j;
}
if(k%10000==0){
System.gc();
}
}
System.out.println(max+" "+secmax+" "+thirdmax+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: t=int(input())
while t>0:
t-=1
n=int(input())
l=list(map(int,input().strip().split()))
li=[0,0,0]
for i in l:
x=i
for j in range(0,3):
y=min(x,li[j])
li[j]=max(x,li[j])
x=y
print(li[0],end=" ")
print(li[1],end=" ")
print(li[2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
vector<long> a(n);
long ans[3]={0};
long x,y;
for(int i=0;i<n;i++){
cin>>a[i];
x=a[i];
for(int j=0;j<3;j++){
y=min(x,ans[j]);
ans[j]=max(x,ans[j]);
// cout<<ans[j]<<" ";
x=y;
}
}
if(ans[1]<ans[0]){
swap(ans[1],ans[0]);
}
if(ans[2]<ans[1]){
swap(ans[1],ans[2]);
}
if(ans[1]<ans[0]){
swap(ans[1],ans[0]);
}
cout<<ans[2]<<" "<<ans[1]<<" "<<ans[0]<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, you need to find its maximum, 2<sup>nd</sup> maximum and 3<sup>rd</sup> maximum element.
Try solving it in O(N) per test caseThe first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N denoting the number of elements in the array A. The following line contains N (space-separated) elements of A.
<b>Constraints:</b>
1 <= T <= 100
3 <= N <= 10<sup>6</sup>
1 <= A[i] <= 10<sup>9</sup>
<b>Note</b>:-It is guaranteed that the sum of N over all text cases does not exceed 10<sup>6</sup>For each test case, output the first, second and third maximum elements in the array.Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77
Sample Output:
5 4 4
9 8 7
77 66 55
<b>Explanation:</b>
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4, I have written this Solution Code: function maxNumbers(arr,n) {
// write code here
// do not console.log the answer
// return the answer as an array of 3 numbers
return arr.sort((a,b)=>b-a).slice(0,3)
};
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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: Two numbers are represented in Linked List such that each digit corresponds to a node in the linked list. Your task is to add these two numbers and return the sum in a linked list.
<b>Note:-</b> Linked list representation of a number is from left to right i.e if the number is 123 then in the linked list it is represented as 3->2->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>addNumber()</b> that takes head nodes of both the linked lists as parameters.
<b>Constraints:</b>
1 <=numbers<=10<sup>1000</sup>Return the head of modified linked list.Sample Input:-
1234 45643
Sample Output:-
46877, I have written this Solution Code: static Node addNumber(Node first, Node second)
{ Node res = null; // res is head node of the resultant list
Node prev = null;
Node temp = null;
int carry = 0, sum;
while (first != null || second != null) //while both lists exist
{
sum = carry + (first != null ? first.data : 0)
+ (second != null ? second.data : 0);
carry = (sum >= 10) ? 1 : 0;
sum = sum % 10;
temp = new Node(sum);
if (res == null) {
res = temp;
} else // If this is not the first node then connect it to the rest.
{
prev.next = temp;
}
prev = temp;
if (first != null) {
first = first.next;
}
if (second != null) {
second = second.next;
}
}
if (carry > 0) {
temp.next = new Node(carry);
}
// return head of the resultant list
return res;
} , In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, find the count of pairs of indices <b>i</b> and <b>j</b> such that <b>1 <= i < j <= N</b> and the <b>mean</b> of elements at these indices is at least <b>K</b>.
More formally, you have to find the number of indices <b>i</b> and <b>j</b> such that <b>1 <= i < j <= N</b> and (A<sub>i</sub> + A<sub>j</sub>)/2 >= KFirst line of the input contains two integers N and K.
The second line contains N space seperated integers A<sub>i</sub>
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= K <= 10<sup>9</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print the number of pairs satisfying the above condition in array A.Sample Input:
5 6
4 7 8 2 5
Sample Output:
4
Explaination:
The following pairs of indices satisfy the condition (1-based indexing)
(1, 3) -> (4 + 8)/2 = 6
(2, 3) -> (7 + 8)/2 = 7.5
(2, 5) -> (7 + 5)/2 = 6
(3, 5) -> (8 + 5)/2 = 6.5
There are no more pairs of indices that satisfy the above condition., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 1e9;
void solve() {
int n, k;
cin >> n >> k;
assert(1 <= n <= 1e5);
assert(1 <= k <= 1e9);
vector<int> a(n);
for (auto &i : a) cin >> i, assert(1 <= i <= 1e9);
sort(all(a));
int ans = 0;
int req_sum = 2 * k;
for (int i = 0; i < n; i++) {
int x = req_sum - a[i];
x = max(x, (int)0);
int l = i + 1, r = n - 1, ind = n;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] >= x) {
r = m - 1;
ind = m;
} else
l = m + 1;
}
ans += n - ind;
}
cout << ans;
}
signed main() {
fast int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve();
if (i != t) cout << endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N, find the count of pairs of indices <b>i</b> and <b>j</b> such that <b>1 <= i < j <= N</b> and the <b>mean</b> of elements at these indices is at least <b>K</b>.
More formally, you have to find the number of indices <b>i</b> and <b>j</b> such that <b>1 <= i < j <= N</b> and (A<sub>i</sub> + A<sub>j</sub>)/2 >= KFirst line of the input contains two integers N and K.
The second line contains N space seperated integers A<sub>i</sub>
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= K <= 10<sup>9</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print the number of pairs satisfying the above condition in array A.Sample Input:
5 6
4 7 8 2 5
Sample Output:
4
Explaination:
The following pairs of indices satisfy the condition (1-based indexing)
(1, 3) -> (4 + 8)/2 = 6
(2, 3) -> (7 + 8)/2 = 7.5
(2, 5) -> (7 + 5)/2 = 6
(3, 5) -> (8 + 5)/2 = 6.5
There are no more pairs of indices that satisfy the above condition., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String nk=br.readLine();
String nkarr[]=nk.split(" ");
int n =Integer.parseInt(nkarr[0]);
int k =Integer.parseInt(nkarr[1]);
String ss = br.readLine();
String sst[]=ss.split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(sst[i]);
}
long val = findMean(arr,n,k);
System.out.println(val);
}
public static long findMean(int []arr,int n,int k){
Arrays.sort(arr);
int low =0;
int high=arr.length-1;
long ans=0;
while(low<high){
long mean =(arr[low]+arr[high])/2;
if(mean>=k){
ans+=(high-low);
high--;
}else{
low++;
}
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string, reverse each word in the sentenceA string containing multiple words
ex:- "Welcome to this Javascript Guide!"A string with all words reversed
ex:- "emocleW ot siht tpircsavaJ !ediuG"Sample input:-
"Welcome to this Javascript Guide!"
Sample output:-
"emocleW ot siht tpircsavaJ !ediuG"
Explanation:-
The first word is reversed from "Welcome" to "emocleW"
and similarly all other wrods are reversed but the order of the words is same, I have written this Solution Code: function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator);
}
function reverseWords (str){
const step1 = reverseBySeparator(str,"")
const step2 = reverseBySeparator(step1," ")
console.log(step2)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string, reverse each word in the sentenceA string containing multiple words
ex:- "Welcome to this Javascript Guide!"A string with all words reversed
ex:- "emocleW ot siht tpircsavaJ !ediuG"Sample input:-
"Welcome to this Javascript Guide!"
Sample output:-
"emocleW ot siht tpircsavaJ !ediuG"
Explanation:-
The first word is reversed from "Welcome" to "emocleW"
and similarly all other wrods are reversed but the order of the words is same, I have written this Solution Code: import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Stack;
import static java.lang.Math.pow;
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static InputReader getInputReader(boolean readFromTextFile) throws FileNotFoundException {
return ((readFromTextFile) ? new InputReader(new FileInputStream("src/input.txt"))
: new InputReader(System.in));
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
class Main {
public static void main(String[] args) throws FileNotFoundException {
InputReader sc = InputReader.getInputReader(false);
String str = sc.readLine();
char[] charArray = str.toCharArray();
Stack<Character> st = new Stack<>();
for (int i = 0; i < charArray.length; i++) {
while (i < charArray.length && charArray[i] != ' ') {
st.push(charArray[i]);
i++;
}
while (!st.isEmpty()) {
System.out.print(st.pop());
}
System.out.print(" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: function Charity(n,m) {
// write code here
// do no console.log the answer
// return the output using return keyword
const per = Math.floor(m / n)
return per > 1 ? per : -1
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: static int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: def Charity(N,M):
x = M//N
if x<=1:
return -1
return x
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number (greater than 2), print two prime numbers whose sum will be equal to given number, else print -1 if no such number exists.
NOTE: A solution will always exist if the number is even. Read Goldbach’s conjecture.
If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then print [a, b] only
and not all possible solutions.The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N.
Constraints:
1 ≤ T ≤ 100
2 ≤ N ≤ 1000000Print the two prime numbers in a single line with space in between if exist else print -1.Sample Input:
2
8
3
Sample Output:
3 5
-1, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1073741824
#define read(type) readInt<type>()
#define max1 1000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
bool isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
ll ncr(ll n, ll r,ll p)
{
// Base case
if (r==0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
ll fac[n+1];
fac[0] = 1;
for (ll i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%p;
return (fac[n]* modInverse(fac[r], p) % p *
modInverse(fac[n-r], p) % p) % p;
}
ll fastexp (ll a, ll b, ll n) {
ll res = 1;
while (b) {
if (b & 1) res = res*a%n;
a = a*a%n;
b >>= 1;
}
return res;
}
bool a[max1];
int main() {
FOR(i,max1){
a[i]=false;}
for(int i=2;i<max1;i++){
if(a[i]==false){
for(int j=i+i;j<max1;j+=i){
a[j]=true;
}
}}
vector<int> v;
map<int,int> m;
for(int i=2;i<max1;i++){
if(a[i]==false){
v.EB(i);
m[i]++;
}
}
int t;
cin>>t;
while(t--){
int n;
cin>>n;
bool win=false;
for(int i=0;i<v.size();i++){
if(m.find(n-v[i])!=m.end()){
if(v[i]>n){break;}
cout<<v[i]<<" "<<n-v[i]<<endl;win=true;break;
}
}
if(win==false){out(-1);}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number (greater than 2), print two prime numbers whose sum will be equal to given number, else print -1 if no such number exists.
NOTE: A solution will always exist if the number is even. Read Goldbach’s conjecture.
If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then print [a, b] only
and not all possible solutions.The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N.
Constraints:
1 ≤ T ≤ 100
2 ≤ N ≤ 1000000Print the two prime numbers in a single line with space in between if exist else print -1.Sample Input:
2
8
3
Sample Output:
3 5
-1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static boolean isPrime(int n)
{
boolean ans=true;
if(n<=1)
{
ans=false;
}
else if(n==2 || n==3)
{
ans=true;
}
else if(n%2==0 || n%3==0)
{
ans=false;
}
else
{
for(int i=5;i*i<=n;i+=6)
{
if(n%i==0 || n%(i+2)==0)
{
ans=false;
break;
}
}
}
return ans;
}
public static void main (String[] args) throws IOException {
BufferedReader scan=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(scan.readLine());
while(t-->0)
{
int n=Integer.parseInt(scan.readLine());
int a=0,b=0;
if(n%2==1)
{
if(isPrime(n-2))
{
a=2;b=n-2;
}
}
else
{
for(int i=2;i<=n/2;i++)
{
if(isPrime(i))
{
if(isPrime(n-i))
{
a=i;b=n-i;
break;
}
}
}
}
if(a!=0 && b!=0)
{
System.out.println(a+" "+b);
}
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 number (greater than 2), print two prime numbers whose sum will be equal to given number, else print -1 if no such number exists.
NOTE: A solution will always exist if the number is even. Read Goldbach’s conjecture.
If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then print [a, b] only
and not all possible solutions.The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N.
Constraints:
1 ≤ T ≤ 100
2 ≤ N ≤ 1000000Print the two prime numbers in a single line with space in between if exist else print -1.Sample Input:
2
8
3
Sample Output:
3 5
-1, I have written this Solution Code: def isprime(num):
if(num==1 or num==0):
return False
for i in range(2,int(num**0.5)+1):
if(num%i==0):
return False
return True
T = int(input())
for test in range(T):
N = int(input())
value = -1
if(N%2==0):
for i in range(2,int(N/2)+1):
if(isprime(i)):
if(isprime(N-i)):
value = i
break
else:
if(isprime(N-2)):
value = 2
if(value==-1):
print(value)
else:
print(str(value)+" "+str(N-value)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: function Charity(n,m) {
// write code here
// do no console.log the answer
// return the output using return keyword
const per = Math.floor(m / n)
return per > 1 ? per : -1
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: static int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: def Charity(N,M):
x = M//N
if x<=1:
return -1
return x
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at some index.
You are given an array arr. The size of the array is given by sizeOfArray. You need to insert an element at given index and print the modified array.The first line of input contains T denoting the number of testcases. T testcases follow.
Each testcase contains two lines of input.
The first line contains size of the array denoted by sizeOfArray, element to be inserted and index.
The third line contains sizeOfArray-1 elements separated by spaces.
Constraints:
1 <= T <= 100
2 <= sizeOfArray <= 10^4
0 <= element, arr(i) <= 10^6
0 <= index <= sizeOfArray-1For each testcase, in a new line, print the modified array.Input:
2
6 90 5
1 2 3 4 5
6 90 2
1 2 3 4 5
Output:
1 2 3 4 5 90
1 2 90 3 4 5
Explanation:
Testcase 1: 90 in inserted at index 5(0-based indexing). After inserting, array elements are like 1, 2, 3, 4, 5, 90.
Testcase 2: 90 in inserted at index 2(0-based indexing). After inserting, array elements are like 1, 2, 90, 3, 4, 5., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws Exception {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
int N = Integer.parseInt(str[0]);
int ele = Integer.parseInt(str[1]);
int index = Integer.parseInt(str[2]);
str = read.readLine().trim().split(" ");
int arr[] = new int[N];
for(int i = 0; i < N-1; i++)
arr[i] = Integer.parseInt(str[i]);
insertAtIndex(arr, N, index, ele);
for(int i = 0; i < N; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}
public static void insertAtIndex(int arr[],int sizeOfArray,int index,int element)
{
// if index is last index
// then insert the element
if(index==sizeOfArray-1)
{
arr[index]=element;
return;
}
// else shift the elements, and then insert
for(int i=sizeOfArray-1;i>index;i--)
{
int temp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=temp;
}
arr[index]=element;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at some index.
You are given an array arr. The size of the array is given by sizeOfArray. You need to insert an element at given index and print the modified array.The first line of input contains T denoting the number of testcases. T testcases follow.
Each testcase contains two lines of input.
The first line contains size of the array denoted by sizeOfArray, element to be inserted and index.
The third line contains sizeOfArray-1 elements separated by spaces.
Constraints:
1 <= T <= 100
2 <= sizeOfArray <= 10^4
0 <= element, arr(i) <= 10^6
0 <= index <= sizeOfArray-1For each testcase, in a new line, print the modified array.Input:
2
6 90 5
1 2 3 4 5
6 90 2
1 2 3 4 5
Output:
1 2 3 4 5 90
1 2 90 3 4 5
Explanation:
Testcase 1: 90 in inserted at index 5(0-based indexing). After inserting, array elements are like 1, 2, 3, 4, 5, 90.
Testcase 2: 90 in inserted at index 2(0-based indexing). After inserting, array elements are like 1, 2, 90, 3, 4, 5., I have written this Solution Code: t=int(input())
while t>0:
t-=1
li = list(map(int,input().strip().split()))
n=li[0]
num=li[1]
i=li[2]
a= list(map(int,input().strip().split()))
a.insert(i,num)
for i in a:
print(i,end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given marks of a student in 5 subjects. You need to find the grade that a student would get on the basis of the percentage obtained. Grades computed are as follows:
If the percentage is >= 80 then print Grade ‘A’
If the percentage is <80 and >=60 then print Grade ‘B’
If the percentage is <60 and >=40 then print Grade ‘C’
else print Grade ‘D’The input contains 5 integers separated by spaces.
<b>Constraints:</b>
1 ≤ marks ≤ 100You need to print the grade obtained by a student.Sample Input:
75 70 80 90 100
Sample Output:
A
<b>Explanation</b>
((75+70+80+90+100)/5)*100=83%
A grade.
, I have written this Solution Code: import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class Main {
static final int MOD = 1000000007;
public static void main(String args[]) throws IOException {
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().trim().split(" ");
int a = Integer.parseInt(str[0]);
int b = Integer.parseInt(str[1]);
int c = Integer.parseInt(str[2]);
int d = Integer.parseInt(str[3]);
int e = Integer.parseInt(str[4]);
System.out.println(grades(a, b, c, d, e));
}
static char grades(int a, int b, int c, int d, int e)
{
int sum = a+b+c+d+e;
int per = sum/5;
if(per >= 80)
return 'A';
else if(per >= 60)
return 'B';
else if(per >= 40)
return 'C';
else
return 'D';
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given marks of a student in 5 subjects. You need to find the grade that a student would get on the basis of the percentage obtained. Grades computed are as follows:
If the percentage is >= 80 then print Grade ‘A’
If the percentage is <80 and >=60 then print Grade ‘B’
If the percentage is <60 and >=40 then print Grade ‘C’
else print Grade ‘D’The input contains 5 integers separated by spaces.
<b>Constraints:</b>
1 ≤ marks ≤ 100You need to print the grade obtained by a student.Sample Input:
75 70 80 90 100
Sample Output:
A
<b>Explanation</b>
((75+70+80+90+100)/5)*100=83%
A grade.
, I have written this Solution Code: li = list(map(int,input().strip().split()))
avg=0
for i in li:
avg+=i
avg=avg/5
if(avg>=80):
print("A")
elif(avg>=60 and avg<80):
print("B")
elif(avg>=40 and avg<60):
print("C")
else:
print("D"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and M check if N candies can be divided in M people such that each person get equal number of candies.Input contains two integers N and M.
Constraints:
1 <= N <= 10^18
1 <= M <= 10^18Print "Yes" if it is possible otherwise "No".Sample Input
10 5
Sample Output
Yes
Explanation: Give 2 candies to all.
Sample Input:
4 3
Sample Output:
No, I have written this Solution Code: m,n = map(int , input().split())
if (m%n==0):
print("Yes")
else:
print("No");, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and M check if N candies can be divided in M people such that each person get equal number of candies.Input contains two integers N and M.
Constraints:
1 <= N <= 10^18
1 <= M <= 10^18Print "Yes" if it is possible otherwise "No".Sample Input
10 5
Sample Output
Yes
Explanation: Give 2 candies to all.
Sample Input:
4 3
Sample Output:
No, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n,m;
cin>>n>>m;
if(n%m==0)
cout<<"Yes";
else
cout<<"No";
#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 and M check if N candies can be divided in M people such that each person get equal number of candies.Input contains two integers N and M.
Constraints:
1 <= N <= 10^18
1 <= M <= 10^18Print "Yes" if it is possible otherwise "No".Sample Input
10 5
Sample Output
Yes
Explanation: Give 2 candies to all.
Sample Input:
4 3
Sample Output:
No, 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);
long n = sc.nextLong();
Long m = sc.nextLong();
if(n%m==0){
System.out.print("Yes");
}
else{
System.out.print("No");
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice wants to go to Bob's house. The location of their houses is given on a 2-D coordinate system. There are a total of 8 directions:
North - Directly above
South - Directly below
East - To the right
West - To the left
North East - In between north and east
North West - In between north and west
South East - In between south and east
South West - In between south and west
Find the direction in which Alice must go to reach Bob's house.There are two lines of input. The first line contains the x and y coordinate of Alice's house. The second line contains x and y coordinate of Bob's house. It is given that these two locations are different.
-100 <= Coordinates <= 100Print a single string denoting the direction in which Alice must move to reach Bob's house.Sample Input 1:
2 5
11 25
Sample Output 1:
North East
Sample Input 2:
23 12
-85 12
Sample Output 2:
West, I have written this Solution Code: x0,y0 =map(int,input().split(" "))
x1,y1 = map(int,input().split(" "))
N = "North " if y0<y1 else "South " if y0> y1 else ""
E = "East " if x0<x1 else "West " if x0> x1 else ""
print(N+E), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice wants to go to Bob's house. The location of their houses is given on a 2-D coordinate system. There are a total of 8 directions:
North - Directly above
South - Directly below
East - To the right
West - To the left
North East - In between north and east
North West - In between north and west
South East - In between south and east
South West - In between south and west
Find the direction in which Alice must go to reach Bob's house.There are two lines of input. The first line contains the x and y coordinate of Alice's house. The second line contains x and y coordinate of Bob's house. It is given that these two locations are different.
-100 <= Coordinates <= 100Print a single string denoting the direction in which Alice must move to reach Bob's house.Sample Input 1:
2 5
11 25
Sample Output 1:
North East
Sample Input 2:
23 12
-85 12
Sample Output 2:
West, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if(x1 == x2){
if(y1 < y2)
cout << "North";
else
cout << "South";
}
else if(y1 == y2){
if(x1 < x2)
cout << "East";
else
cout << "West";
}
else if(x1 < x2 && y1 < y2)
cout << "North East";
else if(x1 < x2 && y1 > y2)
cout << "South East";
else if(x1 > x2 && y1 < y2)
cout << "North West";
else
cout << "South West";
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice wants to go to Bob's house. The location of their houses is given on a 2-D coordinate system. There are a total of 8 directions:
North - Directly above
South - Directly below
East - To the right
West - To the left
North East - In between north and east
North West - In between north and west
South East - In between south and east
South West - In between south and west
Find the direction in which Alice must go to reach Bob's house.There are two lines of input. The first line contains the x and y coordinate of Alice's house. The second line contains x and y coordinate of Bob's house. It is given that these two locations are different.
-100 <= Coordinates <= 100Print a single string denoting the direction in which Alice must move to reach Bob's house.Sample Input 1:
2 5
11 25
Sample Output 1:
North East
Sample Input 2:
23 12
-85 12
Sample Output 2:
West, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int x1, y1, x2, y2;
x1=sc.nextInt();
y1=sc.nextInt();
x2=sc.nextInt();
y2=sc.nextInt();
if(x1 == x2){
if(y1 < y2)
System.out.print("North");
else
System.out.print("South");
}
else if(y1 == y2){
if(x1 < x2)
System.out.print("East");
else
System.out.print("West");
}
else if(x1 < x2 && y1 < y2)
System.out.print("North East");
else if(x1 < x2 && y1 > y2)
System.out.print("South East");
else if(x1 > x2 && y1 < y2)
System.out.print("North West");
else
System.out.print("South West");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a Java program to perform following operations:
1. Input an arraylist of size n
2. Sort the arraylist
3. Search for value 2 in arraylist , if present print out its index else print out -1.First line of input contains value of n.
second line of input contains n space-separated integers.
Constraints:-
1 < = N < = 1000
1 < = Arr[i] < = 100000 Print index of 2 if present else print out -1.Sample Input:-
6
1 2 3 4 5 6
Sample output:-
1
Explanation:
2 is present at index value 1 in the sorted arraylist., I have written this Solution Code: import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
Collections.sort(list);
int index = Collections.binarySearch(list, 2);
if (index >= 0) {
System.out.println(index);
} else {
System.out.println("-1");
}
sc.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: n=int(input())
a=map(int,input().split())
b=[]
mx=-200000
cnt=0
for i in a:
if i>mx:
cnt+=1
mx=i
print(cnt), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: function numberOfRoofs(arr)
{
let count=1;
let max = arr[0];
for(let i=1;i<arrSize;i++)
{
if(arr[i] > max)
{
count++;
max = arr[i];
}
}
return count;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N buildings in a row with different heights H[i] (1 <= i <= N).
You are standing on the left side of the first building .From this position you can see the roof of a building <b>i</b> if no building to the left of the i<sup>th</sup> building has a height greater than or equal to the height of the i<sup>th</sup> building.
You are asked to find the number of buildings whose roofs you can see.The first line contains N denoting number of buildings.
The next line contains N space seperated integers denoting heights of the buildings from left to right.
Constraints
1 <= N <= 100000
1 <= H[i] <= 1000000000000000The output should contain one integer which is the number of buildings whose roofs you can see.Sample input:
5
1 2 2 4 3
Sample output:
3
Explanation:-
the building at index 3 will hide before building at index 2 and building at index 5 will hide before building at index 4
Sample input:
5
1 2 3 4 5
Sample output:
5
, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int count=1;
int max = a[0];
for(int i=1;i<n;i++)
{
if(a[i] > max)
{
count++;
max = a[i];
}
}
System.out.println(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string, reverse each word in the sentenceA string containing multiple words
ex:- "Welcome to this Javascript Guide!"A string with all words reversed
ex:- "emocleW ot siht tpircsavaJ !ediuG"Sample input:-
"Welcome to this Javascript Guide!"
Sample output:-
"emocleW ot siht tpircsavaJ !ediuG"
Explanation:-
The first word is reversed from "Welcome" to "emocleW"
and similarly all other wrods are reversed but the order of the words is same, I have written this Solution Code: function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator);
}
function reverseWords (str){
const step1 = reverseBySeparator(str,"")
const step2 = reverseBySeparator(step1," ")
console.log(step2)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string, reverse each word in the sentenceA string containing multiple words
ex:- "Welcome to this Javascript Guide!"A string with all words reversed
ex:- "emocleW ot siht tpircsavaJ !ediuG"Sample input:-
"Welcome to this Javascript Guide!"
Sample output:-
"emocleW ot siht tpircsavaJ !ediuG"
Explanation:-
The first word is reversed from "Welcome" to "emocleW"
and similarly all other wrods are reversed but the order of the words is same, I have written this Solution Code: import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Stack;
import static java.lang.Math.pow;
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static InputReader getInputReader(boolean readFromTextFile) throws FileNotFoundException {
return ((readFromTextFile) ? new InputReader(new FileInputStream("src/input.txt"))
: new InputReader(System.in));
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
class Main {
public static void main(String[] args) throws FileNotFoundException {
InputReader sc = InputReader.getInputReader(false);
String str = sc.readLine();
char[] charArray = str.toCharArray();
Stack<Character> st = new Stack<>();
for (int i = 0; i < charArray.length; i++) {
while (i < charArray.length && charArray[i] != ' ') {
st.push(charArray[i]);
i++;
}
while (!st.isEmpty()) {
System.out.print(st.pop());
}
System.out.print(" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, your task is to print the string S.You don't have to worry about taking input, you just have to complete the function <b>printString</b>Print the string S.Sample Input 1:-
NewtonSchool
Sample Output 1:-
NewtonSchool
Sample Input 2:-
Hello
Sample Output 2:-
Hello, I have written this Solution Code: static void printString(String stringVariable){
System.out.println(stringVariable);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, your task is to print the string S.You don't have to worry about taking input, you just have to complete the function <b>printString</b>Print the string S.Sample Input 1:-
NewtonSchool
Sample Output 1:-
NewtonSchool
Sample Input 2:-
Hello
Sample Output 2:-
Hello, I have written this Solution Code: S=input()
print(S), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S, your task is to print the string S.You don't have to worry about taking input, you just have to complete the function <b>printString</b>Print the string S.Sample Input 1:-
NewtonSchool
Sample Output 1:-
NewtonSchool
Sample Input 2:-
Hello
Sample Output 2:-
Hello, I have written this Solution Code: void printString(string s){
cout<<s;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, I have written this Solution Code: function bubbleSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => b - a)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, I have written this Solution Code: def bubbleSort(arr):
arr.sort(reverse = True)
return arr
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int t;
for(int i=1;i<n;i++){
if(a[i]>a[i-1]){
for(int j=i;j>0;j--){
if(a[j]>a[j-1]){
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, sort the array in reverse order by simply swapping its adjacent elements.First line of the input contains an integer, N, which denotes the length of the array. Next N inputs are elements of the array that is to be sorted in descending order.
Constraints
1<=N<=1000
-10000<=Arr[i]<=100000Output sorted array in descending order where each element is space separated.Sample Input:
6
3 1 2 7 9 87
Sample Output:
87 9 7 3 2 1, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
const int N = 3e5+5;
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
using vl = vector<ll>;
bool isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
long long phi[max1], result[max1],F[max1];
// Precomputation of phi[] numbers. Refer below link
// for details : https://goo.gl/LUqdtY
void computeTotient()
{
// Refer https://goo.gl/LUqdtY
phi[1] = 1;
for (int i=2; i<max1; i++)
{
if (!phi[i])
{
phi[i] = i-1;
for (int j = (i<<1); j<max1; j+=i)
{
if (!phi[j])
phi[j] = j;
phi[j] = (phi[j]/i)*(i-1);
}
}
}
for(int i=1;i<=100000;i++)
{
for(int j=i;j<=100000;j+=i)
{ int p=j/i;
F[j]+=(i*phi[p])%mod;
F[j]%=mod;
}
}
}
int gcd(int a, int b, int& x, int& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
g = gcd(abs(a), abs(b), x0, y0);
if (c % g) {
return false;
}
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n,greater<int>());
FOR(i,n){
out1(a[i]);}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are Q queries and for each query you are given a positive integer N. Find whether N is a perfect square or not.
Expected Space Complexity: O(1)First line contains an integer Q - the number of queries.
Q lines follow, each line containing a single integer N.
Constraints
1 <= Q <= 10^5
1 <= Ai <= 10^9Print Q lines. Each line should contain either "YES" or "NO".Sample Input 1:
3
1
2
4
Output
YES
NO
YES
Explanation:
1 and 4 are perfect squares.
Sample Input 2:
2
144
63
Output
YES
NO
Explanation:
12 * 12 = 144, I have written this Solution Code: import math
def checkperfectsquare(x):
if (math.ceil(math.sqrt(n)) ==
math.floor(math.sqrt(n))):
print("YES")
else:
print("NO")
t=int(input())
for _ in range(t):
n=int(input())
checkperfectsquare(n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are Q queries and for each query you are given a positive integer N. Find whether N is a perfect square or not.
Expected Space Complexity: O(1)First line contains an integer Q - the number of queries.
Q lines follow, each line containing a single integer N.
Constraints
1 <= Q <= 10^5
1 <= Ai <= 10^9Print Q lines. Each line should contain either "YES" or "NO".Sample Input 1:
3
1
2
4
Output
YES
NO
YES
Explanation:
1 and 4 are perfect squares.
Sample Input 2:
2
144
63
Output
YES
NO
Explanation:
12 * 12 = 144, 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) {
Scanner sc = new Scanner(System.in);
long Q = sc.nextLong();
while(Q-- > 0){
long n = sc.nextLong();
long sqrt = (long)Math.sqrt(n);
if(sqrt*sqrt == n){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are Q queries and for each query you are given a positive integer N. Find whether N is a perfect square or not.
Expected Space Complexity: O(1)First line contains an integer Q - the number of queries.
Q lines follow, each line containing a single integer N.
Constraints
1 <= Q <= 10^5
1 <= Ai <= 10^9Print Q lines. Each line should contain either "YES" or "NO".Sample Input 1:
3
1
2
4
Output
YES
NO
YES
Explanation:
1 and 4 are perfect squares.
Sample Input 2:
2
144
63
Output
YES
NO
Explanation:
12 * 12 = 144, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
int x = sqrt(n);
if (x * x == n) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Internet download speed is often expressed in bit per second whereas file size is expressed in Bytes. It is known that 1 Byte = 8 bits. Given file size in megabytes(MB) and internet speed in megabits per seconds(Mbps), find the time taken in seconds to download the file.The only line of input contains two integers denoting the file size in MB and download speed in Mbps.
1 <= file size <= 1000
1 <= download speed <= 1000Print a single integer denoting the time taken to download the file in seconds.
It is guaranteed that the result will be an integer.Sample Input:
10 16
Sample Output:
5, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
int f, s;
cin >> f >> s;
cout << (8*f)/s << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Internet download speed is often expressed in bit per second whereas file size is expressed in Bytes. It is known that 1 Byte = 8 bits. Given file size in megabytes(MB) and internet speed in megabits per seconds(Mbps), find the time taken in seconds to download the file.The only line of input contains two integers denoting the file size in MB and download speed in Mbps.
1 <= file size <= 1000
1 <= download speed <= 1000Print a single integer denoting the time taken to download the file in seconds.
It is guaranteed that the result will be an integer.Sample Input:
10 16
Sample Output:
5, 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 f = sc.nextInt();
int s = sc.nextInt();
int ans = (8*f)/s;
System.out.print(ans);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Internet download speed is often expressed in bit per second whereas file size is expressed in Bytes. It is known that 1 Byte = 8 bits. Given file size in megabytes(MB) and internet speed in megabits per seconds(Mbps), find the time taken in seconds to download the file.The only line of input contains two integers denoting the file size in MB and download speed in Mbps.
1 <= file size <= 1000
1 <= download speed <= 1000Print a single integer denoting the time taken to download the file in seconds.
It is guaranteed that the result will be an integer.Sample Input:
10 16
Sample Output:
5, I have written this Solution Code: a = list(map(int,input().strip().split()))[:2]
print(int((a[0]*8)/a[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b> having <b>N</b> positive integers. You need to arrange these elements in increasing order using <b>Quick Sort</b> algorithm.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>quickSort()</b> which contains following arguments.
<b>A[]:</b> input array
<b>start:</b> starting index of array
<b>end</b>: ending index of array
Constraints
1 <= T <= 1000
1 <= N <= 10^4
1 <= A[i] <= 10^5
<b>Sum of "N" over all testcases does not exceed 10^5</b>For each testcase you need to return the sorted array. The driver code will do the rest.Sample Input:
2
3
3 1 2
3
4 5 6
Sample Output:
1 2 3
4 5 6, I have written this Solution Code: def partition(array, low, high):
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[high]) = (array[high], array[i + 1])
return i + 1
def quick_sort(array, low, high):
if low < high:
pi = partition(array, low, high)
quick_sort(array, low, pi - 1)
quick_sort(array, pi + 1, high)
t=int(input())
for i in range(t):
n=int(input())
a=input().strip().split()
a=[int(i) for i in a]
quick_sort(a, 0, n - 1)
for i in a:
print(i,end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b> having <b>N</b> positive integers. You need to arrange these elements in increasing order using <b>Quick Sort</b> algorithm.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>quickSort()</b> which contains following arguments.
<b>A[]:</b> input array
<b>start:</b> starting index of array
<b>end</b>: ending index of array
Constraints
1 <= T <= 1000
1 <= N <= 10^4
1 <= A[i] <= 10^5
<b>Sum of "N" over all testcases does not exceed 10^5</b>For each testcase you need to return the sorted array. The driver code will do the rest.Sample Input:
2
3
3 1 2
3
4 5 6
Sample Output:
1 2 3
4 5 6, I have written this Solution Code:
public static int[] quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr, low, high);
// Recursively sort elements before
// partition and after partition
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
return arr;
}
public static int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
// swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// swap arr[i+1] and arr[high] (or pivot)
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b> having <b>N</b> positive integers. You need to arrange these elements in increasing order using <b>Quick Sort</b> algorithm.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>quickSort()</b> which contains following arguments.
<b>A[]:</b> input array
<b>start:</b> starting index of array
<b>end</b>: ending index of array
Constraints
1 <= T <= 1000
1 <= N <= 10^4
1 <= A[i] <= 10^5
<b>Sum of "N" over all testcases does not exceed 10^5</b>For each testcase you need to return the sorted array. The driver code will do the rest.Sample Input:
2
3
3 1 2
3
4 5 6
Sample Output:
1 2 3
4 5 6, I have written this Solution Code: function quickSort(arr, low, high)
{
if(low < high)
{
let pi = partition(arr, low, high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
return arr;
}
function partition(arr, low, high)
{
let pivot = arr[high];
let i = (low-1); // index of smaller element
for (let j=low; j<high; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
// swap arr[i] and arr[j]
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// swap arr[i+1] and arr[high] (or pivot)
let temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: static void simpleSum(int a, int b, int c){
System.out.println(a+b+c);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: void simpleSum(int a, int b, int c){
cout<<a+b+c;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.