Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given an array of N elements where N is even. You have to pair up the elements into N/2 pairs such that each element is in exactly 1 pair. You need to find minimum possible X such that there exists a way to pair the N elements and for no pair sum of its elements is greater than X.First line contains N.
Second line contains N space separated integers, denoting array.
Constraints:
1 <= N <= 100000
1 <= elements of the array <= 1000000000Print a single integer, minimum possible X.Sample Input
4
3 1 1 4
Sample Output
5
Explanation: we can pair (1, 3) and (1, 4) so all pairs have sum less than or equal to 5., I have written this Solution Code: n=int(input())
arr=input().split()
for i in range(0,n):
arr[i]=int(arr[i])
arr=sorted(arr,key=int)
start=0
end=n-1
ans=0
while(start<end):
ans=max(ans,arr[end]+arr[start])
start+=1
end-=1
print (ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements where N is even. You have to pair up the elements into N/2 pairs such that each element is in exactly 1 pair. You need to find minimum possible X such that there exists a way to pair the N elements and for no pair sum of its elements is greater than X.First line contains N.
Second line contains N space separated integers, denoting array.
Constraints:
1 <= N <= 100000
1 <= elements of the array <= 1000000000Print a single integer, minimum possible X.Sample Input
4
3 1 1 4
Sample Output
5
Explanation: we can pair (1, 3) and (1, 4) so all pairs have sum less than or equal to 5., 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;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[i];
}
sort(a,a+n);
int ma=0;
for(int i=0;i<n;++i){
ma=max(ma,a[i]+a[n-i-1]);
}
cout<<ma;
#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 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: You are given an array of N integers <b>A<sub>1</sub>, A<sub>2</sub>,... , A<sub>N</sub></b> (1 <= A[i]. length <= 10<sup>5</sup>). You have to find the lone sum of each of these integers.
To find the <b>lone sum</b> of any integer <b>a</b>, following steps are a must:
<ol>
<li>Take an integer b = Sum of digits of x.</li>
<li>If b < 10, lone sum = b and break</li>
<li>If b is at least 10, replace a with b, repeat from step 1.</li>
</ol>
For example:
Lone Sum of 799:
7 + 9 + 9 = 25
2 + 5 = 7.
Therefore, the lone Sum of 799 is 7.
For each integer j from 1 to 9, print the number of integers A<sub>i</sub> (1 <= i <= N) having their lone sum as j.First line of the input contains N, the size of array.
Second line of the input contains N space- separated integers.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
1 <= A[i]. length <= 10<sup>5</sup>
Sum of lengths of A[i] over all i from 1 to N doesn't exceed 5*10<sup>5</sup>.Print 9 integers B<sub>1</sub>, B<sub>2</sub>,. , B<sub>9</sub> where B<sub>i</sub> is the number of integers A<sub>i</sub> whose <b>lone sum</b> is i.Sample Input:
5
79752 12793 13471 31973 113
Sample Output:
0 0 1 1 2 0 1 0 0
Explanation:
Lone sum of 79752 = 7 + 9 + 7 + 5 + 2 = 30
= 3 + 0 = <b>3</b>
Lone sum of 12793 = 1 + 2 + 7 + 9 + 3 = 22
= 2 + 2 = <b>4</b>
Lone sum of 13471 = 1 + 3 + 4 + 7 + 1 = 16
= 1 + 6 = <b>7</b>
Lone sum of 31973 = 3 + 1 + 9 + 7 + 3 = 23
= 2 + 3 = <b>5</b>
Lone sum of 113 = 1 + 1 + 3 = <b>5</b>
, 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;
string fun(int sum){
string res = "";
while(sum > 0){
int x = sum%10;
res += (char)(x + '0');
sum /= 10;
}
reverse(all(res));
return res;
}
int lone_sum(string a){
int sum = 0;
for(auto i : a) sum += i - '0';
if(sum < 10) return sum;
else return lone_sum(fun(sum));;
}
void solve() {
int n;
cin >> n;
vector<string> a(n);
vector<int> res(10);
for(auto &i : a) cin >> i;
for(int i = 0; i < n; i++){
res[lone_sum(a[i])]++;
}
for(int i = 1; i <= 9; i++){
if(i > 1){
cout << ' ' << res[i];
}
else cout << res[i];
}
}
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: Jerry is having yet another existential crisis. In order to keep him occupied Rick gives him a task.
F(N, K) = The position (1-indexed) of K when numbers from 1 to N are lexicographically sorted.
For example if N = 11, the lexicographically sorted order will be:
1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9. So F(11, 2) = 4, as 2 is in the 4th position.
Given K and M, find the smallest N such that F(N, K) = M.Input contains two integers K and M.
Constraints:
1 <= K, M <= 1000000000Output smallest N such that F(N, K)=M and if no such N exists print 0.Sample Input 1
2 4
Sample Output 1
11
Sample Input 2
2 1
Sample Output 2
0, I have written this Solution Code: def dfs(n, k, c, j = 0):
if k == 0: return c
base = int("1" * len(n))
for i in range(j, 10):
new = int(c + str(i))
t = 0
if str(i) == n[0] and len(n) > 1:
t += int(str(n)[1:]) + 1
base //= 10
t+= base
if t >= k:
return dfs(str(base * 9 // 10) if str(i) != n[0] else n[1:], k - 1, c + str(i))
k -= t
def findKthNumber(n, k):
return dfs(str(n), k, "", 1)
k, m = map(int, input().split())
s = k
e = 1000000000000000000
val = 0
s1 = str(k)
while(s <= e):
mid = (s+e)//2
s2 = str(findKthNumber(mid, m))
if(s1 == s2):
val = mid
e = mid-1
elif(s1 > s2):
e = mid-1
else:
s = mid+1
print(val), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Jerry is having yet another existential crisis. In order to keep him occupied Rick gives him a task.
F(N, K) = The position (1-indexed) of K when numbers from 1 to N are lexicographically sorted.
For example if N = 11, the lexicographically sorted order will be:
1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9. So F(11, 2) = 4, as 2 is in the 4th position.
Given K and M, find the smallest N such that F(N, K) = M.Input contains two integers K and M.
Constraints:
1 <= K, M <= 1000000000Output smallest N such that F(N, K)=M and if no such N exists print 0.Sample Input 1
2 4
Sample Output 1
11
Sample Input 2
2 1
Sample Output 2
0, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
// #define ll int
#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>
/////////////
int d[15];
int dr[15];
int dp[15];
int count(int x, int n)
{
int res = 1;
int b = 10;
while(x * b <= n)
{
if(x * b + b - 1 <= n)
res = res + b;
else
{
res = res + n - x * b + 1;
break;
}
b = b * 10;
}
return res;
}
int dfs(int k, int n)
{
if(k == 1)
{
int res = 0;
for(int i = 1; i < d[0]; ++i)
res += count(i, n);
return res;
}
int res = dfs(k - 1, n);
int x = 0;
if(k >= 2)
x = dr[k - 2];
for(int i = 0; i < d[k - 1]; ++i)
res += count(x * 10 + i, n);
res++;
return res;
}
int slam(int x)
{
int r = 0;
while(x)
{
r++;
x = x / 10;
}
return r;
}
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int k, m, res, ans, mid, tm;
cin>>m>>k;
int l = slam(m);
int tl = l;
tm = m;
while(m)
{
d[--l] = m % 10;
m = m / 10;
}
l = tl;
dr[0] = d[0];
for(int i = 1; i < l; ++i)
dr[i] = dr[i - 1] * 10 + d[i];
int ll = tm > k ? tm : k, rr = 1;
rr <<= 60;
ans = 0;
while(ll <= rr)
{
mid = (ll + rr) >> 1;
if(mid < tm)
{
ll = mid + 1;
continue;
}
res = dfs(l, mid);
if(res+1 >= k)
{
if(res + 1 == k) ans = mid;
rr = mid - 1;
}
else ll = mid + 1;
}
cout<<ans<<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 N integers. You can perform only one type of operation. Take two integers at positions i and j such that abs(i - j) > M and exchange them. You need to find the lexicographically minimum array formed using this operation any number of times.The first line of input contains two integers N and M as mentioned in the problem statement. The next line contains N space-separated integers denoting the elements of array A
<b>Constraints/b>
1 <= M <= N <= 10<sup>5</sup>
1 <= A[i] <= 10<sup>9</sup>Print a single line containing N integers denoting the lexicographically minimum array formed after applying the given operation any number of times.Sample Input 1:
4 1
1 2 4 3
Sample Output 1:
1 2 3 4
</b>Explanation:<b>
Since, M = 1 you cannot any two consecutive elements, other than that you can exchange any other pair
Step 1: Exchange elements at positions 1 and 3. New array - 4 2 1 3
Step 2: Exchange elements at positions 1 and 4. New array - 3 2 1 4
Step 3: Exchange elements at positions 1 and 3. New array - 1 2 3 4
This is the lexicographically minimum array formed., 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 = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
vector<int> v;
void solve(){
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> a[i];
if(m < n/2){
sort(a, a + n);
for(int i = 0; i < n; i++)
cout<< a[i] << " ";
return;
}
for(int i = 0; i < n; i++){
if(i >= n-m-1 && i <= m) continue;
v.push_back(a[i]);
}
sort(v.begin(), v.end());
int j = 0;
for(int i = 0; i < n; i++){
if(i >= n-m-1 && i <= m)
cout<< a[i] <<" ";
else
cout<< v[j++] <<" ";
}
}
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: Given an array A of N integers. You can perform only one type of operation. Take two integers at positions i and j such that abs(i - j) > M and exchange them. You need to find the lexicographically minimum array formed using this operation any number of times.The first line of input contains two integers N and M as mentioned in the problem statement. The next line contains N space-separated integers denoting the elements of array A
<b>Constraints/b>
1 <= M <= N <= 10<sup>5</sup>
1 <= A[i] <= 10<sup>9</sup>Print a single line containing N integers denoting the lexicographically minimum array formed after applying the given operation any number of times.Sample Input 1:
4 1
1 2 4 3
Sample Output 1:
1 2 3 4
</b>Explanation:<b>
Since, M = 1 you cannot any two consecutive elements, other than that you can exchange any other pair
Step 1: Exchange elements at positions 1 and 3. New array - 4 2 1 3
Step 2: Exchange elements at positions 1 and 4. New array - 3 2 1 4
Step 3: Exchange elements at positions 1 and 3. New array - 1 2 3 4
This is the lexicographically minimum array formed., I have written this Solution Code: import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.pow;
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static InputReader getInputReader(boolean readFromTextFile) throws FileNotFoundException {
return ((readFromTextFile) ? new InputReader(new FileInputStream("src/input.txt"))
: new InputReader(System.in));
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
class Main {
public static void main(String[] args) throws FileNotFoundException {
InputReader sc = InputReader.getInputReader(false);
int n = sc.nextInt();
int m = sc.nextInt();
long[] arr = new long[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextLong();
}
if(m < n-1-Math.ceil(Double.valueOf(n)/2)){
Arrays.sort(arr);
}
else{
List<Long> list = new ArrayList<>();
int len = n-1-m;
int idx = 0 , elementsFromBeg = 0;
while(elementsFromBeg < len){
list.add(arr[idx++]);
elementsFromBeg++;
}
int elementsFromEnd = 0;
idx = n-1;
while(elementsFromEnd < len){
list.add(arr[idx--]);
elementsFromEnd++;
}
Collections.sort(list);
idx = 0;
elementsFromBeg = 0;
int listidx = 0;
while(elementsFromBeg < len){
arr[idx++] = list.get(listidx++);
elementsFromBeg++;
}
elementsFromEnd = 0;
idx = n-len;
while(elementsFromEnd < len){
arr[idx++] = list.get(listidx++);
elementsFromEnd++;
}
}
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[ ] of size N containing positive integers, find maximum and minimum elements from the array.The first line of input contains an integer T, denoting the number of testcases. The description of T testcases follows. The first line of each testcase contains a single integer N denoting the size of array. The second line contains N space-separated integers denoting the elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^7For each testcase you need to print the maximum and minimum element found separated by space.Sample Input:
2
5
7 3 4 5 6
4
1 2 3 4
Sample Output:
7 3
4 1
, I have written this Solution Code: def solve(a):
maxi = 0
mini = 1e7+1
for i in a:
if(i < mini):
mini = i
if(i > maxi):
maxi = i
return mini,maxi, 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 containing positive integers, find maximum and minimum elements from the array.The first line of input contains an integer T, denoting the number of testcases. The description of T testcases follows. The first line of each testcase contains a single integer N denoting the size of array. The second line contains N space-separated integers denoting the elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^7For each testcase you need to print the maximum and minimum element found separated by space.Sample Input:
2
5
7 3 4 5 6
4
1 2 3 4
Sample Output:
7 3
4 1
, I have written this Solution Code:
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int n = Integer.parseInt(read.readLine());
int[] arr = new int[n];
String str[] = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
findMinMax(arr, n);
System.out.println();
}
}
public static void findMinMax(int arr[], int n)
{
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++)
{
min = Math.min(arr[i], min);
max = Math.max(arr[i], max);
}
System.out.print(max + " " + min);
}
}, 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, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: int MakeOne(int N){
int cnt=0;
int a =N;
while(a!=1){
if(a&1){a++;}
else{a/=2;}
cnt++;
}
return cnt;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: static int MakeOne(int N){
int cnt=0;
int a =N;
while(a!=1){
if(a%2==1){a++;}
else{a/=2;}
cnt++;
}
return cnt;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: int MakeOne(int N){
int cnt=0;
int a =N;
while(a!=1){
if(a&1){a++;}
else{a/=2;}
cnt++;
}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you have to perform only two types of operations on the number:-
1) If N is odd increase it by 1
2) If N is even divide it by 2
Your task is to find the number of operations it takes to make N equal to 1.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>MakeOne()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the number of operations it takes to make the number N equal to 1.Sample Input:-
7
Sample Output:-
4
Explanation:-
7 - > 8 - > 4 - > 2 - > 1
Sample Input:-
3
Sample Output:-
3, I have written this Solution Code: def MakeOne(N):
cnt=0
while N!=1:
if N%2==1:
N=N+1
else:
N=N//2
cnt=cnt+1
return cnt
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan and Tusla are both students at Newton School. They are both among the best students in the class. In order to know who is better among them, a game was organised. The game consisted of L rounds, where L is an odd integer. The student winning more rounds than the other was declared the winner.
You would be given a string of odd length L in which each character is 'N' or 'T'. If the i<sup>th</sup> character is 'N', then the i<sup>th</sup> round was won by Nutan, else if the character is 'T' it was won by Tusla. Print "Nutan'' if Nutan has won more rounds than Tusla, else print "Tusla'' if Tusla has won more rounds than Nutan.
Note: You have to print everything without quotes.The first line of the input contains a single integer L β the number of rounds (1 ≤ L ≤ 100 and L is odd).
The second line contains a string S of length L. Each character of S is either 'N' or 'T'.Print "Nutan" or "Tusla" according to the input.Sample Input:
3
NNT
Sample Output:
Nutan
Explanation:
Nutan has won two games while Tusla has only won a single game, so the overall winner is Nutan., I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.System.out;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader reader = new FastReader();
int n = reader.nextInt();
String S = reader.next();
int ncount = 0;
int tcount = 0;
for (char c : S.toCharArray()) {
if (c == 'N') ncount++;
else tcount++;
}
if (ncount > tcount) {
out.print("Nutan\n");
} else {
out.print("Tusla\n");
}
out.flush();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan and Tusla are both students at Newton School. They are both among the best students in the class. In order to know who is better among them, a game was organised. The game consisted of L rounds, where L is an odd integer. The student winning more rounds than the other was declared the winner.
You would be given a string of odd length L in which each character is 'N' or 'T'. If the i<sup>th</sup> character is 'N', then the i<sup>th</sup> round was won by Nutan, else if the character is 'T' it was won by Tusla. Print "Nutan'' if Nutan has won more rounds than Tusla, else print "Tusla'' if Tusla has won more rounds than Nutan.
Note: You have to print everything without quotes.The first line of the input contains a single integer L β the number of rounds (1 ≤ L ≤ 100 and L is odd).
The second line contains a string S of length L. Each character of S is either 'N' or 'T'.Print "Nutan" or "Tusla" according to the input.Sample Input:
3
NNT
Sample Output:
Nutan
Explanation:
Nutan has won two games while Tusla has only won a single game, so the overall winner is Nutan., I have written this Solution Code: n = int(input())
s = input()
a1 = s.count('N')
a2 = s.count('T')
if(a1 > a2):
print("Nutan")
else:
print('Tusla'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nutan and Tusla are both students at Newton School. They are both among the best students in the class. In order to know who is better among them, a game was organised. The game consisted of L rounds, where L is an odd integer. The student winning more rounds than the other was declared the winner.
You would be given a string of odd length L in which each character is 'N' or 'T'. If the i<sup>th</sup> character is 'N', then the i<sup>th</sup> round was won by Nutan, else if the character is 'T' it was won by Tusla. Print "Nutan'' if Nutan has won more rounds than Tusla, else print "Tusla'' if Tusla has won more rounds than Nutan.
Note: You have to print everything without quotes.The first line of the input contains a single integer L β the number of rounds (1 ≤ L ≤ 100 and L is odd).
The second line contains a string S of length L. Each character of S is either 'N' or 'T'.Print "Nutan" or "Tusla" according to the input.Sample Input:
3
NNT
Sample Output:
Nutan
Explanation:
Nutan has won two games while Tusla has only won a single game, so the overall winner is Nutan., I have written this Solution Code: //Author: Xzirium
//Time and Date: 02:18:28 24 March 2022
//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()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//-----------------------------------------------------------------------------------------------------------//
READV(N);
string S;
cin>>S;
ll n=0,t=0;
FORI(i,0,N)
{
if(S[i]=='N')
{
n++;
}
else if(S[i]=='T')
{
t++;
}
}
if(n>t)
{
cout<<"Nutan"<<endl;
}
else
{
cout<<"Tusla"<<endl;
}
//-----------------------------------------------------------------------------------------------------------//
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: static void simpleSum(int a, int b, int c){
System.out.println(a+b+c);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: void simpleSum(int a, int b, int c){
cout<<a+b+c;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: x = input()
a, b, c = x.split()
a = int(a)
b = int(b)
c = int(c)
print(a+b+c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the length of the radius of curvature and the type of mirror, calculate its focal length.
<b>Note:</b> '(' represents a convex mirror while ')' represents a concave mirror<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>focal_length()</b> that takes the integer R(radius of curvature) and the character Mirror (type of mirror) as parameter
<b>Constraints:-</b>
1 <= R <= 100
Mirror will be either of '('(convex type) or ')'(concave type)Return the <b>floor value of focal length.</b>Sample Input:-
9 (
Sample Output:-
4
Sample Input:-
9 )
Sample Output:-
-5, I have written this Solution Code: static int focal_length(int R, char Mirror)
{
int f=R/2;
if((R%2==1) && Mirror==')'){f++;}
if(Mirror == ')'){f=-f;}
return f;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the length of the radius of curvature and the type of mirror, calculate its focal length.
<b>Note:</b> '(' represents a convex mirror while ')' represents a concave mirror<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>focal_length()</b> that takes the integer R(radius of curvature) and the character Mirror (type of mirror) as parameter
<b>Constraints:-</b>
1 <= R <= 100
Mirror will be either of '('(convex type) or ')'(concave type)Return the <b>floor value of focal length.</b>Sample Input:-
9 (
Sample Output:-
4
Sample Input:-
9 )
Sample Output:-
-5, I have written this Solution Code:
int focal_length(int R, char Mirror)
{
int f=R/2;
if((R&1) && Mirror==')'){f++;}
if(Mirror == ')'){f=-f;}
return f;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the length of the radius of curvature and the type of mirror, calculate its focal length.
<b>Note:</b> '(' represents a convex mirror while ')' represents a concave mirror<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>focal_length()</b> that takes the integer R(radius of curvature) and the character Mirror (type of mirror) as parameter
<b>Constraints:-</b>
1 <= R <= 100
Mirror will be either of '('(convex type) or ')'(concave type)Return the <b>floor value of focal length.</b>Sample Input:-
9 (
Sample Output:-
4
Sample Input:-
9 )
Sample Output:-
-5, I have written this Solution Code:
int focal_length(int R, char Mirror)
{
int f=R/2;
if((R&1) && Mirror==')'){f++;}
if(Mirror == ')'){f=-f;}
return f;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the length of the radius of curvature and the type of mirror, calculate its focal length.
<b>Note:</b> '(' represents a convex mirror while ')' represents a concave mirror<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>focal_length()</b> that takes the integer R(radius of curvature) and the character Mirror (type of mirror) as parameter
<b>Constraints:-</b>
1 <= R <= 100
Mirror will be either of '('(convex type) or ')'(concave type)Return the <b>floor value of focal length.</b>Sample Input:-
9 (
Sample Output:-
4
Sample Input:-
9 )
Sample Output:-
-5, I have written this Solution Code: def focal_length(R,Mirror):
f=R/2;
if(Mirror == ')'):
f=-f
if R%2==1:
f=f-1
return int(f)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of integers of size N is given to you. You have to print the third- highest integer in the array in linear time.
<b>Note:</b>
The third highest element of the array is the third number in the array when sorted in non- increasing order.
<b>Example</b>
An array is given: 2 4 6 12 1
The third highest number is 4, Hence the output will be 4.A single integer n.
The next line contains n integers separated by space.
<b>Constraints </b>
3 ≤ n ≤ 10<sup>5</sup>
-10<sup>9</sup> ≤ arr[i] ≤ 10<sup>9</sup>A single integer denotes the required answer.Sample Input:
6
-2 7 3 1 6 5
Sample Output:
5
Explanation :
sort in non- increasing order : 7 6 5 3 1 -2, I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
int n=Integer.parseInt(in.next());
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(in.next());
}
int x=-1,y=-1,z=-1;
for(int i=0;i<n;i++){
if(x==-1 || a[i] >= a[x]){
z=y;
y=x;
x=i;
}
else if(y==-1 || a[i] >= a[y]){
z=y;
y=i;
}
else if(z == -1 || a[i] >= a[z]){
z=i;
}
}
out.print(a[z]);
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import math
n = int(input())
for i in range(n):
x = int(input())
count = 0
for i in range(1, int(math.sqrt(x))+1):
if x % i == 0:
if (i%2 == 0):
count+=1
if ((x/i) %2 == 0):
count+=1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int count=0;
for(int i=1;i<=Math.sqrt(n);i++){
if(n%i == 0)
{
if(i%2==0) {
count++;
}
if(i*i != n && (n/i)%2==0) {
count++;
}
}
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
if(n&1){cout<<0<<endl;continue;}
long x=sqrt(n);
int cnt=0;
for(long long i=1;i<=x;i++){
if(!(n%i)){
if(!(i%2)){cnt++;}
if(i*i!=n){
if(!((n/i)%2)){cnt++;}
}
}
}
cout<<cnt<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function <code>callThisFnBack</code>
Such that it takes a number as the first argument and a function (callback function) as the second argument. You have to pass the first argument of the function <code>callThisFnBack</code> to the callback function and execute the callback function inside the <code>callThisFnBack</code> and return its result.The function will take two arguments, one which is a number and the second which will be a function.Returns the result of the second argument which is the callback function when its argument is the first argument of the function <code>callThisFnBack</code>.const result = callThisFnBack(5, (num)=>{
return num+6
})
console.log(result) // prints 11 because 5+6
const newFn = (number) => {
return number - 5
}
const newResult = callThisFnBack(5,newFn)
console.log(newResult) // prints 0 because 5-5=0, 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 num = sc.nextInt();
int ans = 0;
char operate = sc.next().charAt(0);
switch(operate){
case '+':
ans = num + num;
break;
case '-' :
ans = num - num;
break;
case '*':
ans = num * num;
break;
case '/':
ans = num / num;
break;
}
System.out.print(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function <code>callThisFnBack</code>
Such that it takes a number as the first argument and a function (callback function) as the second argument. You have to pass the first argument of the function <code>callThisFnBack</code> to the callback function and execute the callback function inside the <code>callThisFnBack</code> and return its result.The function will take two arguments, one which is a number and the second which will be a function.Returns the result of the second argument which is the callback function when its argument is the first argument of the function <code>callThisFnBack</code>.const result = callThisFnBack(5, (num)=>{
return num+6
})
console.log(result) // prints 11 because 5+6
const newFn = (number) => {
return number - 5
}
const newResult = callThisFnBack(5,newFn)
console.log(newResult) // prints 0 because 5-5=0, I have written this Solution Code: function callThisFnBack(number, fn) {
return fn(number)
// return the output using return keyword
// do not console.log it
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a side of a square, your task is to calculate its area.<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>Area()</b> that takes the side of the square as a parameter.Return the area of the squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36
, I have written this Solution Code: static int Area(int side){
return side*side;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry is trying to find the chamber of secrets but the chamber of secrets is well protected by dark spells. To find the chamber Harry needs to find the initial position of the chamber. Harry knows the final position of the chamber is X. He also knows that in total, the chamber has been repositioned N times. During the i<sup>th</sup> repositioning, the position of the chamber is changed to (previous position * Arr[i]) % 1000000007.
Given X and Arr, help Harry find the initial position of the chamber.
Note:- The initial position of the chamber is less than 1000000007.The first line of the input contains two integers N and X.
The second line of the input contains N integers denoting Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] < 1000000007Print a single integer denoting the initial position of the chamber.Sample Input
5 480
1 4 4 3 1
Sample Output
10
Explanation: Initial position = 10
After first repositioning position = (10*1)%1000000007 = 10
After second repositioning position = (10*4)%1000000007 = 40
After third repositioning position = (40*4)%1000000007 = 160
After fourth repositioning position = (160*3)%1000000007 = 480
After fifth repositioning position = (480*1)%1000000007 = 480, I have written this Solution Code: def inv(a,b,m):
res = 1
while(b):
if b&1:
res = (res*a)%m
a = (a*a)%m
b >>= 1
return res
n,x = map(int,input().split())
a = list(map(int,input().split()))
m = 1000000007
for i in a:
x = (x*inv(i,m-2,m))%m
print(x), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry is trying to find the chamber of secrets but the chamber of secrets is well protected by dark spells. To find the chamber Harry needs to find the initial position of the chamber. Harry knows the final position of the chamber is X. He also knows that in total, the chamber has been repositioned N times. During the i<sup>th</sup> repositioning, the position of the chamber is changed to (previous position * Arr[i]) % 1000000007.
Given X and Arr, help Harry find the initial position of the chamber.
Note:- The initial position of the chamber is less than 1000000007.The first line of the input contains two integers N and X.
The second line of the input contains N integers denoting Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] < 1000000007Print a single integer denoting the initial position of the chamber.Sample Input
5 480
1 4 4 3 1
Sample Output
10
Explanation: Initial position = 10
After first repositioning position = (10*1)%1000000007 = 10
After second repositioning position = (10*4)%1000000007 = 40
After third repositioning position = (40*4)%1000000007 = 160
After fourth repositioning position = (160*3)%1000000007 = 480
After fifth repositioning position = (480*1)%1000000007 = 480, 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>
/////////////
int power_mod(int a,int b,int mod){
int ans = 1;
while(b){
if(b&1)
ans = (ans*a)%mod;
b = b/2;
a = (a*a)%mod;
}
return ans;
}
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int x;
cin>>x;
int mo=1000000007;
int mu=1;
for(int i=0;i<n;++i){
int d;
cin>>d;
mu=(mu*d)%mo;
}
cout<<(x*power_mod(mu,mo-2,mo))%mo;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry is trying to find the chamber of secrets but the chamber of secrets is well protected by dark spells. To find the chamber Harry needs to find the initial position of the chamber. Harry knows the final position of the chamber is X. He also knows that in total, the chamber has been repositioned N times. During the i<sup>th</sup> repositioning, the position of the chamber is changed to (previous position * Arr[i]) % 1000000007.
Given X and Arr, help Harry find the initial position of the chamber.
Note:- The initial position of the chamber is less than 1000000007.The first line of the input contains two integers N and X.
The second line of the input contains N integers denoting Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] < 1000000007Print a single integer denoting the initial position of the chamber.Sample Input
5 480
1 4 4 3 1
Sample Output
10
Explanation: Initial position = 10
After first repositioning position = (10*1)%1000000007 = 10
After second repositioning position = (10*4)%1000000007 = 40
After third repositioning position = (40*4)%1000000007 = 160
After fourth repositioning position = (160*3)%1000000007 = 480
After fifth repositioning position = (480*1)%1000000007 = 480, I have written this Solution Code: import java.io.*;import java.util.*;import java.math.*;
public class Main
{
long mod=1000000007l;int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;
long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;StringBuilder sb;
public void tq()throws Exception
{
st=new StringTokenizer(br.readLine());
int tq=1;
o:
while(tq-->0)
{
int n=i();
long k=l();
long ar[]=arl(n);
long v=1l;
for(long x:ar)v=(v*x)%mod;
v=(k*(mul(v,mod-2,mod)))%mod;
pl(v);
}
}
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];
Arrays.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];
Arrays.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];
Arrays.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 min(int a,int b){return a<b?a:b;}
int min(int a,int b,int c){return a<b?a<c?a:c:b<c?b:c;}
int max(int a,int b){return a>b?a:b;}
int max(int a,int b,int c){return a>b?a>c?a:c:b>c?b:c;}
long min(long a,long b){return a<b?a:b;}
long min(long a,long b,long c){return a<b?a<c?a:c:b<c?b:c;}
long max(long a,long b){return a>b?a:b;}
long max(long a,long b,long c){return a>b?a>c?a:c:b>c?b:c;}
int abs(int a){return Math.abs(a);}
long abs(long a){return Math.abs(a);}
int sq(int a){return (int)Math.sqrt(a);}long sq(long a){return (long)Math.sqrt(a);}
long gcd(long a,long b){return b==0l?a:gcd(b,a%b);}
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(br.readLine());
return Integer.parseInt(st.nextToken());}
long l()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());}String s()throws IOException
{if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());return st.nextToken();}
double d()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Double.parseDouble(st.nextToken());}void p(Object p){System.out.print(p);}
void p(String p){System.out.print(p);}void p(int p){System.out.print(p);}
void p(double p){System.out.print(p);}void p(long p){System.out.print(p);}
void p(char p){System.out.print(p);}void p(boolean p){System.out.print(p);}
void pl(Object p){System.out.println(p);}void pl(String p){System.out.println(p);}
void pl(int p){System.out.println(p);}void pl(char p){System.out.println(p);}
void pl(double p){System.out.println(p);}void pl(long p){System.out.println(p);}
void pl(boolean p){System.out.println(p);}void pl(){System.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(br.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(br.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(br.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(br.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(br.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(br.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(br.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(br.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=br.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(' ');}System.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(' ');}System.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(' ');}System.out.println(sb);}
void p(double ar[]){StringBuilder sb=new StringBuilder(2*ar.length);
for(double a:ar){sb.append(a);sb.append(' ');}System.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(' ');}System.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 a Binary Search Tree (BST) and a node <b>x</b>, your task is to delete the node 'x' from the BST.
If no node with value x exists then, do not make any changes<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>deletInBST()</b> that takes "root" node and the value to be deleted as parameter. The printing is done by the driver code.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^3
1 <= node values <= 10^4
1 <= K <= 10^3
<b>Sum of "N" over all testcases does not exceed 10^5</b>Return the node of BST after deletion.Input:
2
3
2 1 3 N N N N
2
9
1 N 2 N 8 5 11 4 7 9 12
9
Output:
1 3
1 2 4 5 7 8 11 12
Explanation:-
Fortest1:-
before deletion:-
2
/ \
1 3
after deletion:-
1
\
3, I have written this Solution Code: static Node minimumElement(Node root)
{
int minv = root.data;
while (root.left != null)
{
minv = root.left.data;
root = root.left;
}
return root;
}
public static Node deleteInBST(Node root, int value) {
if (root == null)
return null;
if (root.data > value) {
root.left = deleteInBST(root.left, value);
} else if (root.data < value) {
root.right = deleteInBST(root.right, value);
} else {
// if nodeToBeDeleted have both children
if (root.left != null && root.right != null) {
Node temp = root;
// Finding minimum element from right
Node minNodeForRight = minimumElement(temp.right);
// Replacing current node with minimum node from right subtree
root.data = minNodeForRight.data;
// Deleting minimum node from right now
root.right = deleteInBST(root.right, minNodeForRight.data);
}
// if nodeToBeDeleted has only left child
else if (root.left != null) {
root = root.left;
}
// if nodeToBeDeleted has only right child
else if (root.right != null) {
root = root.right;
}
// if nodeToBeDeleted do not have child (Leaf node)
else
root = null;
}
return root;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a circular linked list consisting of N nodes, your task is to exchange the first and last node of the list.
<b>Note:
Examples in Sample Input and Output just show how a linked list will look depending on the questions. Do not copy-paste as it is in custom input</b><b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>exchangeNodes()</b> that takes head node as parameter.
Constraints:
3 <= N <= 1000
1 <= Node. data<= 1000Return the head of the modified linked list.Sample Input 1:-
3
1- >2- >3
Sample Output 1:-
3- >2- >1
Sample Input 2:-
4
1- >2- >3- >4
Sample Output 2:-
4- >2- >3- >1, I have written this Solution Code: public static Node exchangeNodes(Node head) {
Node p = head;
while (p.next.next != head)
p = p.next;
p.next.next = head.next;
head.next = p.next;
p.next = head;
head = head.next;
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the top right or the adjacent point on the left. That is, if she is standing on point (x, y), she can either move to point (x+1, y+1) or point (xβ1, y). YunQian initially stands at point (a, b) and wants to move to point (c, d). Find the minimum number of moves she needs to make or declare that it is impossible.The input consists of 4 space- separated integers a, b, c and d.
<b>Constraints</b>
β10<sup>8</sup> ≤ a, b, c, d ≤ 10<sup>8</sup>if it is possible to move from point (a, b) to point (c, d), output the minimum number of moves. Otherwise, output β1.<b>Sample Input 1</b>
-1 0 -1 2
<b>Sample Output 1</b>
4
<b>Sample Input 2</b>
0 0 4 5
<b>Sample Output 2</b>
6, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
void solveCase()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
if (b > d)
{
cout << "-1\n";
return;
}
int moves = d - b;
a += moves;
if (a < c)
{
cout << "-1\n";
return;
}
moves += a - c;
cout << moves << '\n';
}
int32_t main()
{
mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
if (rng() % 100000 == 0)
assert(false);
ios::sync_with_stdio(false), cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
solveCase();
}, In this Programming Language: C++, 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 its area.<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>Area()</b> that takes the side of the square as a parameter.Return the area of the squareSample Input:-
3
Sample Output:-
9
Sample Input:-
6
Sample Output:-
36
, I have written this Solution Code: static int Area(int side){
return side*side;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a non-negative Integer x, count and print the number of times 0 occurs in that number.The first line contains 1 integer x.
<b>Constraints</b>
0 ≤ x ≤ 10<sup>5</sup>Print count of zeroes in the number.Sample Input 1 :
20
Sample Output 1 :
1
Sample Input 2 :
60701
Sample Output 2 :
2, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
assert n>=0&&n<=100000 : "Input Not Valid";
int ct=0;
if(n==0) ct=1;
while(n>0){
int c=n%10;
n/=10;
if(c==0){
ct++;
}
}
System.out.print(ct);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: static void simpleSum(int a, int b, int c){
System.out.println(a+b+c);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: void simpleSum(int a, int b, int c){
cout<<a+b+c;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three integers A, B, and C, your task is to print the sum of these three integers.The input contains 3 integers separated by spaces A, B, and C.
Constraints:-
1 <= A, B, C <= 100Print the sum of A, B and C.Sample Input
1 2 3
Sample Output:-
6
Sample Input:-
5 4 2
Sample Output:-
11, I have written this Solution Code: x = input()
a, b, c = x.split()
a = int(a)
b = int(b)
c = int(c)
print(a+b+c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to find the simple interest for given principal amount P, time Tm(in years) and rate R.<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>SimpleInterest()</b> that takes the principal amount P, rate R, and time Tm as a parameter.
Constraints:
1 <= P <= 10^3
1 <= Tm <= 20
1 <= R <= 20Return the floor value of the simple interest i.e. interest in integer format.Input:
42 15 8
Output:
50
Explanation:
Testcase 1: Simple interest of given principal amount 42, in 8 years at a 15% rate of interest is 50., I have written this Solution Code: import math
p,t,r = [int(x) for x in input().split()]
res=p*t*r
print(math.floor(res/100)), 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 simple interest for given principal amount P, time Tm(in years) and rate R.<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>SimpleInterest()</b> that takes the principal amount P, rate R, and time Tm as a parameter.
Constraints:
1 <= P <= 10^3
1 <= Tm <= 20
1 <= R <= 20Return the floor value of the simple interest i.e. interest in integer format.Input:
42 15 8
Output:
50
Explanation:
Testcase 1: Simple interest of given principal amount 42, in 8 years at a 15% rate of interest is 50., I have written this Solution Code: static int SimpleInterest(int P, int R, int Tm){
return (P*Tm*R)/100;
}, 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 and two empty strings A and B. You are allowed to perform following two operations.
1. Remove an element from start of S and add it to the end of B.
2. Remove an element from end of B and add it to the end of A.
Find the lexicographical minimum string A that we can obtain using the above procedure.
Note: The final length of string A must be equal to initial length of string S.Input contains string S containing only lowercase alphabets.
Constraints
1 <= |S| <= 100000Print the lexicographical minimum string A that we can obtain.Input
cab
Output
abc
Input
acdb
Output
abdc, 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);
String str = sc.nextLine().trim();
int len = str.length();
Stack<Integer> st = new Stack<Integer>();
StringBuilder sb = new StringBuilder();
int count[] = new int [26];
for(int i=0;i<len;i++){
count[str.charAt(i)-'a']++;
}
int j=0;
for(int i=0;i<len;i++){
while(count[j] == 0){
j++;
}
while(!st.empty() && st.peek() <= j){
sb.append((char) (st.pop() + 'a') );
}
st.push(str.charAt(i) - 'a');
count[str.charAt(i)-'a']--;
}
while(!st.empty()){
sb.append((char) (st.pop() + 'a') );
}
System.out.println(sb);
}
}, 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 and two empty strings A and B. You are allowed to perform following two operations.
1. Remove an element from start of S and add it to the end of B.
2. Remove an element from end of B and add it to the end of A.
Find the lexicographical minimum string A that we can obtain using the above procedure.
Note: The final length of string A must be equal to initial length of string S.Input contains string S containing only lowercase alphabets.
Constraints
1 <= |S| <= 100000Print the lexicographical minimum string A that we can obtain.Input
cab
Output
abc
Input
acdb
Output
abdc, I have written this Solution Code: s = input()
b = []
a = ""
k = 0
for i in "abcdefghijklmnopqrstuvwxyz":
if(i not in s[k:]):
continue
for j in s[k:]:
while(b and b[-1] <= i):
a += b.pop()
if(i == j):
a += j
if(i not in s[k+1:]):
k+=1
break
else:
k += 1
if(len(a) == len(s)):
break
continue
if(len(a) == len(s)):
break
b.append(j)
k += 1
if(len(a) == len(s)):
break
while(b):
a += b.pop()
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a String S and two empty strings A and B. You are allowed to perform following two operations.
1. Remove an element from start of S and add it to the end of B.
2. Remove an element from end of B and add it to the end of A.
Find the lexicographical minimum string A that we can obtain using the above procedure.
Note: The final length of string A must be equal to initial length of string S.Input contains string S containing only lowercase alphabets.
Constraints
1 <= |S| <= 100000Print the lexicographical minimum string A that we can obtain.Input
cab
Output
abc
Input
acdb
Output
abdc, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define speed ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define endl '\n'
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int nxt[N][26];
signed main() {
speed;
string s; cin >> s;
int n = s.length();
for(int i = 0; i < 26; i++)
nxt[n][i] = n;
for(int i = n-1; i >= 0; i--){
for(int j = 0; j < 26; j++)
nxt[i][j] = nxt[i+1][j];
nxt[i][s[i]-'a'] = i;
}
stack<char> st;
int cur = 0;
string t = "";
for(int i = 0; i < n; i++){
while(nxt[i][cur] == n){
cur++;
if(cur == 26)
break;
while(!st.empty() && st.top()-'a' <= cur){
t += st.top();
st.pop();
}
}
if(cur == 26){
while(!st.empty() && st.top() <= s[i]){
t += st.top();
st.pop();
}
st.push(s[i]);
}
else{
if(nxt[i][cur] == i)
t += s[i];
else
st.push(s[i]);
}
}
while(!st.empty()){
t += st.top();
st.pop();
}
cout << t;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N containing 0's, 1's and 2's. The task is to segregate the 0's, 1's and 2's in the array as all the 0's should appear in the first part of the array, 1's should appear in middle part of the array and finally all the 2's in the remaining part of the array.
Note: Do not use inbuilt sort function. Try to solve in O(N) per test caseThe first line contains an integer T denoting the total number of test cases. Then T testcases follow.
Each testcases contains two lines of input. The first line denotes the size of the array N.
The second lines contains the elements of the array A separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= Ai <= 2
Sum of N for each test case does not exceed 10^5For each testcase, in a newline, print the sorted array.Input :
2
5
0 2 1 2 0
3
0 1 0
Output:
0 0 1 2 2
0 0 1
Explanation:
Testcase 1: After segragating the 0s, 1s and 2s, we have 0 0 1 2 2 which shown in the output.
Testcase 2: For the given array input, output will be 0 0 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));
PrintWriter out= new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t>0){
t-=1;
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int arr[]=new int[n];
int zero_counter=0,one_counter=0,two_counter=0;
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(s[i]);
if(arr[i]==0)
zero_counter+=1;
else if(arr[i]==1)
one_counter+=1;
else
two_counter+=1;
}
for(int i=0;i<zero_counter;i++){
out.print(0+" ");
}
for(int i=0;i<one_counter;i++){
out.print(1+" ");
}
for(int i=0;i<two_counter;i++){
out.print(2+" ");
}
out.flush();
out.println(" ");
}
out.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N containing 0's, 1's and 2's. The task is to segregate the 0's, 1's and 2's in the array as all the 0's should appear in the first part of the array, 1's should appear in middle part of the array and finally all the 2's in the remaining part of the array.
Note: Do not use inbuilt sort function. Try to solve in O(N) per test caseThe first line contains an integer T denoting the total number of test cases. Then T testcases follow.
Each testcases contains two lines of input. The first line denotes the size of the array N.
The second lines contains the elements of the array A separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= Ai <= 2
Sum of N for each test case does not exceed 10^5For each testcase, in a newline, print the sorted array.Input :
2
5
0 2 1 2 0
3
0 1 0
Output:
0 0 1 2 2
0 0 1
Explanation:
Testcase 1: After segragating the 0s, 1s and 2s, we have 0 0 1 2 2 which shown in the output.
Testcase 2: For the given array input, output will be 0 0 1., I have written this Solution Code: t=int(input())
while t>0:
t-=1
n=input()
a=map(int,input().split())
z=o=tc=0
for i in a:
if i==0:z+=1
elif i==1:o+=1
else:tc+=1
while z>0:
z-=1
print(0,end=' ')
while o>0:
o-=1
print(1,end=' ')
while tc>0:
tc-=1
print(2,end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N containing 0's, 1's and 2's. The task is to segregate the 0's, 1's and 2's in the array as all the 0's should appear in the first part of the array, 1's should appear in middle part of the array and finally all the 2's in the remaining part of the array.
Note: Do not use inbuilt sort function. Try to solve in O(N) per test caseThe first line contains an integer T denoting the total number of test cases. Then T testcases follow.
Each testcases contains two lines of input. The first line denotes the size of the array N.
The second lines contains the elements of the array A separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= Ai <= 2
Sum of N for each test case does not exceed 10^5For each testcase, in a newline, print the sorted array.Input :
2
5
0 2 1 2 0
3
0 1 0
Output:
0 0 1 2 2
0 0 1
Explanation:
Testcase 1: After segragating the 0s, 1s and 2s, we have 0 0 1 2 2 which shown in the output.
Testcase 2: For the given array input, output will be 0 0 1., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
int a[3] = {0};
for(int i = 1; i <= n; i++){
int p; cin >> p;
a[p]++;
}
a[1] += a[0];
a[2] += a[1];
for(int i = 1; i <= n; i++){
if(i <= a[0]) cout << "0 ";
else if(i <= a[1]) cout << "1 ";
else cout << "2 ";
}
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N containing 0's, 1's and 2's. The task is to segregate the 0's, 1's and 2's in the array as all the 0's should appear in the first part of the array, 1's should appear in middle part of the array and finally all the 2's in the remaining part of the array.
Note: Do not use inbuilt sort function. Try to solve in O(N) per test caseThe first line contains an integer T denoting the total number of test cases. Then T testcases follow.
Each testcases contains two lines of input. The first line denotes the size of the array N.
The second lines contains the elements of the array A separated by spaces.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0 <= Ai <= 2
Sum of N for each test case does not exceed 10^5For each testcase, in a newline, print the sorted array.Input :
2
5
0 2 1 2 0
3
0 1 0
Output:
0 0 1 2 2
0 0 1
Explanation:
Testcase 1: After segragating the 0s, 1s and 2s, we have 0 0 1 2 2 which shown in the output.
Testcase 2: For the given array input, output will be 0 0 1., I have written this Solution Code: function zeroOneTwoSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => a - b)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mark is asked to take a group photo of 2n people. The i- th person has height h<sub>i</sub> units. To do so, he ordered these people into two rows, the front row and the back row, each consisting of n people. However, to ensure that everyone is seen properly, the j- th person of the back row must be at least x units taller than the j- th person of the front row for each j between 1 and n, inclusive.
Help Mark determine if this is possible.The first line of the input consists of two space- separated integers n and x - the number of people in each row and the minimum difference Mark wants. The second line of the input consists of 2n space separated integers denoting the height of each person in units.
<b>Constraints</b>
1 ≤ n ≤ 100
1 ≤ x ≤ 10<sup>3</sup>
1 ≤ h<sub>i</sub> ≤ 10<sup>3</sup>Print "YES" if Mark could arrange people satisfying his condition and "NO" otherwise.<b>Sample Input 1</b>
3 6
1 3 9 10 12 16
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
3 1
2 5 2 2 2 5
<b>Sample Output 2</b>
No, I have written this Solution Code: #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define int long long int
#define endl '\n'
void fastio()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
using namespace __gnu_pbds;
const int M = 1e9 + 7;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// Author:Abhas
void solution()
{
int n, x;
cin >> n >> x;
vector<int> a(2 * n);
map<int, int> m;
for (int i = 0; i < 2 * n; i++)
{
cin >> a[i];
// m[a[i]]++;
}
sort(a.begin(), a.end());
int i = 0, j = n;
while (i < n)
{
if (a[j] < (a[i] + x))
{
cout << "No" << endl;
return;
}
i++;
j++;
}
cout << "Yes" << endl;
}
signed main()
{
fastio();
int t = 1;
//cin >> t;
while (t--)
{
solution();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S find minimum number of changes required to make the string palindrome. In a change you can change any index of the string to any character.
A palindrome is a string that remains the same if reversed.First and only line of input contains a string S.
Constraints
1 <= |S| <= 100000
S contains only lowercase lettersOutput a single integer which is the minimum number of changes required to make string palindrome.sample input 1
abc
sample output 1
1
sample input 2
abcdef
sample output 2
3
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int i=0,ans=0;
int j=str.length()-1;
while(i<=j){
if(str.charAt(i)!=str.charAt(j)){
ans++;
}
i++;
j--;
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S find minimum number of changes required to make the string palindrome. In a change you can change any index of the string to any character.
A palindrome is a string that remains the same if reversed.First and only line of input contains a string S.
Constraints
1 <= |S| <= 100000
S contains only lowercase lettersOutput a single integer which is the minimum number of changes required to make string palindrome.sample input 1
abc
sample output 1
1
sample input 2
abcdef
sample output 2
3
, I have written this Solution Code: n = input()
count = 0
i = 0
j = len(n)-1
while i <= j:
if n[i] != n[j]:
count += 1
i += 1
j -= 1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S find minimum number of changes required to make the string palindrome. In a change you can change any index of the string to any character.
A palindrome is a string that remains the same if reversed.First and only line of input contains a string S.
Constraints
1 <= |S| <= 100000
S contains only lowercase lettersOutput a single integer which is the minimum number of changes required to make string palindrome.sample input 1
abc
sample output 1
1
sample input 2
abcdef
sample output 2
3
, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define 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
string s;
cin>>s;
int ans=0;
for(int i=0;i<s.length()/2;++i)
{
if(s[i]!=s[s.length()-i-1])
++ans;
}
cout<<ans;
#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 a string S find minimum number of changes required to make the string palindrome. In a change you can change any index of the string to any character.
A palindrome is a string that remains the same if reversed.First and only line of input contains a string S.
Constraints
1 <= |S| <= 100000
S contains only lowercase lettersOutput a single integer which is the minimum number of changes required to make string palindrome.sample input 1
abc
sample output 1
1
sample input 2
abcdef
sample output 2
3
, I have written this Solution Code: function palindrome(s) {
// write code here
// do not console.log
// return the number of changes
const n = s.length;
let cc = 0;
for (let i = 0; i < n / 2; i++) {
if (s[i] == s[n - i - 1])
continue;
cc += 1;
if (s[i] < s[n - i - 1])
s[n - i - 1] = s[i];
else
s[i] = s[n - i - 1];
}
return cc;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Daemon has 2 dragon eggs, one is made of A grams of Gold and the other is made of B grams of Silver.
We know that Gold costs G coins per gram and Silver costs S coins per gram. You need to determine which of the two dragon eggs are more valuable.The input contains 4 space-separated integers G, S, A, B.
<b> Constraints: </b>
1 β€ G, S, A, B β€ 10<sup>4</sup>Print "Gold" (without quotes) if the Gold egg costs <b>equal to or more than</b> the silver egg. Otherwise, print "Silver" (without quotes).
Note that the output is case-sensitive.Sample Input 1:
5 4 4 5
Sample Output 1:
Gold
Sample Input 2:
1 1 2 3
Sample Output 2:
Silver, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int [] num = new int[4];
String[] str = bi.readLine().split(" ");
for(int i = 0; i < str.length; i++)
num[i] = Integer.parseInt(str[i]);
if((num[0] * num[2]) >= (num[1] * num[3]))
System.out.print("Gold");
else
System.out.print("Silver");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Daemon has 2 dragon eggs, one is made of A grams of Gold and the other is made of B grams of Silver.
We know that Gold costs G coins per gram and Silver costs S coins per gram. You need to determine which of the two dragon eggs are more valuable.The input contains 4 space-separated integers G, S, A, B.
<b> Constraints: </b>
1 β€ G, S, A, B β€ 10<sup>4</sup>Print "Gold" (without quotes) if the Gold egg costs <b>equal to or more than</b> the silver egg. Otherwise, print "Silver" (without quotes).
Note that the output is case-sensitive.Sample Input 1:
5 4 4 5
Sample Output 1:
Gold
Sample Input 2:
1 1 2 3
Sample Output 2:
Silver, I have written this Solution Code:
G, S, A, B = input().split()
g = int(G)
s = int(S)
a = int(A)
b = int(B)
gold = (g*a)
silver = (s*b)
if(gold>=silver):
print("Gold")
else:
print("Silver"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Daemon has 2 dragon eggs, one is made of A grams of Gold and the other is made of B grams of Silver.
We know that Gold costs G coins per gram and Silver costs S coins per gram. You need to determine which of the two dragon eggs are more valuable.The input contains 4 space-separated integers G, S, A, B.
<b> Constraints: </b>
1 β€ G, S, A, B β€ 10<sup>4</sup>Print "Gold" (without quotes) if the Gold egg costs <b>equal to or more than</b> the silver egg. Otherwise, print "Silver" (without quotes).
Note that the output is case-sensitive.Sample Input 1:
5 4 4 5
Sample Output 1:
Gold
Sample Input 2:
1 1 2 3
Sample Output 2:
Silver, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main() {
int g, s, a, b;
cin >> g >> s >> a >> b;
cout << ((g * a >= s * b) ? "Gold" : "Silver");
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n boys and m toys. Your task is to distribute the toys so that as many boys as possible will get a toy.
Each boy has a desired toy size, and they will accept any toy whose size is close enough to the desired size.
So if the desired toy size of a particular boy is 'a' and a particular toy has size 'b', then boy will only accept the toy if |b-a| <= k.The first input line has three integers n, m, and k: the number of boys, the number of toys, and the maximum allowed difference.
The next line contains n integers a[1], a[2],β¦, a[n]: the desired toy size of each boy. If the desired toy size of a boy is x, he will accept any toy whose size is between xβk and x+k.
The last line contains m integers b[1], b[2],β¦, b[m]: the size of each toy.
<b>Constraints</b>
1 β€ n,m β€ 200000
0 β€ k β€ 10<sup>9</sup>
1 β€ a[i],b[i] β€ 10<sup>9</sup>Print one integer: the number of boys who will get a toy.Sample Input
4 3 5
60 45 80 60
30 60 75
Sample Output
2
Explanation: One possible way can give second toy to first boy and third toy to third boy.
Sample Input:
10 10 0
37 62 56 69 34 46 10 86 16 49
50 95 47 43 9 62 83 71 71 7
Sample Output:
1, I have written this Solution Code: import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import static java.lang.Math.pow;
class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static InputReader getInputReader(boolean readFromTextFile) throws FileNotFoundException {
return ((readFromTextFile) ? new InputReader(new FileInputStream("src/input.txt"))
: new InputReader(System.in));
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
class Main {
public static void main (String[] args) throws FileNotFoundException {
InputReader sc = InputReader.getInputReader(false);
int n = sc.nextInt();
int m = sc.nextInt();
long k = sc.nextLong();
long[] boyArray = new long[n];
long[] toyArray = new long[m];
for (int i = 0; i < n; i++) {
boyArray[i] = sc.nextLong();
}
for (int i = 0; i < m; i++) {
toyArray[i] = sc.nextLong();
}
Arrays.sort(boyArray);
Arrays.sort(toyArray);
int cnt = 0;
int l = 0 , h = n-1;
for (int i = 0; i < m; i++) {
while( h >= l){
if(toyArray[i] < boyArray[l]-k || toyArray[i] > boyArray[h]+k){
break;
}
else if(toyArray[i] >= boyArray[l]-k && toyArray[i] <= boyArray[l]+k){
l++;
cnt++;
break;
}
else if(toyArray[i] >= boyArray[h]-k && toyArray[i] <= boyArray[h]+k){
h--;
cnt++;
break;
}
else{
l++;
}
}
}
System.out.println(cnt);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n boys and m toys. Your task is to distribute the toys so that as many boys as possible will get a toy.
Each boy has a desired toy size, and they will accept any toy whose size is close enough to the desired size.
So if the desired toy size of a particular boy is 'a' and a particular toy has size 'b', then boy will only accept the toy if |b-a| <= k.The first input line has three integers n, m, and k: the number of boys, the number of toys, and the maximum allowed difference.
The next line contains n integers a[1], a[2],β¦, a[n]: the desired toy size of each boy. If the desired toy size of a boy is x, he will accept any toy whose size is between xβk and x+k.
The last line contains m integers b[1], b[2],β¦, b[m]: the size of each toy.
<b>Constraints</b>
1 β€ n,m β€ 200000
0 β€ k β€ 10<sup>9</sup>
1 β€ a[i],b[i] β€ 10<sup>9</sup>Print one integer: the number of boys who will get a toy.Sample Input
4 3 5
60 45 80 60
30 60 75
Sample Output
2
Explanation: One possible way can give second toy to first boy and third toy to third boy.
Sample Input:
10 10 0
37 62 56 69 34 46 10 86 16 49
50 95 47 43 9 62 83 71 71 7
Sample Output:
1, I have written this Solution Code: n,m,k=[int(x) for x in input().split()[:3]]
a=[int(x) for x in input().split()[:n]]
b=[int(x) for x in input().split()[:m]]
a.sort()
b.sort()
j = 0
sum1 = 0
for i in a:
while(j<len(b)):
if(i-k<=b[j] and b[j]<=i+k):
j = j+1
sum1 +=1
break
elif i+k<b[j]:
break
else:
j +=1
if(j>=len(b)):
break
print(sum1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n boys and m toys. Your task is to distribute the toys so that as many boys as possible will get a toy.
Each boy has a desired toy size, and they will accept any toy whose size is close enough to the desired size.
So if the desired toy size of a particular boy is 'a' and a particular toy has size 'b', then boy will only accept the toy if |b-a| <= k.The first input line has three integers n, m, and k: the number of boys, the number of toys, and the maximum allowed difference.
The next line contains n integers a[1], a[2],β¦, a[n]: the desired toy size of each boy. If the desired toy size of a boy is x, he will accept any toy whose size is between xβk and x+k.
The last line contains m integers b[1], b[2],β¦, b[m]: the size of each toy.
<b>Constraints</b>
1 β€ n,m β€ 200000
0 β€ k β€ 10<sup>9</sup>
1 β€ a[i],b[i] β€ 10<sup>9</sup>Print one integer: the number of boys who will get a toy.Sample Input
4 3 5
60 45 80 60
30 60 75
Sample Output
2
Explanation: One possible way can give second toy to first boy and third toy to third boy.
Sample Input:
10 10 0
37 62 56 69 34 46 10 86 16 49
50 95 47 43 9 62 83 71 71 7
Sample Output:
1, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
const int N = 3e5+5;
#define read(type) readInt<type>()
#define max1 200010
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
bool isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
int c[max1];
int main()
{
int n,m;
cin>>n>>m;
ll k;
cin>>k;
ll a[n],b[m];
FOR(i,n){
cin>>a[i];
}
FOR(i,m){
cin>>b[i];}
sort(a,a+n);
sort(b,b+m);
int i=0,j=0;
int cnt=0;
while(i!=n && j!=m){
if(abs(b[j]-a[i])<=k){cnt++;i++;j++;}
else{
if(b[j]>a[i]){i++;}
else{j++;}
}
}
out(cnt);
}
, 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 are given a matrix <b>A</b> of dimensions <b>n x m</b>. The task is to perform <b>boundary traversal</b> on the matrix in clockwise manner.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase has two lines of input. The first line contains dimensions of the matrix A, n and m. The second line contains n*m elements separated by spaces.
Constraints:
1 <= T <= 100
1 <= n, m <= 30
0 <= A[i][j] <= 100For each testcase, in a new line, print the boundary traversal of the matrix A.Input:
4
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4
12 11 10 9 8 7 6 5 4 3 2 1
1 4
1 2 3 4
4 1
1 2 3 4
Output:
1 2 3 4 8 12 16 15 14 13 9 5
12 11 10 9 5 1 2 3 4 8
1 2 3 4
1 2 3 4
Explanation:
Testcase1: The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The boundary traversal is 1 2 3 4 8 12 16 15 14 13 9 5
Testcase 2: Boundary Traversal will be 12 11 10 9 5 1 2 3 4 8.
Testcase 3: Boundary Traversal will be 1 2 3 4.
Testcase 4: Boundary Traversal will be 1 2 3 4., I have written this Solution Code:
def boundaryTraversal(matrix, N, M): #N = 3, M = 4
start_row = 0
start_col = 0
count = 0
if N != 1 and M != 1:
total = 2 * (N - 1) + 2 * (M - 1)
else:
total = max(M, N)
#First row
for i in range(start_col, M, 1): #0,1, 2, 3
count += 1
print(matrix[start_row][i], end = " ")
if count == total:
return
start_row += 1
#Last column
for i in range(start_row, N, 1): # 1, 2
count += 1
print(matrix[i][M - 1], end = " ")
if count == total:
return
M -= 1
#Last Row
for i in range(M - 1, start_col - 1, -1): # 2, 1, 0
count += 1
print(matrix[N - 1][i], end = " ")
if count == total:
return
#First Column
N -= 1 # 2
for i in range(N - 1, start_row - 1, -1):
count += 1
print(matrix[i][start_col], end = " ")
start_col += 1
testcase = int(input().strip())
for test in range(testcase):
dim = input().strip().split(" ")
N = int(dim[0])
M = int(dim[1])
matrix = []
elements = input().strip().split(" ") #N * M
start = 0
for i in range(N):
matrix.append(elements[start : start + M]) # (0, M - 1) | (M, 2*m-1)
start = start + M
boundaryTraversal(matrix, N, M)
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a matrix <b>A</b> of dimensions <b>n x m</b>. The task is to perform <b>boundary traversal</b> on the matrix in clockwise manner.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase has two lines of input. The first line contains dimensions of the matrix A, n and m. The second line contains n*m elements separated by spaces.
Constraints:
1 <= T <= 100
1 <= n, m <= 30
0 <= A[i][j] <= 100For each testcase, in a new line, print the boundary traversal of the matrix A.Input:
4
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4
12 11 10 9 8 7 6 5 4 3 2 1
1 4
1 2 3 4
4 1
1 2 3 4
Output:
1 2 3 4 8 12 16 15 14 13 9 5
12 11 10 9 5 1 2 3 4 8
1 2 3 4
1 2 3 4
Explanation:
Testcase1: The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The boundary traversal is 1 2 3 4 8 12 16 15 14 13 9 5
Testcase 2: Boundary Traversal will be 12 11 10 9 5 1 2 3 4 8.
Testcase 3: Boundary Traversal will be 1 2 3 4.
Testcase 4: Boundary Traversal will be 1 2 3 4., I have written this Solution Code: import java.io.*;
import java.lang.*;
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n1 = sc.nextInt();
int m1 = sc.nextInt();
int arr1[][] = new int[n1][m1];
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < m1; j++)
arr1[i][j] = sc.nextInt();
}
boundaryTraversal(n1, m1,arr1);
System.out.println();
}
}
static void boundaryTraversal( int n1, int m1, int arr1[][])
{
// base cases
if(n1 == 1)
{
int i = 0;
while(i < m1)
System.out.print(arr1[0][i++] + " ");
}
else if(m1 == 1)
{
int i = 0;
while(i < n1)
System.out.print(arr1[i++][0]+" ");
}
else
{
// traversing the first row
for(int j=0;j<m1;j++)
{
System.out.print(arr1[0][j]+" ");
}
// traversing the last column
for(int j=1;j<n1;j++)
{
System.out.print(arr1[j][m1-1]+ " ");
}
// traversing the last row
for(int j=m1-2;j>=0;j--)
{
System.out.print(arr1[n1-1][j]+" ");
}
// traversing the first column
for(int j=n1-2;j>=1;j--)
{
System.out.print(arr1[j][0]+" ");
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Currently, Adk is researching a virus that has conquered the planet. For killing the virus, three chemicals (A, B, and C) are required in a particular proportion. Let the quantities of chemicals be a, b, and c respectively.
The virus will be killed if a<sup>2</sup>+b<sup>2</sup>+c<sup>2</sup>=n.
You need to find the number of triplets satisfying the condition for all the values of n ranging from 1 to N.
<b>Note:</b> a, b and c must be integers.
<b>Note:</b> Obviously, the amount of chemical cannot be negative.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10000Output N integers, the number of triplets satisfying the condition for n = 1, 2, 3, ..., N on a new line.Sample Input:
10
Sample Output:
3
3
1
3
6
3
0
3
6
6
<b>Explanation:</b>
For n=1, triplets are (0, 0, 1), (1, 0, 0), (0, 1, 0)
For n=7, there are no triplets satisfying the condition.
For n=10, triplets are (0, 1, 3), (0, 3, 1), (1, 0, 3), (1, 3, 0), (3, 0, 1), (3, 1, 0)., I have written this Solution Code: import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
String s=sc.readLine();
int n=Integer.parseInt(s);
int temp=1;
StringBuilder br=new StringBuilder();
int j=0,k=1;
while(temp<=n){
if(7*k+j==temp) { br.append(0+"\n");k++;j++;}
else
br.append(count(temp)+"\n");
temp++;
}
System.out.print(br);
}
static int count(int temp){
int i,j,k,count=0;
int temp1=(int)Math.sqrt(temp);
for(i=0;i<=temp1;i++){
for(j=0;j<=temp1;j++){
int x=(temp-i*i-j*j);
int y=(int)Math.sqrt(x);
if(y*y==x) count++;
}
if(i*i>temp) break;
}
return count;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Currently, Adk is researching a virus that has conquered the planet. For killing the virus, three chemicals (A, B, and C) are required in a particular proportion. Let the quantities of chemicals be a, b, and c respectively.
The virus will be killed if a<sup>2</sup>+b<sup>2</sup>+c<sup>2</sup>=n.
You need to find the number of triplets satisfying the condition for all the values of n ranging from 1 to N.
<b>Note:</b> a, b and c must be integers.
<b>Note:</b> Obviously, the amount of chemical cannot be negative.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10000Output N integers, the number of triplets satisfying the condition for n = 1, 2, 3, ..., N on a new line.Sample Input:
10
Sample Output:
3
3
1
3
6
3
0
3
6
6
<b>Explanation:</b>
For n=1, triplets are (0, 0, 1), (1, 0, 0), (0, 1, 0)
For n=7, there are no triplets satisfying the condition.
For n=10, triplets are (0, 1, 3), (0, 3, 1), (1, 0, 3), (1, 3, 0), (3, 0, 1), (3, 1, 0)., I have written this Solution Code: n = int(input())
ans = [0]*(n+1)
for i in range(0,101):
temp1 = i*i
for j in range(0,101):
temp2 = j*j
for k in range(0,101):
temp3 = k*k
if temp1+temp2+temp3<=n:
ans[temp1+temp2+temp3] += 1
for i in range(1,n+1):
print(ans[i]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Currently, Adk is researching a virus that has conquered the planet. For killing the virus, three chemicals (A, B, and C) are required in a particular proportion. Let the quantities of chemicals be a, b, and c respectively.
The virus will be killed if a<sup>2</sup>+b<sup>2</sup>+c<sup>2</sup>=n.
You need to find the number of triplets satisfying the condition for all the values of n ranging from 1 to N.
<b>Note:</b> a, b and c must be integers.
<b>Note:</b> Obviously, the amount of chemical cannot be negative.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10000Output N integers, the number of triplets satisfying the condition for n = 1, 2, 3, ..., N on a new line.Sample Input:
10
Sample Output:
3
3
1
3
6
3
0
3
6
6
<b>Explanation:</b>
For n=1, triplets are (0, 0, 1), (1, 0, 0), (0, 1, 0)
For n=7, there are no triplets satisfying the condition.
For n=10, triplets are (0, 1, 3), (0, 3, 1), (1, 0, 3), (1, 3, 0), (3, 0, 1), (3, 1, 0)., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
int ans[100001];
void solve(){
int n; cin>>n;
For(i, 0, 101){
For(j, 0, 101){
For(k, 0, 101){
int x = i*i+j*j+k*k;
ans[x]++;
}
}
}
int sv = 0;
For(i, 1, n+1){
cout<<ans[i]<<"\n";
}
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
// cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: function Charity(n,m) {
// write code here
// do no console.log the answer
// return the output using return keyword
const per = Math.floor(m / n)
return per > 1 ? per : -1
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: static int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: def Charity(N,M):
x = M//N
if x<=1:
return -1
return x
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a charity which has N people. Penny wants to donate some of her clothes to the charity in such a way that all people receive equal clothes and each individual receives <b> more than 1 </b>. If she has M clothes with her what is the maximum number of clothes one individual can get?<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Charity()</b> that takes integers N, and M as arguments.
Constraints:-
1 <= M, N <= 1000Return the maximum number of clothes one individual can get if it is impossible to distribute clothes return -1.Sample Input
6 20
Sample Output
3
Sample Input
8 5
Sample Output
-1, I have written this Solution Code: int Charity(int n, int m){
int x= m/n;
if(x<=1){return -1;}
return x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Doubly linked list consisting of <b>N</b> nodes and given a number <b>K</b>. The task is to delete the Kth node from the end of the linked list.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>deleteElement()</b> that takes head node and the position K as parameter.
Constraints:
1 <=K<=N<= 1000
1 <=value<= 1000Return the head of the modified Doubly linked listInput:
5 3
1 2 3 4 5
Output:
1 2 4 5
Explanation:
After deleting 3rd node from the end of the linked list, 3 will be deleted and the list will become 1, 2, 4, 5., I have written this Solution Code: public static Node deleteElement(Node head,int k) {
int cnt=0;
Node temp=head;
while(temp!=null){
cnt++;
temp=temp.next;
}
k=cnt-k;
if(k==0){head=head.next;
head.prev=null;
return head;}
temp=head;
int i=0;
while(i!=k-1){
temp=temp.next;
i++;
}
temp.next=temp.next.next;
if(k!=cnt-1){temp.next.prev=temp;}
return head;
}, 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 "Newton" instead of the number.
For each multiple of 5, print "School" instead of the number.
For numbers that are multiples of both 3 and 5, print "NewtonSchool" instead of the number.The first line of the input contains N.
<b>Constraints</b>
1 < = N < = 1000
Print N space separated number or Newton School according to the condition.Sample Input:-
3
Sample Output:-
1 2 Newton
Sample Input:-
5
Sample Output:-
1 2 Newton 4 School, I have written this Solution Code: n=int(input())
for i in range(1,n+1):
if i%3==0 and i%5==0:
print("NewtonSchool",end=" ")
elif i%3==0:
print("Newton",end=" ")
elif i%5==0:
print("School",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 "Newton" instead of the number.
For each multiple of 5, print "School" instead of the number.
For numbers that are multiples of both 3 and 5, print "NewtonSchool" instead of the number.The first line of the input contains N.
<b>Constraints</b>
1 < = N < = 1000
Print N space separated number or Newton School according to the condition.Sample Input:-
3
Sample Output:-
1 2 Newton
Sample Input:-
5
Sample Output:-
1 2 Newton 4 School, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static void NewtonSchool(int n){
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.print("NewtonSchool ");}
else if(i%5==0){System.out.print("School ");}
else if(i%3==0){System.out.print("Newton ");}
else{System.out.print(i+" ");}
}
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int x= sc.nextInt();
NewtonSchool(x);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Phoebe is a big GCD fan. Being bored, she starts counting number of pairs of integers (A, B) such that following conditions are satisfied:
<li> GCD(A, B) = X (As X is Phoebe's favourite integer)
<li> A <= B <= L
As Phoebe's performance is coming up, she needs your help to find the number of such pairs possible.
Note: GCD refers to the <a href="https://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest common divisor</a>.Input contains two integers L and X.
Constraints:
1 <= L, X <= 1000000000Print a single integer denoting number of pairs possible.Sample Input
5 2
Sample Output
2
Explanation: Pairs satisfying all conditions are: (2, 2), (2, 4)
Sample Input
5 3
Sample Output
1
Explanation: Pairs satisfying all conditions are: (3, 3), I have written this Solution Code: import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
out.println(sumTotient(ni()/ni()));
}
public static int[] enumTotientByLpf(int n, int[] lpf)
{
int[] ret = new int[n+1];
ret[1] = 1;
for(int i = 2;i <= n;i++){
int j = i/lpf[i];
if(lpf[j] != lpf[i]){
ret[i] = ret[j] * (lpf[i]-1);
}else{
ret[i] = ret[j] * lpf[i];
}
}
return ret;
}
public static int[] enumLowestPrimeFactors(int n)
{
int tot = 0;
int[] lpf = new int[n+1];
int u = n+32;
double lu = Math.log(u);
int[] primes = new int[(int)(u/lu+u/lu/lu*1.5)];
for(int i = 2;i <= n;i++)lpf[i] = i;
for(int p = 2;p <= n;p++){
if(lpf[p] == p)primes[tot++] = p;
int tmp;
for(int i = 0;i < tot && primes[i] <= lpf[p] && (tmp = primes[i]*p) <= n;i++){
lpf[tmp] = primes[i];
}
}
return lpf;
}
public static long sumTotient(int n)
{
if(n == 0)return 0L;
if(n == 1)return 1L;
int s = (int)Math.sqrt(n);
long[] cacheu = new long[n/s];
long[] cachel = new long[s+1];
int X = (int)Math.pow(n, 0.66);
int[] lpf = enumLowestPrimeFactors(X);
int[] tot = enumTotientByLpf(X, lpf);
long sum = 0;
int p = cacheu.length-1;
for(int i = 1;i <= X;i++){
sum += tot[i];
if(i <= s){
cachel[i] = sum;
}else if(p > 0 && i == n/p){
cacheu[p] = sum;
p--;
}
}
for(int i = p;i >= 1;i--){
int x = n/i;
long all = (long)x*(x+1)/2;
int ls = (int)Math.sqrt(x);
for(int j = 2;x/j > ls;j++){
long lval = i*j < cacheu.length ? cacheu[i*j] : cachel[x/j];
all -= lval;
}
for(int v = ls;v >= 1;v--){
long w = x/v-x/(v+1);
all -= cachel[v]*w;
}
cacheu[(int)i] = all;
}
return cacheu[1];
}
void run() throws Exception
{
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new Main().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Phoebe is a big GCD fan. Being bored, she starts counting number of pairs of integers (A, B) such that following conditions are satisfied:
<li> GCD(A, B) = X (As X is Phoebe's favourite integer)
<li> A <= B <= L
As Phoebe's performance is coming up, she needs your help to find the number of such pairs possible.
Note: GCD refers to the <a href="https://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest common divisor</a>.Input contains two integers L and X.
Constraints:
1 <= L, X <= 1000000000Print a single integer denoting number of pairs possible.Sample Input
5 2
Sample Output
2
Explanation: Pairs satisfying all conditions are: (2, 2), (2, 4)
Sample Input
5 3
Sample Output
1
Explanation: Pairs satisfying all conditions are: (3, 3), I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
template<class C> void mini(C&a4, C b4){a4=min(a4,b4);}
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define mod 1000000007ll
#define pii pair<int,int>
/////////////
ull X[20000001];
ull cmp(ull N){
return N*(N+1)/2;
}
ull solve(ull N){
if(N==1)
return 1;
if(N < 20000001 && X[N] != 0)
return X[N];
ull res = 0;
ull q = floor(sqrt(N));
for(int k=2;k<N/q+1;++k){
res += solve(N/k);
}
for(int m=1;m<q;++m){
res += (N/m - N/(m+1)) * solve(m);
}
res = cmp(N) - res;
if(N < 20000001)
X[N] = res;
return res;
}
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int l,x;
cin>>l>>x;
if(l<x)
cout<<0;
else
cout<<solve(l/x);
#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: Calculate inversion count of array of integers.
Inversion count of an array is quantisation of how much unsorted an array is. A sorted array has inversion count 0, while an unsorted array has maximum inversion count.
Formally speaking inversion count = number of pairs i, j such that i < j and a[i] > a[j].The first line contain integers N.
The second line of the input contains N singly spaces integers.
1 <= N <= 100000
1 <= A[i] <= 1000000000Output one integer the inversion count.Sample Input
5
1 1 3 2 2
Sample Output
2
Sample Input
5
5 4 3 2 1
Sample Output
10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static long count(int[] arr, int l, int h, int[] aux) {
if (l >= h) return 0;
int mid = (l +h) / 2;
long count = 0;
count += count(aux, l, mid, arr);
count += count(aux, mid + 1, h, arr);
count += merge(arr, l, mid, h, aux);
return count;
}
static long merge(int[] arr, int l, int mid, int h, int[] aux) {
long count = 0;
int i = l, j = mid + 1, k = l;
while (i <= mid || j <= h) {
if (i > mid) {
arr[k++] = aux[j++];
} else if (j > h) {
arr[k++] = aux[i++];
} else if (aux[i] <= aux[j]) {
arr[k++] = aux[i++];
} else {
arr[k++] = aux[j++];
count += mid + 1 - i;
}
}
return count;
}
public static void main (String[] args)throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String str[];
str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
str = br.readLine().split(" ");
int arr[] =new int[n];
for (int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(str[j]);
}
int[] aux = arr.clone();
System.out.print(count(arr, 0, n - 1, aux));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculate inversion count of array of integers.
Inversion count of an array is quantisation of how much unsorted an array is. A sorted array has inversion count 0, while an unsorted array has maximum inversion count.
Formally speaking inversion count = number of pairs i, j such that i < j and a[i] > a[j].The first line contain integers N.
The second line of the input contains N singly spaces integers.
1 <= N <= 100000
1 <= A[i] <= 1000000000Output one integer the inversion count.Sample Input
5
1 1 3 2 2
Sample Output
2
Sample Input
5
5 4 3 2 1
Sample Output
10, I have written this Solution Code: count=0
def implementMergeSort(arr,s,e):
global count
if e-s==1:
return
mid=(s+e)//2
implementMergeSort(arr,s,mid)
implementMergeSort(arr,mid,e)
count+=merge_sort_place(arr,s,mid,e)
return count
def merge_sort_place(arr,s,mid,e):
arr3=[]
i=s
j=mid
count=0
while i<mid and j<e:
if arr[i]>arr[j]:
arr3.append(arr[j])
j+=1
count+=(mid-i)
else:
arr3.append(arr[i])
i+=1
while (i<mid):
arr3.append(arr[i])
i+=1
while (j<e):
arr3.append(arr[j])
j+=1
for x in range(len(arr3)):
arr[s+x]=arr3[x]
return count
n=int(input())
arr=list(map(int,input().split()[:n]))
c=implementMergeSort(arr,0,len(arr))
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculate inversion count of array of integers.
Inversion count of an array is quantisation of how much unsorted an array is. A sorted array has inversion count 0, while an unsorted array has maximum inversion count.
Formally speaking inversion count = number of pairs i, j such that i < j and a[i] > a[j].The first line contain integers N.
The second line of the input contains N singly spaces integers.
1 <= N <= 100000
1 <= A[i] <= 1000000000Output one integer the inversion count.Sample Input
5
1 1 3 2 2
Sample Output
2
Sample Input
5
5 4 3 2 1
Sample Output
10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
long long _mergeSort(long long arr[], int temp[], int left, int right);
long long merge(long long arr[], int temp[], int left, int mid, int right);
/* This function sorts the input array and returns the
number of inversions in the array */
long long mergeSort(long long arr[], int array_size)
{
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function that sorts the input array and
returns the number of inversions in the array. */
long long _mergeSort(long long arr[], int temp[], int left, int right)
{
long long mid, inv_count = 0;
if (right > left) {
/* Divide the array into two parts and
call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of
inversions in left-part, right-part
and number of inversions in merging */
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left, mid + 1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
long long merge(long long arr[], int temp[], int left,
int mid, int right)
{
int i, j, k;
long long inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
/* this is tricky -- see above
explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;}
int main(){
int n;
cin>>n;
long long a[n];
for(int i=0;i<n;i++){
cin>>a[i];}
long long ans = mergeSort(a, n);
cout << ans; }
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import math
n = int(input())
for i in range(n):
x = int(input())
count = 0
for i in range(1, int(math.sqrt(x))+1):
if x % i == 0:
if (i%2 == 0):
count+=1
if ((x/i) %2 == 0):
count+=1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int count=0;
for(int i=1;i<=Math.sqrt(n);i++){
if(n%i == 0)
{
if(i%2==0) {
count++;
}
if(i*i != n && (n/i)%2==0) {
count++;
}
}
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
if(n&1){cout<<0<<endl;continue;}
long x=sqrt(n);
int cnt=0;
for(long long i=1;i<=x;i++){
if(!(n%i)){
if(!(i%2)){cnt++;}
if(i*i!=n){
if(!((n/i)%2)){cnt++;}
}
}
}
cout<<cnt<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, we define the set A<sub>N</sub> as {1, 2, 3, ... N-1, N}.
Find the size of the largest subset of A<sub>N</sub>, such that if x belongs to the subset, then 2x does not belong to the subset.The first line of the input contains a single integer T β the number of test cases.
Each test case consists of one line containing a single integer N.
<b>Constraints:</b>
1 β€ T β€ 10<sup>5</sup>
1 β€ N β€ 10<sup>9</sup>For each test case, print one integer β the size of the largest subset of A<sub>N</sub> satisfying the condition in the problem statement.Sample Input:
4
1
2
3
4
Sample Output:
1
1
2
3
Explanation:
For test case 4, where N = 4, the subset {1, 3, 4} is the largest subset satisfying the conditions.
1 is in the subset whereas 2 is not.
3 is in the subset whereas 6 is not.
4 is in the subset whereas 8 is not., I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br;
static long compute(long n){
if(n==1||n==2) return 1;
if(n==3) return 2;
if(n%2==0){
long ans=(n/2);
return ans+compute(ans/2);
}
else {
long ans=(n+1)/2;
if(ans%2==1) return ans+compute(ans/2);
else return ans+compute((ans/2)-1);
}
}
public static void main(String[] args) throws Exception
{
br= new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0){
long n=Long.parseLong(br.readLine());
long ans=compute(n);
pw.println(ans);
}
pw.flush();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, we define the set A<sub>N</sub> as {1, 2, 3, ... N-1, N}.
Find the size of the largest subset of A<sub>N</sub>, such that if x belongs to the subset, then 2x does not belong to the subset.The first line of the input contains a single integer T β the number of test cases.
Each test case consists of one line containing a single integer N.
<b>Constraints:</b>
1 β€ T β€ 10<sup>5</sup>
1 β€ N β€ 10<sup>9</sup>For each test case, print one integer β the size of the largest subset of A<sub>N</sub> satisfying the condition in the problem statement.Sample Input:
4
1
2
3
4
Sample Output:
1
1
2
3
Explanation:
For test case 4, where N = 4, the subset {1, 3, 4} is the largest subset satisfying the conditions.
1 is in the subset whereas 2 is not.
3 is in the subset whereas 6 is not.
4 is in the subset whereas 8 is not., I have written this Solution Code: //Author: Xzirium
//Time and Date: 15:50:05 28 September 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(T);
while(T--)
{
READV(N);
ll ans=0;
while (N>0)
{
ans+=(N+1)/2;
N=N/4;
}
cout<<ans<<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 the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code: static int King(int X, int Y){
int a[] = {0,0,1,1,1,-1,-1,-1};
int b[] = {-1,1,1,-1,0,1,-1,0};
int cnt=0;
for(int i=0;i<8;i++){
if(X+a[i]<=8 && X+a[i]>=1 && Y+b[i]>=1 && Y+b[i]<=8){cnt++;}
}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code:
int King(int X, int Y){
int a[] = {0,0,1,1,1,-1,-1,-1};
int b[] = {-1,1,1,-1,0,1,-1,0};
int cnt=0;
for(int i=0;i<8;i++){
if(X+a[i]<=8 && X+a[i]>=1 && Y+b[i]>=1 && Y+b[i]<=8){cnt++;}
}
return cnt;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the position of the king in an 8X8 chessboard. Your task is to find the number of positions King can go into in one move.
Note:-
Remember king can move in all eight directions but can not go outside of the chess board<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>King()</b> that takes integers X, and Y as arguments.
Constraints:-
1 <= X, Y <= 8Return the number of positions King can go into in one move.Sample Input:-
2 1
Sample Output:-
5
Explanation:-
the king can go into:- (1, 1), (3, 1), (1, 2). (2, 2), (3, 2)
Sample Input:-
4 4
Sample Output:-
8, I have written this Solution Code: import array as arr
def King(X, Y):
a = arr.array('i', [0,0,1,1,1,-1,-1,-1])
b = arr.array('i', [-1,1,1,-1,0,1,-1,0])
cnt=0
for i in range (0,8):
if(X+a[i]<=8 and X+a[i]>=1 and Y+b[i]>=1 and Y+b[i]<=8):
cnt=cnt+1
return cnt
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.