Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p > 0)
sum += p;
}
cout << sum;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: n=int(input())
li = list(map(int,input().strip().split()))
sum=0
for i in li:
if i>0:
sum+=i
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long arr[] = new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
if(arr[i]>0){
sum+=arr[i];
}
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N which is to be reduced to 1 by performing the given operation:-
In one operation you can subtract any divisor of N other than N itself from N.
Your task is to find the minimum number to reduce N to 1.The input contains a single integer N.
<b>Constraints:-</b>
1 <= N <= 1000Print the minimum number of operations need to convert N to 1.Sample Input 1:-
5
Sample Output 1:-
3
<b>Explanation:-</b>
5 - > 4 - > 2 - > 1
Sample Input 2:-
8
Sample Output 2:-
3
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int dp[1001];
int solve(int n){
if(n<=1){
return 0;
}
if(dp[n]==-1){
int ans = INT_MAX;
int x = sqrt(n);
ans=min(ans,solve(n-1));
for(int i=2;i<=x;i++){
if(n%i==0){
ans=min(ans,solve(n-i));
ans=min(ans,solve(n-(n/i)));
}}
dp[n]=ans+1;
}
return dp[n];
}
int main(){
for(int i=0;i<1001;i++){
dp[i]=-1;
}
int n;
cin>>n;
cout<<solve(n);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N which is to be reduced to 1 by performing the given operation:-
In one operation you can subtract any divisor of N other than N itself from N.
Your task is to find the minimum number to reduce N to 1.The input contains a single integer N.
<b>Constraints:-</b>
1 <= N <= 1000Print the minimum number of operations need to convert N to 1.Sample Input 1:-
5
Sample Output 1:-
3
<b>Explanation:-</b>
5 - > 4 - > 2 - > 1
Sample Input 2:-
8
Sample Output 2:-
3
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static short min = 127;
public static void main (String[] args)throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(sc.readLine());
int[] dp = new int[n+1];
Arrays.fill(dp, 1000);
dp[0] = 0;
dp[1] = 0;
for(int i=2; i<=n; i++){
for(int j=2; j*j <= i; j++){
if(i%j == 0){
dp[i] = Math.min(dp[i], dp[i-j]+1);
dp[i] = Math.min(dp[i], dp[i-(i/j)]+1);
}
}
dp[i] = Math.min(dp[i], dp[i-1]+1);
}
System.out.println(dp[n]);
}
public static void check(short n, short count){
if(n == 4 || n == 3){
count +=2;
min = (short)Math.min(min, count);
return;
}
if(n ==2 || n==1){
count += (n-1);
min = (short)Math.min(min, count);
return;
}
HashSet<Short> ar = div(n);
for(short i: ar){
check((short)(n-i), (short)(count+1));
}
}
public static HashSet<Short> div(short n){
HashSet<Short> ar = new HashSet<>();
ar.add((short)1);
for(short i=2; i*i<=n; i++){
if(n%i == 0){
ar.add(i);
ar.add((short)(n/i));
}
}
return ar;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
import java.math.BigInteger;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger sum;
String ip1 = sc.next();
String ip2 = sc.next();
BigInteger a = new BigInteger(ip1);
BigInteger b = new BigInteger(ip2);
sum = a.add(b);
System.out.println(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-02-03 02:46:30
**/
#include <bits/stdc++.h>
#define NX 105
#define MX 3350
using namespace std;
const int mod = 998244353;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
typedef long long INT;
const int pb = 10;
const int base_digits = 9;
const int base = 1000000000;
const int DIV = 100000;
struct bigint {
vector<int> a;
int sign;
bigint() : sign(1) {}
bigint(INT v) { *this = v; }
bigint(const string &s) { read(s); }
void operator=(const bigint &v) { sign = v.sign, a = v.a; }
void operator=(INT v) {
sign = 1;
if (v < 0) sign = -1, v = -v;
for (; v > 0; v = v / base) a.push_back(v % base);
}
bigint operator+(const bigint &v) const {
if (sign == v.sign) {
bigint res = v;
for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) ||
carry;
i++) {
if (i == (int)res.a.size()) res.a.push_back(0);
res.a[i] += carry + (i < (int)a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry) res.a[i] -= base;
}
return res;
}
return *this - (-v);
}
bigint operator-(const bigint &v) const {
if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int)v.a.size() || carry; i++) {
res.a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0);
carry = res.a[i] < 0;
if (carry) res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0) sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int)a.size() || carry; i++) {
if (i == (int)a.size()) a.push_back(0);
INT cur = a[i] * (INT)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
}
trim();
}
bigint operator*(int v) const {
bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> DIVmod(const bigint &a1, const bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
int d = ((INT)base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0) r += b, --d;
q.a[i] = d;
}
q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}
bigint operator/(const bigint &v) const { return DIVmod(*this, v).first; }
bigint operator%(const bigint &v) const { return DIVmod(*this, v).second; }
void operator/=(int v) {
if (v < 0) sign = -sign, v = -v;
for (int i = (int)a.size() - 1, rem = 0; i >= 0; i--) {
INT cur = a[i] + rem * (INT)base;
a[i] = (int)(cur / v);
rem = (int)(cur % v);
}
trim();
}
bigint operator/(int v) const {
bigint res = *this;
res /= v;
return res;
}
int operator%(int v) const {
if (v < 0) v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (INT)base) % v;
return m * sign;
}
void operator+=(const bigint &v) { *this = *this + v; }
void operator-=(const bigint &v) { *this = *this - v; }
void operator*=(const bigint &v) { *this = *this * v; }
void operator/=(const bigint &v) { *this = *this / v; }
bool operator<(const bigint &v) const {
if (sign != v.sign) return sign < v.sign;
if (a.size() != v.a.size()) return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i]) return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const { return v < *this; }
bool operator<=(const bigint &v) const { return !(v < *this); }
bool operator>=(const bigint &v) const { return !(*this < v); }
bool operator==(const bigint &v) const { return !(*this < v) && !(v < *this); }
bool operator!=(const bigint &v) const { return *this < v || v < *this; }
void trim() {
while (!a.empty() && !a.back()) a.pop_back();
if (a.empty()) sign = 1;
}
bool isZero() const { return a.empty() || (a.size() == 1 && !a[0]); }
bigint operator-() const {
bigint res = *this;
res.sign = -sign;
return res;
}
bigint abs() const {
bigint res = *this;
res.sign *= res.sign;
return res;
}
INT longValue() const {
INT res = 0;
for (int i = a.size() - 1; i >= 0; i--) res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) { return b.isZero() ? a : gcd(b, a % b); }
friend bigint lcm(const bigint &a, const bigint &b) { return a / gcd(a, b) * b; }
void read(const string &s) {
sign = 1;
a.clear();
int pos = 0;
while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-') sign = -sign;
pos++;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++) x = x *
pb +
s[j] - '0';
a.push_back(x);
}
trim();
}
friend istream &operator>>(istream &stream, bigint &v) {
string s;
stream >> s;
v.read(s);
return stream;
}
friend ostream &operator<<(ostream &stream, const bigint &v) {
if (v.sign == -1) stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int)v.a.size() - 2; i >= 0; --i) stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
vector<INT> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int)p.size(); i++) p[i] = p[i - 1] * pb;
vector<int> res;
INT cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int)a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int)cur);
while (!res.empty() && !res.back()) res.pop_back();
return res;
}
typedef vector<INT> vll;
static vll karatsubaMultiply(const vll &a, const vll &b) {
int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}
int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for (int i = 0; i < k; i++) a2[i] += a1[i];
for (int i = 0; i < k; i++) b2[i] += b1[i];
vll r = karatsubaMultiply(a2, b2);
for (int i = 0; i < (int)a1b1.size(); i++) r[i] -= a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++) r[i] -= a2b2[i];
for (int i = 0; i < (int)r.size(); i++) res[i + k] += r[i];
for (int i = 0; i < (int)a1b1.size(); i++) res[i] += a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++) res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a5 = convert_base(this->a, base_digits, 5);
vector<int> b5 = convert_base(v.a, base_digits, 5);
vll a(a5.begin(), a5.end());
vll b(b5.begin(), b5.end());
while (a.size() < b.size()) a.push_back(0);
while (b.size() < a.size()) b.push_back(0);
while (a.size() & (a.size() - 1)) a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int)c.size(); i++) {
INT cur = c[i] + carry;
res.a.push_back((int)(cur % DIV));
carry = (int)(cur / DIV);
}
res.a = convert_base(res.a, 5, base_digits);
res.trim();
return res;
}
inline bool isOdd() { return a[0] & 1; }
};
int main() {
bigint n, m;
cin >> n >> m;
cout << n + m << "\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: n,m = map(int,input().split())
print(n+m) , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer <b>N</b>, you need to typecast this integer to String. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>checkConvertion()</b>, which contains N as a parameter.You need to return the typecasted string value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
5
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: def checkConevrtion(a):
return str(a)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer <b>N</b>, you need to typecast this integer to String. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>checkConvertion()</b>, which contains N as a parameter.You need to return the typecasted string value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
5
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: static String checkConevrtion(int a)
{
return String.valueOf(a);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: static void verticalFive(){
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
}
static void horizontalFive(){
System.out.print("* * * * *");
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: def vertical5():
for i in range(0,5):
print("*",end="\n")
#print()
def horizontal5():
for i in range(0,5):
print("*",end=" ")
vertical5()
print(end="\n")
horizontal5(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N independent variables X1, X2. . Xn. You have to find the number of solutions of equation :
X1 (xor) X2 (xor) X3. (xor) Xn = K (Xor of all variables should be equal to K)
0 <= Xi <= 2^D - 1
You will be given N, K and D as input.
As the answer can be very large report answer modulo 1000000007First line of input contains three space seperated integers N, D, K
1 <= N <= 100000000000
1 <= D <= 100000000000
1 <= K <= 100000000000Find the number of solutions of equation modulo 1000000007.Sample input 1
2 2 1
Sample output 1
4
Explaination : (x1=0, x2=1) (x1=1, x2=0) (x1=2, x3=3) (x1=3, x2=2) 4 solutions, 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 inf 1e8+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();
//////////////
#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>
/////////////
long long powerm(long long x, unsigned long long y, long long p)
{
long long res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
signed main()
{
fastio;
int n,k,d;
cin>>n>>d>>k;
int mo=1000000007;
if(d>60)
cout<<powerm(powerm(2,d,mo),n-1,mo);
else
{
if(k<(1ll<<d))
cout<<powerm(powerm(2,d,mo),n-1,mo);
else
cout<<0;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and a queue of integers, your task is to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>ReverseK()</b>:- that takes the Queue and the integer K as parameters.
Constraints:
1 ≤ K ≤ N ≤ 10000
1 ≤ elements ≤ 10000
You need to return the modified Queue.Input 1:
5 3
1 2 3 4 5
Output 1:
3 2 1 4 5
Input 2:
5 5
1 2 3 4 5
Output 2:
5 4 3 2 1, I have written this Solution Code: static Queue<Integer> ReverseK(Queue<Integer> queue, int k) {
Stack<Integer> stack = new Stack<Integer>();
// Push the first K elements into a Stack
for (int i = 0; i < k; i++) {
stack.push(queue.peek());
queue.remove();
}
// Enqueue the contents of stack at the back
// of the queue
while (!stack.empty()) {
queue.add(stack.peek());
stack.pop();
}
// Remove the remaining elements and enqueue
// them at the end of the Queue
for (int i = 0; i < queue.size() - k; i++) {
queue.add(queue.peek());
queue.remove();
}
return queue;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An element of the matrix is called strange if its corresponding row and column contains all 1's. Given a boolean matrix of size N*M, your task is to find the number of strange elements.The first line of input contains two space-separated integers N and M. Next N lines of input contain M space-separated integers depicting the values of the matrix.
<b>Constraints:-</b>
3 ≤ N, M ≤ 50
0 ≤ Matrix[][] ≤ 1Print the number of strange elementsSample Input:-
3 2
1 1
0 1
1 1
Sample Output:-
2
Sample Input:-
4 4
1 0 1 1
0 1 1 1
1 1 1 1
0 1 1 1
Sample Output:-
2
<b>Explanation 1:-</b>
(0, 1) and (2, 1), I have written this Solution Code: n = [int(i) for i in input().split()]
a = [[int(i) for i in input().split()] for j in range(n[0])]
c = 0
for i in range(n[0]):
t = True
for j in range(n[1]):
if a[i][j] == 0:
t = False
break;
if t:
c = c+1
r = 0
for j in range(n[1]):
t = True
for i in range(n[0]):
if a[i][j]==0:
t = False
break;
if t:
r = r+1
print(r*c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An element of the matrix is called strange if its corresponding row and column contains all 1's. Given a boolean matrix of size N*M, your task is to find the number of strange elements.The first line of input contains two space-separated integers N and M. Next N lines of input contain M space-separated integers depicting the values of the matrix.
<b>Constraints:-</b>
3 ≤ N, M ≤ 50
0 ≤ Matrix[][] ≤ 1Print the number of strange elementsSample Input:-
3 2
1 1
0 1
1 1
Sample Output:-
2
Sample Input:-
4 4
1 0 1 1
0 1 1 1
1 1 1 1
0 1 1 1
Sample Output:-
2
<b>Explanation 1:-</b>
(0, 1) and (2, 1), I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int a[][] = new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]= sc.nextInt();
}
}
int cnt=0;
int p=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
p=0;
if(a[i][j]==1){
for(int k=0;k<n;k++){
if(a[k][j]==1){p++;}
}
for(int k=0;k<m;k++){
if(a[i][k]==1){p++;}
}
if(p==(n+m)){
cnt++;}
}
}
}
System.out.print(cnt);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An element of the matrix is called strange if its corresponding row and column contains all 1's. Given a boolean matrix of size N*M, your task is to find the number of strange elements.The first line of input contains two space-separated integers N and M. Next N lines of input contain M space-separated integers depicting the values of the matrix.
<b>Constraints:-</b>
3 ≤ N, M ≤ 50
0 ≤ Matrix[][] ≤ 1Print the number of strange elementsSample Input:-
3 2
1 1
0 1
1 1
Sample Output:-
2
Sample Input:-
4 4
1 0 1 1
0 1 1 1
1 1 1 1
0 1 1 1
Sample Output:-
2
<b>Explanation 1:-</b>
(0, 1) and (2, 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 max1 1000001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n,m;
cin>>n>>m;
int a[n][m];
FOR(i,n){
FOR(j,m){
cin>>a[i][j];}}
bool b[n];
bool c[m];
FOR(i,n){
b[i]=false;}
FOR(i,m){
c[i]=false;}
bool win;
for(int i=0;i<n;i++){
win=true;
for(int j=0;j<m;j++){
if(a[i][j]==0){win=false;}
}
b[i]=win;
}
for(int i=0;i<m;i++){
win=true;
for(int j=0;j<n;j++){
if(a[j][i]==0){win=false;}
}
c[i]=win;
}
int cnt=0;
for(int i=0;i<n;i++){
win=true;
for(int j=0;j<m;j++){
if(b[i] && c[j]){cnt++;}
// if(a[j][i]==0){win=false;}
}
}
out(cnt);
}
, 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 (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The 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(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code: n = int(input())
all_no = input().split(' ')
i = 0
joined_str = ''
while(i < n-1):
if(i == 0):
joined_str = str(int(all_no[i]) + int(all_no[i+1]))
else:
joined_str = joined_str + ' ' + str(int(all_no[i]) + int(all_no[i+1]))
i = i + 2
print(joined_str), 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 (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The 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(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int t;
for(int i=0;i<n;i+=2){
System.out.print(a[i]+a[i+1]+" ");
}
}
}, 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 (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The 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(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i+=2){
cout<<a[i]+a[i+1]<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, 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());
int max = 1000001;
boolean isNotPrime[] = new boolean[max];
ArrayList<Integer> arr = new ArrayList<Integer>();
isNotPrime[0] = true; isNotPrime[1] = true;
for (int i=2; i*i <max; i++) {
if (!isNotPrime[i]) {
for (int j=i*i; j<max; j+= i) {
isNotPrime[j] = true;
}
}
}
for(int i=2; i<max; i++) {
if(!isNotPrime[i]) {
arr.add(i);
}
}
while(t-- > 0) {
String str[] = br.readLine().trim().split(" ");
int l = Integer.parseInt(str[0]);
int r = Integer.parseInt(str[1]);
System.out.println(primeRangeSum(l,r,arr));
}
}
static long primeRangeSum(int l , int r, ArrayList<Integer> arr) {
long sum = 0;
for(int i=l; i<=r;i++) {
sum += arr.get(i-1);
}
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, I have written this Solution Code: def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
pri = []
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
pri.append(p)
return pri
N = int(input())
X = []
prim = SieveOfEratosthenes(1000000)
for i in range(1,len(prim)):
prim[i] = prim[i]+prim[i-1]
for i in range(N):
nnn = input()
X.append((int(nnn.split()[0]),int(nnn.split()[1])))
for xx,yy in X:
if xx==1:
print(prim[yy-1])
else:
print(prim[yy-1]-prim[xx-2])
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
vector<int> v;
v.push_back(0);
for(int i = 2; i < N; i++){
if(a[i]) continue;
v.push_back(i);
for(int j = i*i; j < N; j += i)
a[j] = 1;
}
int p = 0;
for(auto &i: v){
i += p;
p = i;
}
int t; cin >> t;
while(t--){
int l, r;
cin >> l >> r;
cout << v[r] - v[l-1] << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, 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().split(" ");
int n=Integer.parseInt(str[0]);
int t=Integer.parseInt(str[1]);
int[] arr=new int[n];
str=br.readLine().split(" ");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
int sum=0;
for(int i=1;i<n;i++){
int dif=arr[i]-arr[i-1];
if(dif>t){
sum=sum+t;
}else{
sum=sum+dif;
}
}
sum+=t;
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, I have written this Solution Code: n , t = [int(x) for x in input().split() ]
l= [int(x) for x in input().split() ]
c = 0
for i in range(len(l)-1):
if l[i+1] - l[i]<=t:
c+=l[i+1] - l[i]
else:
c+=t
c+=t
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
long n,t;
cin>>n>>t;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long cur=t;
long ans=t;
for(int i=1;i<n;i++){
ans+=min(a[i]-a[i-1],t);
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int count=search(a,0,n-1);
System.out.println(count);
}
}
public static int search(int[] a,int l,int h){
while(l<=h){
int mid=l+(h-l)/2;
if ((mid==h||a[mid+1]==0)&&(a[mid]==1))
return mid+1;
if (a[mid]==1)
l=mid+1;
else h=mid-1;
}
return 0;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = n+1;
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] == 1)
l = m;
else
h = m;
}
cout << l << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: c=int(input())
for x in range(c):
size=int(input())
s=input()
print(s.count('1')), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., 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());
String ans = check(n);
StringBuffer sbr = new StringBuffer(ans);
System.out.println(sbr.reverse());
}
}
static String check(int x)
{
String ans="";
while(x>0)
{
switch(x%4)
{
case 1:ans +="2"; break;
case 2:ans +="3"; break;
case 3:ans+="5"; break;
case 0:ans+="7"; break;
}
if(x%4==0)
x--;
x/=4;
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: def nthprimedigitsnumber(number):
num=""
while (number > 0):
rem = number % 4
if (rem == 1):
num += '2'
if (rem == 2):
num += '3'
if (rem == 3):
num += '5'
if (rem == 0):
num += '7'
if (number % 4 == 0):
number = number - 1
number = number // 4
return num[::-1]
T=int(input())
for i in range(T):
number = int(input())
print(nthprimedigitsnumber(number)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
void nthprimedigitsnumber(long long n)
{
long long len = 1;
long long prev_count = 0;
while (true) {
long long curr_count = prev_count + pow(4, len);
if (prev_count < n && curr_count >= n)
break;
len++;
prev_count = curr_count;
}
for (int i = 1; i <= len; i++) {
for (long long j = 1; j <= 4; j++) {
if (prev_count + pow(4, len - i) < n)
prev_count += pow(4, len - i);
else {
if (j == 1)
cout << "2";
else if (j == 2)
cout << "3";
else if (j == 3)
cout << "5";
else if (j == 4)
cout << "7";
break;
}
}
}
cout << endl;
}
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
nthprimedigitsnumber(n);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Andrew loves to solve problems related to prime numbers. One of Andrew's friend has asked him to solve below problem for him.
Given two positive integers <b>N</b> and <b>M</b>, the task is to check that N is the Mth power of a <b>prime number</b> or not.First line of input contains testcases <b>T</b>. For each testcase, there will be two positive integers N and M.
Constraints :
1 <= T <= 100
2 <= N <= 10^6
1 <= M <= 10For each testcase you need to print "<b>Yes</b>" if N is the Mth power of a prime number otherrwise "<b>No</b>". Input :
2
16 4
16 3
Output:
Yes
No
Explanation :
16 is m-th (4th) power of 2, where 2 is prime., I have written this Solution Code: import math
def mroot(n,m):
check = round(n**(1/m))
# print(check)
if check**m == n:
return check
else:
return -1
def prime(n,m):
check = mroot(n,m)
if check == -1:
return "No"
if check == 2:
return "Yes"
for i in range(2, int(math.sqrt(check))+1):
if n%i == 0:
return "No"
return "Yes"
T = int(input())
for _ in range(T):
n,m = list(map(int, input().split()))
print(prime(n,m)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Andrew loves to solve problems related to prime numbers. One of Andrew's friend has asked him to solve below problem for him.
Given two positive integers <b>N</b> and <b>M</b>, the task is to check that N is the Mth power of a <b>prime number</b> or not.First line of input contains testcases <b>T</b>. For each testcase, there will be two positive integers N and M.
Constraints :
1 <= T <= 100
2 <= N <= 10^6
1 <= M <= 10For each testcase you need to print "<b>Yes</b>" if N is the Mth power of a prime number otherrwise "<b>No</b>". Input :
2
16 4
16 3
Output:
Yes
No
Explanation :
16 is m-th (4th) power of 2, where 2 is prime., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(rd.readLine());
while(t-->0){
String b[]=rd.readLine().split(" ");
int n=Integer.parseInt(b[0]);
int m=Integer.parseInt(b[1]);
if(m==1){
int a=(int)Math.sqrt(n);
int p=1;
for(int k=2;k<=a;k++){
if(n%k==0){
p=0;
break;
}
}
if(p==1)
System.out.println("Yes");
else
System.out.println("No");
}
else{
for(int i=2;i<=n/2;i++){
boolean p=true;
for(int j=2;j<=(int)Math.sqrt(i);j++){
if(i%j==0){
p=false;
break;}
}
if(p){
if((int)Math.pow(i,m)==n){
System.out.println("Yes");
break;}
else if((int)Math.pow(i,m)>n){
System.out.println("No");
break;
}
}
}
}}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Andrew loves to solve problems related to prime numbers. One of Andrew's friend has asked him to solve below problem for him.
Given two positive integers <b>N</b> and <b>M</b>, the task is to check that N is the Mth power of a <b>prime number</b> or not.First line of input contains testcases <b>T</b>. For each testcase, there will be two positive integers N and M.
Constraints :
1 <= T <= 100
2 <= N <= 10^6
1 <= M <= 10For each testcase you need to print "<b>Yes</b>" if N is the Mth power of a prime number otherrwise "<b>No</b>". Input :
2
16 4
16 3
Output:
Yes
No
Explanation :
16 is m-th (4th) power of 2, where 2 is prime., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
const int n = 1000001;
bool a[n];
int main(){
for(int i=0;i<n;i++){
a[i]=false;
}
for(int i=2;i<n;i++){
if(a[i]==false){
for(int j=i+i;j<n;j+=i){
a[j]=true;
}
}
}
int t;
cin>>t;
while(t--){
int x,y;
cin>>x>>y;
if(y>1){
for(int i=2;i<1000;i++){
if(a[i]==false){
int s=1;
for(int j=0;j<y;j++){
s*=i;
}
if(s==x){
cout<<"Yes"<<endl;
goto f;
}
}
}
cout<<"No"<<endl;
f:;
}
else{
if(a[x]==false){cout<<"Yes"<<endl;}
else{cout<<"No"<<endl;}
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader(){
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1){
if (c == '\n')break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)c = read();
do{
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)return -ret;return ret;
}
public long nextLong() throws IOException{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.'){
while ((c = read()) >= '0' && c <= '9'){
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)buffer[0] = -1;
}
private byte read() throws IOException{
if (bufferPointer == bytesRead)fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException{
if (din == null)return;
din.close();
}
}
public static void main (String[] args) throws IOException{
Reader sc = new Reader();
int m = sc.nextInt();
int n = sc.nextInt();
int[][] arr = new int[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
arr[i][j] = sc.nextInt();
}
}
int max_row_index = 0;
int j = n - 1;
for (int i = 0; i < m; i++) {
while (j >= 0 && arr[i][j] == 1) {
j = j - 1;
max_row_index = i;
}
}
System.out.println(max_row_index);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: r, c = list(map(int, input().split()))
max_count = 0
max_r = 0
for i in range(r):
count = input().count("1")
if count > max_count:
max_count = count
max_r = i
print(max_r), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
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 max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int a[max1][max1];
signed main()
{
int n,m;
cin>>n>>m;
FOR(i,n){
FOR(j,m){cin>>a[i][j];}}
int cnt=0;
int ans=0;
int res=0;
FOR(i,n){
cnt=0;
FOR(j,m){
if(a[i][j]==1){
cnt++;
}}
if(cnt>res){
res=cnt;
ans=i;
}
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: // mat is the matrix/ 2d array
// n,m are dimensions
function max1Row(mat, n, m) {
// write code here
// do not console.log
// return the answer as a number
let j, max_row_index = 0;
j = m - 1;
for (let i = 0; i < n; i++)
{
// Move left until a 0 is found
let flag = false;
// to check whether a row has more 1's than previous
while (j >= 0 && mat[i][j] == 1)
{
j = j - 1; // Update the index of leftmost 1
// seen so far
flag = true;//present row has more 1's than previous
}
// if the present row has more 1's than previous
if (flag)
{
max_row_index = i; // Update max_row_index
}
}
if (max_row_index == 0 && mat[0][m - 1] == 0)
return -1;
return max_row_index;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a function <code>reverseString</code>, which takes in a string as a parameter. Your task is to complete the function such that it returns the reverse of the string.(hello changes to olleh)
// Complete the reverseString function
function reverseString(n) {
//Write Code Here
}A string nReturns the reverse of nconst n = 'hello'
reverseString(n) //displays 'olleh' in console, 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 n=sc.nextLine();
char tra[] = n.toCharArray();
for(int i=tra.length-1;i>=0;i--){
System.out.print(tra[i]);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a function <code>reverseString</code>, which takes in a string as a parameter. Your task is to complete the function such that it returns the reverse of the string.(hello changes to olleh)
// Complete the reverseString function
function reverseString(n) {
//Write Code Here
}A string nReturns the reverse of nconst n = 'hello'
reverseString(n) //displays 'olleh' in console, I have written this Solution Code: function reverseString (n) {
return n.split("").reverse().join("");
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code: m,n=map(int ,input().split())
matrix=[]
for i in range(m):
l1=[eval(x) for x in input().split()]
matrix.append(l1)
l2=[]
for coloumn in range(n):
sum1=0
for row in range(m):
sum1+= matrix[row][coloumn]
l2.append(sum1)
print(max(l2))
'''for row in range(n):
sum2=0
for col in range(m):
sum2 += matrix[row][col]
print(sum2)''', In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code: // mat is the matrix/ 2d array
// the dimensions of array are:- a rows, b columns
function colMaxSum(mat,a,b) {
// write code here
// do not console.log
// return the answer as a number
let idx = -1;
// Variable to store max sum
let maxSum = Number.MIN_VALUE;
// Traverse matrix column wise
for (let i = 0; i < b; i++) {
let sum = 0;
// calculate sum of column
for (let j = 0; j < a; j++) {
sum += mat[j][i];
}
// Update maxSum if it is
// less than current sum
if (sum > maxSum) {
maxSum = sum;
// store index
idx = i;
}
}
let res;
res = [idx, maxSum];
// return result
return maxSum;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n,m;
cin>>n>>m;
int a[m];
for(int i=0;i<m;i++){
a[i]=0;
}
int x;
int sum=0;
FOR(i,n){
FOR(j,m){
cin>>x;
a[j]+=x;
sum=max(sum,a[j]);
}
}
out(sum);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, 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 m = sc.nextInt();
int n = sc.nextInt();
int a[][] = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
int sum=0;
int ans=0;
for(int i=0;i<n;i++){
sum=0;
for(int j=0;j<m;j++){
sum+=a[j][i];
}
if(sum>ans){ans=sum;}
}
System.out.print(ans);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.
<b>Note:-</b>
In the case of custom input, you will get 1 if your code is correct else get a 0.<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>threeWayPartition()</b> which contains following arguments.
A: input array list
low: starting integer of range
high: ending integer of range
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10<sup>4</sup>
1 <= A[i] <= 10<sup>5</sup>
1 <= low <= high <= 10<sup>5</sup>
The Sum of N over all test case doesn't exceed 10^5For each test case return the modified array.Sample Input:
2
5
1 8 3 3 4
3 5
3
1 2 3
1 3
Sample Output:
1 3 3 4 8
1 2 3
<b>Explanation:</b>
Testcase 1: First, the array has elements less than or equal to 3. Then, elements between 3 and 5. And, finally elements greater than 5. So, one of the possible outputs is 1 3 3 4 8.
Testcase 2: First, the array has elements less than or equal to 1. Then, elements between 1 and 3. And, finally elements greater than 3. So, the output is 1 2 3., I have written this Solution Code:
public static ArrayList<Integer> threeWayPartition(ArrayList<Integer> A, int lowVal, int highVal)
{
int n = A.size();
ArrayList<Integer> arr = A;
int start = 0, end = n-1;
for (int i=0; i<=end;)
{
// swapping the element with those at start
// if array element is less than lowVal
if (arr.get(i) < lowVal){
int temp=arr.get(i);
arr.add(i,arr.get(start));
arr.remove(i+1);
arr.add(start,temp);
arr.remove(start+1);
i++;
start++;
}
// swapping the element with those at end
// if array element is greater than highVal
else if (arr.get(i) > highVal){
int temp=arr.get(i);
arr.add(i,arr.get(end));
arr.remove(i+1);
arr.add(end,temp);
arr.remove(end+1);
end--;
}
// else just move ahead
else
i++;
}
return arr;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.
<b>Note:-</b>
In the case of custom input, you will get 1 if your code is correct else get a 0.<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>threeWayPartition()</b> which contains following arguments.
A: input array list
low: starting integer of range
high: ending integer of range
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10<sup>4</sup>
1 <= A[i] <= 10<sup>5</sup>
1 <= low <= high <= 10<sup>5</sup>
The Sum of N over all test case doesn't exceed 10^5For each test case return the modified array.Sample Input:
2
5
1 8 3 3 4
3 5
3
1 2 3
1 3
Sample Output:
1 3 3 4 8
1 2 3
<b>Explanation:</b>
Testcase 1: First, the array has elements less than or equal to 3. Then, elements between 3 and 5. And, finally elements greater than 5. So, one of the possible outputs is 1 3 3 4 8.
Testcase 2: First, the array has elements less than or equal to 1. Then, elements between 1 and 3. And, finally elements greater than 3. So, the output is 1 2 3., I have written this Solution Code: def threewayPartition(arr,low,high):
i=low-1
pivot = arr[high]
for j in range(low,high):
if arr[j]<=pivot:
i+=1
arr[i],arr[j]=arr[j],arr[i]
i+=1
arr[i],arr[high]=arr[high],arr[i]
return i
def quickSort(arr,low,high):
if low<high:
key=threewayPartition(arr,low,high)
quickSort(arr,low,key-1)
quickSort(arr,key+1,high)
return arr
t= int(input())
while t>0:
s = int(input())
arr = list(map(int,input().split()))
a,b = list(map(int,input().split()))
print(1)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements and an integer D. Your task is to rotate the array D times in a circular manner from the right to left direction. Consider the examples for better understanding:-
Try to do without creating another arrayUser task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>rotate()</b> that takes the array, size of the array, and the integer d as a parameter.
Constraints:
1 <= T <= 25
2 <= N <= 10^4
1<=D<=10^5
1 <= A[i] <= 10^5For each test case, you just need to rotate the array by D times. The driver code will prin the rotated array in a new line.Sample Input:
2
8
4
1 2 3 4 5 6 7 8
10
3
1 2 3 4 5 6 7 8 9 10
Sample Output:
5 6 7 8 1 2 3 4
4 5 6 7 8 9 10 1 2 3
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
After the first rotation, the array becomes 2 3 4 5 6 7 8 1
After the second rotation, the array becomes 3 4 5 6 7 8 1 2
After the third rotation, the array becomes 4 5 6 7 8 1 2 3
After the fourth rotation, the array becomes 5 6 7 8 1 2 3 4
Hence the final result: 5 6 7 8 1 2 3 4, I have written this Solution Code: public static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static void rotate(int arr[], int n, int d){
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
int temp = arr[i];
int j = i;
boolean win=true;
while (win) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a stack of integers and N queries. Your task is to perform these operations:-
<b>push:-</b>this operation will add an element to your current stack.
<b>pop:-</b>remove the element that is on top
<b>top:-</b>print the element which is currently on top of stack
<b>Note:-</b>if the stack is already empty then the pop operation will do nothing and 0 will be printed as a top element of the stack if it is empty.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push()</b>:- that takes the stack and the integer to be added as a parameter.
<b>pop()</b>:- that takes the stack as parameter.
<b>top()</b> :- that takes the stack as parameter.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>3</sup>You don't need to print anything else other than in <b>top</b> function in which you require to print the topmost element of your stack in a new line, if the stack is empty you just need to print 0.Input:
7
push 1
push 2
top
pop
top
pop
top
Output:
2
1
0
, I have written this Solution Code:
public static void push (Stack < Integer > st, int x)
{
st.push (x);
}
// Function to pop element from stack
public static void pop (Stack < Integer > st)
{
if (st.isEmpty () == false)
{
int x = st.pop ();
}
}
// Function to return top of stack
public static void top(Stack < Integer > st)
{
int x = 0;
if (st.isEmpty () == false)
{
x = st.peek ();
}
System.out.println (x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N and value K. The elements of the array A contains positive integers. You have to print all the elements which are greater than K in the array in sorted order (including K as well if present in the array A), and print all the elements which are smaller than K in sorted order both of them in separate lines. If the elements greater than or equal to K are not present in the array then print "-1". Similarly, in the case of smaller elements print -1 if elements smaller than K doesn’t exist. If a number appears more than once print number more than once.First line of input contains number of testcases T. For each testcase, there are two lines, first of which contains N and K separated by space, next line contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 100000
1 <= K <= 1000000
1 <= A[i] <= 1000000
Sum of N over all test cases do not exceed 100000For each testcase, print the required elements(if any), else print "-1" (without quotes)Input:
1
5 1
2 1 5 7 6
Output:
1 2 5 6 7
-1
Explanation:
Testcase 1 : Since, 1, 2, 5, 6, 7 are greater or equal to given K. Also, no element less than K is present in the array., I have written this Solution Code: import java.io.*;
import java.util.*;
class Reader{
BufferedReader reader;
Reader(){
this.reader = new BufferedReader(new InputStreamReader(System.in));
}
public String read() throws IOException {
return reader.readLine();
}
public int readInt() throws IOException {
return Integer.parseInt(reader.readLine());
}
public long readLong() throws IOException {
return Long.parseLong(reader.readLine());
}
public String[] readArray() throws IOException {
return reader.readLine().split(" ");
}
public int[] readIntegerArray() throws IOException {
String[] str = reader.readLine().split(" ");
int[] arr = new int[str.length];
for(int i=0;i<str.length;i++) arr[i] = Integer.parseInt(str[i]);
return arr;
}
public long[] readLongArray() throws IOException {
String[] str = reader.readLine().split(" ");
long[] arr = new long[str.length];
for(int i=0;i<str.length;i++) arr[i] = Long.parseLong(str[i]);
return arr;
}
}
public class Main {
public static void main(String[] args) throws IOException {
Reader rdr = new Reader();
int t = rdr.readInt();
while(t-- > 0){
int[] str = rdr.readIntegerArray();
int n = str[0];
int k = str[1];
int[] arr = rdr.readIntegerArray();
Arrays.sort(arr);
int c=0;
for(int i=0;i<n;i++){
if(arr[i]>=k){
System.out.print(arr[i]+" ");
c++;
}
}
if(c==0) System.out.print(-1);
System.out.println();
if(c==n) System.out.print(-1);
else for(int i=0;i<n-c;i++) System.out.print(arr[i]+" ");
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N and value K. The elements of the array A contains positive integers. You have to print all the elements which are greater than K in the array in sorted order (including K as well if present in the array A), and print all the elements which are smaller than K in sorted order both of them in separate lines. If the elements greater than or equal to K are not present in the array then print "-1". Similarly, in the case of smaller elements print -1 if elements smaller than K doesn’t exist. If a number appears more than once print number more than once.First line of input contains number of testcases T. For each testcase, there are two lines, first of which contains N and K separated by space, next line contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 100000
1 <= K <= 1000000
1 <= A[i] <= 1000000
Sum of N over all test cases do not exceed 100000For each testcase, print the required elements(if any), else print "-1" (without quotes)Input:
1
5 1
2 1 5 7 6
Output:
1 2 5 6 7
-1
Explanation:
Testcase 1 : Since, 1, 2, 5, 6, 7 are greater or equal to given K. Also, no element less than K is present in the array., I have written this Solution Code: t=int(input())
while t>0:
a=input().split()
b=input().split()
for i in range(len(b)):
b[i]=int(b[i])
lesser=[]
greater=[]
for i in b:
if i>=int(a[1]):
greater.append(i)
else:
lesser.append(i)
if len(greater)==0:
print(-1,end="")
else:
greater.sort()
for x in greater:
print(x,end=" ")
print()
if len(lesser)==0:
print(-1,end="")
else:
lesser.sort()
for y in lesser:
print(y,end=" ")
print()
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N and value K. The elements of the array A contains positive integers. You have to print all the elements which are greater than K in the array in sorted order (including K as well if present in the array A), and print all the elements which are smaller than K in sorted order both of them in separate lines. If the elements greater than or equal to K are not present in the array then print "-1". Similarly, in the case of smaller elements print -1 if elements smaller than K doesn’t exist. If a number appears more than once print number more than once.First line of input contains number of testcases T. For each testcase, there are two lines, first of which contains N and K separated by space, next line contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 100000
1 <= K <= 1000000
1 <= A[i] <= 1000000
Sum of N over all test cases do not exceed 100000For each testcase, print the required elements(if any), else print "-1" (without quotes)Input:
1
5 1
2 1 5 7 6
Output:
1 2 5 6 7
-1
Explanation:
Testcase 1 : Since, 1, 2, 5, 6, 7 are greater or equal to given K. Also, no element less than K is present in the array., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
signed main()
{
int t;
cin>>t;
while(t>0)
{
t--;
int n,k;
cin>>n>>k;
vector<int> A,B;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
if(a<k) B.pu(a);
else A.pu(a);
}
sort(all(A));
sort(all(B));
for(auto it:A)
{
cout<<it<<" ";
}if(A.size()==0) cout<<-1;
cout<<endl;
for(auto it:B)
{
cout<<it<<" ";
}if(B.size()==0) cout<<-1;
cout<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String [] str=br.readLine().trim().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(str[i]);
}
Arrays.sort(a);
int size=a[n-1]+1;
int c[]=new int[size];
for(int i=0;i<size;i++) c[i]=0;
for(int i=0;i<n;i++) c[a[i]]++;
int max=0,freq=c[1];
for(int i=2;i<size;i++){
if(freq<=c[i]){
freq=c[i];
max=i;
}
}
System.out.println(max);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: n = int(input())
a = [int(x) for x in input().split()]
freq = {}
for x in a:
if x not in freq:
freq[x] = 1
else:
freq[x] += 1
mx = max(freq.values())
rf = sorted(freq)
for i in range(len(rf) - 1, -1, -1):
if freq[rf[i]] == mx:
print(rf[i])
break, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int n; cin >> n;
for(int i = 1; i <= n; i++){
int p; cin >> p;
a[p]++;
}
int mx = 0, id = -1;
for(int i = 1; i <= 100; i++){
if(a[i] >= mx)
mx = a[i], id = i;
}
cout << id;
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 the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.print(nonRepeatChar(s));
}
static int nonRepeatChar(String s){
char count[] = new char[256];
for(int i=0; i< s.length(); i++){
count[s.charAt(i)]++;
}
for (int i=0; i<s.length(); i++) {
if (count[s.charAt(i)]==1){
return i;
}
}
return -1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: from collections import defaultdict
s=input()
d=defaultdict(int)
for i in s:
d[i]+=1
ans=-1
for i in range(len(s)):
if(d[s[i]]==1):
ans=i
break
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-10 12:51:16
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
int firstUniqChar(string s) {
map<char, int> charCount;
int len = s.length();
for (int i = 0; i < len; i++) {
charCount[s[i]]++;
}
for (int i = 0; i < len; i++) {
if (charCount[s[i]] == 1)
return i;
}
return -1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string str;
cin>>str;
cout<<firstUniqChar(str)<<"\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted doubly linked list containing n nodes. Your task is to remove duplicate nodes from the given list.
Example 1:
Input
1<->2<->2-<->3<->3<->4
Output:
1<->2<->3<->4
Example 2:
Input
1<->1<->1<->1
Output
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>deleteDuplicates()</b> that takes head node as parameter.
Constraints:
1 <=N <= 10000
1 <= Node. data<= 2*10000Return the head of the modified list.Sample Input:-
6
1 2 2 3 3 4
Sample Output:-
1 2 3 4
Sample Input:-
4
1 1 1 1
Sample Output:-
1, I have written this Solution Code: public static Node deleteDuplicates(Node head)
{
/* if list is empty */
if (head== null)
return head;
Node current = head;
while (current.next != null)
{
/* Compare current node with next node */
if (current.val == current.next.val)
/* delete the node pointed to by
' current->next' */
deleteNode(head, current.next);
/* else simply move to the next node */
else
current = current.next;
}
return head;
}
/* Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
public static void deleteNode(Node head, Node del)
{
/* base case */
if(head==null || del==null)
{
return ;
}
/* If node to be deleted is head node */
if(head==del)
{
head=del.next;
}
/* Change next only if node to be deleted
is NOT the last node */
if(del.next!=null)
{
del.next.prev=del.prev;
}
/* Change prev only if node to be deleted
is NOT the first node */
if (del.prev != null)
del.prev.next = del.next;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>sumMaxMin</code>, which should take 5 numbers as input arguments.
The function should return the sum of max and min elements of those 5 numbers (Use JS In built functions)Function will take 5 arguments which will be numbers.Function will return a number which is the sum of min and max element of those 5 arguments.console. log(sumMaxMin(100, 100, -200, 300, 0)) // prints 100 because 300+(-200) = 300-200
console. log(sumMaxMin(1, 3, 2, 4, 5)) // prints 6 because 1+5
console. log(sumMaxMin(-1000, -2000, -10, -120, -60)) // prints -2010 because -2000 min and -10 max sums to -2010, 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[] arr1=new int[5];
for(int i=0;i< arr1.length;i++)
arr1[i]=sc.nextInt();
Main m=new Main();
System.out.println(m.sumMaxMin(arr1));
}
public static int sumMaxMin(int []array){
int max =array[0];
int min=array[0];
int sum1=0;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
sum1= max +(min);
return sum1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>sumMaxMin</code>, which should take 5 numbers as input arguments.
The function should return the sum of max and min elements of those 5 numbers (Use JS In built functions)Function will take 5 arguments which will be numbers.Function will return a number which is the sum of min and max element of those 5 arguments.console. log(sumMaxMin(100, 100, -200, 300, 0)) // prints 100 because 300+(-200) = 300-200
console. log(sumMaxMin(1, 3, 2, 4, 5)) // prints 6 because 1+5
console. log(sumMaxMin(-1000, -2000, -10, -120, -60)) // prints -2010 because -2000 min and -10 max sums to -2010, I have written this Solution Code:
function sumMaxMin(a,b,c,d,e){
// write code here
// return the output , do not use console.log here
return Math.max(a,b,c,d,e) + Math.min(a,b,c,d,e)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function <code>solve</code> which takes <code>obj</code> as a parameter which is an <code>object</code>.
Iterate <code>obj</code> using a <code>for of</code> and a <code>for in</code> loop. The <code>for...of</code> loop should print all the <code>values</code> of the <code>obj</code> in the console and the <code>for...in</code> loop should print the <code>key</code> and <code>values</code> of <code>obj</code> in the format <code>{key}: {value}</code>.
See the example for more clarity.
Note: Generate Expected Output section will not work for this questionInput will have the object which is passed as a parameter to the function solve
You need not worry about the same, it is handled properly by the hidden pre-function code.
Example:
{"name": "John","age": "32","location": "New York"}The <code>for of</code> loop should print all the <code>values</code> of the <code>obj</code> object in the console
The <code>for in</code> loop should print the <code>key</code> and <code>values</code> of <code>obj</code> object in the format <code>{key}: {value}</code>const obj = {"name": "John","age": "32","location": "New York"}
solve(obj)
/*
The console output should be
John
32
New York
name: John
age: 32
location: New York
*/, I have written this Solution Code: function solve(obj){
for (const value of Object.values(obj)) {
console.log(value);
}
for (const key in obj) {
console.log(`${key}: ${obj[key]}`);
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N, find the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai and aj (where i != j ) is |ai - aj|.First line of input contains a single integer N, next line contains N space separated integers containing values of array A.
Constraints :
2 < = N < = 10^5
0 < = A[i] < = 10^10Print the minimum absolute difference between any two elements of the array.Sample Input :
5
2 9 0 4 5
Sample Input :
1
Sample Input:-
3
1 2 1
Sample Output:-
0, I have written this Solution Code: import math
import os
import random
import re
import sys
def minimumAbsoluteDifference(arr):
diff_arr = []
sort_arr = sorted(arr)
for i in range(0, len(sort_arr)-1):
diff_arr.append(abs(sort_arr[i] - sort_arr[i+1]))
return min(diff_arr)
if __name__ == '__main__':
n=int(input())
n=input().split()
for i in range(len(n)):
n[i]=int(n[i])
print(minimumAbsoluteDifference(n)%(10**9+7)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N, find the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai and aj (where i != j ) is |ai - aj|.First line of input contains a single integer N, next line contains N space separated integers containing values of array A.
Constraints :
2 < = N < = 10^5
0 < = A[i] < = 10^10Print the minimum absolute difference between any two elements of the array.Sample Input :
5
2 9 0 4 5
Sample Input :
1
Sample Input:-
3
1 2 1
Sample Output:-
0, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int arrSize = sc.nextInt();
long arr[]= new long[arrSize];
for(int i = 0; i < arrSize; i++)
arr[i] = sc.nextLong();
System.out.println(minABSDiff(arr, arrSize));
}
static long minABSDiff(long arr[], int arrSize)
{
Arrays.sort(arr);
long ans=Long.MAX_VALUE;
for(int i = 1; i < arrSize; i++){
ans = Math.min(ans,arr[i]-arr[i-1]);}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N, find the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai and aj (where i != j ) is |ai - aj|.First line of input contains a single integer N, next line contains N space separated integers containing values of array A.
Constraints :
2 < = N < = 10^5
0 < = A[i] < = 10^10Print the minimum absolute difference between any two elements of the array.Sample Input :
5
2 9 0 4 5
Sample Input :
1
Sample Input:-
3
1 2 1
Sample Output:-
0, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
const int N = 3e5+5;
#define read(type) readInt<type>()
#define max1 20000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long int
signed main()
{
int n;
cin>>n;
int a[n];
FOR(i,n){
cin>>a[i];}
sort(a,a+n);
int ans=LONG_MAX;
FOR1(i,1,n){
ans=min(ans,a[i]-a[i-1]);}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an unsorted array of length n and must sort it using merge sort while also printing the amount of merges that occur throughout the sorting process.The first line of input will be n, which represents the array's length, followed by the n array items in the second line.
Constraints:
0<= n <=100000First- line should be the sorted array and the second should be the number of mergers that occurs when the array is sorted using merge sort.Sample Input:
5
5 1 2 7 3
Output:
1 2 3 5 7
4, I have written this Solution Code: def mergeSort(arr,count):
if len(arr)>1:
mid= len(arr)//2
a=arr[:mid]
b=arr[mid:]
count=mergeSort(a,count)
count=mergeSort(b,count)
count+=1
i = j = k = 0
l1 = len(a)
l2 = len(b)
while i< l1 and j <l2:
if a[i]<b[j]:
arr[k]=a[i]
i +=1
else:
arr[k]=b[j]
j+=1
k+=1
while i <l1:
arr[k]=a[i]
i+=1
k+=1
while j<l2:
arr[k]=b[j]
j+=1
k+=1
return count
N=int(input())
arr=list(map(int,input().split()))
count=mergeSort(arr,0)
print(' '.join(map(str,arr)))
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an unsorted array of length n and must sort it using merge sort while also printing the amount of merges that occur throughout the sorting process.The first line of input will be n, which represents the array's length, followed by the n array items in the second line.
Constraints:
0<= n <=100000First- line should be the sorted array and the second should be the number of mergers that occurs when the array is sorted using merge sort.Sample Input:
5
5 1 2 7 3
Output:
1 2 3 5 7
4, I have written this Solution Code: import java.util.Scanner;
public class Main
{
int noOfMerge=0;
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0,k=l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
noOfMerge+=1;
}
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] array=new int[n];
for(int i=0;i<n;i++)
array[i]=scanner.nextInt();
Main ob = new Main();
ob.sort(array, 0, n-1);
for (int i=0; i<n; ++i)
System.out.print(array[i] + " ");
System.out.println();
System.out.println(ob.noOfMerge);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code: public static long SumOfDivisors(long N){
long sum=0;
long c=(long)Math.sqrt(N);
for(long i=1;i<=c;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){sum+=N/i;}
}
}
return sum;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
def SumOfDivisors(num) :
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
i = 1
while i<= (math.sqrt(num)) :
# if 'i' is divisor of 'num'
if (num % i == 0) :
# if both divisors are same then
# add it only once else add both
if (i == (num / i)) :
result = result + i;
else :
result = result + (i + num/i);
i = i + 1
# Add 1 to the result as 1 is also
# a divisor
return (result); , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a reference to the head of a sorted doubly-linked list and an integer k, your task is to insert the integer k in your doubly linked-list while maintaining the sort.<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>insertnew()</b> that takes head node of the linked list and the integer k as parameter.
Constraints:
1 <= T <= 100
1 <= Node.data <= 1000Return the head of the modified linked list.Sample Input
1
4
1
3
4
10
5
Sample Output
1 3 4 5 10, I have written this Solution Code: public static Node insertnew(Node head,int k) {
if(head == null){
Node temp = new Node(k);
return temp;
}
Node temp=head;
while(temp != null){
if (temp.val >= k){
Node x=new Node(k);
x.prev = temp.prev;
x.next = temp;
temp.prev = x;
if (x.prev == null){
return x;
}
else {
x.prev.next =x;
return head;
}
}
if (temp.next == null){
Node x=new Node(k);
x.prev = temp;
x.next = null;
temp.next = x;
break;
}
temp = temp.next;
}
return head;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers containing only 0 or 1. You can do the following operations on the array:
<ul><li>swap elements at two indices</li><li>choose one index and change its value from 0 to 1 or vice-versa.</li></ul>
You have to do the minimum number of the above operations such that the final array is non-decreasing.
<b>Note</b> Consider 1 based Array-indexingThe first line of input contains a single integer N.
The second line of input contains N space-separated integers denoting the array.
Constraints:
1 ≤ N ≤ 100000
elements of the array are 0 or 1.Minimum number of moves required such that the final array is non- decreasing.Sample Input 1
5
1 1 0 0 1
Sample Output 1
2
Explanation:
Swap indices (1, 3)
Swap indices (2, 4)
Sample Input 2
5
0 0 1 1 1
Sample Output 2
0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String array[] = br.readLine().trim().split(" ");
boolean decreasingOrder = false;
int[] arr = new int[n];
int totalZeroCount = 0,
totalOneCount = 0;
for(int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(array[i]);
if(i != 0 && arr[i] < arr[i - 1])
decreasingOrder = true;
if(arr[i] % 2 == 0)
++totalZeroCount;
else
++totalOneCount;
}
if(!decreasingOrder) {
System.out.println("0");
} else {
int oneCount = 0;
for(int i = 0; i < totalZeroCount; i++) {
if(arr[i] == 1)
++oneCount;
}
System.out.println(oneCount);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers containing only 0 or 1. You can do the following operations on the array:
<ul><li>swap elements at two indices</li><li>choose one index and change its value from 0 to 1 or vice-versa.</li></ul>
You have to do the minimum number of the above operations such that the final array is non-decreasing.
<b>Note</b> Consider 1 based Array-indexingThe first line of input contains a single integer N.
The second line of input contains N space-separated integers denoting the array.
Constraints:
1 ≤ N ≤ 100000
elements of the array are 0 or 1.Minimum number of moves required such that the final array is non- decreasing.Sample Input 1
5
1 1 0 0 1
Sample Output 1
2
Explanation:
Swap indices (1, 3)
Swap indices (2, 4)
Sample Input 2
5
0 0 1 1 1
Sample Output 2
0, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
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];
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i]==0) cnt++;
}
int ans = 0;
for (int i = 0; i < cnt; i++) if (a[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 an array of N integers containing only 0 or 1. You can do the following operations on the array:
<ul><li>swap elements at two indices</li><li>choose one index and change its value from 0 to 1 or vice-versa.</li></ul>
You have to do the minimum number of the above operations such that the final array is non-decreasing.
<b>Note</b> Consider 1 based Array-indexingThe first line of input contains a single integer N.
The second line of input contains N space-separated integers denoting the array.
Constraints:
1 ≤ N ≤ 100000
elements of the array are 0 or 1.Minimum number of moves required such that the final array is non- decreasing.Sample Input 1
5
1 1 0 0 1
Sample Output 1
2
Explanation:
Swap indices (1, 3)
Swap indices (2, 4)
Sample Input 2
5
0 0 1 1 1
Sample Output 2
0, I have written this Solution Code: n=int(input())
l=list(map(int,input().split()))
x=l.count(0)
c=0
for i in range(0,x):
if(l[i]==1):
c+=1
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long minCost(long arr[], int n)
{
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) pq.add(arr[i]);
Long cost = new Long("0");
while (pq.size() != 1)
{
long x = pq.poll();
long y = pq.poll();
cost += (x + y);
pq.add(x + y);
}
arr = null;
System.gc();
return cost;
}
public static void main (String[] args) {
FastReader sc = new FastReader();
OutputStream outputstream = System.out;
PrintWriter out = new PrintWriter(outputstream);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextLong();
System.out.println(minCost(arr, arr.length));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: import heapq
from heapq import heappush, heappop
def findMinCost(prices):
heapq.heapify(prices)
cost = 0
while len(prices) > 1:
x = heappop(prices)
y = heappop(prices)
total = x + y
heappush(prices, total)
cost += total
return cost
t=int(input())
for _ in range(t):
n=int(input())
prices=list(map(int,input().split()))
print( findMinCost(prices)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
priority_queue<long long> pq;
int n;
cin>>n;
long long x;
long long sum=0;
for(int i=0;i<n;i++){
cin>>x;
x=-x;
pq.push(x);
}
long long y;
while(pq.size()!=1){
x=pq.top();
pq.pop();
y=pq.top();
pq.pop();
sum+=x+y;
pq.push(x+y);
}
cout<<-sum<<endl;
}}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: static void Icecreams (int N, int D){
int x=N;
while(D-->0){
x-=x/2;
x*=3;
}
System.out.println(x);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
cout << x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
printf("%d", x);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: def Icecreams(N,D):
ans = N
while D > 0:
ans = ans - ans//2
ans = ans*3
D = D-1
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a 2d matrix of size M*N, print the zig traversal of the matrix as shown:-
Consider a matrix of size 5*4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
ZigZag traversal:-
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20First line of input contains two integers M and N. Next M lines contains N space- separated integers each.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[i][j] <= 100000Print the zig- zag traversal of the matrix as shown.Sample Input:-
4 3
1 2 3
4 5 6
7 8 9
10 11 12
Sample Output:-
1
4 2
7 5 3
10 8 6
11 9
12, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args)throws Exception {
Reader sc=new Reader();
int m =sc.nextInt();
int n=sc.nextInt();
int [][]M=new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
M[i][j]=sc.nextInt();
}
}
int i,j;
for(int k=0;k<m-1;k++){
i=k;
j=0;
while(i>=0 && j < n){
System.out.print(M[i][j]+" ");
i=i-1;
j=j+1;
}
System.out.println("");
}
for(int k=0;k<n;k++){
i=m-1;
j=k;
while(j<=n-1 && i >= 0){
System.out.print(M[i][j]+" ");
i=i-1;
j=j+1;
}
System.out.println("");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a 2d matrix of size M*N, print the zig traversal of the matrix as shown:-
Consider a matrix of size 5*4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
ZigZag traversal:-
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20First line of input contains two integers M and N. Next M lines contains N space- separated integers each.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[i][j] <= 100000Print the zig- zag traversal of the matrix as shown.Sample Input:-
4 3
1 2 3
4 5 6
7 8 9
10 11 12
Sample Output:-
1
4 2
7 5 3
10 8 6
11 9
12, I have written this Solution Code: M, N = [int(x) for x in input().split()]
mat = []
for i in range(M):
single_row = list(map(int, input().split()))
mat.append(single_row)
def diagonalOrder(arr, n, m):
ans = [[] for i in range(n + m - 1)]
for i in range(m):
for j in range(n):
ans[i + j].append(arr[j][i])
for i in range(len(ans)):
for j in range(len(ans[i])):
print(ans[i][j], end = " ")
print()
diagonalOrder(mat, M, N), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a 2d matrix of size M*N, print the zig traversal of the matrix as shown:-
Consider a matrix of size 5*4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
ZigZag traversal:-
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20First line of input contains two integers M and N. Next M lines contains N space- separated integers each.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[i][j] <= 100000Print the zig- zag traversal of the matrix as shown.Sample Input:-
4 3
1 2 3
4 5 6
7 8 9
10 11 12
Sample Output:-
1
4 2
7 5 3
10 8 6
11 9
12, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int a[max1][max1];
signed main(){
int m,n;
cin>>m>>n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
for(int i=0;i<m;i++){
for(int j=i;j>=0;j--){
if((i-j)>=n){break;}
cout<<a[j][i-j]<<" ";
}
cout<<endl;
}
for(int i=1;i<n;i++){
for(int j=m-1;j>=0;j--){
if((i+m-1-j)>=n){break;}
cout<<a[j][i+(m-1-j)]<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a chessboard of size N x N, where the top left square is black. Each square contains a value. Find the sum of the values of all black squares and all white squares.
Remember that in a chessboard black and white squares are alternate.The first line of input will be the N size of the matrix. Then next N lines will consist of elements of the matrix. Each row will contain N elements since it is a square matrix.
<b>Constraints:-</b>
1 ≤ N ≤ 800
1 ≤ Matrix[i][j] ≤ 100000
Print two lines, the first line containing the sum of black squares and the second line containing the sum of white squares.Input 1:
3
1 2 3
4 5 6
7 8 9
Output 1:
25
20
Sample Input 2:
4
1 2 3 4
6 8 9 10
11 12 13 14
15 16 17 18
Sample Output 2:
80
79
<b>Explanation 1</b>
The black square contains 1, 3, 5, 7, 9; sum = 25
The white square contains 2, 4, 6, 8; sum = 20, I have written this Solution Code: n=int(input())
bSum=0
wSum=0
for i in range(n):
c=(input().split())
for j in range(len(c)):
if(i%2==0):
if(j%2==0):
bSum+=int(c[j])
else:
wSum+=int(c[j])
else:
if(j%2==0):
wSum+=int(c[j])
else:
bSum+=int(c[j])
print(bSum)
print(wSum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a chessboard of size N x N, where the top left square is black. Each square contains a value. Find the sum of the values of all black squares and all white squares.
Remember that in a chessboard black and white squares are alternate.The first line of input will be the N size of the matrix. Then next N lines will consist of elements of the matrix. Each row will contain N elements since it is a square matrix.
<b>Constraints:-</b>
1 ≤ N ≤ 800
1 ≤ Matrix[i][j] ≤ 100000
Print two lines, the first line containing the sum of black squares and the second line containing the sum of white squares.Input 1:
3
1 2 3
4 5 6
7 8 9
Output 1:
25
20
Sample Input 2:
4
1 2 3 4
6 8 9 10
11 12 13 14
15 16 17 18
Sample Output 2:
80
79
<b>Explanation 1</b>
The black square contains 1, 3, 5, 7, 9; sum = 25
The white square contains 2, 4, 6, 8; sum = 20, I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws IOException {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int mat[][] = new int[N][N];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
mat[i][j] = sc.nextInt();
}
alternate_Matrix_Sum(mat,N);
}
static void alternate_Matrix_Sum(int mat[][], int N)
{
long sum =0, sum1 = 0;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if((i+j)%2 == 0)
sum += mat[i][j];
else sum1 += mat[i][j];
}
}
System.out.println(sum);
System.out.print(sum1);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array (a binary array is an array consisting only of zeroes and ones) of size N. You can do following operation on the array any number of times. In an operation you can select any subarray of size 3 of the array such that all three elements of that subarray are not same and replace that subarray with a single element which is the element with frequency 2 in that subarray. For example if we are given array [0, 1, 0, 1, 1, 0] if we select subarray (0-2) then we will replace [0, 1, 0] with [0] so the array finally becomes [0, 1, 1, 0]. You have to find if it is possible to apply this operation some number of times to finally reduce the array to size 1.First line of input contains a single integer N, size of the array.
Second line contains N integers denoting the binary array.
Constraints:
3 <= N <= 100000
0 <= Arr[i] <= 1Print 1 if it is possible to finally reduce the array to size 1 else print 0.Sample Input 1
5
0 1 0 1 0
Sample Output 1
1
Explanation:
First we select subarray (0-2) array gets changed to 0 1 0
Second we select subarray (0-2) array gets changed to 0
As the size became 1 therefore it is possible.
Sample Input 2
5
1 1 1 1 0
Sample Output 2
0
Explanation -
First we select subarray (2-4) array gets changed to 1 1 1 1
Now we cannot do the operation anymore., I have written this Solution Code: a=int(input())
b=input().split(" ")
p=0
if b[0]=="1" and b[1]=="1" and b[2]=="1" and b[3]=="1" and b[4]=="1" and b[5]=="1":
print(1)
p=1
exit(0)
bb=""
for i in b:
bb=bb+i
b=bb
while len(b)!=1 and p==0:
if "101" in b:
b=b.replace("101","1")
elif "110" in b:
b=b.replace("110","1")
elif "011" in b:
b=b.replace("011","1")
elif "010" in b:
b=b.replace("010","0")
elif "001" in b:
b=b.replace("001","0")
elif "100" in b:
b=b.replace("100","0")
else:
break
if len(b)==1 and p==0:
print("1")
elif p==0 and len(b)!=1:
print("0"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array (a binary array is an array consisting only of zeroes and ones) of size N. You can do following operation on the array any number of times. In an operation you can select any subarray of size 3 of the array such that all three elements of that subarray are not same and replace that subarray with a single element which is the element with frequency 2 in that subarray. For example if we are given array [0, 1, 0, 1, 1, 0] if we select subarray (0-2) then we will replace [0, 1, 0] with [0] so the array finally becomes [0, 1, 1, 0]. You have to find if it is possible to apply this operation some number of times to finally reduce the array to size 1.First line of input contains a single integer N, size of the array.
Second line contains N integers denoting the binary array.
Constraints:
3 <= N <= 100000
0 <= Arr[i] <= 1Print 1 if it is possible to finally reduce the array to size 1 else print 0.Sample Input 1
5
0 1 0 1 0
Sample Output 1
1
Explanation:
First we select subarray (0-2) array gets changed to 0 1 0
Second we select subarray (0-2) array gets changed to 0
As the size became 1 therefore it is possible.
Sample Input 2
5
1 1 1 1 0
Sample Output 2
0
Explanation -
First we select subarray (2-4) array gets changed to 1 1 1 1
Now we cannot do the operation anymore., I have written this Solution Code: #pragma GCC optimize ("O3")
#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
int n;
cin>>n;
int v=0;
for(int i=0;i<n;++i){
int d;
cin>>d;
if(d==0)
++v;
else
--v;
}
if(abs(v)==1){
cout<<1;
}
else{
cout<<0;
}
#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 binary array (a binary array is an array consisting only of zeroes and ones) of size N. You can do following operation on the array any number of times. In an operation you can select any subarray of size 3 of the array such that all three elements of that subarray are not same and replace that subarray with a single element which is the element with frequency 2 in that subarray. For example if we are given array [0, 1, 0, 1, 1, 0] if we select subarray (0-2) then we will replace [0, 1, 0] with [0] so the array finally becomes [0, 1, 1, 0]. You have to find if it is possible to apply this operation some number of times to finally reduce the array to size 1.First line of input contains a single integer N, size of the array.
Second line contains N integers denoting the binary array.
Constraints:
3 <= N <= 100000
0 <= Arr[i] <= 1Print 1 if it is possible to finally reduce the array to size 1 else print 0.Sample Input 1
5
0 1 0 1 0
Sample Output 1
1
Explanation:
First we select subarray (0-2) array gets changed to 0 1 0
Second we select subarray (0-2) array gets changed to 0
As the size became 1 therefore it is possible.
Sample Input 2
5
1 1 1 1 0
Sample Output 2
0
Explanation -
First we select subarray (2-4) array gets changed to 1 1 1 1
Now we cannot do the operation anymore., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(br.readLine());
int zero = 0,one = 0;
String[] s = br.readLine().trim().split(" ");
for(int i=0;i<n;i++){
int x = Integer.parseInt(s[i]);
if(x%2==0)
zero++;
else one++;
}
if(zero-one==1 || zero-one==-1 )
System.out.print(1);
else
System.out.print(0);
}
}, 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> of <b>N</b> integers. Your teacher has asked you to find the maximum quotient of the array. Quotient of an array A of size N can be calculated in the following way:
1) Let the sum of the N elements of array A be <b>X</b>.
2) Let pre[] be another array of size N in which for every index i (1 <= i <= N), <b>pre<sub>i</sub></b> gives us the sum of elements <b>A<sub>1</sub> + A<sub>2</sub> +. . + A<sub>i</sub></b>.
3) Quotient of the array A will be equal to the <b>summation of the floored value of X/pre<sub>i</sub> over every value of i from 1 to N</b>.
You can swap any two elements of the array A. FInd the <b>maximum</b> possible quotient of array A.First line of the input contains a single integer N.
The second line contains N space seperated integers A<sub>i</sub>
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>8</sup>Print the maximum possible quotient of array A.Sample Input:
3
1 1 3
Sample Output:
8
Explaination:
Array pre[] = [1, 2, 5]
Sum X of array A = 5
Quotient of array A = 5/1 + 5/2 + 5/5 = 5 + 2 + 1 = 8
It can be proved that their is no greater quotient of array A possible., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
long sum= 0;
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
sum += arr[i];
}
Arrays.sort(arr);
long currSum = 0;
long ans = 0;
for(int i=0; i<n; i++){
currSum += arr[i];
ans += sum/currSum;
}
System.out.println(ans);
}
}, 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> of <b>N</b> integers. Your teacher has asked you to find the maximum quotient of the array. Quotient of an array A of size N can be calculated in the following way:
1) Let the sum of the N elements of array A be <b>X</b>.
2) Let pre[] be another array of size N in which for every index i (1 <= i <= N), <b>pre<sub>i</sub></b> gives us the sum of elements <b>A<sub>1</sub> + A<sub>2</sub> +. . + A<sub>i</sub></b>.
3) Quotient of the array A will be equal to the <b>summation of the floored value of X/pre<sub>i</sub> over every value of i from 1 to N</b>.
You can swap any two elements of the array A. FInd the <b>maximum</b> possible quotient of array A.First line of the input contains a single integer N.
The second line contains N space seperated integers A<sub>i</sub>
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>8</sup>Print the maximum possible quotient of array A.Sample Input:
3
1 1 3
Sample Output:
8
Explaination:
Array pre[] = [1, 2, 5]
Sum X of array A = 5
Quotient of array A = 5/1 + 5/2 + 5/5 = 5 + 2 + 1 = 8
It can be proved that their is no greater quotient of array A possible., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 1e9;
void solve(){
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for(auto &i : a) cin >> i, sum += i;
int ans = 0;
int cur = 0;
sort(all(a));
for(int i = 0; i < n; i++){
cur += a[i];
ans += sum/cur;
}
cout << ans;
}
signed main(){
fast
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
solve();
if(i != t) cout << endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer N of at least 100. Print the last two digits of N. Strictly speaking, print the tens and one's digits of N in this order.The input consists of an integer.
N
<b>Constraints</b>
100≤N≤999
N is an integer.Print the answer.<b>Sample Input 1</b>
254
<b>Sample Output 1</b>
54
<b>Sample Input 2</b>
101
<b>Sample Output 2</b>
01, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
cout<<(n%100)/10<<n%10<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code: public static long SumOfDivisors(long N){
long sum=0;
long c=(long)Math.sqrt(N);
for(long i=1;i<=c;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){sum+=N/i;}
}
}
return sum;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<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>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
def SumOfDivisors(num) :
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
i = 1
while i<= (math.sqrt(num)) :
# if 'i' is divisor of 'num'
if (num % i == 0) :
# if both divisors are same then
# add it only once else add both
if (i == (num / i)) :
result = result + i;
else :
result = result + (i + num/i);
i = i + 1
# Add 1 to the result as 1 is also
# a divisor
return (result); , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For an integer N, your task is to calculate sum of first N natural numbers.<b>User Task:</b>
Since this will be a functional problem, you don't have to worry about input. You just have to complete the function <b>sum()</b> which takes the integer N as a parameter.
Constraints:
1 <= N < = 100000000Print the sum of first N natural numbers.Sample Input:-
5
Sample Output:-
15
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code:
static void sum(int N){
long x=N;
x=x*(x+1);
x=x/2;
System.out.print(x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code: m,n=map(int ,input().split())
matrix=[]
for i in range(m):
l1=[eval(x) for x in input().split()]
matrix.append(l1)
l2=[]
for coloumn in range(n):
sum1=0
for row in range(m):
sum1+= matrix[row][coloumn]
l2.append(sum1)
print(max(l2))
'''for row in range(n):
sum2=0
for col in range(m):
sum2 += matrix[row][col]
print(sum2)''', In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code: // mat is the matrix/ 2d array
// the dimensions of array are:- a rows, b columns
function colMaxSum(mat,a,b) {
// write code here
// do not console.log
// return the answer as a number
let idx = -1;
// Variable to store max sum
let maxSum = Number.MIN_VALUE;
// Traverse matrix column wise
for (let i = 0; i < b; i++) {
let sum = 0;
// calculate sum of column
for (let j = 0; j < a; j++) {
sum += mat[j][i];
}
// Update maxSum if it is
// less than current sum
if (sum > maxSum) {
maxSum = sum;
// store index
idx = i;
}
}
let res;
res = [idx, maxSum];
// return result
return maxSum;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.