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: 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: Your task is to implement a stack using a linked list and perform given queries
Note:-if stack is already empty than pop operation will do nothing and 0 will be printed as a top element of stack if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push()</b>:- that takes the integer to be added as a parameter.
<b>pop()</b>:- that takes no parameter.
<b>top()</b> :- that takes no parameter.
Constraints:
1 <= N(number of queries) <= 10<sup>3</sup>You don't need to print anything else other than in top function in which you require to print the top most element of your stack in a new line, if the stack is empty you just need to print 0.Input:
7
push 1
push 2
top
pop
top
pop
top
Output:
2
1
0
, I have written this Solution Code:
Node top = null;
public void push(int x)
{
Node temp = new Node(x);
temp.next = top;
top = temp;
}
public void pop()
{
if (top == null) {
}
else {
top = (top).next;}
}
public void top()
{
// check for stack underflow
if (top == null) {
System.out.println("0");
}
else {
Node temp = top;
System.out.println(temp.val);
}
}
, 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: Write a program to find the compound interest for given principal amount P, time Tm(in years), and interest rate R.
<b>Note:</b> Compound interest is the interest you earn on interest.
This can be illustrated by using basic math: if you have $100 and it earns 5% interest each year, you'll have $105 at the end of the first year. At the end of the second year, you'll have $110.25The input contains three integers P, R, and Tm.
<b>Constraints:- </b>
1 < = P < = 10^3
1 < = R < = 100
1 < = Tm < = 20Print the compound interest by <b> 2 decimal places </b>.Sample Input:
100 1 2
Sample Output:-
2.01
Sample Input:
1 99 2
Sample Output:-
2.96, I have written this Solution Code: def compound_interest(principle, rate, time):
Amount = principle * (pow((1 + rate / 100), time))
CI = Amount - principle
print( '%.2f'%CI)
principle,rate,time=map(int, input().split())
compound_interest(principle,rate,time), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to find the compound interest for given principal amount P, time Tm(in years), and interest rate R.
<b>Note:</b> Compound interest is the interest you earn on interest.
This can be illustrated by using basic math: if you have $100 and it earns 5% interest each year, you'll have $105 at the end of the first year. At the end of the second year, you'll have $110.25The input contains three integers P, R, and Tm.
<b>Constraints:- </b>
1 < = P < = 10^3
1 < = R < = 100
1 < = Tm < = 20Print the compound interest by <b> 2 decimal places </b>.Sample Input:
100 1 2
Sample Output:-
2.01
Sample Input:
1 99 2
Sample Output:-
2.96, I have written this Solution Code: function calculateCI(P, R, T)
{
let interest = P * (Math.pow(1.0 + R/100.0, T) - 1);
return interest.toFixed(2);
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to find the compound interest for given principal amount P, time Tm(in years), and interest rate R.
<b>Note:</b> Compound interest is the interest you earn on interest.
This can be illustrated by using basic math: if you have $100 and it earns 5% interest each year, you'll have $105 at the end of the first year. At the end of the second year, you'll have $110.25The input contains three integers P, R, and Tm.
<b>Constraints:- </b>
1 < = P < = 10^3
1 < = R < = 100
1 < = Tm < = 20Print the compound interest by <b> 2 decimal places </b>.Sample Input:
100 1 2
Sample Output:-
2.01
Sample Input:
1 99 2
Sample Output:-
2.96, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int p,r,t;
cin>>p>>r>>t;
double rate= (float)r/100;
double amt = (float)p*(pow(1+rate,t));
cout << fixed << setprecision(2) << (amt - p);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to find the compound interest for given principal amount P, time Tm(in years), and interest rate R.
<b>Note:</b> Compound interest is the interest you earn on interest.
This can be illustrated by using basic math: if you have $100 and it earns 5% interest each year, you'll have $105 at the end of the first year. At the end of the second year, you'll have $110.25The input contains three integers P, R, and Tm.
<b>Constraints:- </b>
1 < = P < = 10^3
1 < = R < = 100
1 < = Tm < = 20Print the compound interest by <b> 2 decimal places </b>.Sample Input:
100 1 2
Sample Output:-
2.01
Sample Input:
1 99 2
Sample Output:-
2.96, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.lang.Math;
class Main {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s= br.readLine().split(" ");
double[] darr = new double[s.length];
for(int i=0;i<s.length;i++){
darr[i] = Double.parseDouble(s[i]);
}
double ans = darr[0]*Math.pow(1+darr[1]/100,darr[2])-darr[0];
System.out.printf("%.2f",ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: New Year festival of Ying Yang is approaching. There are <b>N</b> spaces available for stalls to be put up. The i<sup>th</sup> stall's price is p<sub>i</sub>. You have X amount of money with you right now and you want to buy <b>atleast 2 stalls</b> from this money. Find whether it is possible. Print "YES" if it is possible, else print "NO", without the quotes.First line of the input contains two integers N, the number of stall spaces and X, the amount of money you have.
The second line of the input contains N space seperated integers.
Constraints:
2 <= N <= 10<sup>5</sup>
1 <= X <= 10<sup>9</sup>
1 <= p<sub>i</sub> <= 10<sup>9</sup>Print "YES" if it is possible for you to buy atleast two spaces in the festival, else print "NO", without the quotes.Sample Input:
5 10
3 6 5 8 11
Sample Input:
YES
Explaination:
You can buy stall no. s 1 and 2 (1- based indexing) to bring the total amount to 3 + 6 = 9, which is less than 10., I have written this Solution Code: n,p=input().split()
arr=list(map(int,input().split()))
arr.sort()
i=0
for j in range(i+1,int(n)):
if arr[i]+arr[j] <= int(p) :
print("YES")
break
i+=1
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: New Year festival of Ying Yang is approaching. There are <b>N</b> spaces available for stalls to be put up. The i<sup>th</sup> stall's price is p<sub>i</sub>. You have X amount of money with you right now and you want to buy <b>atleast 2 stalls</b> from this money. Find whether it is possible. Print "YES" if it is possible, else print "NO", without the quotes.First line of the input contains two integers N, the number of stall spaces and X, the amount of money you have.
The second line of the input contains N space seperated integers.
Constraints:
2 <= N <= 10<sup>5</sup>
1 <= X <= 10<sup>9</sup>
1 <= p<sub>i</sub> <= 10<sup>9</sup>Print "YES" if it is possible for you to buy atleast two spaces in the festival, else print "NO", without the quotes.Sample Input:
5 10
3 6 5 8 11
Sample Input:
YES
Explaination:
You can buy stall no. s 1 and 2 (1- based indexing) to bring the total amount to 3 + 6 = 9, which is less than 10., 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, x;
cin >> n >> x;
vector<int> p(n);
for(auto &i : p) cin >> i;
sort(all(p));
cout << ((p[0] + p[1]) <= x ? "YES" : "NO");
}
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: There is a marathon to be run on a circular track. There are N checkpoints on the track where energy drinks are placed. Checkpoints are numbered from 1 to N (both inclusive). For each checkpoint, you know the distance to the next checkpoint and also the energy replenished by the drink at this checkpoint.
Toros is going to take part in the marathon. For every 1 unit distance covered by him, his energy decreases by 1. He can't run further if he has 0 energy. Find the minimum index of the point where he should start so that he can visit every checkpoint at least once. Initially, he has 0 energy.The first line contains a single integer N, denoting the number of checkpoints.
The next N lines contain two space-separated integers, E[i] and D[i], denoting the energy replenished by the energy drink kept at the ith checkpoint and the distance to the next checkpoint.
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ E[i], D[i] ≤ 10<sup>9</sup>
Print a single integer containing the minimum index of the checkpoint where Toros should start so that he can complete the race.
If no such point exists, print -1Sample Input 1:
3
1 5
10 3
3 4
Sample Output 1:
2
Sample Input 2:-
3
2 4
5 4
2 7
Sample Output 2:-
-1
Sample Input 3:-
5
1 4
5 6
3 1
7 2
5 8
Sample Output 3:-
3
<b>Explanation 1:</b>
Toros starts at index 2 and drinks energy drinks. So, current energy = 10. At index 3, energy = 10 - 3 + 3 = 10. At index 1 energy = 10 - 4 + 1 = 7., I have written this Solution Code: #include<bits/stdc++.h>
//#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], b[N];
void solve(){
int n; cin >> n;
bool flag = 0;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
}
int l = 0, r = 0;
int sum = 0, d, c = 0;
while((l + 1)%n != r && c < 2){
d = a[l] - b[l];
if(sum + d >= 0)
l++, sum += d;
else
sum = 0, l++, r = l;
if(l == n) c++;
l %= n;
}
if(c == 2) cout << -1 << endl;
else cout << r+1 << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a marathon to be run on a circular track. There are N checkpoints on the track where energy drinks are placed. Checkpoints are numbered from 1 to N (both inclusive). For each checkpoint, you know the distance to the next checkpoint and also the energy replenished by the drink at this checkpoint.
Toros is going to take part in the marathon. For every 1 unit distance covered by him, his energy decreases by 1. He can't run further if he has 0 energy. Find the minimum index of the point where he should start so that he can visit every checkpoint at least once. Initially, he has 0 energy.The first line contains a single integer N, denoting the number of checkpoints.
The next N lines contain two space-separated integers, E[i] and D[i], denoting the energy replenished by the energy drink kept at the ith checkpoint and the distance to the next checkpoint.
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ E[i], D[i] ≤ 10<sup>9</sup>
Print a single integer containing the minimum index of the checkpoint where Toros should start so that he can complete the race.
If no such point exists, print -1Sample Input 1:
3
1 5
10 3
3 4
Sample Output 1:
2
Sample Input 2:-
3
2 4
5 4
2 7
Sample Output 2:-
-1
Sample Input 3:-
5
1 4
5 6
3 1
7 2
5 8
Sample Output 3:-
3
<b>Explanation 1:</b>
Toros starts at index 2 and drinks energy drinks. So, current energy = 10. At index 3, energy = 10 - 3 + 3 = 10. At index 1 energy = 10 - 4 + 1 = 7., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n>10000){
System.out.print(-1);
return;
}
int[] remEnrgy = new int[n];
for(int i=0;i<n;i++) {
int e = sc.nextInt();
int d = sc.nextInt();
remEnrgy[i] = e-d;
}
for(int i=0;i<n;i++) {
if(remEnrgy[i] >= 0){
if(run(remEnrgy,i)) {
System.out.print(i+1);
return;
}
}
}
System.out.print(-1);
}
private static boolean run(int[]arr,int idx) {
long enrgy = 0;
if(idx==0){
for(int i=0;i<arr.length-1;i++) {
enrgy += arr[i];
if(enrgy<0){
return false;
}
}
return true;
}
for(int i=idx;i<arr.length;i++) {
enrgy += arr[i];
if(enrgy<0){
return false;
}
}
for(int i=0;i<idx-1;i++) {
enrgy += arr[i];
if(enrgy<0){
return false;
}
}
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String arr[][]=new String[n][n];
String transpose[][]=new String[n][n];
int row;
int cols;
for(row=0;row<n;row++)
{
String rowNum=br.readLine();
String rowVals[]=rowNum.split(" ");
for(cols=0; cols<n;cols++)
{
arr[row][cols]=rowVals[cols];
}
}
for(row=0;row<n;row++)
{
for(cols=0; cols<n;cols++)
{
transpose[row][cols]=arr[cols][row];
System.out.print(transpose[row][cols]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: x=int(input())
l1=[]
for i in range(x):
a1=list(map(int,input().split()))
l1.append(a1)
l4=[]
for j in range(x):
l3=[]
for i in range(x):
l3.append(l1[i][j])
l4.append(l3)
for i in range(x):
for j in range(x):
print(l4[i][j], end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[j][i];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b, your task is to check following conditions:-
1. If a <= 10 and b >= 10 (Logical AND).
2. Atleast one from a or b will be even (Logical OR).
3. if a is not equal to b (Logical NOT).The first line of the input contains 2 integers a and b.
<b>Constraints:</b>
1 <= a, b <= 100Print the string <b>"true"</b> if the condition holds in each function else <b>"false"</b> .
Sample Input:-
3 12
Sample Output:-
true true true
Explanation
So a = 3 and b = 12, so a<=10 and b>=10 hence first condition true, a is not even but b is even so atleast one of them is even hence true, third a != b which is also true hence the final output comes true true true.
Sample Input:-
10 10
Sample Output:-
true true false
, I have written this Solution Code: a, b = list(map(int, input().split(" ")))
print(str(a <= 10 and b >= 10).lower(), end=' ')
print(str(a % 2 == 0 or b % 2 == 0).lower(), end=' ')
print(str(not a == b).lower()), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b, your task is to check following conditions:-
1. If a <= 10 and b >= 10 (Logical AND).
2. Atleast one from a or b will be even (Logical OR).
3. if a is not equal to b (Logical NOT).The first line of the input contains 2 integers a and b.
<b>Constraints:</b>
1 <= a, b <= 100Print the string <b>"true"</b> if the condition holds in each function else <b>"false"</b> .
Sample Input:-
3 12
Sample Output:-
true true true
Explanation
So a = 3 and b = 12, so a<=10 and b>=10 hence first condition true, a is not even but b is even so atleast one of them is even hence true, third a != b which is also true hence the final output comes true true true.
Sample Input:-
10 10
Sample Output:-
true true false
, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Main {
static boolean Logical_AND(int a, int b){
if(a<=10 && b>=10){
return true;}
return false;}
static boolean Logical_OR(int a, int b){
if(a%2==0 || b%2==0){
return true;}
return false;}
static boolean Logical_NOT(int a, int b){
if(a!=b){
return true;}
return false;}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.print(Logical_AND(a, b)+" ");
System.out.print(Logical_OR(a,b)+" ");
System.out.print(Logical_NOT(a,b)+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: function supermarket(prices, n, k) {
// write code here
// do not console.log the answer
// return sorted array
const newPrices = prices.sort((a, b) => a - b).slice(2)
let kk = k
let price = 0;
let i = 0
while (kk-- && i < newPrices.length) {
price += newPrices[i]
i += 1
}
return price
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, 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]);
Arrays.sort(arr);
long sum=0;
for(int i=2;i<k+2;i++)
sum+=arr[i];
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: a=input().split()
for i in range(len(a)):
a[i]=int(a[i])
b=input().split()
for i in range(len(b)):
b[i]=int(b[i])
b.sort()
b.reverse()
b.pop()
b.pop()
s=0
while a[1]>0:
s+=b.pop()
a[1]-=1
print(s), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Toros went to the supermarket to buy K items. There were a total of N items. Each item had a distinct price P<sub>i</sub>. He is cost-effective, so he would buy the K cheapest item. But he knows that the more cheaper an item is, the more is the chances that it can be defective. So he planned to ignore 2 cheapest items and buy K from the remaining ones.
Find the total cost of all items that he would buy.The first line contains two integers N and K, denoting the total number of items in the supermarket and the number of items Toros is going to buy.
The second line contains N distinct integers P<sub>i</sub> , denoting the prices of the items
<b>Constraints:</b>
1 <= N <= 100000
1 <= K <= N - 2
1 <= P<sub>i</sub> <= 1000000Print a single integer denoting the total cost of items Toros would buy.Sample Input:
5 2
4 1 2 3 5
Sample Output:
7
Explanation:
Toros will ignore items with price 1 and 2 and would buy items with price 4 and 3.
Sample Input:
10 8
99 56 50 93 47 36 65 25 87 16
Sample Output:
533, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
void solve(){
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
int ans = 0;
for(int i = 3; i <= 2 + k; i++)
ans += a[i];
cout << ans << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, 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: In the morning assembly, there are N students standing in a line, such that the ith student from the front has height H[i].
In one move, you can pick any two students and swap their positions.
If you are allowed to do only one swap, can you arrange the students in increasing order of their hieghts?The first line of input contains T, the number of test cases.
The first line of each test case contains N, the number of students in the line.
The second line of each test case contains the heights of N students, where the ith index is the height of ith student.
<b>Constraints:</b>
1 <= T <= 10<sup>3</sup>
1 <= N <= 10<sup>4</sup>
1 <= H_i <= 10<sup>5</sup>For each test case, output YES or NOSample Input
2
3
1 2 3
4
2 1 4 3
Sample Output
YES
NO
Explanation:
The students are already standing in increasing order in the first test case, hence the answer is YES., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int t = input.nextInt();
for(int i = 0; i< t; i++){
int n = input.nextInt();
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = input.nextInt();
}
System.out.println(arrStu(arr, n));
}
}
public static String arrStu(int[] arr, int n ){
int[] newArr = new int[n];
for(int i = 0; i<n ;i++){
newArr[i] = arr[i];
}
Arrays.sort(newArr);
int count = 0;
for(int i = 0 ; i< n ;i++){
if(arr[i] != newArr[i]) count++;
}
if(count > 2) return "NO";
else return "YES";
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In the morning assembly, there are N students standing in a line, such that the ith student from the front has height H[i].
In one move, you can pick any two students and swap their positions.
If you are allowed to do only one swap, can you arrange the students in increasing order of their hieghts?The first line of input contains T, the number of test cases.
The first line of each test case contains N, the number of students in the line.
The second line of each test case contains the heights of N students, where the ith index is the height of ith student.
<b>Constraints:</b>
1 <= T <= 10<sup>3</sup>
1 <= N <= 10<sup>4</sup>
1 <= H_i <= 10<sup>5</sup>For each test case, output YES or NOSample Input
2
3
1 2 3
4
2 1 4 3
Sample Output
YES
NO
Explanation:
The students are already standing in increasing order in the first test case, hence the answer is YES., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main(){
int t=1;
cin>>t;
while(t--){
int n; cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
vector<int> b= v;
sort(b.begin(),b.end());
int cnt=0;
for(int i=0;i<n;i++){
if(b[i]!=v[i]) cnt++;
}
if(cnt==0 or cnt==2){
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: Given an integer n , your task is to print the lowercase English word corresponding to the number if it is <=5 else print "Greater than 5".
Numbers <=5 and their corresponding words :
1 = one
2 = two
3 = three
4 = four
5 = fiveThe input contains a single integer N.
Constraint:
1 <= n <= 100Print a string consisting of the lowercase English word corresponding to the number if it is <=5 else print the string "Greater than 5"Sample Input:
4
Sample Output
four
Sample Input:
6
Sample Output:
Greater than 5, I have written this Solution Code: N = int(input())
if N > 5:
print("Greater than 5")
elif(N == 1):
print("one")
elif(N == 2):
print("two")
elif(N == 3):
print("three")
elif(N == 4):
print("four")
elif(N == 5):
print("five"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n , your task is to print the lowercase English word corresponding to the number if it is <=5 else print "Greater than 5".
Numbers <=5 and their corresponding words :
1 = one
2 = two
3 = three
4 = four
5 = fiveThe input contains a single integer N.
Constraint:
1 <= n <= 100Print a string consisting of the lowercase English word corresponding to the number if it is <=5 else print the string "Greater than 5"Sample Input:
4
Sample Output
four
Sample Input:
6
Sample Output:
Greater than 5, 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();
String area = conditional(side);
System.out.println(area);
}static String conditional(int n){
if(n==1){return "one";}
else if(n==2){return "two";}
else if(n==3){return "three";}
else if(n==4){return "four";}
else if(n==5){return "five";}
else{
return "Greater than 5";}
}}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n, m;
cin >> n >> m;
cout << __gcd(n, m);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
li= list(map(int,input().strip().split()))
print(hcf(li[0], li[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 2 non-negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer.
NOTE: DO NOT USE LIBRARY FUNCTIONSInput contains two space separated integers, m and n
Constraints:-
1 < = m, n < = 10^18Output the single integer denoting the gcd of the above integers.Sample Input:
6 9
Sample Output:
3
Sample Input:-
5 6
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] sp = br.readLine().trim().split(" ");
long m = Long.parseLong(sp[0]);
long n = Long.parseLong(sp[1]);
System.out.println(GCDAns(m,n));
}
private static long GCDAns(long m,long n){
if(m==0)return n;
if(n==0)return m;
return GCDAns(n%m,m);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a side of a square, your task is to calculate and print its area.The first line of the input contains the side of the square.
<b>Constraints:</b>
1 <= side <=100You just have to print the area of a squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36, I have written this Solution Code: def area(side_of_square):
print(side_of_square*side_of_square)
def main():
N = int(input())
area(N)
if __name__ == '__main__':
main(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a side of a square, your task is to calculate and print its area.The first line of the input contains the side of the square.
<b>Constraints:</b>
1 <= side <=100You just have to print the area of a squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int side = Integer.parseInt(br.readLine());
System.out.print(side*side);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: void fizzbuzz(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){cout<<"FizzBuzz"<<" ";}
else if(i%5==0){cout<<"Buzz ";}
else if(i%3==0){cout<<"Fizz ";}
else{cout<<i<<" ";}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int x= sc.nextInt();
fizzbuzz(x);
}
static void fizzbuzz(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.print("FizzBuzz ");}
else if(i%5==0){System.out.print("Buzz ");}
else if(i%3==0){System.out.print("Fizz ");}
else{System.out.print(i+" ");}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: def fizzbuzz(n):
for i in range (1,n+1):
if (i%3==0 and i%5==0):
print("FizzBuzz",end=' ')
elif i%3==0:
print("Fizz",end=' ')
elif i%5==0:
print("Buzz",end=' ')
else:
print(i,end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: void fizzbuzz(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){printf("FizzBuzz ");}
else if(i%5==0){printf("Buzz ");}
else if(i%3==0){printf("Fizz ");}
else{printf("%d ",i);}
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and a matrix of size (N x N). Check whether given matrix is identity or not.
<b> Note </b>
An identity matrix is a square matrix in which all the elements of principal diagonal are one, and all other elements are zeros.First line contain a single integer N
Next N line contain N space- separated integer i. e. elements of matrix.If the given matrix is the identity matrix. print "Yes" otherwise print "NO"
Constraints:
1<=N<=100Sample Input 1:
3
1 0 0
0 1 0
0 0 1
Sample Output 1:
Yes
Explanation:
Given matrix is an identity matrix because all main diagonal elements are 1 and rest of elements are 0., I have written this Solution Code: import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int [][]arr=new int[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
arr[i][j] = s.nextInt();
}
boolean flag=false;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{
if(i==j && arr[i][j]!=1)flag=true;
else if(i!=j && arr[i][j]!=0)flag=true;
}
}
if(flag==true){
System.out.println("No");
}
else {
System.out.println("Yes");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A basic queue has the following operations:
Enqueue: add a new element to the end of the queue.
Dequeue: remove the element from the front of the queue and return it.
In this challenge, you must first implement a queue using two stacks. Then process q queries, where each query is one of the following 3 types:
1 x: Enqueue element x into the end of the queue.
2: Dequeue the element at the front of the queue.
3: Print the element at the front of the queue.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:-
<b>enQueue()</b> that takes the integer x(input element) as a parameter.
<b>deQueue()</b>that takes no parameter
<b>Print1()</b>that print the element which is at the front of the queue
Constraints:-
1 <= x <= 10^9
1 <= number of queries <= 1000
Note:- It is guaranteed that the queue will never be empty whenever Print function or deQueue function is called.Print the value of the element which is at the front of the queue on a new line in Print1().Sample Input:-
10
1 42
2
1 14
3
1 28
3
1 60
1 78
2
2
Sample Output:-
14
14, I have written this Solution Code:
static void enQueue(int x)
{
// Move all elements from s1 to s2
while (!s1.isEmpty())
{
s2.push(s1.pop());
//s1.pop();
}
// Push item into s1
s1.push(x);
// Push everything back to s1
while (!s2.isEmpty())
{
s1.push(s2.pop());
//s2.pop();
}
}
// Dequeue an item from the queue
static void deQueue()
{
// if first stack is empty
if (s1.isEmpty())
{
System.out.println("Q is Empty");
System.exit(0);
}
// Return top of s1
int x = s1.peek();
s1.pop();
}
static void Print1(){
int x=s1.peek();
System.out.println(x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: void fizzbuzz(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){cout<<"FizzBuzz"<<" ";}
else if(i%5==0){cout<<"Buzz ";}
else if(i%3==0){cout<<"Fizz ";}
else{cout<<i<<" ";}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int x= sc.nextInt();
fizzbuzz(x);
}
static void fizzbuzz(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.print("FizzBuzz ");}
else if(i%5==0){System.out.print("Buzz ");}
else if(i%3==0){System.out.print("Fizz ");}
else{System.out.print(i+" ");}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: def fizzbuzz(n):
for i in range (1,n+1):
if (i%3==0 and i%5==0):
print("FizzBuzz",end=' ')
elif i%3==0:
print("Fizz",end=' ')
elif i%5==0:
print("Buzz",end=' ')
else:
print(i,end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N for each I (1 < = I < = N), you have to print the number except:-
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 1000Print N space separated number or Fizz buzz according to the condition.Sample Input:-
3
Sample Output:-
1 2 Fizz
Sample Input:-
5
Sample Output:-
1 2 Fizz 4 Buzz, I have written this Solution Code: void fizzbuzz(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){printf("FizzBuzz ");}
else if(i%5==0){printf("Buzz ");}
else if(i%3==0){printf("Fizz ");}
else{printf("%d ",i);}
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three integers A, B and C, which are the side lengths of a triangle in some order. Print "Yes" if this triangle is right-angled, otherwise print "No".The input consists of a single line containing three space-separated integers A, B and C.
Constraints:
1 ≤ A, B, C ≤ 1000Print a single word "Yes" if the given triangle is right-angled, otherwise print "No".
Note:
The quotation marks are only for clarity, you should not print them in output.
The output is case-sensitive.Sample Input 1:
3 4 5
Sample Output 1:
Yes
Sample Input 2:
10 9 10
Sample Output 2:
No
Sample Input 3:
5 4 3
Sample Output 3:
Yes, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] inp = br.readLine().split(" ");
int a = Integer.parseInt(inp[0]);
int b = Integer.parseInt(inp[1]);
int c = Integer.parseInt(inp[2]);
if(a * a == b * b + c * c)
bw.write("Yes"+"\n");
else if(b * b == a * a + c * c)
bw.write("Yes"+"\n");
else if(c * c == b * b + a * a)
bw.write("Yes"+"\n");
else
bw.write("No"+"\n");
bw.flush();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three integers A, B and C, which are the side lengths of a triangle in some order. Print "Yes" if this triangle is right-angled, otherwise print "No".The input consists of a single line containing three space-separated integers A, B and C.
Constraints:
1 ≤ A, B, C ≤ 1000Print a single word "Yes" if the given triangle is right-angled, otherwise print "No".
Note:
The quotation marks are only for clarity, you should not print them in output.
The output is case-sensitive.Sample Input 1:
3 4 5
Sample Output 1:
Yes
Sample Input 2:
10 9 10
Sample Output 2:
No
Sample Input 3:
5 4 3
Sample Output 3:
Yes, I have written this Solution Code: a,b,c=map(int,input().split())
if(a*a+b*b==c*c or a*a+c*c==b*b or b*b+c*c==a*a):
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 three integers A, B and C, which are the side lengths of a triangle in some order. Print "Yes" if this triangle is right-angled, otherwise print "No".The input consists of a single line containing three space-separated integers A, B and C.
Constraints:
1 ≤ A, B, C ≤ 1000Print a single word "Yes" if the given triangle is right-angled, otherwise print "No".
Note:
The quotation marks are only for clarity, you should not print them in output.
The output is case-sensitive.Sample Input 1:
3 4 5
Sample Output 1:
Yes
Sample Input 2:
10 9 10
Sample Output 2:
No
Sample Input 3:
5 4 3
Sample Output 3:
Yes, I have written this Solution Code:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %mod;
a = a * a %mod;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("input.txt","r"))
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
#endif
int a[3];
for(int i=0;i<3;i++)
cin>>a[i];
sort(a,a+3);
if(a[0]*a[0]+a[1]*a[1]==a[2]*a[2])
cout<<"Yes";
else
cout<<"No";
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the "mul" function which will properly return the answer by performing multiplication when invoked as below syntax.
Ex:- mul(2)*(3)*(4) - > 24Three integers will be given as input
<b>Constarints</b>
1≤ a,b,c ≤1000The number resulting after the multiplication of 3 input numbersSample input:-
1 5 2
Sample output:-
10
<b>Explanation:-</b>
1*5*2 = 10, I have written this Solution Code:
function mul (x) {
return function (y) { // anonymous function
return function (z) { // anonymous function
console.log(x*y*z);
};
};
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the "mul" function which will properly return the answer by performing multiplication when invoked as below syntax.
Ex:- mul(2)*(3)*(4) - > 24Three integers will be given as input
<b>Constarints</b>
1≤ a,b,c ≤1000The number resulting after the multiplication of 3 input numbersSample input:-
1 5 2
Sample output:-
10
<b>Explanation:-</b>
1*5*2 = 10, 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 a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
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: Complete the "mul" function which will properly return the answer by performing multiplication when invoked as below syntax.
Ex:- mul(2)*(3)*(4) - > 24Three integers will be given as input
<b>Constarints</b>
1≤ a,b,c ≤1000The number resulting after the multiplication of 3 input numbersSample input:-
1 5 2
Sample output:-
10
<b>Explanation:-</b>
1*5*2 = 10, I have written this Solution Code: x,y,z= map(int,input().split())
print(x*y*z), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array of integers. Consider absolute difference between all the pairs of the the elements. You need to find Kth smallest absolute difference. If the size of the array is N then value of K will be less than N and more than or equal to 1.The first line of input contains number of test cases T.
The first line of each test case contains a two integers N and K denoting the number of elements in the array A and difference you need to output. The second line of each test case contains N space separated integers denoting the elements of the array A
Constraints:
1<= T <= 10
2 <= N <= 100000
1 <= K < N < 100000
0 <= A[i] <= 100000For each test case, output Kth smallest absolute difference.Input :
1
6 2
1 3 4 1 3 8
Output :
0
Explanation :
Test case 1: First smallest difference is 0, between the pair (1, 1) and second smallest absolute difference difference is also 0 between the pairs (3, 3)., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine().trim());
while (t-- > 0) {
String str[] = read.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int arr[] = new int[n];
str = read.readLine().trim().split(" ");
for (int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
System.out.println(Math.abs(small(arr, k)));
}
}
public static int small(int arr[], int k) {
Arrays.sort(arr);
int l = 0, r = arr[arr.length - 1] - arr[0];
while (r > l) {
int mid = l + (r - l) / 2;
if (count(arr, mid) < k) {
l = mid + 1;
} else {
r = mid;
}
}
return r;
}
public static int count(int arr[], int mid) {
int ans = 0, j = 0;
for (int i = 1; i < arr.length; ++i) {
while (j < i && arr[i] - arr[j] > mid) {
++j;
}
ans += i - j;
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string S of length N consisting of lowercase characters 'a' and 'b'. In one operation, you can select a character and make it equal to one of its adjacent characters. For example, if S = "aab", in one operation you can convert it to any of the following:
1. "aab": By changing the 1<sup>st</sup> character to the 2<sup>nd</sup> character.
2. "aab": By changing the 2<sup>nd</sup> character to the 1<sup>st</sup> character.
3. "abb": By changing the 2<sup>nd</sup> character to the 3<sup>rd</sup> character.
4. "aaa": By changing the 3<sup>rd</sup> character to the 2<sup>nd</sup> character.
Find the minimum number of operations to make all the characters in the string equal.The first line of the input contains a single integer N.
The second line contains a string S of length N consisting of only 'a' and 'b'.
<b> Constraints: </b>
1 ≤ N ≤ 5000Print the minimum number of operations to make all the characters in the string equal.Sample Input 1:
4
abaa
Sample Output 1:
1
Sample Explanation 1:
You can replace the 'b' at the second position by the 'a' at the first position. Now the string becomes "aaaa".
Sample Input 2:
5
bbbaa
Sample Output 2:
2
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int c = 0;
String str = br.readLine();
for(int i=0;i<n;i++){
if(str.charAt(i) == 'a'){
c++;
}
}
if((n-c)<c){
System.out.println(n-c);
}else{
System.out.println(c);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string S of length N consisting of lowercase characters 'a' and 'b'. In one operation, you can select a character and make it equal to one of its adjacent characters. For example, if S = "aab", in one operation you can convert it to any of the following:
1. "aab": By changing the 1<sup>st</sup> character to the 2<sup>nd</sup> character.
2. "aab": By changing the 2<sup>nd</sup> character to the 1<sup>st</sup> character.
3. "abb": By changing the 2<sup>nd</sup> character to the 3<sup>rd</sup> character.
4. "aaa": By changing the 3<sup>rd</sup> character to the 2<sup>nd</sup> character.
Find the minimum number of operations to make all the characters in the string equal.The first line of the input contains a single integer N.
The second line contains a string S of length N consisting of only 'a' and 'b'.
<b> Constraints: </b>
1 ≤ N ≤ 5000Print the minimum number of operations to make all the characters in the string equal.Sample Input 1:
4
abaa
Sample Output 1:
1
Sample Explanation 1:
You can replace the 'b' at the second position by the 'a' at the first position. Now the string becomes "aaaa".
Sample Input 2:
5
bbbaa
Sample Output 2:
2
, I have written this Solution Code: input()
s = input()
a = s.count("a")
b = s.count("b")
print(a if a<b else b), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a string S of length N consisting of lowercase characters 'a' and 'b'. In one operation, you can select a character and make it equal to one of its adjacent characters. For example, if S = "aab", in one operation you can convert it to any of the following:
1. "aab": By changing the 1<sup>st</sup> character to the 2<sup>nd</sup> character.
2. "aab": By changing the 2<sup>nd</sup> character to the 1<sup>st</sup> character.
3. "abb": By changing the 2<sup>nd</sup> character to the 3<sup>rd</sup> character.
4. "aaa": By changing the 3<sup>rd</sup> character to the 2<sup>nd</sup> character.
Find the minimum number of operations to make all the characters in the string equal.The first line of the input contains a single integer N.
The second line contains a string S of length N consisting of only 'a' and 'b'.
<b> Constraints: </b>
1 ≤ N ≤ 5000Print the minimum number of operations to make all the characters in the string equal.Sample Input 1:
4
abaa
Sample Output 1:
1
Sample Explanation 1:
You can replace the 'b' at the second position by the 'a' at the first position. Now the string becomes "aaaa".
Sample Input 2:
5
bbbaa
Sample Output 2:
2
, I have written this Solution Code: //Author: Xzirium
//Time and Date: 03:04:29 27 December 2021
//Optional FAST
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
//Required Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//Required namespaces
using namespace std;
using namespace __gnu_pbds;
//Required defines
#define endl '\n'
#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(long long a=b;a<c;a++)
#define FORD(a,b,c) for(long long a=b;a>c;a--)
//Required typedefs
template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <typename T> using ordered_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
//Required Constants
const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);
const long double PI=3.14159265358979;
// Required random number generators
// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
//Required Functions
ll power(ll b, ll e)
{
ll r = 1ll;
for(; e > 0; e /= 2, (b *= b) %= MOD)
if(e % 2) (r *= b) %= MOD;
return r;
}
ll modInverse(ll a)
{
return power(a,MOD-2);
}
//Work
int main()
{
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen ("INPUT.txt" , "r" , stdin);
//freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t clk;
clk = clock();
//-----------------------------------------------------------------------------------------------------------//
READV(N);
string S;
cin>>S;
ll a=0,b=0;
FORI(i,0,N)
{
if(S[i]=='a')
{
a++;
}
else
{
b++;
}
}
cout<<min(a,b)<<endl;
//-----------------------------------------------------------------------------------------------------------//
clk = clock() - clk;
cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N 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: Given an integer N and a matrix of size (N x N). Check whether given matrix is identity or not.
<b> Note </b>
An identity matrix is a square matrix in which all the elements of principal diagonal are one, and all other elements are zeros.First line contain a single integer N
Next N line contain N space- separated integer i. e. elements of matrix.If the given matrix is the identity matrix. print "Yes" otherwise print "NO"
Constraints:
1<=N<=100Sample Input 1:
3
1 0 0
0 1 0
0 0 1
Sample Output 1:
Yes
Explanation:
Given matrix is an identity matrix because all main diagonal elements are 1 and rest of elements are 0., I have written this Solution Code: import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int [][]arr=new int[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
arr[i][j] = s.nextInt();
}
boolean flag=false;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{
if(i==j && arr[i][j]!=1)flag=true;
else if(i!=j && arr[i][j]!=0)flag=true;
}
}
if(flag==true){
System.out.println("No");
}
else {
System.out.println("Yes");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon 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>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code:
int DragonSlayer(int A, int B, int C,int D){
int x = C/B;
if(C%B!=0){x++;}
int y = A/D;
if(A%D!=0){y++;}
if(x<y){return 0;}
return 1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon 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>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code: static int DragonSlayer(int A, int B, int C,int D){
int x = C/B;
if(C%B!=0){x++;}
int y = A/D;
if(A%D!=0){y++;}
if(x<y){return 0;}
return 1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon 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>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code: int DragonSlayer(int A, int B, int C,int D){
int x = C/B;
if(C%B!=0){x++;}
int y = A/D;
if(A%D!=0){y++;}
if(x<y){return 0;}
return 1;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natsu is fighting with a dragon that has A Health and B attack power and Natsu has C health and D attack power. The fight goes in turns first Natsu will attack the Dragon then Dragon will attack Natsu and this goes on. The fight will stop when either the dragon's or Natsu's health drops zero or below. Your task is to check whether Natsu will able to slay the Dragon 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>DragonSlayer()</b> that takes integers A, B, C, and D as arguments.
Constraints:-
1 <= A, B, C, D <= 1000Return 0 if Dragon wins else return 1.Sample Input:-
8 2 5 3
Sample Output:-
1
Explanation:-
Natsu's attack:- A = 5, B = 2, C = 5, D = 3
Dragon's attack:- A = 5, B = 2, C = 3, D =3
Natsu's attack:- A = 2, B =2, C = 3, D=3
Dragon's attack:- A = 2, B =2, C = 1, D=3
Natsu's attack:- A = -1, B =2, C = 1, D=3
Natsu's win, I have written this Solution Code:
def DragonSlayer(A,B,C,D):
x = C//B
if(C%B!=0):
x=x+1
y = A//D
if(A%D!=0):
y=y+1
if(x<y):
return 0
return 1
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code: def Average(A,B,C):
return (A+B+C)//3
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code:
int Average(int A,int B, int C){
return (A+B+C)/3;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code:
static int Average(int A,int B, int C){
return (A+B+C)/3;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mohit has three integers A, B, and C with him he wants to find the average of these three integers however he is weak in maths, so help him to find the average. You need to report the floor of the average value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Average()</b> that takes integers A, B, and C as arguments.
Constraints:-
1 <= A, B, and C <= 10000Return the floor of average of these numbers.Sample Input:-
3 4 5
Sample Output:-
4
Sample Input:-
3 4 4
Sample Output:-
3, I have written this Solution Code:
int Average(int A,int B, int C){
return (A+B+C)/3;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String arr[][]=new String[n][n];
String transpose[][]=new String[n][n];
int row;
int cols;
for(row=0;row<n;row++)
{
String rowNum=br.readLine();
String rowVals[]=rowNum.split(" ");
for(cols=0; cols<n;cols++)
{
arr[row][cols]=rowVals[cols];
}
}
for(row=0;row<n;row++)
{
for(cols=0; cols<n;cols++)
{
transpose[row][cols]=arr[cols][row];
System.out.print(transpose[row][cols]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: x=int(input())
l1=[]
for i in range(x):
a1=list(map(int,input().split()))
l1.append(a1)
l4=[]
for j in range(x):
l3=[]
for i in range(x):
l3.append(l1[i][j])
l4.append(l3)
for i in range(x):
for j in range(x):
print(l4[i][j], end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a NxN matrix. You need to find the <a href = "https://en.wikipedia.org/wiki/Transpose">transpose</a> of the matrix.
The matrix is of form:
a b c ...
d e f ...
g h i ...
...........
There are N elements in each row.The first line of the input contains an integer N denoting the size of the square matrix.
The next N lines contain N single-spaced integers.
<b>Constraints</b>
1 <= N <= 100
1 <=Ai <= 100000Output the transpose of the matrix in similar format as that of the input.Sample Input
2
1 3
2 2
Sample Output
1 2
3 2
Sample Input:
1 2
3 4
Sample Output:
1 3
2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[j][i];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n cities in the universe and our beloved Spider-Man is in city 1. He doesn't like to travel by vehicles, so he shot webs forming edges between some pairs of cities. Eventually, there were m edges and each had some cost associated with it.
Spider-Man now defines the cost of a path p from cities p<sub>1</sub> to p<sub>k</sub> as w<sub>1</sub> + 2w<sub>2</sub> + 3w<sub>3</sub> . . . + (k-1)*w<sub>k-1</sub>, where w<sub>i</sub> is the cost of an edge from p<sub>i</sub> to p<sub>i+1</sub>.
Thus, the minimum distance between cities i and j is the smallest cost of a path starting from i and ending at j.
Find the minimum distance from city 1 to all the cities i (1 ≤ i ≤ n). If there exists no way to go from city 1 to city i, print -1.
<b>Note: </b>
All the edges are bidirectional. There may be multiple edges and self-loops in the input.The first line contains two space separated integers n and m - the number of nodes and edges respectively.
The next m lines contain three-space separated integers x, y, w - representing an edge between x and y with cost w.
<b>Constraints:</b>
1 ≤ n ≤ 3000
0 ≤ m ≤ 10000
1 ≤ x, y ≤ n
1 ≤ w ≤ 10<sup>9</sup>Output n lines. In the i<sup>th</sup> line, output the minimum distance from city 1 to the i<sup>th</sup> city. If there exists no such path, output -1.Sample Input
6 5
2 4 3
2 3 4
2 1 2
2 5 6
1 5 2
Sample Output
0
2
10
8
2
-1
Explanation:
Shortest path from 1 to 3 is (1->2->3) with total weight= 1*2+2*4=10
Shortest path from 1 to 5 is (1->5) with total weight= 1*2=2
There doesn't exist any path from 1 to 6 so print -1
, I have written this Solution Code:
import sys
from collections import defaultdict
from heapq import heappush, heappop
n, m = map(int, input().split())
d = defaultdict(list)
dist = [sys.maxsize]*n
dist[0] = 0
for _ in range(m):
start, dest, wt = map(int, input().split())
d[start-1].append((dest-1, wt))
d[dest-1].append((start-1, wt))
heap = [(0, 0, 0)]
while heap:
count, cost, u= heappop(heap)
for vertex, weight in d[u]:
if dist[vertex] > cost + weight*(count+1):
dist[vertex] = cost + weight*(count+1)
heappush(heap, (count+1, dist[vertex], vertex))
for d in dist:
if d == sys.maxsize:
print(-1)
else:
print(d), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n cities in the universe and our beloved Spider-Man is in city 1. He doesn't like to travel by vehicles, so he shot webs forming edges between some pairs of cities. Eventually, there were m edges and each had some cost associated with it.
Spider-Man now defines the cost of a path p from cities p<sub>1</sub> to p<sub>k</sub> as w<sub>1</sub> + 2w<sub>2</sub> + 3w<sub>3</sub> . . . + (k-1)*w<sub>k-1</sub>, where w<sub>i</sub> is the cost of an edge from p<sub>i</sub> to p<sub>i+1</sub>.
Thus, the minimum distance between cities i and j is the smallest cost of a path starting from i and ending at j.
Find the minimum distance from city 1 to all the cities i (1 ≤ i ≤ n). If there exists no way to go from city 1 to city i, print -1.
<b>Note: </b>
All the edges are bidirectional. There may be multiple edges and self-loops in the input.The first line contains two space separated integers n and m - the number of nodes and edges respectively.
The next m lines contain three-space separated integers x, y, w - representing an edge between x and y with cost w.
<b>Constraints:</b>
1 ≤ n ≤ 3000
0 ≤ m ≤ 10000
1 ≤ x, y ≤ n
1 ≤ w ≤ 10<sup>9</sup>Output n lines. In the i<sup>th</sup> line, output the minimum distance from city 1 to the i<sup>th</sup> city. If there exists no such path, output -1.Sample Input
6 5
2 4 3
2 3 4
2 1 2
2 5 6
1 5 2
Sample Output
0
2
10
8
2
-1
Explanation:
Shortest path from 1 to 3 is (1->2->3) with total weight= 1*2+2*4=10
Shortest path from 1 to 5 is (1->5) with total weight= 1*2=2
There doesn't exist any path from 1 to 6 so print -1
, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF =1e18;
vector<tuple<int, int, int>> adj;
void solve()
{
int n, m;
cin>>n>>m;
// assert(1<=n && n<=3000);
// assert(0<=m && m<=10000);
adj.resize(n);
for(int i = 0;i<m;i++)
{
int x, y, w;
cin>>x>>y>>w;
x--;
y--;
// assert(0<=x && x<n);
// assert(0<=y && y<n);
// assert(1<=w && w<=1e9);
adj.push_back({x, y, w});
adj.push_back({y, x, w});
}
vector<int> dp_old(n, INF);
vector<int> dp_new(n, INF);
dp_old[0] = 0;
for(int i = 1;i<=n;i++)
{
fill(dp_new.begin(), dp_new.end(), INF);
for(auto [x, y,w]:adj)
{
dp_new[y]= min({dp_new[y], dp_old[x] + i * w});
}
for(int j = 0;j<n;j++)
dp_new[j] = min(dp_new[j], dp_old[j]);
swap(dp_new, dp_old);
}
for(int i = 0;i<n;i++)
{
if(dp_old[i] == INF)
dp_old[i] = -1;
cout<<dp_old[i]<<"\n";
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#endif
solve();
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n cities in the universe and our beloved Spider-Man is in city 1. He doesn't like to travel by vehicles, so he shot webs forming edges between some pairs of cities. Eventually, there were m edges and each had some cost associated with it.
Spider-Man now defines the cost of a path p from cities p<sub>1</sub> to p<sub>k</sub> as w<sub>1</sub> + 2w<sub>2</sub> + 3w<sub>3</sub> . . . + (k-1)*w<sub>k-1</sub>, where w<sub>i</sub> is the cost of an edge from p<sub>i</sub> to p<sub>i+1</sub>.
Thus, the minimum distance between cities i and j is the smallest cost of a path starting from i and ending at j.
Find the minimum distance from city 1 to all the cities i (1 ≤ i ≤ n). If there exists no way to go from city 1 to city i, print -1.
<b>Note: </b>
All the edges are bidirectional. There may be multiple edges and self-loops in the input.The first line contains two space separated integers n and m - the number of nodes and edges respectively.
The next m lines contain three-space separated integers x, y, w - representing an edge between x and y with cost w.
<b>Constraints:</b>
1 ≤ n ≤ 3000
0 ≤ m ≤ 10000
1 ≤ x, y ≤ n
1 ≤ w ≤ 10<sup>9</sup>Output n lines. In the i<sup>th</sup> line, output the minimum distance from city 1 to the i<sup>th</sup> city. If there exists no such path, output -1.Sample Input
6 5
2 4 3
2 3 4
2 1 2
2 5 6
1 5 2
Sample Output
0
2
10
8
2
-1
Explanation:
Shortest path from 1 to 3 is (1->2->3) with total weight= 1*2+2*4=10
Shortest path from 1 to 5 is (1->5) with total weight= 1*2=2
There doesn't exist any path from 1 to 6 so print -1
, I have written this Solution Code: import java.io.*;import java.util.*;import java.math.*;import static java.lang.Math.*;import static java.
util.Map.*;import static java.util.Arrays.*;import static java.util.Collections.*;
import static java.lang.System.*;
public class Main
{
public void tq()throws Exception
{
st=new StringTokenizer(bq.readLine());
int tq=1;
sb=new StringBuilder(2000000);
o:
while(tq-->0)
{
int n=i();
int m=i();
LinkedList<int[]> l[]=new LinkedList[n];
for(int x=0;x<n;x++)l[x]=new LinkedList<>();
for(int x=0;x<m;x++)
{
int a=i()-1;
int b=i()-1;
int c=i();
l[a].add(new int[]{b,c});
l[b].add(new int[]{a,c});
}
long d[]=new long[n];
for(int x=0;x<n;x++)d[x]=maxl;
d[0]=0l;
PriorityQueue<long[]> p=new PriorityQueue<>(5000,(a,b)->a[2]-b[2]<1l?-1:1);
p.add(new long[]{0l,0,0});
while(p.size()>0)
{
long r[]=p.poll();
long di=r[0];
int no=(int)r[1];
long mu=r[2];
for(int e[]:l[no])
{
int node=e[0];
int w=e[1];
long de=di+w*(mu+1);
if(d[node]>de)
{
d[node]=de;
p.add(new long[]{de,node,mu+1});
}
}
}
for(long x:d)
{
sl(x==maxl?-1:x);
}
}
p(sb);
}
int di[][]={{-1,0},{1,0},{0,-1},{0,1}};
int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};
long mod=1000000007l;int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE, minl=Long.
MIN_VALUE;BufferedReader bq=new BufferedReader(new InputStreamReader(in));StringTokenizer st;
StringBuilder sb;public static void main(String[] a)throws Exception{new Main().tq();}int[] so(int ar[])
{Integer r[]=new Integer[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];sort(r);for(int x=0;x<ar.length;
x++)ar[x]=r[x];return ar;}long[] so(long ar[]){Long r[]=new Long[ar.length];for(int x=0;x<ar.length;x++)
r[x]=ar[x];sort(r);for(int x=0;x<ar.length;x++)ar[x]=r[x];return ar;}
char[] so(char ar[]) {Character
r[]=new Character[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];sort(r);for(int x=0;x<ar.length;x++)
ar[x]=r[x];return ar;}void s(String s){sb.append(s);}void s(int s){sb.append(s);}void s(long s){sb.
append(s);}void s(char s){sb.append(s);}void s(double s){sb.append(s);}void ss(){sb.append(' ');}void sl
(String s){sb.append(s);sb.append("\n");}void sl(int s){sb.append(s);sb.append("\n");}void sl(long s){sb
.append(s);sb.append("\n");}void sl(char s) {sb.append(s);sb.append("\n");}void sl(double s){sb.append(s)
;sb.append("\n");}void sl(){sb.append("\n");}int l(int v){return 31-Integer.numberOfLeadingZeros(v);}
long l(long v){return 63-Long.numberOfLeadingZeros(v);}int sq(int a){return (int)sqrt(a);}long sq(long a)
{return (long)sqrt(a);}long gcd(long a,long b){while(b>0l){long c=a%b;a=b;b=c;}return a;}int gcd(int a,int b)
{while(b>0){int c=a%b;a=b;b=c;}return a;}boolean pa(String s,int i,int j){while(i<j)if(s.charAt(i++)!=
s.charAt(j--))return false;return true;}boolean[] si(int n) {boolean bo[]=new boolean[n+1];bo[0]=true;bo[1]
=true;for(int x=4;x<=n;x+=2)bo[x]=true;for(int x=3;x*x<=n;x+=2){if(!bo[x]){int vv=(x<<1);for(int y=x*x;y<=n;
y+=vv)bo[y]=true;}}return bo;}long mul(long a,long b,long m) {long r=1l;a%=m;while(b>0){if((b&1)==1)
r=(r*a)%m;b>>=1;a=(a*a)%m;}return r;}int i()throws IOException{if(!st.hasMoreTokens())st=new
StringTokenizer(bq.readLine());return Integer.parseInt(st.nextToken());}long l()throws IOException
{if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Long.parseLong(st.nextToken());}String
s()throws IOException {if (!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return st.nextToken();}
double d()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Double.
parseDouble(st.nextToken());}void p(Object p){out.print(p);}void p(String p){out.print(p);}void p(int p)
{out.print(p);}void p(double p){out.print(p);}void p(long p){out.print(p);}void p(char p){out.print(p);}void
p(boolean p){out.print(p);}void pl(Object p){out.println(p);}void pl(String p){out.println(p);}void pl(int p)
{out.println(p);}void pl(char p){out.println(p);}void pl(double p){out.println(p);}void pl(long p){out.
println(p);}void pl(boolean p)
{out.println(p);}void pl(){out.println();}void s(int a[]){for(int e:a)
{sb.append(e);sb.append(' ');}sb.append("\n");}
void s(long a[])
{for(long e:a){sb.append(e);sb.append(' ')
;}sb.append("\n");}void s(int ar[][]){for(int a[]:ar){for(int e:a){sb.append(e);sb.append(' ');}sb.append
("\n");}}
void s(char a[])
{for(char e:a){sb.append(e);sb.append(' ');}sb.append("\n");}void s(char ar[][])
{for(char a[]:ar){for(char e:a){sb.append(e);sb.append(' ');}sb.append("\n");}}int[] ari(int n)throws
IOException {int ar[]=new int[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0;
x<n;x++)ar[x]=Integer.parseInt(st.nextToken());return ar;}int[][] ari(int n,int m)throws
IOException {int ar[][]=new int[n][m];for(int x=0;x<n;x++){if (!st.hasMoreTokens())st=new StringTokenizer
(bq.readLine());for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());}return ar;}long[] arl
(int n)throws IOException {long ar[]=new long[n];if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine())
;for(int x=0;x<n;x++)ar[x]=Long.parseLong(st.nextToken());return ar;}long[][] arl(int n,int m)throws
IOException {long ar[][]=new long[n][m];for(int x=0;x<n;x++) {if(!st.hasMoreTokens()) st=new
StringTokenizer(bq.readLine());for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());}return ar;}
String[] ars(int n)throws IOException {String ar[] =new String[n];if(!st.hasMoreTokens())st=new
StringTokenizer(bq.readLine());for(int x=0;x<n;x++)ar[x]=st.nextToken();return ar;}double[] ard
(int n)throws IOException {double ar[] =new double[n];if(!st.hasMoreTokens())st=new StringTokenizer
(bq.readLine());for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());return ar;}double[][] ard
(int n,int m)throws IOException{double ar[][]=new double[n][m];for(int x=0;x<n;x++) {if(!st.hasMoreTokens())
st=new StringTokenizer(bq.readLine());for(int y=0;y<m;y++) ar[x][y]=Double.parseDouble(st.nextToken());}
return ar;}char[] arc(int n)throws IOException{char ar[]=new char[n];if(!st.hasMoreTokens())st=new
StringTokenizer(bq.readLine());for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);return ar;}char[][]
arc(int n,int m)throws IOException {char ar[][]=new char[n][m];for(int x=0;x<n;x++){String s=bq.readLine();
for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);}return ar;}void p(int ar[])
{StringBuilder sb=new StringBuilder
(2*ar.length);for(int a:ar){sb.append(a);sb.append(' ');}out.println(sb);}void p(int ar[][])
{StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);for(int a[]:ar){for(int aa:a){sb.append(aa);
sb.append(' ');}sb.append("\n");}p(sb);}void p(long ar[]){StringBuilder sb=new StringBuilder
(2*ar.length);for(long a:ar){ sb.append(a);sb.append(' ');}out.println(sb);}
void p(long ar[][])
{StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);for(long a[]:ar){for(long aa:a){sb.append(aa);
sb.append(' ');}sb.append("\n");}p(sb);}void p(String ar[]){int c=0;for(String s:ar)c+=s.length()+1;
StringBuilder sb=new StringBuilder(c);for(String a:ar){sb.append(a);sb.append(' ');}out.println(sb);}
void p(double ar[])
{StringBuilder sb=new StringBuilder(2*ar.length);for(double a:ar){sb.append(a);
sb.append(' ');}out.println(sb);}void p
(double ar[][]){StringBuilder sb=new StringBuilder(2*
ar.length*ar[0].length);for(double a[]:ar){for(double aa:a){sb.append(aa);sb.append(' ');}sb.append("\n")
;}p(sb);}void p(char ar[])
{StringBuilder sb=new StringBuilder(2*ar.length);for(char aa:ar){sb.append(aa);
sb.append(' ');}out.println(sb);}void p(char ar[][]){StringBuilder sb=new StringBuilder(2*ar.length*ar[0]
.length);for(char a[]:ar){for(char aa:a){sb.append(aa);sb.append(' ');}sb.append("\n");}p(sb);}
}, 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: 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: Sara loves patterns, today she encounters an interesting pattern and wants to write a code that can print the pattern of a given height N. As Sara is weak in programming help her to code it.
The pattern for height 6:-
0 4 8 12 16 20
6 10 14 18 22 26
12 16 20 24 28 32
18 22 26 30 34 38
24 28 32 36 40 44
30 34 38 42 46 50<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 100Print the given pattern.Sample Input:-
3
Sample Output:-
0 4 8
6 10 14
12 16 20
Sample Input:-
5
Sample Output:-
0 4 8 12 16
6 10 14 18 22
12 16 20 24 28
18 22 26 30 34
24 28 32 36 40, I have written this Solution Code: static void Pattern(int N){
int x=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
System.out.print(x+4*j+" ");
}
System.out.println();
x+=6;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves patterns, today she encounters an interesting pattern and wants to write a code that can print the pattern of a given height N. As Sara is weak in programming help her to code it.
The pattern for height 6:-
0 4 8 12 16 20
6 10 14 18 22 26
12 16 20 24 28 32
18 22 26 30 34 38
24 28 32 36 40 44
30 34 38 42 46 50<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 100Print the given pattern.Sample Input:-
3
Sample Output:-
0 4 8
6 10 14
12 16 20
Sample Input:-
5
Sample Output:-
0 4 8 12 16
6 10 14 18 22
12 16 20 24 28
18 22 26 30 34
24 28 32 36 40, I have written this Solution Code: def Pattern(N):
x=0
for i in range (0,N):
for j in range (0,N):
print(x+4*j,end=' ')
print()
x = x+6
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves patterns, today she encounters an interesting pattern and wants to write a code that can print the pattern of a given height N. As Sara is weak in programming help her to code it.
The pattern for height 6:-
0 4 8 12 16 20
6 10 14 18 22 26
12 16 20 24 28 32
18 22 26 30 34 38
24 28 32 36 40 44
30 34 38 42 46 50<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 100Print the given pattern.Sample Input:-
3
Sample Output:-
0 4 8
6 10 14
12 16 20
Sample Input:-
5
Sample Output:-
0 4 8 12 16
6 10 14 18 22
12 16 20 24 28
18 22 26 30 34
24 28 32 36 40, I have written this Solution Code: void Pattern(int N){
int x=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
printf("%d ",x+4*j);
}
printf("\n");
x+=6;
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves patterns, today she encounters an interesting pattern and wants to write a code that can print the pattern of a given height N. As Sara is weak in programming help her to code it.
The pattern for height 6:-
0 4 8 12 16 20
6 10 14 18 22 26
12 16 20 24 28 32
18 22 26 30 34 38
24 28 32 36 40 44
30 34 38 42 46 50<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 100Print the given pattern.Sample Input:-
3
Sample Output:-
0 4 8
6 10 14
12 16 20
Sample Input:-
5
Sample Output:-
0 4 8 12 16
6 10 14 18 22
12 16 20 24 28
18 22 26 30 34
24 28 32 36 40, I have written this Solution Code: void Pattern(int N){
int x=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
printf("%d ",x+4*j);
}
printf("\n");
x+=6;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<b>User Task</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: def Pattern(N):
print('*')
for i in range (0,N-2):
print('*',end='')
for j in range (0,i+1):
print('^',end='')
print('*')
for i in range (0,N+1):
print('*',end='')
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<b>User Task</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: void Pattern(int N){
cout<<'*'<<endl;
for(int i=0;i<N-2;i++){
cout<<'*';
for(int j=0;j<=i;j++){
cout<<'^';
}
cout<<'*'<<endl;
}
for(int i=0;i<=N;i++){
cout<<'*';
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<b>User Task</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: static void Pattern(int N){
System.out.println('*');
for(int i=0;i<N-2;i++){
System.out.print('*');
for(int j=0;j<=i;j++){
System.out.print('^');
}System.out.println('*');
}
for(int i=0;i<=N;i++){
System.out.print('*');
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, you have to print the given below pattern for N ≥ 3.
<b>Example</b>
Pattern for N = 4
*
*^*
*^^*
*****<b>User Task</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Pattern()</b> that takes integers N as an argument.
<b>Constraints</b>
3 ≤ N ≤ 100Print the given pattern for size N.<b>Sample Input 1</b>
3
<b>Sample Output 1</b>
*
*^*
****
<b>Sample Input 2</b>
6
<b>Sample Output 2</b>
*
*^*
*^^*
*^^^*
*^^^^*
*******, I have written this Solution Code: void Pattern(int N){
printf("*\n");
for(int i=0;i<N-2;i++){
printf("*");
for(int j=0;j<=i;j++){
printf("^");}printf("*\n");
}
for(int i=0;i<=N;i++){
printf("*");
}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 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, if the year is a multiple of 100 and not a multiple of 400, then it is not a leap year.<b>User Task:</b>
Complete the function <b>LeapYear()</b> that takes integer n as a parameter.
<b>Constraint:</b>
1 <= n <= 5000If it is a leap year then print <b>YES</b> and if it is not a leap year, then print <b>NO</b>Sample Input:
2000
Sample Output:
YES
Sample Input:
2003
Sample Output:
NO
Sample Input:
1900
Sample Output:
NO, I have written this Solution Code: n = int(input())
if (n%4==0 and n%100!=0 or n%400==0):
print("YES")
elif 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 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, if the year is a multiple of 100 and not a multiple of 400, then it is not a leap year.<b>User Task:</b>
Complete the function <b>LeapYear()</b> that takes integer n as a parameter.
<b>Constraint:</b>
1 <= n <= 5000If it is a leap year then print <b>YES</b> and if it is not a leap year, then print <b>NO</b>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 n = scanner.nextInt();
LeapYear(n);
}
static void LeapYear(int year){
if(year%400==0 || (year%100 != 0 && year%4==0)){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 take part in <b>N</b> quizzes and in the i<sup>th</sup> quiz you get a score of Q<sub>i</sub>. Your friends are very competitive with you and they ask you the strength of your quiz scores. Strength of an array is defined as the following:
The maximum growth Q<sub>j</sub> - Q<sub>i</sub> (Q<sub>j</sub> > Q<sub>i</sub>) between two quizzes i and j such that i < j and there is no quiz <b>k</b> such that <b>i < k < j</b> and <b>Q<sub>k</sub> > Q<sub>i</sub></b>.
<b>If there is no such pair of indexes, print -1. </b>
Print the strength of your quiz marks in order to impress your friends.First line contains a single integer N, the number of quizzes.
The second line contains N space seperated integers Q<sub>1</sub>, Q<sub>2</sub>,. , Q<sub>N</sub> the score in each quiz.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= Q<sub>i</sub> <= 10<sup>9</sup>Print the strength of your quiz marks.Sample Input:
6
7 10 7 2 1 8
Sample Output:
7
Explaination:
There is a growth of 7 from Q<sub>5</sub> (= 1) to Q<sub>6</sub>(= 8). There is no growth in the array greater than this., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(br.readLine());
String[] str=br.readLine().split(" ");
int[] arr=new int[N];
Deque<Integer> dq=new LinkedList<Integer>();
for(int i=0;i<N;i++)
{
arr[i]=Integer.parseInt(str[i]);
}
int res;
for(int i=1;i<N;i++)
{
res=arr[i]-arr[i-1];
while(!dq.isEmpty() && dq.getLast()<res)
{
dq.removeLast();
}
dq.addLast(res);
}
System.out.print(dq.getFirst());
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You take part in <b>N</b> quizzes and in the i<sup>th</sup> quiz you get a score of Q<sub>i</sub>. Your friends are very competitive with you and they ask you the strength of your quiz scores. Strength of an array is defined as the following:
The maximum growth Q<sub>j</sub> - Q<sub>i</sub> (Q<sub>j</sub> > Q<sub>i</sub>) between two quizzes i and j such that i < j and there is no quiz <b>k</b> such that <b>i < k < j</b> and <b>Q<sub>k</sub> > Q<sub>i</sub></b>.
<b>If there is no such pair of indexes, print -1. </b>
Print the strength of your quiz marks in order to impress your friends.First line contains a single integer N, the number of quizzes.
The second line contains N space seperated integers Q<sub>1</sub>, Q<sub>2</sub>,. , Q<sub>N</sub> the score in each quiz.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= Q<sub>i</sub> <= 10<sup>9</sup>Print the strength of your quiz marks.Sample Input:
6
7 10 7 2 1 8
Sample Output:
7
Explaination:
There is a growth of 7 from Q<sub>5</sub> (= 1) to Q<sub>6</sub>(= 8). There is no growth in the array greater than this., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long double ld;
const int mod = 1e9 + 7;
const int INF = 1e9;
void solve(){
int n;
cin >> n;
vector<int> a(n);
for(auto &i : a) cin >> i;
int ans = -1;
stack<int> s;
for(int i = n - 1; i >= 0; i--){
while(s.size() > 0 && s.top() <= a[i]){
s.pop();
}
if(s.size()) ans = max(ans, s.top() - a[i]);
s.push(a[i]);
}
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 of n pairs of integers. Your task is to sort the array on the basis of the first element of pairs in descending order. If the first element is equal in two or more pairs then give preference to the pair that has a greater second element value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SortPair()</b> that takes the array of pairs and the integer N(size of the array) as a parameter.
<b>Custom Input <b/>
The first line of input will be a single integer N. The next line of input contains 2*N space-separated integers where unique adjacent elements are pairs. Custom input for 1st sample:-
4
1 2 3 4 5 6 7 8
<b>Constraints:-</b>
1<=N<=10<sup>3</sup>
1<=value<=10<sup>5</sup>Return the sorted array of pairs.Sample Input 1:
4
(1, 2), (3, 4), (5, 6), (7, 8)
Sample Output 1:
(7, 8), (5, 6), (3, 4), (1, 2)
Sample Input 2:
3
(1, 1), (2, 2), (3, 3)
Sample Output 2:
(3, 3), (2, 2), (1, 1)
Sample Input 3:
3
(1, 1), (1, 2), (3, 3)
Sample Output 3:
(3, 3), (1, 2), (1, 1)
<b>Explanation :</b>
(1,2) and (1,1) have the same first element. But (1,2) has a greater second element so (1,2) comes before (1,1) in a sorted array.
, I have written this Solution Code: def SortPair(items,n):
items.sort(reverse = True)
return items, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of n pairs of integers. Your task is to sort the array on the basis of the first element of pairs in descending order. If the first element is equal in two or more pairs then give preference to the pair that has a greater second element value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SortPair()</b> that takes the array of pairs and the integer N(size of the array) as a parameter.
<b>Custom Input <b/>
The first line of input will be a single integer N. The next line of input contains 2*N space-separated integers where unique adjacent elements are pairs. Custom input for 1st sample:-
4
1 2 3 4 5 6 7 8
<b>Constraints:-</b>
1<=N<=10<sup>3</sup>
1<=value<=10<sup>5</sup>Return the sorted array of pairs.Sample Input 1:
4
(1, 2), (3, 4), (5, 6), (7, 8)
Sample Output 1:
(7, 8), (5, 6), (3, 4), (1, 2)
Sample Input 2:
3
(1, 1), (2, 2), (3, 3)
Sample Output 2:
(3, 3), (2, 2), (1, 1)
Sample Input 3:
3
(1, 1), (1, 2), (3, 3)
Sample Output 3:
(3, 3), (1, 2), (1, 1)
<b>Explanation :</b>
(1,2) and (1,1) have the same first element. But (1,2) has a greater second element so (1,2) comes before (1,1) in a sorted array.
, I have written this Solution Code:
static Pair[] SortPair(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x==p2.x){
return p2.y-p1.y;
}
return p2.x-p1.x;
}
});
return arr;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of n pairs of integers. Your task is to sort the array on the basis of the first element of pairs in descending order. If the first element is equal in two or more pairs then give preference to the pair that has a greater second element value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SortPair()</b> that takes the array of pairs and the integer N(size of the array) as a parameter.
<b>Custom Input <b/>
The first line of input will be a single integer N. The next line of input contains 2*N space-separated integers where unique adjacent elements are pairs. Custom input for 1st sample:-
4
1 2 3 4 5 6 7 8
<b>Constraints:-</b>
1<=N<=10<sup>3</sup>
1<=value<=10<sup>5</sup>Return the sorted array of pairs.Sample Input 1:
4
(1, 2), (3, 4), (5, 6), (7, 8)
Sample Output 1:
(7, 8), (5, 6), (3, 4), (1, 2)
Sample Input 2:
3
(1, 1), (2, 2), (3, 3)
Sample Output 2:
(3, 3), (2, 2), (1, 1)
Sample Input 3:
3
(1, 1), (1, 2), (3, 3)
Sample Output 3:
(3, 3), (1, 2), (1, 1)
<b>Explanation :</b>
(1,2) and (1,1) have the same first element. But (1,2) has a greater second element so (1,2) comes before (1,1) in a sorted array.
, I have written this Solution Code: def SortPair(items,n):
items.sort(reverse = True)
return items, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of n pairs of integers. Your task is to sort the array on the basis of the first element of pairs in descending order. If the first element is equal in two or more pairs then give preference to the pair that has a greater second element value.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SortPair()</b> that takes the array of pairs and the integer N(size of the array) as a parameter.
<b>Custom Input <b/>
The first line of input will be a single integer N. The next line of input contains 2*N space-separated integers where unique adjacent elements are pairs. Custom input for 1st sample:-
4
1 2 3 4 5 6 7 8
<b>Constraints:-</b>
1<=N<=10<sup>3</sup>
1<=value<=10<sup>5</sup>Return the sorted array of pairs.Sample Input 1:
4
(1, 2), (3, 4), (5, 6), (7, 8)
Sample Output 1:
(7, 8), (5, 6), (3, 4), (1, 2)
Sample Input 2:
3
(1, 1), (2, 2), (3, 3)
Sample Output 2:
(3, 3), (2, 2), (1, 1)
Sample Input 3:
3
(1, 1), (1, 2), (3, 3)
Sample Output 3:
(3, 3), (1, 2), (1, 1)
<b>Explanation :</b>
(1,2) and (1,1) have the same first element. But (1,2) has a greater second element so (1,2) comes before (1,1) in a sorted array.
, I have written this Solution Code:
static Pair[] SortPair(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x==p2.x){
return p2.y-p1.y;
}
return p2.x-p1.x;
}
});
return arr;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.