Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given two sorted linked list of size s1 and s2(sizes may or may not be same), your task is to merge them such that resultant list is also sorted.<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>Merge()</b> that takes the head node of both the linked list as the parameter.
Use <b>insert()</b> function for inserting nodes in the linked list.
<b>Constraints:</b>
1 < = s1, s2 < = 1000
1 < = val < = 10000Return the head of the merged linked list, printing will be done by the driver codeSample Input:
5 6
1 2 3 4 5
3 4 6 8 9 10
Sample Output:
1 2 3 3 4 4 5 6 8 9 10, I have written this Solution Code: public static Node Merge (Node head1, Node head2){
Node head =null;
while(head1!=null && head2!=null){
if(head1.val<head2.val){
head=insert(head,head1.val);
head1=head1.next;
}
else{
head=insert(head,head2.val);
head2=head2.next;
}
}
while(head1!=null){
head=insert(head,head1.val);
head1=head1.next;
}
while(head2!=null){
head=insert(head,head2.val);
head2=head2.next;
}
return head;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., 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 n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(minValue(arr, n, k));
}
static int minValue(int arr[], int N, int k)
{
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(arr, m, N) <= k)
h = m;
else
l = m;
}
return h;
}
static int f(int a[], int x, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum += Math.max(a[i]-x, 0);
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: def minCut(arr,k):
mini = 1
maxi = k
mid = 0
while mini<=maxi:
mid = mini + int((maxi - mini)/2)
wood = 0
for j in range(n):
if(arr[j]-mid>=0):
wood += arr[j] - mid
if wood == k:
break;
elif wood > k:
mini = mid+1
else:
maxi = mid-1
print(mini)
n,k = list(map(int, input().split()))
arr = list(map(int, input().split()))
minCut(arr,k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], n, k;
int f(int x){
int sum = 0;
for(int i = 1; i <= n; i++)
sum += max(a[i]-x, 0);
return sum;
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(m) <= k)
h = m;
else
l = m;
}
cout << h;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
testcases();
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the length, breadth, and height of a cuboid. Your task is to calculate its Perimeter.
Note:- Formula for the perimeter of the cuboid is 4(Length + Breadth + height)<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>Perimeter()</b> that takes integers L, B, and H as parameters.
Constraints:-
1 <= L, B, H <= 100Return the length of the Cuboid.Sample Input:-
L = 3, B = 5, H = 1
Sample Output:-
36
Sample Input:-
L = 1, B = 1, H = 1
Sample Output:-
12, I have written this Solution Code:
L,B,H=input().split()
a=4*(int(L)+int(B)+int(H))
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the length, breadth, and height of a cuboid. Your task is to calculate its Perimeter.
Note:- Formula for the perimeter of the cuboid is 4(Length + Breadth + height)<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>Perimeter()</b> that takes integers L, B, and H as parameters.
Constraints:-
1 <= L, B, H <= 100Return the length of the Cuboid.Sample Input:-
L = 3, B = 5, H = 1
Sample Output:-
36
Sample Input:-
L = 1, B = 1, H = 1
Sample Output:-
12, I have written this Solution Code: static int Perimeter(int L, int B, int H){
return 4*(L+B+H);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Create a new class VipCustomer
it should have 3 fields name, creditLimit(double), and email address, there default value is as {name:"XYZ", creditLimit:"10", email:"xyz@abc. com"} respectively means ordering of parameter should be same.
E.g constructor(name,creditLimit,email);
create 3 constructor
1st constructor empty should call the constructor with 3 parameters with default values
2nd constructor should pass on the 2 values it receives as name and creditLimit respectively and, add a default value for the 3rd
3rd constructor should save all fields.
create getters only for this name getName, getCreditLimit and getEmail and confirm it works.
Note: each methods and variables should of public typeYou don't have to take any input, You only have to write class <b>VipCustomer</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
class VipCustomer{
// if your code works fine for the tester
}
Sample Output:
Correct, I have written this Solution Code:
class VipCustomer{
public double creditLimit;
public String name;
public String email;
VipCustomer(){
this("XYZ",10.0,"[email protected]");
}
VipCustomer(String name,double creditLimit){
this(name,creditLimit,"[email protected]");
}
VipCustomer(String _name,double _creditLimit,String _email){
email=_email;
name=_name;
creditLimit=_creditLimit;
}
public String getName(){
return this.name;
}
public String getEmail(){
return this.email;
}
public double getCreditLimit(){
return this.creditLimit;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str = br.readLine().split(" ");
String ref = str[0];
for(String s: str){
if(s.length()<ref.length()){
ref = s;
}
}
for(int i=0; i<n; i++){
if(str[i].contains(ref) == false){
ref = ref.substring(0, ref.length()-1);
}
}
if(ref.length()>0)
System.out.println(ref);
else
System.out.println(-1);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: def longestPrefix( strings ):
size = len(strings)
if (size == 0):
return -1
if (size == 1):
return strings[0]
strings.sort()
end = min(len(strings[0]), len(strings[size - 1]))
i = 0
while (i < end and
strings[0][i] == strings[size - 1][i]):
i += 1
pre = strings[0][0: i]
if len(pre) > 1:
return pre
else:
return -1
N=int(input())
strings=input().split()
print( longestPrefix(strings) ), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: function commonPrefixUtil(str1,str2)
{
let result = "";
let n1 = str1.length, n2 = str2.length;
// Compare str1 and str2
for (let i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
if (str1[i] != str2[j]) {
break;
}
result += str1[i];
}
return (result);
}
// n is number of individual space seperated strings inside strings variable,
// strings is the string which contains space seperated words.
function longestCommonPrefix(strings,n){
// write code here
// do not console,log answer
// return the answer as string
if(n===1) return strings;
const arr= strings.split(" ")
let prefix = arr[0];
for (let i = 1; i <= n - 1; i++) {
prefix = commonPrefixUtil(prefix, arr[i]);
}
if(!prefix) return -1;
return (prefix);
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
string a[n];
int x=INT_MAX;
for(int i=0;i<n;i++){
cin>>a[i];
x=min(x,(int)a[i].length());
}
string ans="";
for(int i=0;i<x;i++){
for(int j=1;j<n;j++){
if(a[j][i]==a[0][i]){continue;}
goto f;
}
ans+=a[0][i];
}
f:;
if(ans==""){cout<<-1;return 0;}
cout<<(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S you have to remove all pairs of identical letters, which are consecutive. If after the removal there appear new pairs, remove them as well. Repeat this untill no adjacent letter in the string is same.
Note that deleting of the consecutive identical letters can be done in any order, as any order leads to the same result.The input data consists of a single string S.
Constraints:
1 <= |S| <= 100000
S contains lowercase english letters only.Print the given string after it is processed. It is guaranteed that the result will contain at least one character.Sample Input
hhoowaaaareyyoouu
Sample Output
wre
Explanation:
First we remove "hh" then "oo" then "aa" then "yy" then "oo" then "uu" and we are left with "wre".
Now we cannot remove anything.
Sample Input:-
abcde
Sample Output:-
abcde
Sample Input:-
abcddcb
Sample Output:-
a, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
StringBuilder s = new StringBuilder();
String text=null;
while ((text = in.readLine ()) != null)
{
s.append(text);
}
int len=s.length();
for(int i=0;i<len-1;i++){
if(s.charAt(i)==s.charAt(i+1)){
int flag=0;
s.delete(i,i+2);
int left=i-1;
len=len-2;
i=i-2;
if(i<0){
i=-1;
}
}
}
System.out.println(s);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S you have to remove all pairs of identical letters, which are consecutive. If after the removal there appear new pairs, remove them as well. Repeat this untill no adjacent letter in the string is same.
Note that deleting of the consecutive identical letters can be done in any order, as any order leads to the same result.The input data consists of a single string S.
Constraints:
1 <= |S| <= 100000
S contains lowercase english letters only.Print the given string after it is processed. It is guaranteed that the result will contain at least one character.Sample Input
hhoowaaaareyyoouu
Sample Output
wre
Explanation:
First we remove "hh" then "oo" then "aa" then "yy" then "oo" then "uu" and we are left with "wre".
Now we cannot remove anything.
Sample Input:-
abcde
Sample Output:-
abcde
Sample Input:-
abcddcb
Sample Output:-
a, I have written this Solution Code: s=input()
l=["aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm","nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"]
while True:
do=False
for i in range(len(l)):
if l[i] in s:
do=True
while l[i] in s:
s=s.replace(l[i],"")
if do==False:
break
print(s), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S you have to remove all pairs of identical letters, which are consecutive. If after the removal there appear new pairs, remove them as well. Repeat this untill no adjacent letter in the string is same.
Note that deleting of the consecutive identical letters can be done in any order, as any order leads to the same result.The input data consists of a single string S.
Constraints:
1 <= |S| <= 100000
S contains lowercase english letters only.Print the given string after it is processed. It is guaranteed that the result will contain at least one character.Sample Input
hhoowaaaareyyoouu
Sample Output
wre
Explanation:
First we remove "hh" then "oo" then "aa" then "yy" then "oo" then "uu" and we are left with "wre".
Now we cannot remove anything.
Sample Input:-
abcde
Sample Output:-
abcde
Sample Input:-
abcddcb
Sample Output:-
a, 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
string s;
cin>>s;
int len=s.length();
char stk[410000];
int k = 0;
for (int i = 0; i < len; i++)
{
stk[k++] = s[i];
while (k > 1 && stk[k - 1] == stk[k - 2])
k -= 2;
}
for (int i = 0; i < k; i++)
cout << stk[i];
return 0;
}, 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, your task is to calculate the sum of bit difference in all pairs which can be formed.The first line of input contains a single integer N, the second line of input contains N space separated integers depicting values of the array.
Constraints:-
1 <= N <= 100000
0 <= Arr[i] <= 1000000000Print the sum of bit difference of all possible pairs.
Note:- Since the answer can be quite large print your answer modulo 10<sup>9</sup> + 7Sample Input:-
2
1 3
Sample Output:-
2
Explanation:-
(1, 1) = 0
(1, 3) = 1
(3, 1) = 1
(3, 3) = 0
Sample Input:-
2
1 2
Sample Output:-
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String str[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(str[i]);
}
long res=0;
for (int i=0;i<32;i++){
long cnt=0;
for (int j=0;j<n;j++)
if ((a[j] & (1 << i)) == 0)
cnt++;
res=(res+(cnt*(n-cnt)*2))%1000000007;
}
System.out.println(res%1000000007);
}
}, 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, your task is to calculate the sum of bit difference in all pairs which can be formed.The first line of input contains a single integer N, the second line of input contains N space separated integers depicting values of the array.
Constraints:-
1 <= N <= 100000
0 <= Arr[i] <= 1000000000Print the sum of bit difference of all possible pairs.
Note:- Since the answer can be quite large print your answer modulo 10<sup>9</sup> + 7Sample Input:-
2
1 3
Sample Output:-
2
Explanation:-
(1, 1) = 0
(1, 3) = 1
(3, 1) = 1
(3, 3) = 0
Sample Input:-
2
1 2
Sample Output:-
4, I have written this Solution Code: def suBD(arr, n):
ans = 0 # Initialize result
for i in range(0, 64):
count = 0
for j in range(0, n):
if ( (arr[j] & (1 << i)) ):
count+= 1
ans += (count * (n - count)) * 2;
return (ans)%(10**9+7)
n=int(input())
arr = map(int,input().split())
arr=list(arr)
print(suBD(arr, n)), 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, your task is to calculate the sum of bit difference in all pairs which can be formed.The first line of input contains a single integer N, the second line of input contains N space separated integers depicting values of the array.
Constraints:-
1 <= N <= 100000
0 <= Arr[i] <= 1000000000Print the sum of bit difference of all possible pairs.
Note:- Since the answer can be quite large print your answer modulo 10<sup>9</sup> + 7Sample Input:-
2
1 3
Sample Output:-
2
Explanation:-
(1, 1) = 0
(1, 3) = 1
(3, 1) = 1
(3, 3) = 0
Sample Input:-
2
1 2
Sample Output:-
4, 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 101
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}signed main(){
int N;
cin>>N;
int a[55];
int A[N];
FOR(i,N){
cin>>A[i];}
for(int i=0;i<55;i++){
a[i]=0;
}
int ans=1,p=2;
for(int i=0;i<55;i++){
for(int j=0;j<N;j++){
if(ans&A[j]){a[i]++;}
}
ans*=p;
// out(ans);
}
ans=0;
for(int i=0;i<55;i++){
ans+=(a[i]*(N-a[i])*2);
ans%=MOD;
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the number of prime numbers before N (Including that number too).The first line of the input contains the number of test cases T.
Next T lines contain the value N.
<b>Constraints</b>
1 <= T <= 1e5
1 <= N <= 1e5Print the number of primes number before that number.Sample Input 1:
2
3
11
Sample Output 1:
2
5, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
class Main
{
static int [] booleanArray(int num)
{
boolean [] bool = new boolean[num+1];
int [] count = new int [num+1];
bool[0] = true;
bool[1] = true;
for(int i=2; i*i<=num; i++)
{
if(bool[i]==false)
{
for(int j=i*i; j<=num; j+=i)
bool[j] = true;
}
}
int counter = 0;
for(int i=1; i<=num; i++)
{
if(bool[i]==false)
{
counter = counter+1;
count[i] = counter;
}
else
{
count[i] = counter;
}
}
return count;
}
public static void main (String[] args) throws IOException {
InputStreamReader ak = new InputStreamReader(System.in);
BufferedReader hk = new BufferedReader(ak);
int[] v = booleanArray(100000);
int t = Integer.parseInt(hk.readLine());
for (int i = 1; i <= t; i++) {
int a = Integer.parseInt(hk.readLine());
System.out.println(v[a]);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the number of prime numbers before N (Including that number too).The first line of the input contains the number of test cases T.
Next T lines contain the value N.
<b>Constraints</b>
1 <= T <= 1e5
1 <= N <= 1e5Print the number of primes number before that number.Sample Input 1:
2
3
11
Sample Output 1:
2
5, I have written this Solution Code: m=100001
prime=[True for i in range(m)]
p=2
while(p*p<=m):
if prime[p]:
for i in range(p*p,m,p):
prime[i]=False
p+=1
c=[0 for i in range(m)]
c[2]=1
for i in range(3,m):
if prime[i]:
c[i]=c[i-1]+1
else:
c[i]=c[i-1]
t=int(input())
while t>0:
n=int(input())
print(c[n])
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the number of prime numbers before N (Including that number too).The first line of the input contains the number of test cases T.
Next T lines contain the value N.
<b>Constraints</b>
1 <= T <= 1e5
1 <= N <= 1e5Print the number of primes number before that number.Sample Input 1:
2
3
11
Sample Output 1:
2
5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
vector<bool> sieve(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
return is_prime;
}
int main() {
vector<bool> prime = sieve(1e5 + 1);
vector<int> prefix(1e5 + 1, 0);
for (int i = 1; i <= 1e5; i++) {
if (prime[i]) {
prefix[i] = prefix[i - 1] + 1;
} else {
prefix[i] = prefix[i - 1];
}
}
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
cout << prefix[n] << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Solo likes to solve simple problems, but this time she is stuck with an easy problem she created herself. Since she cannot visit her friends currently (neither should you), can you code this problem for her? The problem is short and sweet.
Given an integer n, can you find the smallest n-digit number that is divisible by both 3 and 7? (Of course, the number cannot begin with a 0)The only line of input contains a single integer n, the number of digits in the required number.
Constraints
2 <= n <= 100000Output a single integer, the required n digit number. (The answer may not fit into 32 or 64 bit data types).Sample Input 1
2
Sample Output 1
21
Sample Input 2
4
Sample Output
1008
Explanation 1: 21 is the first 2 digit number divisible by both 3 and 7., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(rdr.readLine());
if(n==0 || n==1){
return ;
}
if(n==2){
System.out.println(21);
}
else{
StringBuilder str= new StringBuilder();
str.append("1");
for(long i=0;i<n-3;i++){
str.append("0");
}
if(n%6==0){
str.append("02");
System.out.println(str.toString());
}
else if(n%6==1){
str.append("20");
System.out.println(str.toString());
}
else if(n%6==2){
str.append("11");
System.out.println(str.toString());
}
else if(n%6==3){
str.append("05");
System.out.println(str.toString());
}
if(n%6==4){
str.append("08");
System.out.println(str.toString());
return;
}
else if(n%6==5){
str.append("17");
System.out.println(str.toString());
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Solo likes to solve simple problems, but this time she is stuck with an easy problem she created herself. Since she cannot visit her friends currently (neither should you), can you code this problem for her? The problem is short and sweet.
Given an integer n, can you find the smallest n-digit number that is divisible by both 3 and 7? (Of course, the number cannot begin with a 0)The only line of input contains a single integer n, the number of digits in the required number.
Constraints
2 <= n <= 100000Output a single integer, the required n digit number. (The answer may not fit into 32 or 64 bit data types).Sample Input 1
2
Sample Output 1
21
Sample Input 2
4
Sample Output
1008
Explanation 1: 21 is the first 2 digit number divisible by both 3 and 7., I have written this Solution Code: n = input()
n=int(n)
n1=10**(n-1)
n2=10**(n)
while(n1<n2):
if((n1%3==0) and (n1%7==0)):
print(n1)
break
n1 = n1+1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Solo likes to solve simple problems, but this time she is stuck with an easy problem she created herself. Since she cannot visit her friends currently (neither should you), can you code this problem for her? The problem is short and sweet.
Given an integer n, can you find the smallest n-digit number that is divisible by both 3 and 7? (Of course, the number cannot begin with a 0)The only line of input contains a single integer n, the number of digits in the required number.
Constraints
2 <= n <= 100000Output a single integer, the required n digit number. (The answer may not fit into 32 or 64 bit data types).Sample Input 1
2
Sample Output 1
21
Sample Input 2
4
Sample Output
1008
Explanation 1: 21 is the first 2 digit number divisible by both 3 and 7., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
signed main()
{
fast
int n; cin>>n;
if(n==2){
cout<<"21";
return 0;
}
int mod=1;
for(int i=2; i<=n; i++){
mod = (mod*10)%7;
}
int av = 2;
mod = (mod+2)%7;
while(mod != 0){
av += 3;
mod = (mod+3)%7;
}
string sav = to_string(av);
if(sz(sav)==1){
sav.insert(sav.begin(), '0');
}
string ans = "1";
for(int i=0; i<n-3; i++){
ans += '0';
}
ans += sav;
cout<<ans;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 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));
int N = Integer.parseInt(br.readLine());
double arr[] = new double[N];
String str[] = br.readLine().trim().split(" ");
for(int i=0;i<N;i++)
arr[i]=Integer.parseInt(str[i]);
double resistance=0;
int equResistance=0;
for(int i=0;i<N;i++)
arr[i]=Integer.parseInt(str[i]);
for(int i=0;i<N;i++)
{
resistance=resistance+(1/arr[i]);
}
equResistance = (int)Math.floor((1/resistance));
System.out.println(equResistance);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, I have written this Solution Code: r = input("")
r = int(r)
n = input("").split()
resistance=0.0
for i in range(0,r):
resistor = float(n[i])
resistance = resistance + (1/resistor)
print(int(1/resistance)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, 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;
double s = 0;
for(int i = 1; i <= n; i++){
double p; cin >> p;
s = s + (1/p);
}
s = 1/s;
cout << floor(s);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a queue of integers and N queries. Your the task is to perform these operations:-
enqueue:-this operation will add an element to your current queue.
dequeue:-this operation will delete the element from the starting of the queue
displayfront:-this operation will print the element presented at front
Note:-if queue is empty than dequeue operation will do nothing, and 0 will be printed as a front element of queue if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>enqueue()</b>:- that takes integer to be added as a parameter.
<b>dequeue()</b>:- that takes no parameter.
<b>displayfront()</b> :- that takes no parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>You don't need to print anything else other than in displayfront() in which you require to print the element at front of your queue in a new line, if the queue is empty you just need to print 0.Sample Input:-
7
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
Sample Output:-
0
2
2
4
Sample input:
5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: class Queue
{
private Node front, rear;
private int currentSize;
class Node {
Node next;
int val;
Node(int val) {
this.val = val;
next = null;
}
}
public Queue()
{
front = null;
rear = null;
currentSize = 0;
}
public boolean isEmpty()
{
return (currentSize <= 0);
}
public void dequeue()
{
if (isEmpty())
{
}
else{
front = front.next;
currentSize--;
}
}
//Add data to the end of the list.
public void enqueue(int data)
{
Node oldRear = rear;
rear = new Node(data);
if (isEmpty())
{
front = rear;
}
else
{
oldRear.next = rear;
}
currentSize++;
}
public void displayfront(){
if(isEmpty()){
System.out.println("0");
}
else{
System.out.println(front.val);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: static int isPresent(long arr[], int n, long k)
{
int left = 0;
int right = n-1;
int res = -1;
while(left<=right){
int mid = (left+right)/2;
if(arr[mid] == k){
res = 1;
break;
}else if(arr[mid] < k){
left = mid + 1;
}else{
right = mid - 1;
}
}
return res;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
unordered_map<long long,int> m;
long k;
cin>>k;
long long a;
for(int i=0;i<n;i++){
cin>>a;
m[a]++;
}
if(m.find(k)!=m.end()){
cout<<1<<endl;
}
else{
cout<<-1<<endl;
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return 1
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
def position(n,arr,x):
return binary_search(arr,0,n-1,x)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: // arr is they array to search from
// x is target
function binSearch(arr, x) {
// write code here
// do not console.log
// return the 1 or -1
let l = 0;
let r = arr.length - 1;
let mid;
while (r >= l) {
mid = l + Math.floor((r - l) / 2);
// If the element is present at the middle
// itself
if (arr[mid] == x)
return 1;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
r = mid - 1;
// Else the element can only be present
// in right subarray
else
l = mid + 1;
}
// We reach here when element is not
// present in array
return -1;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
Find the unique number.An integer, N, representing the size of the array. In the next line, N space-separated integers follow.
<b>Constraints:</b>
1 <= N <=10<sup>5</sup>
1 <= A[i] <=10<sup>8</sup>Output the element with frequency 1.Input :
5
1 1 2 2 3
Output:
3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int noofterm=Integer.parseInt(br.readLine());
int arr[] = new int[noofterm];
String s[] = br.readLine().split(" ");
for(int i=0; i<noofterm;i++){
arr[i]= Integer.parseInt(s[i]);
}
System.out.println(unique(arr));
}
public static int unique(int[] inputArray)
{
int result = 0;
for(int i=0;i<inputArray.length;i++)
{
result ^= inputArray[i];
}
return (result>0 ? result : -1);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
Find the unique number.An integer, N, representing the size of the array. In the next line, N space-separated integers follow.
<b>Constraints:</b>
1 <= N <=10<sup>5</sup>
1 <= A[i] <=10<sup>8</sup>Output the element with frequency 1.Input :
5
1 1 2 2 3
Output:
3, I have written this Solution Code: n = int(input())
a = [int (x) for x in input().split()]
mapp={}
for index,val in enumerate(a):
if val in mapp:
del mapp[val]
else:
mapp[val]=1
for key, value in mapp.items():
print(key), 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. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
Find the unique number.An integer, N, representing the size of the array. In the next line, N space-separated integers follow.
<b>Constraints:</b>
1 <= N <=10<sup>5</sup>
1 <= A[i] <=10<sup>8</sup>Output the element with frequency 1.Input :
5
1 1 2 2 3
Output:
3, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main()
{
int n;
cin>>n;
int p=0;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
p^=a;
}
cout<<p<<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 matrix of N*N dimensions. Take the input and display the matrix as an output.First line contains N.
N lines follow each containing N space seperated integers.
Constraints:-
2 <= N <= 500
1 <= Mat[i][j] <= 1000Print the given matrix.Input:
2
3 4
7 6
Output:
3 4
7 6
Input:
3
1 2 3
4 5 6
7 8 9
Output:
1 2 3
4 5 6
7 8 9, 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();
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++){
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of N*N dimensions. Take the input and display the matrix as an output.First line contains N.
N lines follow each containing N space seperated integers.
Constraints:-
2 <= N <= 500
1 <= Mat[i][j] <= 1000Print the given matrix.Input:
2
3 4
7 6
Output:
3 4
7 6
Input:
3
1 2 3
4 5 6
7 8 9
Output:
1 2 3
4 5 6
7 8 9, I have written this Solution Code: n = int(input())
for _ in range(n):
print(input()), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of N*N dimensions. Take the input and display the matrix as an output.First line contains N.
N lines follow each containing N space seperated integers.
Constraints:-
2 <= N <= 500
1 <= Mat[i][j] <= 1000Print the given matrix.Input:
2
3 4
7 6
Output:
3 4
7 6
Input:
3
1 2 3
4 5 6
7 8 9
Output:
1 2 3
4 5 6
7 8 9, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
}}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, You have to find the maximum product of two integers.The first line of input contains a single integer N. The next line of input contains N space separated integers.
Constraints:-
2 <= N <= 100000
-10000 <= Arr[i] <= 10000Print the maximum product of two elements.Sample Input:-
5
-1 -2 3 4 -5
Sample Output:-
12
Explanation:-
4*3 = 12
Sample Input:-
4
-1 -1 1 1
Sample Output:-
1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n,l;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
int res = max(a[0]*a[1],a[n-1]*a[n-2]);
cout<<res;
}
, 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: Ram has a military dog who has the special ability of hearing, He can hear frequencies starting from 70Hz to 44000Hz (both inclusive). Now Ram wants to detect some unusual sounds across him. Tell "Yes" or "No" whether his dog can detect the sound or not.First line of the input contains the value of Test cases T.
Each test case consists of a single integer X - the frequency of unusual sound in Hertz.
<b>Constraints</b>
1 ≤ T ≤ 10<sup>4</sup>
1 ≤ X ≤ 10<sup>5</sup>Print Yes or No wether his dog can detect them or not for each test case.Sample Input
3
21
453
45000
Sample Output
No
Yes
No, I have written this Solution Code: /**
* author: tourist1256
* created: 2022-09-20 14:02:39
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
#define int long long
#define yn(ans) printf("%s\n", (ans) ? "Yes" : "No");
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(int l = LLONG_MIN, int r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
auto start = std::chrono::high_resolution_clock::now();
int tt;
cin >> tt;
while (tt--) {
int N;
cin >> N;
yn(N >= 70 && N <= 44000);
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
cerr << "Time taken : " << ((long double)duration.count()) / ((long double)1e9) << "s " << endl;
return 0;
};
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram has a military dog who has the special ability of hearing, He can hear frequencies starting from 70Hz to 44000Hz (both inclusive). Now Ram wants to detect some unusual sounds across him. Tell "Yes" or "No" whether his dog can detect the sound or not.First line of the input contains the value of Test cases T.
Each test case consists of a single integer X - the frequency of unusual sound in Hertz.
<b>Constraints</b>
1 ≤ T ≤ 10<sup>4</sup>
1 ≤ X ≤ 10<sup>5</sup>Print Yes or No wether his dog can detect them or not for each test case.Sample Input
3
21
453
45000
Sample Output
No
Yes
No, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0){
int x=s.nextInt();
boolean flag=false;
for(int i=70;i<=44000;i++){
if(x==i){
flag=true;
}
}
if(flag){
System.out.println("Yes");
}
else{
System.out.println("No");
}
t--;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram has a military dog who has the special ability of hearing, He can hear frequencies starting from 70Hz to 44000Hz (both inclusive). Now Ram wants to detect some unusual sounds across him. Tell "Yes" or "No" whether his dog can detect the sound or not.First line of the input contains the value of Test cases T.
Each test case consists of a single integer X - the frequency of unusual sound in Hertz.
<b>Constraints</b>
1 ≤ T ≤ 10<sup>4</sup>
1 ≤ X ≤ 10<sup>5</sup>Print Yes or No wether his dog can detect them or not for each test case.Sample Input
3
21
453
45000
Sample Output
No
Yes
No, I have written this Solution Code: T = int(input())
for i in range(T):
X = int(input())
if X>=70 and X<=44000:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String s=reader.readLine();
long c=0;
for(int i=1;i<s.length();i=i+2){
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u')
c++;
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: def check(c):
if c=='a' or c=='e' or c=='i' or c=='o' or c=='u':
return True
else:
return False
n=int(input())
s=input()
j=0
cnt=0
ans=0
for i in range(0,n):
j=i+1
if j%2==0:
if check(s[i]):
#=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u':
cnt+=1
print(cnt)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
bool check(char c){
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){return true;}
else{
return false;
}
}
int main() {
int n; cin>>n;
string s;
cin>>s;
int j=0;
int cnt=0;
int ans=0;
for(int i=0;i<n;i++){
if(i&1){
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
cnt++;
}}
}
cout<<cnt;
}
, 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 matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, 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;
cin>>n;
int a[n][n];
FOR(i,n){
FOR(j,n){
cin>>a[i][j];}}
int sum=0,sum1=0;;
FOR(i,n){
sum+=a[i][i];
sum1+=a[n-i-1][i];
}
out1(sum);out(sum1);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String args[])throws Exception {
InputStreamReader inr= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(inr);
String str=br.readLine();
int row = Integer.parseInt(str);
int col=row;
int [][] arr=new int [row][col];
for(int i=0;i<row;i++){
String line =br.readLine();
String[] elements = line.split(" ");
for(int j=0;j<col;j++){
arr[i][j]= Integer.parseInt(elements[j]);
}
}
int sumPrimary=0;
int sumSecondary=0;
for(int i=0;i<row;i++){
sumPrimary=sumPrimary + arr[i][i];
sumSecondary= sumSecondary + arr[i][row-1-i];
}
System.out.println(sumPrimary+ " " +sumSecondary);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code: // mat is the matrix/ 2d array
// the dimensions of array are n * n
function diagonalSum(mat, n) {
// write code here
// console.log the answer as in example
let principal = 0, secondary = 0;
for (let i = 0; i < n; i++) {
principal += mat[i][i];
secondary += mat[i][n - i - 1];
}
console.log(`${principal} ${secondary}`);
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code: n = int(input())
sum1 = 0
sum2 = 0
for i in range(n):
a = [int(j) for j in input().split()]
sum1 = sum1+a[i]
sum2 = sum2+a[n-1-i]
print(sum1,sum2), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two sorted linked list of size s1 and s2(sizes may or may not be same), your task is to merge them such that resultant list is also sorted.<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>Merge()</b> that takes the head node of both the linked list as the parameter.
Use <b>insert()</b> function for inserting nodes in the linked list.
<b>Constraints:</b>
1 < = s1, s2 < = 1000
1 < = val < = 10000Return the head of the merged linked list, printing will be done by the driver codeSample Input:
5 6
1 2 3 4 5
3 4 6 8 9 10
Sample Output:
1 2 3 3 4 4 5 6 8 9 10, I have written this Solution Code: public static Node Merge (Node head1, Node head2){
Node head =null;
while(head1!=null && head2!=null){
if(head1.val<head2.val){
head=insert(head,head1.val);
head1=head1.next;
}
else{
head=insert(head,head2.val);
head2=head2.next;
}
}
while(head1!=null){
head=insert(head,head1.val);
head1=head1.next;
}
while(head2!=null){
head=insert(head,head2.val);
head2=head2.next;
}
return head;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After saving the planet of Xander in 2014 from Ronan, the guardians of the galaxy decided to play a game on it. Between the vast furious oceans, they discover a row of buildings. The buildings are of different heights arranged at each index along a number line. Gamora starts at building 0 and a height of 0. Groot gets the task to <b>determine the minimum thrust that Gamora needs at the start of the game so that she can jump to the top of each building without her thrust going below zero</b>. Write a code to help Groot with his task.
The units of height relate directly to units of thrust. Gamora’s thrust level is calculated as follows:
• If gamora's thrust is less than the height of the building, her new thrust = gamora's thrust – (height – gamora's thrust)
• If gamora's thrust is greater than equal to the height of the building, her new thrust = gamora's thrust + (gamora's thrust - height)The first line contains an integer n, the number of buildings.
The second line n space-separated integers, arr[1], arr[2]…arr[n], the heights of the building.
<b>Constraints:-</b>
1 ≤ n ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>The output contains a single integer denoting the minimum thrust Gamora needs at the start of the game.Sample Input:-
5
3 4 3 2 4
Sample Output:-
4
Explanation:-
If we take 3 then:-
at index 1:- 3 + 3-3 = 3
at index 2:- 3 - (4-3) = 2
at index 3:- 2 - (3-2) = 1
at index 4:- 1 - (2-1) = 0
Sample Input:-
3
4 4 4
Sample Output:-
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i]= sc.nextLong();
}
long l = 0;
long r= 1000000000;
long x=0;
while (l!=r){
x= (l+r)/2;
if(checkThrust(arr,x)) {
r=x;
}
else {
l=x+1;
}
}
System.out.print(l);
}
static boolean checkThrust(long[] arr,long r){
long thrust = r;
for (int i = 0; i < arr.length; i++) {
thrust = 2*thrust-arr[i];
if(thrust>=1000000000000L) return true;
if(thrust<0) return false;
}
return true;
}
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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After saving the planet of Xander in 2014 from Ronan, the guardians of the galaxy decided to play a game on it. Between the vast furious oceans, they discover a row of buildings. The buildings are of different heights arranged at each index along a number line. Gamora starts at building 0 and a height of 0. Groot gets the task to <b>determine the minimum thrust that Gamora needs at the start of the game so that she can jump to the top of each building without her thrust going below zero</b>. Write a code to help Groot with his task.
The units of height relate directly to units of thrust. Gamora’s thrust level is calculated as follows:
• If gamora's thrust is less than the height of the building, her new thrust = gamora's thrust – (height – gamora's thrust)
• If gamora's thrust is greater than equal to the height of the building, her new thrust = gamora's thrust + (gamora's thrust - height)The first line contains an integer n, the number of buildings.
The second line n space-separated integers, arr[1], arr[2]…arr[n], the heights of the building.
<b>Constraints:-</b>
1 ≤ n ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>The output contains a single integer denoting the minimum thrust Gamora needs at the start of the game.Sample Input:-
5
3 4 3 2 4
Sample Output:-
4
Explanation:-
If we take 3 then:-
at index 1:- 3 + 3-3 = 3
at index 2:- 3 - (4-3) = 2
at index 3:- 2 - (3-2) = 1
at index 4:- 1 - (2-1) = 0
Sample Input:-
3
4 4 4
Sample Output:-
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
const long long linf = 0x3f3f3f3f3f3f3f3fLL;
const int N = 111111;
int n;
int h[N];
int check(long long x) {
long long energy = x;
for(int i = 1; i <= n; i++) {
energy += energy - h[i];
if(energy >= linf) return 1;
if(energy < 0) return 0;
}
return 1;
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
long long L = 0, R = linf;
long long ans=0;
while(L < R) {
long long M = (L + R) / 2;
if(check(M)) {
R = M;
ans=M;
} else {
L = M + 1;
}
}
cout << ans << 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: 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 double linked list consisting of N nodes, your task is to reverse the linked list and return the head of the modified list.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Reverse()</b> that takes head node of the linked list as a parameter.
Constraints:
1 <= N <= 10^3
1<=value<=100Return the head of the modified linked list.Input:
6
1 2 3 4 5 6
Output:
6 5 4 3 2 1
Explanation:
After reversing the list, elements are as 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1., I have written this Solution Code: public static Node Reverse(Node head) {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code:
#include <iostream>
using namespace std;
int Dishes(int N, int T){
return T-N;
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
printf("%d",Dishes(n,k));
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let us create a table 'STORY' from existing table 'POSTS' which will contain the information about the stories users will create on LinkedIn. This table 'story' will contain the fields (USERNAME, DATETIME_CREATED, PHOTO) of the table 'posts'
( USE ONLY UPPERCASE LETTERS FOR CODE)
<schema>[{'name': 'STORY', 'columns': [{'name': 'USERNAME', 'type': 'VARCHAR(24)'}, {'name': 'DATETIME_CREATED', 'type': 'TEXT'}, {'name': 'PHOTO', 'type': 'BLOB'}]}]</schema>nannannan, I have written this Solution Code:
CREATE TABLE STORY AS
SELECT USERNAME, DATETIME_CREATED, PHOTO
FROM POST;
, In this Programming Language: SQL, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a double linked list consisting of N nodes, your task is to reverse the linked list and return the head of the modified list.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Reverse()</b> that takes head node of the linked list as a parameter.
Constraints:
1 <= N <= 10^3
1<=value<=100Return the head of the modified linked list.Input:
6
1 2 3 4 5 6
Output:
6 5 4 3 2 1
Explanation:
After reversing the list, elements are as 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1., I have written this Solution Code: public static Node Reverse(Node head) {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
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 size N, your task is to calculate the total sum of maximum and minimum elements in each subarray of size K.
See example for better understanding.First line of input contains an two space separated integers depicting values of N and K, next line contains N space separated integers depicting values of Arr[i].
Constraints:-
1 < = k < = N < = 100000
-100000 < = Arr[i] < = 100000Print the required sumSample Input:-
5 3
1 2 3 4 5
Sample Output:-
18
Explanation:-
For subarray 1 2 3 :- 1 + 3 = 4
For subarray 2 3 4 :- 2 + 4 = 6
For subarray 3 4 5 :- 3 + 5 = 8
total sum = 4+6+8 = 18
Sample Input:-
7 4
2 5 -1 7 -3 -1 -2
Sample Output:-
18, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a[]=br.readLine().split(" ");
int n=Integer.parseInt(a[0]);
int k=Integer.parseInt(a[1]);
String s[]=br.readLine().split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(s[i]);
int min=100000,max=-100000;
long sum=0;
for(int i=0;i<n-k+1;i++){
min=100000;
max=-100000;
for(int j=i;j<k+i;j++){
min=Math.min(arr[j],min);
max=Math.max(arr[j],max);
}
sum=sum+max+min;
}
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 array of size N, your task is to calculate the total sum of maximum and minimum elements in each subarray of size K.
See example for better understanding.First line of input contains an two space separated integers depicting values of N and K, next line contains N space separated integers depicting values of Arr[i].
Constraints:-
1 < = k < = N < = 100000
-100000 < = Arr[i] < = 100000Print the required sumSample Input:-
5 3
1 2 3 4 5
Sample Output:-
18
Explanation:-
For subarray 1 2 3 :- 1 + 3 = 4
For subarray 2 3 4 :- 2 + 4 = 6
For subarray 3 4 5 :- 3 + 5 = 8
total sum = 4+6+8 = 18
Sample Input:-
7 4
2 5 -1 7 -3 -1 -2
Sample Output:-
18, I have written this Solution Code: from collections import deque
def solve(arr,n,k):
Sum = 0
S = deque()
G = deque()
for i in range(k):
while ( len(S) > 0 and arr[S[-1]] >= arr[i]):
S.pop()
while ( len(G) > 0 and arr[G[-1]] <= arr[i]):
G.pop()
G.append(i)
S.append(i)
for i in range(k, n):
Sum += arr[S[0]] + arr[G[0]]
while ( len(S) > 0 and S[0] <= i - k):
S.popleft()
while ( len(G) > 0 and G[0] <= i - k):
G.popleft()
while ( len(S) > 0 and arr[S[-1]] >= arr[i]):
S.pop()
while ( len(G) > 0 and arr[G[-1]] <= arr[i]):
G.pop()
G.append(i)
S.append(i)
Sum += arr[S[0]] + arr[G[0]]
return Sum
n,k=map(int,input().split())
l=list(map(int,input().split()))
print(solve(l,n,k)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of size N, your task is to calculate the total sum of maximum and minimum elements in each subarray of size K.
See example for better understanding.First line of input contains an two space separated integers depicting values of N and K, next line contains N space separated integers depicting values of Arr[i].
Constraints:-
1 < = k < = N < = 100000
-100000 < = Arr[i] < = 100000Print the required sumSample Input:-
5 3
1 2 3 4 5
Sample Output:-
18
Explanation:-
For subarray 1 2 3 :- 1 + 3 = 4
For subarray 2 3 4 :- 2 + 4 = 6
For subarray 3 4 5 :- 3 + 5 = 8
total sum = 4+6+8 = 18
Sample Input:-
7 4
2 5 -1 7 -3 -1 -2
Sample Output:-
18, I have written this Solution Code: // C++ program to find sum of all minimum and maximum
// elements Of Sub-array Size k.
#include<bits/stdc++.h>
using namespace std;
#define int long long int
int SumOfKsubArray(int arr[] , int n , int k)
{
int sum = 0; // Initialize result
// The queue will store indexes of useful elements
// in every window
// In deque 'G' we maintain decreasing order of
// values from front to rear
// In deque 'S' we maintain increasing order of
// values from front to rear
deque< int > S(k), G(k);
// Process first window of size K
int i = 0;
for (i = 0; i < k; i++)
{
// Remove all previous greater elements
// that are useless.
while ( (!S.empty()) && arr[S.back()] >= arr[i])
S.pop_back(); // Remove from rear
// Remove all previous smaller that are elements
// are useless.
while ( (!G.empty()) && arr[G.back()] <= arr[i])
G.pop_back(); // Remove from rear
// Add current element at rear of both deque
G.push_back(i);
S.push_back(i);
}
// Process rest of the Array elements
for ( ; i < n; i++ )
{
// Element at the front of the deque 'G' & 'S'
// is the largest and smallest
// element of previous window respectively
sum += arr[S.front()] + arr[G.front()];
// Remove all elements which are out of this
// window
while ( !S.empty() && S.front() <= i - k)
S.pop_front();
while ( !G.empty() && G.front() <= i - k)
G.pop_front();
// remove all previous greater element that are
// useless
while ( (!S.empty()) && arr[S.back()] >= arr[i])
S.pop_back(); // Remove from rear
// remove all previous smaller that are elements
// are useless
while ( (!G.empty()) && arr[G.back()] <= arr[i])
G.pop_back(); // Remove from rear
// Add current element at rear of both deque
G.push_back(i);
S.push_back(i);
}
// Sum of minimum and maximum element of last window
sum += arr[S.front()] + arr[G.front()];
return sum;
}
// Driver program to test above functions
signed main()
{
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout << SumOfKsubArray(a, n, k) ;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*1).<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 <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: def factorial(n):
if(n == 1):
return 1
return n * factorial(n-1)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*1).<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 <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: static int Factorial(int N)
{
if(N==0){
return 1;}
return N*Factorial(N-1);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*1).<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 <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: // n is the input number
function factorial(n) {
// write code here
// do not console.log
// return the answer as a number
if (n == 1 ) return 1;
return n * factorial(n-1)
}, In this Programming Language: JavaScript, 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: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String StrInput[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(StrInput[0]);
int s = Integer.parseInt(StrInput[1]);
int arr[] = new int[n];
String StrInput2[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(StrInput2[i]);
}
int sum = arr[0];
int startingindex = 0;
int endingindex = 1;
int j = 0;
int i;
for(i=1;i<=n;i++)
{
if(sum < s && arr[i] != 0)
{
sum += arr[i];
}
while(sum > s && startingindex < i-1)
{
sum -= arr[startingindex];
startingindex++;
}
if(sum == s)
{
endingindex = i+1;
if(arr[0] == 0)
{
System.out.print(startingindex+2 + " " + endingindex);
}
else
{
System.out.print(startingindex+1 + " "+ endingindex);
}
break;
}
if(i == n && sum < s)
{
System.out.print(-1);
break;
}
}
}
catch(Exception e)
{
System.out.print(-1);
}
}
}, 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 of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
unordered_map<int,int> m;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==k){cout<<1<<" "<<i+1;return 0;}
if(m.find(sum-k)!=m.end()){
cout<<m[sum-k]+2<<" "<<i+1;
return 0;
}
m[sum]=i;
}
cout<<-1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: def sumFinder(N,S,a):
currentSum = a[0]
start = 0
i = 1
while i <= N:
while currentSum > S and start < i-1:
currentSum = currentSum - a[start]
start += 1
if currentSum == S:
return (start+1,i)
if i < N:
currentSum = currentSum + a[i]
i += 1
return(-1)
N, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = sumFinder(N,S,a)
if(ans==-1):
print(ans)
else:
print(ans[0],ans[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rahul wants to impress a girl named Tanu whom he loves very dearly. Rahul gets to know that Tanu is stuck on a programming problem. Help Rahul to solve the problem so that he can impress Tanu.
Here is the description of the problem:-
Choose an integer n and subtract the first digit of the number n from it i.e if n is 245 then subtract 2 from it making it 245-2 = 243.
Keep on doing this operation until the number becomes 0 (for eg. 25 requires 14 operations to reduce to 0)
You are given a number of operation limit X. You have to find the greatest integer such that you can reduce it to 0 using atmost X operations.The first line of input contains the number of test cases T. The next T line contains a single integer X denotes the number of operations.
Constraints:-
1 <= T <= 100
1 <= X <= 314329798For each test case in a new line print the maximum number that can be obtained by performing exactly X operations.Sample Input:-
3
2
20
29
Sample Output:-
10
45
100
Explanation:-
For test 2:- 45 is the largest number that will become 0 after exactly 20 operations., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long check(long x)
{
int count = 0;
while(x > 0 )
{
long temp = x;
while(temp >= 10)
temp /= 10;
x -= temp;
count++;
}
return count;
}
public static long new_check(long x)
{
int count = 0;
while(x >= 10 )
{
long temp_ans =1;
long temp = x;
while(temp >= 10)
{
temp /= 10;
temp_ans *= 10;
}
temp_ans *= temp;
if (x != temp_ans){
long diff = x - temp_ans;
if (diff >= temp)
{
temp_ans += diff - ((diff/temp)*temp);
count += (diff/temp);
}
else
{
temp_ans = x;
temp_ans -= temp;
count++;
}
}
else{
temp_ans -= temp;
count++;
}
x = temp_ans;
}
if (x > 0 && x < 10)
count++;
return count;
}
public static long calculate (long start,long end,long x)
{
if (start <= end )
{
long mid = (start+end)/2;
long temp = new_check(mid);
if (temp == x)
{
return mid;
}
else if (temp > x)
return calculate(start,mid-1,x);
else
return calculate(mid+1,end,x);
}
return -1;
}
public static void main (String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(reader.readLine());
while(t-- > 0)
{
long n = Integer.parseInt(reader.readLine().trim());
long x = n*10;
long ans = calculate(n,x,n);
while (n == new_check(ans+1))
ans++;
System.out.println(ans);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rahul wants to impress a girl named Tanu whom he loves very dearly. Rahul gets to know that Tanu is stuck on a programming problem. Help Rahul to solve the problem so that he can impress Tanu.
Here is the description of the problem:-
Choose an integer n and subtract the first digit of the number n from it i.e if n is 245 then subtract 2 from it making it 245-2 = 243.
Keep on doing this operation until the number becomes 0 (for eg. 25 requires 14 operations to reduce to 0)
You are given a number of operation limit X. You have to find the greatest integer such that you can reduce it to 0 using atmost X operations.The first line of input contains the number of test cases T. The next T line contains a single integer X denotes the number of operations.
Constraints:-
1 <= T <= 100
1 <= X <= 314329798For each test case in a new line print the maximum number that can be obtained by performing exactly X operations.Sample Input:-
3
2
20
29
Sample Output:-
10
45
100
Explanation:-
For test 2:- 45 is the largest number that will become 0 after exactly 20 operations., I have written this Solution Code:
def countdig(m) :
if (m == 0) :
return 0
else :
return 1 + countdig(m // 10)
def countSteps(x) :
c = 0
last = x
while (last) :
digits = countdig(last)
digits -= 1
divisor = pow(10, digits)
first = last // divisor
lastnumber = first * divisor
skipped = (last - lastnumber) // first
skipped += 1
c += skipped
last = last - (first * skipped)
return c
def fun(l,r,ans,x):
#print(l,r,ans,"gjgufy")
if(l>r):
return ans
m=(l+r)//2
midans=countSteps(m)
#print(m,midans)
if(midans<=x):
ans=max(ans,m)
if(midans<=x):
return fun(m+1,r,ans,x)
else:
return fun(l,m-1,ans,x)
for _ in range(int(input())):
x=int(input())
print(fun(0,9999999999,0,x)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rahul wants to impress a girl named Tanu whom he loves very dearly. Rahul gets to know that Tanu is stuck on a programming problem. Help Rahul to solve the problem so that he can impress Tanu.
Here is the description of the problem:-
Choose an integer n and subtract the first digit of the number n from it i.e if n is 245 then subtract 2 from it making it 245-2 = 243.
Keep on doing this operation until the number becomes 0 (for eg. 25 requires 14 operations to reduce to 0)
You are given a number of operation limit X. You have to find the greatest integer such that you can reduce it to 0 using atmost X operations.The first line of input contains the number of test cases T. The next T line contains a single integer X denotes the number of operations.
Constraints:-
1 <= T <= 100
1 <= X <= 314329798For each test case in a new line print the maximum number that can be obtained by performing exactly X operations.Sample Input:-
3
2
20
29
Sample Output:-
10
45
100
Explanation:-
For test 2:- 45 is the largest number that will become 0 after exactly 20 operations., 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
#define read(type) readInt<type>()
#define max1 10000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll cal(ll n){
ll ans=0;
ll cnt=0;
ll p=n;
ll res=n;
while(res/10>0){
cnt=0;
p=res;
n=res;
while(n/10>0){
cnt++;
n=n/10;}
ll x=lround(pow(10,cnt));
ll t=p/x;
ll y=p%x;
ans+=(y/t+1);
res=res-t*(y/t+1);
}
ans++;
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
ll x;
cin>>x;
ll i=1,j=1e9+50;
ll mid;
while(i<j){
mid= (i+j)/2;
ll ans=cal(mid);
if(ans>x){j=mid;}
else if(ans==x){break;}
else{
i=mid+1;
}
}
for(ll k=mid-1;k<1e9+20;k++){
if(cal(k)>x){out(k-1);break;}
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <i>We didn't like the final season much, so finale isn't GOT! :P </i>
Making a sword involves three stages, forging it, carrying it to polishing center and then polishing it. There are two types of swords that must be made.
Forging sword 1 and sword 2 takes X hours.
Carrying sword 1 from forging centre to polishing centre takes T1 hours.
Carrying sword 2 from forging centre to polishing centre takes T2 hours.
Polishing sword 1 and sword 2 takes X hours.
No two swords can be in same stage at a time, but two swords can be in different stages in parallel.
Thomas is required to make A swords of type 1 and B swords of type 2. Find the minimum time required by Thomas to make all the swords. You can make swords in any order.Input contains 5 integers X T1 T2 A B.
Constraints:
1 <= X, T1, T2 <= 10000
1 <= A, B <= 100000The minimum time required to produce the required swords.Sample Input
6 4 8 2 2
Sample Output
38
Explanation: Optimal order is sword 2 sword 1 sword 1 sword 2.
First sword will be finished forging after 6 hours.
First sword will be finished transporting after 14 hours.
First sword will be finished polishing after 20 hours.
Second sword can enter forging after 6 hours so it will be finished forging at 12 hours.
Second sword can enter transporting after 14 hours so it will be finished transporting at 18 hours.
Second sword can enter polishing after 20 hours so it will be finished polishing at 26 hours.
Third sword can enter forging after 12 hours so it will be finished forging at 18 hours.
Third sword can enter transporting after 18 hours so it will be finished transporting at 22 hours.
Third sword can enter polishing after 26 hours so it will be finished polishing at 32 hours.
Fourth sword can enter forging after 18 hours so it will be finished forging at 24 hours.
Fourth sword can enter transporting after 24 hours so it will be finished transporting at 32 hours.
Fourth sword can enter polishing after 32 hours so it will be finished polishing at 38 hours., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
template<class C> void mini(C&a4, C b4){a4=min(a4,b4);}
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
int t,c1,c2,a1,a2;
bool spr(int x){
int zapas = x;
int b1 = a1;
int b2 = a2;
while(b1 || b2){
mini(zapas,x);
if(b2 && zapas >= c2){
zapas -= c2;
b2--;
}else{
if(b1 == 0 || zapas < c1){
return 0;
}
zapas -= c1;
b1--;
}
zapas += t;
}
return 1;
}
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
cin >> t >> c1 >> c2 >> a1 >> a2;
if(a1 + a2 == 0){
cout << "0\n";
return 0;
}
int po = 0;
int ko = 2e9;
while(po+1 < ko){
int m = (po+ko) >>1;
if(spr(m))
ko = m;
else
po = m;
}
cout << t*(a1+a2+1) + ko << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Gian and Suneo want their heights to be equal so they asked Doraemon's help. Doraemon gave a big light to both of them but the both big lights have different speed of magnifying. Let's assume the big light given to Gian can increase height of a person by v1 m/s and that of Suneo's big light is v2 m/s.
At the end of each second Doraemon check if their heights are equal or not.
Given initial height of Gian and Suneo, your task is to check whether the height of Gian and Suneo will become equal at some point or not, assuming they both started at the same time.First line takes the input of integer h1(height of gian), h2(height of suneo), v1(speed of Gian's big light) and v2(speed of Suneo's big light) as parameter.
<b>Constraints:-</b>
1 <b>≤</b> h2 < h1<b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v1 <b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v2 <b>≤</b> 10<sup>4</sup>complete the function EqualOrNot and return a boolean True if their height will become equal at some point (as seen by Doraemon) else print False
Sample input:-
4 2 2 4
Sample output:-
Yes
Explanation:-
height of Gian goes as- 4 6 8 10. .
height of Suneo goes as:- 2 6 10..
at the end of 1 second their height will become equal.
Sample Input:-
5 4 1 6
Sample Output:
No, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
bool EqualOrNot(int h1, int h2, int v1,int v2){
if (v2>v1&&(h1-h2)%(v2-v1)==0){
return true;
}
return false;
}
int main(){
int n1,n2,v1,v2;
cin>>n1>>n2>>v1>>v2;
if(EqualOrNot(n1,n2,v1,v2)){
cout<<"Yes";}
else{
cout<<"No";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Gian and Suneo want their heights to be equal so they asked Doraemon's help. Doraemon gave a big light to both of them but the both big lights have different speed of magnifying. Let's assume the big light given to Gian can increase height of a person by v1 m/s and that of Suneo's big light is v2 m/s.
At the end of each second Doraemon check if their heights are equal or not.
Given initial height of Gian and Suneo, your task is to check whether the height of Gian and Suneo will become equal at some point or not, assuming they both started at the same time.First line takes the input of integer h1(height of gian), h2(height of suneo), v1(speed of Gian's big light) and v2(speed of Suneo's big light) as parameter.
<b>Constraints:-</b>
1 <b>≤</b> h2 < h1<b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v1 <b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v2 <b>≤</b> 10<sup>4</sup>complete the function EqualOrNot and return a boolean True if their height will become equal at some point (as seen by Doraemon) else print False
Sample input:-
4 2 2 4
Sample output:-
Yes
Explanation:-
height of Gian goes as- 4 6 8 10. .
height of Suneo goes as:- 2 6 10..
at the end of 1 second their height will become equal.
Sample Input:-
5 4 1 6
Sample Output:
No, I have written this Solution Code: def EqualOrNot(h1,h2,v1,v2):
if (v2>v1 and (h1-h2)%(v2-v1)==0):
return True
else:
return False
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Gian and Suneo want their heights to be equal so they asked Doraemon's help. Doraemon gave a big light to both of them but the both big lights have different speed of magnifying. Let's assume the big light given to Gian can increase height of a person by v1 m/s and that of Suneo's big light is v2 m/s.
At the end of each second Doraemon check if their heights are equal or not.
Given initial height of Gian and Suneo, your task is to check whether the height of Gian and Suneo will become equal at some point or not, assuming they both started at the same time.First line takes the input of integer h1(height of gian), h2(height of suneo), v1(speed of Gian's big light) and v2(speed of Suneo's big light) as parameter.
<b>Constraints:-</b>
1 <b>≤</b> h2 < h1<b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v1 <b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v2 <b>≤</b> 10<sup>4</sup>complete the function EqualOrNot and return a boolean True if their height will become equal at some point (as seen by Doraemon) else print False
Sample input:-
4 2 2 4
Sample output:-
Yes
Explanation:-
height of Gian goes as- 4 6 8 10. .
height of Suneo goes as:- 2 6 10..
at the end of 1 second their height will become equal.
Sample Input:-
5 4 1 6
Sample Output:
No, I have written this Solution Code: static boolean EqualOrNot(int h1, int h2, int v1,int v2){
if (v2>v1&&(h1-h2)%(v2-v1)==0){
return true;
}
return false;
}
, 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: 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 an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main{
public static void swap(int[] arr,int i, int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static int partition(int[] arr,int l,int r){
int pivot=arr[r];
int i=l-1;
for(int j=l;j<r;j++){
if(arr[j]>pivot){
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,r);
return i+1;
}
public static void quickSort(int[] arr,int l,int r){
if(l<r){
int pivot=partition(arr,l,r);
quickSort(arr,l,pivot-1);
quickSort(arr,pivot+1,r);
}
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int target=sc.nextInt();
quickSort(arr,0,n-1);
int i=0,j=n-1;
while(i<j){
if((arr[i]+arr[j])==target){
System.out.print("Pair found ("+arr[i]+", "+arr[j]+")");
return;
}
else if((arr[i]+arr[j])<target){
j--;
}
else{
i++;
}
}
System.out.print("Pair not found");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code:
N=int(input())
arr=list(map(int,input().split()))
target=int(input())
arr.sort(reverse=True)
'''
ans='Pair not found'
for i in range(N):
for j in range(i+1,N):
if arr[i]+arr[j]==target:
ans='Pair found ({}, {})'.format(arr[i],arr[j])
print(ans)
'''
i=0
j=N-1
ans='Pair not found'
while i<j:
if arr[i]+arr[j]<target:
j-=1
elif arr[i]+arr[j]>target:
i+=1
j+=1
else:
ans='Pair found ({}, {})'.format(arr[i],arr[j])
break
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code: #include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
// Function to find a pair in an array with a given sum using sorting
void findPair(vector<int> &nums, int n, int target)
{
// sort the array in ascending order
sort(nums.begin(), nums.end());
// maintain two indices pointing to endpoints of the array
int low = 0;
int high = n - 1;
// reduce the search space `nums[low…high]` at each iteration of the loop
// loop till the search space is exhausted
while (low < high)
{
// sum found
if (nums[low] + nums[high] == target)
{
if (nums[low] < nums[high])
swap(nums[low], nums[high]);
cout << "Pair found (" << nums[low] << ", " << nums[high] << ")\n";
return;
}
// increment `low` index if the total is less than the desired sum;
// decrement `high` index if the total is more than the desired sum
(nums[low] + nums[high] < target) ? low++ : high--;
}
// we reach here if the pair is not found
cout << "Pair not found";
}
int main()
{
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++)
cin >> nums[i];
int target;
cin >> target;
findPair(nums, n, target);
return 0;
}
, 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: 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: Find factorial of a given number where n! = n * n-1 * n-2 .....* 1First line consists of a single integer denoting n
Constraints:-
0 <= n <= 20Output is a single line containing factorial(n)Sample Input
5
Sample Output
120
Explanation:-
5!= 5 * 4 * 3 * 2 * 1 = 120
Sample Input
10
Sample Output
3628800, 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());
long fact=1;
for(int i=1; i<=n;i++){
fact*=i;
}
System.out.print(fact);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number where n! = n * n-1 * n-2 .....* 1First line consists of a single integer denoting n
Constraints:-
0 <= n <= 20Output is a single line containing factorial(n)Sample Input
5
Sample Output
120
Explanation:-
5!= 5 * 4 * 3 * 2 * 1 = 120
Sample Input
10
Sample Output
3628800, I have written this Solution Code: def fact(n):
if( n==0 or n==1):
return 1
return n*fact(n-1);
n=int(input())
print(fact(n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number where n! = n * n-1 * n-2 .....* 1First line consists of a single integer denoting n
Constraints:-
0 <= n <= 20Output is a single line containing factorial(n)Sample Input
5
Sample Output
120
Explanation:-
5!= 5 * 4 * 3 * 2 * 1 = 120
Sample Input
10
Sample Output
3628800, I have written this Solution Code: #include <bits/stdc++.h>
// #define ll long long
using namespace std;
int main(){
int t;
t=1;
while(t--){
int n;
cin>>n;
unsigned long long sum=1;
for(int i=1;i<=n;i++){
sum*=i;
}
cout<<sum<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.