Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3The first line contains two integers N and K.
Next line contains N integers denoting the elements of arr[]
<b>Constraints:</b>
1 <= N <= 100000
1 <= arr[i] <= 100000
1 <= K <= 100000000Print a single integer the value of smallest number MSample Input:
4 6
2 3 4 9
Sample Output:
4
Explanation:
When every element is divided by 4 - 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3 - 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4., I have written this Solution Code: from math import ceil
def kSum(arr, n, k):
low = 0
high = 10 ** 9
while (low < high):
mid = (low + high) // 2
sum = 0
for i in range(int(n)):
sum += ceil( int(arr[i]) / mid)
if (sum <= int(k)):
high = mid
else:
low = mid + 1
return low
n, k =input().split()
arr = input().split()
print( kSum(arr, n, k) ), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3The first line contains two integers N and K.
Next line contains N integers denoting the elements of arr[]
<b>Constraints:</b>
1 <= N <= 100000
1 <= arr[i] <= 100000
1 <= K <= 100000000Print a single integer the value of smallest number MSample Input:
4 6
2 3 4 9
Sample Output:
4
Explanation:
When every element is divided by 4 - 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3 - 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4., 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;
bool f(int x){
int sum = 0;
for(int i = 1; i <= n; i++)
sum += (a[i]-1)/x + 1;
return (sum <= k);
}
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))
h = m;
else
l = m;
}
cout << h;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Deque and Q queries. The task is to perform some operation on Deque according to the queries as described in input:
Note:-if deque is empty than pop operation will do nothing, and -1 will be printed as a front and rear 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>push_front_pf()</b>:- that takes the deque and the integer to be added as a parameter.
<b>push_bac_pb()</b>:- that takes the deque and the integer to be added as a parameter.
<b>pop_back_ppb()</b>:- that takes the deque as parameter.
<b>front_dq()</b>:- that takes the deque as parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>
<b>Custom Input: </b>
First line of input should contain the number of queries Q. Next, Q lines should contain any of the given operations:-
For <b>push_front</b> use <b> pf x</b> where x is the element to be added
For <b>push_rear</b> use <b> pb x</b> where x is the element to be added
For <b>pop_back</b> use <b> pp_b</b>
For <b>Display Front</b> use <b>f</b>
Moreover driver code will print
Front element of deque in each push_front opertion
Last element of deque in each push_back operation
Size of deque in each pop_back operation The front_dq() function will return the element at front of your deque in a new line, if the deque is empty you just need to return -1 in the function.Sample Input:
6
push_front 2
push_front 3
push_rear 5
display_front
pop_rear
display_front
Sample Output:
3
3, I have written this Solution Code:
static void push_back_pb(Deque<Integer> dq, int x)
{
dq.add(x);
}
static void push_front_pf(Deque<Integer> dq, int x)
{
dq.addFirst(x);
}
static void pop_back_ppb(Deque<Integer> dq)
{
if(!dq.isEmpty())
dq.pollLast();
else return;
}
static int front_dq(Deque<Integer> dq)
{
if(!dq.isEmpty())
return dq.peek();
else return -1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array of size N. Count number of 1's and 0's in the array.First line contains n.
Next line contains n space separated integers.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
0 <= arr[i] <= 1Print two integers, count of 1s and count of 0s.Input:
5
0 1 1 0 1
Output:
3 2, I have written this Solution Code: n=int(input())
arr= input().split()
one=zero=0
for i in range(0, n):
if arr[i]=='1':
one+=1
else:
zero+=1
print(one, zero), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array of size N. Count number of 1's and 0's in the array.First line contains n.
Next line contains n space separated integers.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
0 <= arr[i] <= 1Print two integers, count of 1s and count of 0s.Input:
5
0 1 1 0 1
Output:
3 2, I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
int n=Integer.parseInt(in.next());
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = Integer.parseInt(in.next());
}
int ones=0;
for(int i=0;i<n;i++){
ones += a[i];
}
int zeroes=n-ones;
out.print(ones + " " + zeroes);
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 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: A number (n) is represented in Linked List such that each digit corresponds to a node in linked list. Add 1 to it.
<b>Note:-</b> Linked list representation of a number is from left to right i.e if the number is 123 than in linked list it is represented as 3->2->1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>addOne()</b> that takes head node of the linked list as parameter.
Constraints:
1 <=length of n<= 1000Return the head of the modified linked list.Input 1:
456
Output 1:
457
Input 2:
999
Output 2:
1000, I have written this Solution Code:
static Node addOne(Node head)
{
Node res = head;
Node temp = null, prev = null;
int carry = 1, sum;
while (head != null) //while both lists exist
{
sum = carry + head.data;
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
head.data = sum;
temp = head;
head = head.next;
}
if (carry > 0) {
Node x=new Node(carry);
temp.next=x;}
return res;
} , In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves triangles. Whenever she sees three points she connects them by straight lines to form a triangle. In triangles, her favorite one is a right-angled triangle. If the triangle is not right-angled but it can be converted into one by moving one of the points exactly by distance 1 so, that all the coordinates remain integer, she calls such triangles "Special".
Given three points A, B, and C your task is to check if the formed triangle is "Right", "Special" or "Simple".The first line of input contains the position of A(Ax, Ay). The second line of input contains the position of B(Bx, By). The third line of input contains the position of C(Cx, Cy).
Constraints:-
|X|, |Y| <= 10^9Print "Right" if the triangle is right- angled, print "Special" if the triangle can be formed into a right- angled by moving one of the points exactly by distance 1, else print "Simple".Sample Input:-
0 0
2 0
0 1
Sample Output:-
Right
Sample Input:-
-1 0
2 0
0 1
Sample Output:-
Special
Sample Input:-
-1 0
2 0
10 10
Sample Output:-
Simple, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
static boolean ifRight(int x1, int y1, int x2, int y2, int x3, int y3) {
int a = ((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2));
int b = ((x1 - x3) * (x1 - x3)) + ((y1 - y3) * (y1 - y3));
int c = ((x2 - x3) * (x2 - x3)) + ((y2 - y3) * (y2 - y3));
if ((a == (b + c) && a != 0 && b != 0 && c != 0) ||
(b == (a + c) && a != 0 && b != 0 && c != 0) ||
(c == (a + b) && a != 0 && b != 0 && c != 0)) {
return true;
}
return false;
}
static void isValidCombination(int x1, int y1, int x2, int y2, int x3, int y3) {
int x, y;
boolean possible = false;
if (ifRight(x1, y1, x2, y2, x3, y3)) {
System.out.print("Right");
return;
}
else {
for (int i = 0; i < 4; i++)
{
x = dx[i] + x1;
y = dy[i] + y1;
if(ifRight(x, y, x2, y2, x3, y3))
{
System.out.print("Special");
return;
}
x = dx[i] + x2;
y = dy[i] + y2;
if(ifRight(x1, y1, x, y, x3, y3)) {
System.out.print("Special");
return;
}
x = dx[i] + x3;
y = dy[i] + y3;
if(ifRight(x1, y1, x2, y2, x, y)) {
System.out.print("Special");
return;
}
}
}
if (!possible) {
System.out.println("Simple");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x1, y1, x2, y2, x3, y3;
x1 = sc.nextInt();
y1 = sc.nextInt();
x2 = sc.nextInt();
y2 = sc.nextInt();
x3 = sc.nextInt();
y3 = sc.nextInt();
isValidCombination(x1, y1, x2, y2, x3, y3);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves triangles. Whenever she sees three points she connects them by straight lines to form a triangle. In triangles, her favorite one is a right-angled triangle. If the triangle is not right-angled but it can be converted into one by moving one of the points exactly by distance 1 so, that all the coordinates remain integer, she calls such triangles "Special".
Given three points A, B, and C your task is to check if the formed triangle is "Right", "Special" or "Simple".The first line of input contains the position of A(Ax, Ay). The second line of input contains the position of B(Bx, By). The third line of input contains the position of C(Cx, Cy).
Constraints:-
|X|, |Y| <= 10^9Print "Right" if the triangle is right- angled, print "Special" if the triangle can be formed into a right- angled by moving one of the points exactly by distance 1, else print "Simple".Sample Input:-
0 0
2 0
0 1
Sample Output:-
Right
Sample Input:-
-1 0
2 0
0 1
Sample Output:-
Special
Sample Input:-
-1 0
2 0
10 10
Sample Output:-
Simple, I have written this Solution Code: def check(s):
a=pow((d[0]-d[2]),2)+pow((d[1]-d[3]),2)
b=pow((d[0]-d[4]),2)+pow((d[1]-d[5]),2)
c=pow((d[2]-d[4]),2)+pow((d[3]-d[5]),2)
if ((a and b and c)==0):
return
if (a+b==c or a+c==b or b+c==a):
print(s)
exit(0)
d = list()
for i in range(0,3):
val = list(map(int,input().strip().split()))
d.append(val[0])
d.append(val[1])
check("Right\n")
for i in range(0,6):
d[i]=d[i]-1
check("Special\n")
d[i]=d[i]+2
check("Special\n")
d[i]=d[i]-1
print("Simple"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves triangles. Whenever she sees three points she connects them by straight lines to form a triangle. In triangles, her favorite one is a right-angled triangle. If the triangle is not right-angled but it can be converted into one by moving one of the points exactly by distance 1 so, that all the coordinates remain integer, she calls such triangles "Special".
Given three points A, B, and C your task is to check if the formed triangle is "Right", "Special" or "Simple".The first line of input contains the position of A(Ax, Ay). The second line of input contains the position of B(Bx, By). The third line of input contains the position of C(Cx, Cy).
Constraints:-
|X|, |Y| <= 10^9Print "Right" if the triangle is right- angled, print "Special" if the triangle can be formed into a right- angled by moving one of the points exactly by distance 1, else print "Simple".Sample Input:-
0 0
2 0
0 1
Sample Output:-
Right
Sample Input:-
-1 0
2 0
0 1
Sample Output:-
Special
Sample Input:-
-1 0
2 0
10 10
Sample Output:-
Simple, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int d[6];
int sq(int n)
{
return n*n;
}
void check(char *s)
{
int a,b,c;
a=sq(d[0]-d[2])+sq(d[1]-d[3]);
b=sq(d[0]-d[4])+sq(d[1]-d[5]);
c=sq(d[2]-d[4])+sq(d[3]-d[5]);
if ((a&&b&&c)==0)
return;
if (a+b==c||a+c==b||b+c==a)
{
cout << s;
exit(0);
}
}
int main()
{
int i;
for (i=0;i<6;i++)
cin >> d[i];
check("Right\n");
for (i=0;i<6;i++)
{
d[i]--;
check("Special\n");
d[i]+=2;
check("Special\n");
d[i]--;
}
cout << "Simple";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function <code>solve</code> which takes <code>obj</code> as a parameter which is an <code>object</code>.
Iterate <code>obj</code> using a <code>for of</code> and a <code>for in</code> loop. The <code>for...of</code> loop should print all the <code>values</code> of the <code>obj</code> in the console and the <code>for...in</code> loop should print the <code>key</code> and <code>values</code> of <code>obj</code> in the format <code>{key}: {value}</code>.
See the example for more clarity.
Note: Generate Expected Output section will not work for this questionInput will have the object which is passed as a parameter to the function solve
You need not worry about the same, it is handled properly by the hidden pre-function code.
Example:
{"name": "John","age": "32","location": "New York"}The <code>for of</code> loop should print all the <code>values</code> of the <code>obj</code> object in the console
The <code>for in</code> loop should print the <code>key</code> and <code>values</code> of <code>obj</code> object in the format <code>{key}: {value}</code>const obj = {"name": "John","age": "32","location": "New York"}
solve(obj)
/*
The console output should be
John
32
New York
name: John
age: 32
location: New York
*/, I have written this Solution Code: function solve(obj){
for (const value of Object.values(obj)) {
console.log(value);
}
for (const key in obj) {
console.log(`${key}: ${obj[key]}`);
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two sorted arrays A and B of size N and M respectively.
You have to find the value V, which is the summation of (A[j] - A[i]) for all pairs of i and j such that (j - i) is present in array B.The first line contains two space separated integers N and M – size of arrays A and B respectively.
The second line contains N integers A<sub>1</sub>, A<sub>2</sub>, ... A<sub>N</sub>.
The third line contains M integers B<sub>1</sub>, B<sub>2</sub>, ... B<sub>M</sub>.
<b> Constraints: </b>
1 ≤ N ≤ 2×10<sup>5</sup>
1 ≤ M < N
1 ≤ A<sub>1</sub> ≤ A<sub>2</sub> ≤ ... ≤ A<sub>N</sub> ≤ 10<sup>8</sup>.
1 ≤ B<sub>1</sub> < B<sub>2</sub> < ... < B<sub>M</sub> < N.Print a single integer, the value of V.Sample Input 1:
4 2
1 2 3 4
1 3
Sample Output 1:
6
Sample Explanation 1:
Valid pairs of (i, j) are (1, 2), (2, 3), (3, 4), (1, 4)., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define epsi (double)(0.00000000001)
typedef long long int ll;
typedef unsigned long long int ull;
#define vi vector<ll>
#define pii pair<ll,ll>
#define vii vector<pii>
#define vvi vector<vi>
//#define max(a,b) ((a>b)?a:b)
//#define min(a,b) ((a>b)?b:a)
#define min3(a,b,c) min(min(a,b),c)
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max3(a,b,c) max(max(a,b),c)
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define ff(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define REP(a,b,c) for(int a=b; a<c; a++)
#define pb push_back
#define mp make_pair
#define endl "\n"
#define all(v) v.begin(),v.end()
#define sz(a) (ll)a.size()
#define F first
#define S second
#define ld long double
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define setbits(x) __builtin_popcountll(x)
#define trav(a,x) for(auto &a:x)
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define rev(arr) reverse(all(arr))
#define gcd(a,b) __gcd(a,b);
#define ub upper_bound // '>'
#define lb lower_bound // '>='
#define qi queue<ll>
#define fsh cout.flush()
#define si stack<ll>
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define fill(a,b) memset(a, b, sizeof(a))
template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;}
const ll INF=1LL<<60;
void solve(){
ll n,m;
cin >> n >> m;
vi v1(n),v2(m);
for(auto &i:v1){
cin >> i;
}
for(auto &i:v2){
cin >> i;
}
ll ans=0;
for(int i=1 ; i<=n ; i++){
auto it=lower_bound(all(v2),i);
ll x=it-v2.begin();
ans+=(x*v1[i-1]);
it=lower_bound(all(v2),n-i+1);
x=it-v2.begin();
ans-=(x*v1[i-1]);
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t=1;
// cin >> t;
while(t--){
solve();
}
}, 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: 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: 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: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
cout << a[i] << " ";
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: // arr is unsorted array
// n is the number of elements in the array
function bubbleSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => a - b)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int 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 temp;
for(int i=1;i<n;i++){
if(a[i]<a[i-1]){
for(int j=i;j>0;j--){
if(a[j]<a[j-1]){
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
else{
break;
}
}
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: def bubbleSort(arr):
arr.sort()
return arr
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For an integer N, your task is to calculate sum of first N natural numbers.<b>User Task:</b>
Since this will be a functional problem, you don't have to worry about input. You just have to complete the function <b>sum()</b> which takes the integer N as a parameter.
Constraints:
1 <= N < = 100000000Print the sum of first N natural numbers.Sample Input:-
5
Sample Output:-
15
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code:
static void sum(int N){
long x=N;
x=x*(x+1);
x=x/2;
System.out.print(x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Fibonacci numbers are interesting but Tribonacci numbers are more interesting.
A Tribonacci number T(n) is the sum of the preceding three elements in a series. Consider its first three numbers to be 0, 0, and 1. i. e T(1) = 0, T(2) = 0, T(3) = 1.
Given a number N, your task is to return the nth Tribonacci number.The input contains a single integer N.
Constraints:-
1 <= N <= 20Return the Nth Tribonacci number.Sample Input:-
4
Sample Output:-
1
Sample Input:-
7
Sample Output:-
7, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[4];
a[0]=0;
a[1]=0;
a[2]=1;
if(n<=3){
cout<<a[n-1];
return 0;
}
for(int i=0;i<n-3;i++){
a[3]=a[0]+a[1]+a[2];
for(int j=0;j<3;j++){
a[j]=a[j+1];
}
//cout<<a[2]<<endl;
}
cout<<a[2];
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Fibonacci numbers are interesting but Tribonacci numbers are more interesting.
A Tribonacci number T(n) is the sum of the preceding three elements in a series. Consider its first three numbers to be 0, 0, and 1. i. e T(1) = 0, T(2) = 0, T(3) = 1.
Given a number N, your task is to return the nth Tribonacci number.The input contains a single integer N.
Constraints:-
1 <= N <= 20Return the Nth Tribonacci number.Sample Input:-
4
Sample Output:-
1
Sample Input:-
7
Sample Output:-
7, I have written this Solution Code: class Solution:
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
elif n == 1 or n == 2:
return 1
dp = []
for i in range(n+1):
dp.append(-1)
dp[0] =dp[1] = 0
dp[2] = 1
for i in range(3, n+1):
dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
return dp[n-1]
n=int(input())
num=tribonacci(n,n)
print(num), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Fibonacci numbers are interesting but Tribonacci numbers are more interesting.
A Tribonacci number T(n) is the sum of the preceding three elements in a series. Consider its first three numbers to be 0, 0, and 1. i. e T(1) = 0, T(2) = 0, T(3) = 1.
Given a number N, your task is to return the nth Tribonacci number.The input contains a single integer N.
Constraints:-
1 <= N <= 20Return the Nth Tribonacci number.Sample Input:-
4
Sample Output:-
1
Sample Input:-
7
Sample Output:-
7, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(Tribonacci(n));
}static int Tribonacci(int N){
if(N<=2){return 0;}
if(N==3){return 1;}
return Tribonacci(N-1)+Tribonacci(N-2)+Tribonacci(N-3);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.
<b>Note:-</b>
In the case of custom input, you will get 1 if your code is correct else get a 0.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>threeWayPartition()</b> which contains following arguments.
A: input array list
low: starting integer of range
high: ending integer of range
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10<sup>4</sup>
1 <= A[i] <= 10<sup>5</sup>
1 <= low <= high <= 10<sup>5</sup>
The Sum of N over all test case doesn't exceed 10^5For each test case return the modified array.Sample Input:
2
5
1 8 3 3 4
3 5
3
1 2 3
1 3
Sample Output:
1 3 3 4 8
1 2 3
<b>Explanation:</b>
Testcase 1: First, the array has elements less than or equal to 3. Then, elements between 3 and 5. And, finally elements greater than 5. So, one of the possible outputs is 1 3 3 4 8.
Testcase 2: First, the array has elements less than or equal to 1. Then, elements between 1 and 3. And, finally elements greater than 3. So, the output is 1 2 3., I have written this Solution Code:
public static ArrayList<Integer> threeWayPartition(ArrayList<Integer> A, int lowVal, int highVal)
{
int n = A.size();
ArrayList<Integer> arr = A;
int start = 0, end = n-1;
for (int i=0; i<=end;)
{
// swapping the element with those at start
// if array element is less than lowVal
if (arr.get(i) < lowVal){
int temp=arr.get(i);
arr.add(i,arr.get(start));
arr.remove(i+1);
arr.add(start,temp);
arr.remove(start+1);
i++;
start++;
}
// swapping the element with those at end
// if array element is greater than highVal
else if (arr.get(i) > highVal){
int temp=arr.get(i);
arr.add(i,arr.get(end));
arr.remove(i+1);
arr.add(end,temp);
arr.remove(end+1);
end--;
}
// else just move ahead
else
i++;
}
return arr;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.
<b>Note:-</b>
In the case of custom input, you will get 1 if your code is correct else get a 0.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>threeWayPartition()</b> which contains following arguments.
A: input array list
low: starting integer of range
high: ending integer of range
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10<sup>4</sup>
1 <= A[i] <= 10<sup>5</sup>
1 <= low <= high <= 10<sup>5</sup>
The Sum of N over all test case doesn't exceed 10^5For each test case return the modified array.Sample Input:
2
5
1 8 3 3 4
3 5
3
1 2 3
1 3
Sample Output:
1 3 3 4 8
1 2 3
<b>Explanation:</b>
Testcase 1: First, the array has elements less than or equal to 3. Then, elements between 3 and 5. And, finally elements greater than 5. So, one of the possible outputs is 1 3 3 4 8.
Testcase 2: First, the array has elements less than or equal to 1. Then, elements between 1 and 3. And, finally elements greater than 3. So, the output is 1 2 3., I have written this Solution Code: def threewayPartition(arr,low,high):
i=low-1
pivot = arr[high]
for j in range(low,high):
if arr[j]<=pivot:
i+=1
arr[i],arr[j]=arr[j],arr[i]
i+=1
arr[i],arr[high]=arr[high],arr[i]
return i
def quickSort(arr,low,high):
if low<high:
key=threewayPartition(arr,low,high)
quickSort(arr,low,key-1)
quickSort(arr,key+1,high)
return arr
t= int(input())
while t>0:
s = int(input())
arr = list(map(int,input().split()))
a,b = list(map(int,input().split()))
print(1)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono and Solo are one of the best programmers on the planet. Solo knows how to solve the problem on maximum subarray sum using the Kadane algorithm (don't you?). Now, Tono gives Solo a trickier problem to solve. In this problem, Solo can choose any non- empty subarray, the value of the subarray will be the <b>(sum of all the elements of the subarray - largest element of the subarray)</b>. Can you find the maximum value of any subarray?The first line of the input contains a single integer N, the size of the array.
The second line of the input contains N elements A[1], A[2],. , A[N].
Constraints
1 <= N <= 100000
-30 <= A[i] <= 30Output a single integer, the value of the subarray having maximum value.Sample Input
7
5 2 5 3 -30 -30 6
Sample Output
10
Explanation: Solo chooses the subarray [5, 2, 5, 3]. The value of the subarray is 10.
Sample Input
3
-10 6 -15
Sample Output
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 size = Integer.parseInt(br.readLine());
String line[] = br.readLine().split(" ");
long a[] = new long[size + 1];
for(int i = 1 ; i <= size; i++)
a[i] = Long.parseLong(line[i - 1]);
long value1 = maxofsubarray(a,size);
long value2 = maxofsubarrayBreakingatNegativeVal(a, size);
System.out.print(Math.max(value1, value2));
}
static long maxofsubarray(long[] a , int size)
{
long currsum = 0 , maxsum = 0, maxEl = 0;
for(int i = 1 ; i <= size ; i++)
{
currsum += a[i];
if(currsum < 0) currsum =0;
if(currsum > maxsum)
{
maxsum = currsum; maxEl = Math.max(maxEl, a[i]);
}
} return (maxsum - maxEl);
}
static long maxofsubarrayBreakingatNegativeVal(long[] a , int size)
{
long currsum = 0 , maxsum = 0, maxEl = 0, maxofall = 0;
for(int i = 1 ; i <= size ; i++)
{
maxEl = Math.max(maxEl, a[i]);
currsum += a[i];
if(currsum - maxEl < 0)
{
currsum =0; maxEl = 0;
}
if(currsum - maxEl > maxsum)
{
maxofall = currsum - maxEl; maxsum = maxofall;
}
}
return (maxofall);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono and Solo are one of the best programmers on the planet. Solo knows how to solve the problem on maximum subarray sum using the Kadane algorithm (don't you?). Now, Tono gives Solo a trickier problem to solve. In this problem, Solo can choose any non- empty subarray, the value of the subarray will be the <b>(sum of all the elements of the subarray - largest element of the subarray)</b>. Can you find the maximum value of any subarray?The first line of the input contains a single integer N, the size of the array.
The second line of the input contains N elements A[1], A[2],. , A[N].
Constraints
1 <= N <= 100000
-30 <= A[i] <= 30Output a single integer, the value of the subarray having maximum value.Sample Input
7
5 2 5 3 -30 -30 6
Sample Output
10
Explanation: Solo chooses the subarray [5, 2, 5, 3]. The value of the subarray is 10.
Sample Input
3
-10 6 -15
Sample Output
0, I have written this Solution Code: def maxSumTill(arr,n):
currSum = 0
maxSum = 0
maxEle = 0
maxi = 0
for i in range(1,n+1):
maxEle = max(maxEle,arr[i])
currSum += arr[i]
if currSum-maxEle < 0:
currSum = 0
maxEle =0
if currSum-maxEle > maxSum:
maxi = currSum - maxEle
maxSum = maxi
return maxi
def maxOfSum(arr,n):
currSum = 0
maxSum = 0
maxEle = 0
for i in range(1,n+1):
currSum += arr[i]
if currSum < 0:
currSum = 0
if currSum > maxSum:
maxSum = currSum
maxEle = max(maxEle, arr[i])
return maxSum - maxEle
n = int(input())
arr = list(map(int,input().split()))
arr = [0]+arr
val1 = maxSumTill(arr,n)
val2 = maxOfSum(arr,n)
print(max(val1,val2))
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono and Solo are one of the best programmers on the planet. Solo knows how to solve the problem on maximum subarray sum using the Kadane algorithm (don't you?). Now, Tono gives Solo a trickier problem to solve. In this problem, Solo can choose any non- empty subarray, the value of the subarray will be the <b>(sum of all the elements of the subarray - largest element of the subarray)</b>. Can you find the maximum value of any subarray?The first line of the input contains a single integer N, the size of the array.
The second line of the input contains N elements A[1], A[2],. , A[N].
Constraints
1 <= N <= 100000
-30 <= A[i] <= 30Output a single integer, the value of the subarray having maximum value.Sample Input
7
5 2 5 3 -30 -30 6
Sample Output
10
Explanation: Solo chooses the subarray [5, 2, 5, 3]. The value of the subarray is 10.
Sample Input
3
-10 6 -15
Sample Output
0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 1e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
int arr[N];
int n;
int kadane(){
int mx = 0;
int cur = 0;
For(i, 0, n){
cur += arr[i];
mx = max(mx, cur);
if(cur < 0)
cur = 0;
}
return mx;
}
void play(int x){
For(i, 0, n){
if(arr[i]==x){
arr[i]=-1000000000;
}
}
}
void solve(){
cin>>n;
For(i, 0, n){
cin>>arr[i];
assert(arr[i]>=-30 && arr[i]<=30);
}
int ans = 0;
for(int i=30; i>=1; i--){
ans = max(ans, kadane()-i);
play(i);
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have a box of paint. The paint can be used to cover area of 1 square unit. You have N magic spells with magic value from 1 to N. You can use each spell at max once.
When you use a spell on the box of paint, the quantity of paint in the box gets multiplied by the magic value of that spell. For example, if currently the paint in the box can cover A square units and a spell of B magic value is cast on the box, then after the spell the paint in the box can be used to cover A*B square units of area.
You want to paint a square (completely filled) of maximum area with integer side length with the paint such that after painting the square, the paint in the box gets empty. You can apply any magic spells you want in any order before the painting starts and not during or after the painting.
Find the area of maximum side square you can paint. As the answer can be huge find the area modulo 1000000007.The first and the only line of input contains a single integer N.
Constraints:
1 <= N <= 1000000Print the area of maximum side square you can paint modulo 1000000007.Sample Input 1
3
Sample Output 1
1
Explanation: We apply no magic spell.
Sample Input 2
4
Sample Output 2
4
Explanation: We apply magic spell number 4., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
int si[1000001];
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int ans=1;
int mo=1000000007;
for(int i=2;i<=n;++i){
if(si[i]==0){
int x=i;
int c=0;
while(x<=n){
c+=n/x;
x*=i;
}
if(c%2==0){
ans=(ans*i)%mo;
}
int j=i;
while(j<=n){
si[j]=1;
j+=i;
}
}
else{
ans=(ans*i)%mo;
}
}
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: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code: def DiceProblem(N):
return (7-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
static int diceProblem(int N){
return (7-N);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For an integer N, your task is to calculate sum of first N natural numbers.<b>User Task:</b>
Since this will be a functional problem, you don't have to worry about input. You just have to complete the function <b>sum()</b> which takes the integer N as a parameter.
Constraints:
1 <= N < = 100000000Print the sum of first N natural numbers.Sample Input:-
5
Sample Output:-
15
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code:
static void sum(int N){
long x=N;
x=x*(x+1);
x=x/2;
System.out.print(x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: void magicTrick(int a, int b){
cout<<a+b/2;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: static void magicTrick(int a, int b){
System.out.println(a + b/2);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: A,B = map(int,input().split(' '))
C = A+B//2
print(C)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer <b>N</b>, you need to typecast this integer to String. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>checkConvertion()</b>, which contains N as a parameter.You need to return the typecasted string value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
5
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: def checkConevrtion(a):
return str(a)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer <b>N</b>, you need to typecast this integer to String. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>checkConvertion()</b>, which contains N as a parameter.You need to return the typecasted string value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
5
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: static String checkConevrtion(int a)
{
return String.valueOf(a);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long subarraysDivByK(int[] A, int k)
{
long ans =0 ;
int rem;
int[] freq = new int[k];
for(int i=0;i<A.length;i++)
{
rem = A[i]%k;
ans += freq[(k - rem)% k] ;
freq[rem]++;
}
return ans;
}
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int [] a = new int [n];
for(int i=0; i<n; i++)
a[i] = Integer.parseInt(input[i]);
System.out.println(subarraysDivByK(a, k));
}
}, 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 and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code: def countKdivPairs(A, n, K):
freq = [0] * K
for i in range(n):
freq[A[i] % K]+= 1
sum = freq[0] * (freq[0] - 1) / 2;
i = 1
while(i <= K//2 and i != (K - i) ):
sum += freq[i] * freq[K-i]
i+= 1
if( K % 2 == 0 ):
sum += (freq[K//2] * (freq[K//2]-1)/2);
return int(sum)
a,b=input().split()
a=int(a)
b=int(b)
arr=input().split()
for i in range(0,a):
arr[i]=int(arr[i])
print (countKdivPairs(arr,a, b)), 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 and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
fast();
int n;
cin>>n;
int a;
int k;
cin>>k;
int fre[k];
FOR(i,k){
fre[i]=0;}
FOR(i,n){
cin>>a;
fre[a%k]++;
}
int ans=(fre[0]*(fre[0]-1))/2;
for(int i=1;i<=(k-1)/2;i++){
ans+=fre[i]*fre[k-i];
}
if(k%2==0){
ans+=(fre[k/2]*(fre[k/2]-1))/2;
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader sc= new BufferedReader(new InputStreamReader(System.in));
int a=0, b=0, c=0, d=0;
long z=0;
String[] str;
str = sc.readLine().split(" ");
a= Integer.parseInt(str[0]);
b= Integer.parseInt(str[1]);
c= Integer.parseInt(str[2]);
d= Integer.parseInt(str[3]);
BigInteger m = new BigInteger("1000000007");
BigInteger n = new BigInteger("1000000006");
BigInteger zero = new BigInteger("0");
BigInteger ans, y;
if(d==0){
z =1;
}else{
z = (long)Math.pow(c, d);
}
if(b==0){
y= zero;
}else{
y = (BigInteger.valueOf(b)).modPow((BigInteger.valueOf(z)), n);
}
if(y == zero){
System.out.println("1");
}else if(a==0){
System.out.println("0");
}else{
ans = (BigInteger.valueOf(a)).modPow(y, m);
System.out.println(ans);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, 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: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
int powmod(int a, int b, int c = MOD){
int ans = 1;
while(b){
if(b&1){
ans = (ans*a)%c;
}
a = (a*a)%c;
b >>= 1;
}
return ans;
}
void solve(){
int a, b, c, d; cin>>a>>b>>c>>d;
int x = pow(c, d);
int y = powmod(b, x, MOD-1);
int ans = powmod(a, y, MOD);
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a postfix expression, your task is to evaluate given expression.
Infix expression: The expression of the form a operator b. When an operator is in-between every pair of operands.
Postfix expression: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.The first line denotes the size of the string(which contains number or operand in form of string) i.e. N.
The next line contains string. The string contains an integer called operand or any of the four operators (+ - * /)
Constraints:-
1 <= n <= 40
1<=number<=500
Output the value of arithmetic expression formed using reverse Polish Notation.Input 1:
5
2 1 + 3 *
Output 1:
9
Explaination 1:
starting from backside:
*: ( )*( )
3: ()*(3)
+: ( () + () )*(3)
1: ( () + (1) )*(3)
2: ( (2) + (1) )*(3)
((2)+(1))*(3) = 9
Input 2:
5
4 13 5 / +
Output 2:
6
Explanation 2:
+: ()+()
/: ()+(() / ())
5: ()+(() / (5))
1: ()+((13) / (5))
4: (4)+((13) / (5))
(4)+((13) / (5)) = 6, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
Stack <Integer> st = new Stack<>();
for(int i=0;i<t;i++){
char ch = str[i].charAt(0);
if(Character.isDigit(ch)){
st.push(Integer.parseInt(str[i]));
}
else{
int str1 = st.pop();
int str2 = st.pop();
switch(ch) {
case '+':
st.push(str2+str1);
break;
case '-':
st.push(str2- str1);
break;
case '/':
st.push(str2/str1);
break;
case '*':
st.push(str2*str1);
break;
}
}
}
System.out.println(st.peek());
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a postfix expression, your task is to evaluate given expression.
Infix expression: The expression of the form a operator b. When an operator is in-between every pair of operands.
Postfix expression: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.The first line denotes the size of the string(which contains number or operand in form of string) i.e. N.
The next line contains string. The string contains an integer called operand or any of the four operators (+ - * /)
Constraints:-
1 <= n <= 40
1<=number<=500
Output the value of arithmetic expression formed using reverse Polish Notation.Input 1:
5
2 1 + 3 *
Output 1:
9
Explaination 1:
starting from backside:
*: ( )*( )
3: ()*(3)
+: ( () + () )*(3)
1: ( () + (1) )*(3)
2: ( (2) + (1) )*(3)
((2)+(1))*(3) = 9
Input 2:
5
4 13 5 / +
Output 2:
6
Explanation 2:
+: ()+()
/: ()+(() / ())
5: ()+(() / (5))
1: ()+((13) / (5))
4: (4)+((13) / (5))
(4)+((13) / (5)) = 6, I have written this Solution Code: stack = []
n = int(input())
exp = [i for i in input().split()]
for i in exp:
try:
stack.append(int(i))
except:
a1 = stack.pop()
a2 = stack.pop()
if i == '+':
stack.append(a1+a2)
if i == '-':
stack.append(a2-a1)
if i == '/':
stack.append(a2//a1)
if i == '*':
stack.append(a1*a2)
print(*stack), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a postfix expression, your task is to evaluate given expression.
Infix expression: The expression of the form a operator b. When an operator is in-between every pair of operands.
Postfix expression: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.The first line denotes the size of the string(which contains number or operand in form of string) i.e. N.
The next line contains string. The string contains an integer called operand or any of the four operators (+ - * /)
Constraints:-
1 <= n <= 40
1<=number<=500
Output the value of arithmetic expression formed using reverse Polish Notation.Input 1:
5
2 1 + 3 *
Output 1:
9
Explaination 1:
starting from backside:
*: ( )*( )
3: ()*(3)
+: ( () + () )*(3)
1: ( () + (1) )*(3)
2: ( (2) + (1) )*(3)
((2)+(1))*(3) = 9
Input 2:
5
4 13 5 / +
Output 2:
6
Explanation 2:
+: ()+()
/: ()+(() / ())
5: ()+(() / (5))
1: ()+((13) / (5))
4: (4)+((13) / (5))
(4)+((13) / (5)) = 6, 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;
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int size = tokens.size();
if (size == 0)
return 0;
std::stack<int> operands;
for (int i = 0; i < size; ++i)
{
std::string cur_token = tokens[i];
if ((cur_token == "*") || (cur_token == "/") || (cur_token == "+") || (cur_token == "-"))
{
int opr2 = operands.top();
operands.pop();
int opr1 = operands.top();
operands.pop();
int result = this->eval(opr1, opr2, cur_token);
operands.push(result);
}
else{
operands.push(std::atoi(cur_token.c_str()));
}
}
return operands.top();
}
int eval(int opr1, int opr2, string opt)
{
if (opt == "*")
{
return opr1 * opr2;
}
else if (opt == "+")
{
return opr1 + opr2;
}
else if (opt == "-")
{
return opr1 - opr2;
}
else if (opt == "/")
{
return opr1 / opr2;
}
return 0;
}
};
signed main() {
IOS;
int n; cin >> n;
vector<string> v(n, "");
for(int i = 0; i < n; i++)
cin >> v[i];
Solution s;
cout << s.evalRPN(v);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Binary Search Tree. The task is to find the <b>minimum element</b> in this given BST. If the tree is empty, there is no minimum element, so print <b>-1</b> in that case.<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>minValue()</b> that takes "root" node as parameter and returns the minimum value(-1 in case tree is empty). The printing is done by the driver code.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^3
1 <= node values <= 10^4
<b>Sum of "N" over all testcases does not exceed 10^5</b>For each testcase you need to return the minimum element in BST.Input:
2
5 4 6 3 N N 7 1
9 N 10 N 11
Output:
1
9
Explanation:
Testcase 1: We construct the following BST by inserting given values one by one in an empty BST.
5
/ \
4 6
/ \
3 7
/
1
The minimum value in the given BST is 1.
Testcase 2: We construct the following BST by inserting given values one by one in an empty BST.
9
\
10
\
11
The minimum value in the given BST is 9., I have written this Solution Code:
static int minValue(Node node)
{
// base case
if(node==null)
return -1;
Node current = node;
// iterate left till node is not null
while (current.left != null)
{
current = current.left;
}
return (current.data);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array A[] of size N. The task is to arrange an array in increasing order.
(O(N) time complexity solution)Every test case contains two lines, first line contains an integer N (size of array) and second line contains space separated elements of array.
1 <= N <= 20
0 <= A[i] <= 1Print a single line containing space separated elements of sorted arrays.Input:
5
1 0 1 1 0
Output:
0 0 1 1 1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void cal(int arr[], int n){
int countZ = 0;
for(int i = 0; i < n; i++) {
if(arr[i] == 0) {
countZ++;
}
}
for(int i = 1; i <= countZ; i++) {
System.out.print("0 ");
}
for(int i = 1; i <= n - countZ; i++) {
System.out.print("1 ");
}
}
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String nD = br.readLine();
String nDArr[] = nD.split(" ");
int n = Integer.parseInt(nDArr[0]);
int arr[]= new int[n];
String input = br.readLine();
String sar[] = input.split(" ");
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(sar[i]);
}
cal(arr, n);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array A[] of size N. The task is to arrange an array in increasing order.
(O(N) time complexity solution)Every test case contains two lines, first line contains an integer N (size of array) and second line contains space separated elements of array.
1 <= N <= 20
0 <= A[i] <= 1Print a single line containing space separated elements of sorted arrays.Input:
5
1 0 1 1 0
Output:
0 0 1 1 1, I have written this Solution Code: n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
for i in l:
print(i, end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array A[] of size N. The task is to arrange an array in increasing order.
(O(N) time complexity solution)Every test case contains two lines, first line contains an integer N (size of array) and second line contains space separated elements of array.
1 <= N <= 20
0 <= A[i] <= 1Print a single line containing space separated elements of sorted arrays.Input:
5
1 0 1 1 0
Output:
0 0 1 1 1, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int a[2] = {0};
for(int i = 1; i <= n; i++){
int p; cin >> p;
a[p]++;
}
for(int i = 0; i <= 1; i++)
for(int j = 0; j < a[i]; j++)
cout << 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, A of N integers. Find the product of maximum values for every subarray of size K. Print the answer modulo 10<sup>9</sup>+7.
A subarray is any contiguous sequence of elements in an array.The first line contains two integers N and K, denoting the size of the array and the size of the subarray respectively.
The next line contains N integers denoting the elements of the array.
<b>Constarints</b>
1 <= K <= N <= 1000000
1 <= A[i] <= 1000000Print a single integer denoting the product of maximums for every subarray of size K modulo 1000000007Sample Input 1:
6 4
1 5 2 3 6 4
Sample Output 1:
180
<b>Explanation:</b>
For subarray [1, 5, 2, 3], maximum = 5
For subarray [5, 2, 3, 6], maximum = 6
For subarray [2, 3, 6, 4], maximum = 6
Therefore, ans = 5*6*6 = 180, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
Reader sc=new Reader();
int n=sc.nextInt();
int b=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int j, max;
long mul=1;
Deque<Integer> dq= new LinkedList<Integer>();
for (int i = 0;i<b;i++)
{
while (!dq.isEmpty() && a[i] >=a[dq.peekLast()])
dq.removeLast();
dq.addLast(i);
}
mul=((mul%1000000007)*(a[dq.peek()]%1000000007))%1000000007;
for (int i=b; i < n;i++)
{
while ((!dq.isEmpty()) && dq.peek() <=i-b)
dq.removeFirst();
while ((!dq.isEmpty()) && a[i]>=a[dq.peekLast()])
dq.removeLast();
dq.addLast(i);
mul=((mul%1000000007)*(a[dq.peek()]%1000000007))%1000000007;
}
System.out.println(mul%1000000007);
}
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();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of N integers. Find the product of maximum values for every subarray of size K. Print the answer modulo 10<sup>9</sup>+7.
A subarray is any contiguous sequence of elements in an array.The first line contains two integers N and K, denoting the size of the array and the size of the subarray respectively.
The next line contains N integers denoting the elements of the array.
<b>Constarints</b>
1 <= K <= N <= 1000000
1 <= A[i] <= 1000000Print a single integer denoting the product of maximums for every subarray of size K modulo 1000000007Sample Input 1:
6 4
1 5 2 3 6 4
Sample Output 1:
180
<b>Explanation:</b>
For subarray [1, 5, 2, 3], maximum = 5
For subarray [5, 2, 3, 6], maximum = 6
For subarray [2, 3, 6, 4], maximum = 6
Therefore, ans = 5*6*6 = 180, I have written this Solution Code: from collections import deque
deq=deque()
n,k=list(map(int,input().split()))
array=list(map(int,input().split()))
for i in range(k):
while(deq and array[deq[-1]]<=array[i]):
deq.pop()
deq.append(i)
ans=1
for j in range(k,n):
ans=ans*array[deq[0]]
ans=(ans)%1000000007
while(deq and deq[0]<=j-k):
deq.popleft()
while(deq and array[deq[-1]]<=array[j]):
deq.pop()
deq.append(j)
ans=ans*array[deq[0]]
ans=(ans)%1000000007
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of N integers. Find the product of maximum values for every subarray of size K. Print the answer modulo 10<sup>9</sup>+7.
A subarray is any contiguous sequence of elements in an array.The first line contains two integers N and K, denoting the size of the array and the size of the subarray respectively.
The next line contains N integers denoting the elements of the array.
<b>Constarints</b>
1 <= K <= N <= 1000000
1 <= A[i] <= 1000000Print a single integer denoting the product of maximums for every subarray of size K modulo 1000000007Sample Input 1:
6 4
1 5 2 3 6 4
Sample Output 1:
180
<b>Explanation:</b>
For subarray [1, 5, 2, 3], maximum = 5
For subarray [5, 2, 3, 6], maximum = 6
For subarray [2, 3, 6, 4], maximum = 6
Therefore, ans = 5*6*6 = 180, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
void solve(){
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
deque<int> q;
for(int i = 1; i <= k; i++){
while(!q.empty() && a[q.back()] <= a[i])
q.pop_back();
q.push_back(i);
}
int ans = a[q.front()];
for(int i = k+1; i <= n; i++){
if(q.front() == i-k)
q.pop_front();
while(!q.empty() && a[q.back()] <= a[i])
q.pop_back();
q.push_back(i);
ans = (ans*a[q.front()]) % mod;
}
cout << ans;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram is learning chess for the first time, So his father first starts his training with the piece <b>Knight</b>. His father gave Ram 2 random points ((x, y) and (a, b)), ie position of any piece on a 8x8 chess board and then asks Ram can he put the knight on the board so that both of the pieces get attacked at the same time? If yes then print "YES" we can otherwise "NO".The first line of the input contains a space separated integer T.
The first line of each test case contains two space separated integer x, y.
The second line of each test case contains two space separated integer a, b.
<b>Constraints</b>
1 ≤ T ≤ 5000
1 ≤ x, y ≤ 8
1 ≤ a, b ≤ 8
Both the cells are distinctPrint "YES" if the fork exists otherwise print "NO".Sample Input
3
8 1
7 3
3 7
5 2
3 5
2 2
Sample Output
NO
NO
YES
<b>Explanation</b>
For test cases 1 and 2 no such point exists, but for test case 3 (1, 4) is the point that can cause a fork., I have written this Solution Code: import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.io.PrintStream;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TheAttackOfKnight solver = new TheAttackOfKnight();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TheAttackOfKnight {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int i, j, k;
int x1 = in.nextInt();
int y1 = in.nextInt();
int x2 = in.nextInt();
int y2 = in.nextInt();
HashSet<String> hs = new HashSet<>();
HashSet<String> hs2 = new HashSet<>();
int a1 = x1, b1 = y1;
int a2 = x2, b2 = y2;
if (a1 + 2 < 9) {
if (b1 + 1 < 9) hs.add((a1 + 2) + " " + (b1 + 1));
if (b1 - 1 > 0) hs.add((a1 + 2) + " " + (b1 - 1));
}
if (a1 + 1 < 9) {
if (b1 + 2 < 9) hs.add((a1 + 1) + " " + (b1 + 2));
if (b1 - 2 > 0) hs.add((a1 + 1) + " " + (b1 - 2));
}
if (a1 - 2 > 0) {
if (b1 + 1 < 9) hs.add((a1 - 2) + " " + (b1 + 1));
if (b1 - 1 > 0) hs.add((a1 - 2) + " " + (b1 - 1));
}
if (a1 - 1 > 0) {
if (b1 + 2 < 9) hs.add((a1 - 1) + " " + (b1 + 2));
if (b1 - 2 > 0) hs.add((a1 - 1) + " " + (b1 - 2));
}
boolean b = false;
if (a2 + 2 < 9) {
if (b2 + 1 < 9) {
if (hs.contains((a2 + 2) + " " + (b2 + 1))) b = true;
}
if (b2 - 1 > 0) {
if (hs.contains((a2 + 2) + " " + (b2 - 1))) b = true;
}
}
if (a2 + 1 < 9) {
if (b2 + 2 < 9) {
if (hs.contains((a2 + 1) + " " + (b2 + 2))) b = true;
}
if (b2 - 2 > 0) {
if (hs.contains((a2 + 1) + " " + (b2 - 2))) b = true;
}
}
if (a2 - 2 > 0) {
if (b2 + 1 < 9) {
if (hs.contains((a2 - 2) + " " + (b2 + 1))) b = true;
}
if (b2 - 1 > 0) {
if (hs.contains((a2 - 2) + " " + (b2 - 1))) b = true;
}
}
if (a2 - 1 > 0) {
if (b2 + 2 < 9) {
if (hs.contains((a2 - 1) + " " + (b2 + 2))) b = true;
}
if (b2 - 2 > 0) {
if (hs.contains((a2 - 1) + " " + (b2 - 2))) b = true;
}
}
if (b) out.println("YES");
else out.println("NO");
}
}
static class FastReader {
static final int BUFSIZE = 1 << 20;
static byte[] buf;
static int index;
static int total;
static InputStream in;
public FastReader(InputStream is) {
try {
in = is;
buf = new byte[BUFSIZE];
} catch (Exception e) {
}
}
private int scan() {
try {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
} catch (Exception | Error e) {
System.err.println(e.getMessage());
return 13 / 0;
}
}
public String next() {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan())
sb.append((char) c);
return sb.toString();
}
public int nextInt() {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram is learning chess for the first time, So his father first starts his training with the piece <b>Knight</b>. His father gave Ram 2 random points ((x, y) and (a, b)), ie position of any piece on a 8x8 chess board and then asks Ram can he put the knight on the board so that both of the pieces get attacked at the same time? If yes then print "YES" we can otherwise "NO".The first line of the input contains a space separated integer T.
The first line of each test case contains two space separated integer x, y.
The second line of each test case contains two space separated integer a, b.
<b>Constraints</b>
1 ≤ T ≤ 5000
1 ≤ x, y ≤ 8
1 ≤ a, b ≤ 8
Both the cells are distinctPrint "YES" if the fork exists otherwise print "NO".Sample Input
3
8 1
7 3
3 7
5 2
3 5
2 2
Sample Output
NO
NO
YES
<b>Explanation</b>
For test cases 1 and 2 no such point exists, but for test case 3 (1, 4) is the point that can cause a fork., I have written this Solution Code: def chess(x,y):
pos1=((x+2),(y+1))
pos2=((x+2),(y-1))
pos3=((x-2),(y+1))
pos4=((x-2),(y-1))
pos5=((x+1),(y+2))
pos6=((x-1),(y+2))
pos7=((x+1),(y-2))
pos8=((x-1),(y-2))
arr=[pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8]
final=[]
for i in range(len(arr)):
if (arr[i][0] > 0 and arr[i][0] < 9 ) and (arr[i][1] > 0 and arr[i][1] < 9) :
final.append(arr[i])
return final
t=int(input())
while t>0 :
x,y = list(map(int,input().split()))
point1 = chess(x,y)
p,q = list(map(int,input().split()))
point2 = chess(p,q)
flag= True
for i in (point1):
for j in (point2):
if i==j :
print("YES")
flag = False
break
if flag == False:
break
else:
print("NO")
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ram is learning chess for the first time, So his father first starts his training with the piece <b>Knight</b>. His father gave Ram 2 random points ((x, y) and (a, b)), ie position of any piece on a 8x8 chess board and then asks Ram can he put the knight on the board so that both of the pieces get attacked at the same time? If yes then print "YES" we can otherwise "NO".The first line of the input contains a space separated integer T.
The first line of each test case contains two space separated integer x, y.
The second line of each test case contains two space separated integer a, b.
<b>Constraints</b>
1 ≤ T ≤ 5000
1 ≤ x, y ≤ 8
1 ≤ a, b ≤ 8
Both the cells are distinctPrint "YES" if the fork exists otherwise print "NO".Sample Input
3
8 1
7 3
3 7
5 2
3 5
2 2
Sample Output
NO
NO
YES
<b>Explanation</b>
For test cases 1 and 2 no such point exists, but for test case 3 (1, 4) is the point that can cause a fork., I have written this Solution Code: /**
* author: tourist1256
* created: 2022-06-14 14:26:47
**/
#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
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
void solve() {
pair<int, int> a, b;
cin >> a.first >> a.second >> b.first >> b.second;
set<pair<int, int>> pointA, pointB;
auto getPoints = [&](int x, int y) -> set<pair<int, int>> {
set<pair<int, int>> tempPoints;
// Top Left
tempPoints.insert({x - 1, y + 2});
tempPoints.insert({x - 2, y + 1});
// Top Right
tempPoints.insert({x + 1, y + 2});
tempPoints.insert({x + 2, y + 1});
// Bottom Left
tempPoints.insert({x - 2, y - 1});
tempPoints.insert({x - 1, y - 2});
// Bottom Right
tempPoints.insert({x + 2, y - 1});
tempPoints.insert({x + 1, y - 2});
return tempPoints;
};
pointA = getPoints(a.first, a.second);
pointB = getPoints(b.first, b.second);
for (auto &it : pointA) {
if (it.first < 1 or it.first > 8) {
continue;
}
if (it.second < 1 or it.second > 8) {
continue;
}
if (pointB.find(it) != pointB.end()) {
debug(it.first, it.second);
cout << "YES" << endl;
return;
}
}
cout << "NO\n";
}
int32_t main() {
int tt;
cin >> tt;
while (tt--) {
solve();
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a saying in Sara's village that "A number is called prime if the sum of all the factors of N is less than or equal to (3*N)/2 ". Given the number N, your task is to check if it is a myth or a fact for the number given.<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>mythOrFact()</b> that takes integer N as argument.
Constraints:-
2 <= N <= 1000Return 1 if it is a fact else return 0.Sample Input:-
2
Sample Output:-
1
Explanation:-
Sum = 2 + 1 = 3
3*(2) / 2 = 3
Sample Input:-
9
Sample Output:-
0, I have written this Solution Code:
int mythOrFact(int N){
int prime =1;
int cnt = N+1;
for(int i=2;i<N;i++){
if(N%i==0){cnt+=i;prime=0;}
}
int p = 3*N;
p/=2;
if((cnt<=p && prime==1) || (cnt>p && prime==0) ){return 1;}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a saying in Sara's village that "A number is called prime if the sum of all the factors of N is less than or equal to (3*N)/2 ". Given the number N, your task is to check if it is a myth or a fact for the number given.<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>mythOrFact()</b> that takes integer N as argument.
Constraints:-
2 <= N <= 1000Return 1 if it is a fact else return 0.Sample Input:-
2
Sample Output:-
1
Explanation:-
Sum = 2 + 1 = 3
3*(2) / 2 = 3
Sample Input:-
9
Sample Output:-
0, I have written this Solution Code:
int mythOrFact(int N){
int prime =1;
int cnt = N+1;
for(int i=2;i<N;i++){
if(N%i==0){cnt+=i;prime=0;}
}
int p = 3*N;
p/=2;
if((cnt<=p && prime==1) || (cnt>p && prime==0) ){return 1;}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a saying in Sara's village that "A number is called prime if the sum of all the factors of N is less than or equal to (3*N)/2 ". Given the number N, your task is to check if it is a myth or a fact for the number given.<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>mythOrFact()</b> that takes integer N as argument.
Constraints:-
2 <= N <= 1000Return 1 if it is a fact else return 0.Sample Input:-
2
Sample Output:-
1
Explanation:-
Sum = 2 + 1 = 3
3*(2) / 2 = 3
Sample Input:-
9
Sample Output:-
0, I have written this Solution Code: static int mythOrFact(int N){
int prime =1;
int cnt = N+1;
for(int i=2;i<N;i++){
if(N%i==0){cnt+=i;prime=0;}
}
int p = 3*N;
p/=2;
if((cnt<=p && prime==1) || (cnt>p && prime==0) ){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a saying in Sara's village that "A number is called prime if the sum of all the factors of N is less than or equal to (3*N)/2 ". Given the number N, your task is to check if it is a myth or a fact for the number given.<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>mythOrFact()</b> that takes integer N as argument.
Constraints:-
2 <= N <= 1000Return 1 if it is a fact else return 0.Sample Input:-
2
Sample Output:-
1
Explanation:-
Sum = 2 + 1 = 3
3*(2) / 2 = 3
Sample Input:-
9
Sample Output:-
0, I have written this Solution Code: def mythOrFact(N):
prime=1
cnt=N+1
for i in range (2,N):
if N%i==0:
prime=0
cnt=cnt+i
x = 3*N
x=x//2
if(cnt <= x and prime==1) or (cnt>x and prime==0):
return 1
return 0
, 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: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int t=Integer.parseInt(str[1]);
int[] arr=new int[n];
str=br.readLine().split(" ");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
int sum=0;
for(int i=1;i<n;i++){
int dif=arr[i]-arr[i-1];
if(dif>t){
sum=sum+t;
}else{
sum=sum+dif;
}
}
sum+=t;
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, I have written this Solution Code: n , t = [int(x) for x in input().split() ]
l= [int(x) for x in input().split() ]
c = 0
for i in range(len(l)-1):
if l[i+1] - l[i]<=t:
c+=l[i+1] - l[i]
else:
c+=t
c+=t
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
long n,t;
cin>>n>>t;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long cur=t;
long ans=t;
for(int i=1;i<n;i++){
ans+=min(a[i]-a[i-1],t);
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a 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: Create a new class for a bank account (BankAccount)
Create fields for the balance(balance), customer name(name).
Create constructor with two parameter as balance and name
Create two additional methods
1. To allow the customer to deposit funds named depositFund() with one parameter as fund to be added (this should increment the balance field).
2. To allow the customer to withdraw funds named withdrawFund() with one parameter as fund to be withdrawn from account and should return boolean as if withdraw went successful else false. This should deduct from the balance field, but not allow
the withdrawal to complete if their are insufficient funds.
Note: Each methods and variable should be publicYou don't have to take any input, You only have to write class <b>BankAccount</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
class BankAccount{
// if your code works fine for the testert
}
Sample Output:
Correct, I have written this Solution Code: class BankAccount{
public int balance;
public String name;
BankAccount(int _balance,String _name){
balance=_balance;
name =_name;
}
public void depositFund(int fund){
balance += fund;
}
public boolean withdrawFund(int fund){
if(fund <= balance){
balance -= fund;
return true;
}
return false;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Create a new class for a bank account (BankAccount)
Create fields for the balance(balance), customer name(name).
Create constructor with two parameter as balance and name
Create two additional methods
1. To allow the customer to deposit funds named depositFund() with one parameter as fund to be added (this should increment the balance field).
2. To allow the customer to withdraw funds named withdrawFund() with one parameter as fund to be withdrawn from account and should return boolean as if withdraw went successful else false. This should deduct from the balance field, but not allow
the withdrawal to complete if their are insufficient funds.
Note: Each methods and variable should be publicYou don't have to take any input, You only have to write class <b>BankAccount</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
class BankAccount{
// if your code works fine for the testert
}
Sample Output:
Correct, I have written this Solution Code: class Bank_Account:
def __init__(self,b,n):
self.balance=b
self.name=n
def depositFund(self,b):
self.balance += b
def withdrawFund(self,f):
if self.balance>=f:
self.balance-=f
return True
else:
return False
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You're given a string S of lowercase letters of the english alphabet. Find whether you can choose some characters of the string S in any order to create the string "red".The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100
All the characters in S are lowercase letters of the english alphabet.Output "Yes" (without quotes) if you can create the string "red", else output "No" (without quotes).Sample Input
damngrey
Sample Output
Yes
Explanation: We choose character at position 6, then position 7, then position 1.
Sample Input
newtonschool
Sample Output
No, 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();
boolean r=false,e=false,d=false;
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if(ch=='r')r=true;
if(ch=='e')e=true;
if(ch=='d')d=true;
}
String ans = (r && e && d)?"Yes":"No";
System.out.print(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You're given a string S of lowercase letters of the english alphabet. Find whether you can choose some characters of the string S in any order to create the string "red".The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100
All the characters in S are lowercase letters of the english alphabet.Output "Yes" (without quotes) if you can create the string "red", else output "No" (without quotes).Sample Input
damngrey
Sample Output
Yes
Explanation: We choose character at position 6, then position 7, then position 1.
Sample Input
newtonschool
Sample Output
No, I have written this Solution Code: S = input()
if ("r" in S and "e" in S and "d" in S ) :
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You're given a string S of lowercase letters of the english alphabet. Find whether you can choose some characters of the string S in any order to create the string "red".The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100
All the characters in S are lowercase letters of the english alphabet.Output "Yes" (without quotes) if you can create the string "red", else output "No" (without quotes).Sample Input
damngrey
Sample Output
Yes
Explanation: We choose character at position 6, then position 7, then position 1.
Sample Input
newtonschool
Sample Output
No, 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(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
string s; cin>>s;
map<char, int> m;
For(i, 0, sz(s)){
m[s[i]]++;
}
if(m['r'] && m['e'] && m['d']){
cout<<"Yes";
}
else{
cout<<"No";
}
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, 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));
br.readLine();
String[] line = br.readLine().split(" ");
int happyBalloons = 0;
for(int i=1;i<=line.length;++i){
int num = Integer.parseInt(line[i-1]);
if(num%2 == i%2 ){
happyBalloons++;
}
}
System.out.println(happyBalloons);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, I have written this Solution Code: x=int(input())
arr=input().split()
for i in range(0,x):
arr[i]=int(arr[i])
count=0
for i in range(0,x):
if(arr[i]%2==0 and (i+1)%2==0):
count+=1
elif (arr[i]%2!=0 and (i+1)%2!=0):
count+=1
print (count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
int ans = 0;
For(i, 1, n+1){
int a; cin>>a;
if(i%2 == a%2)
ans++;
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let S(n) denote the sum of the digits in the decimal notation of n. For example, S(101)=1+0+1=2.
Given an integer N, determine if S(N) divides N.The input consists of a single integer.
N
<b>Constraints</b>
1≤N≤10^9If S(N) divides N, print Yes; if it does not, print No.<b>Sample Input 1</b>
12
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
101
<b>Sample Output 2</b>
No
<b>Sample Input 3</b>
999999999
<b>Sample Output 3</b>
Yes, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int x = n;
int ans = 0;
while(x){
ans += x%10;
x /= 10;
}
if(n % ans == 0)
cout<<"Yes";
else
cout<<"No";
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: Ronaldo has challenged Messi to beat his team in the upcoming “Friendly” fixture. But both these legends are tired of the original football rules, so they decided to play Football with a twist. Each player is assigned an ID, from 1 to 10^6. Initially Ronaldo has the ball in his posession. Given a sequence of N passes, help Messi Know which ID player has the ball, after N passes.
Note: Passes are of two types :
i) P ID, which means the player currently having the ball passes it to Player with identity ID.
ii) B, which means the player currently having the ball passes it back to the Player he got the Pass From.
Also, It is guaranteed that the given order of passes will be a valid order.The first line of input contains number of testcases T. The 1st line of each testcase, contains two integers, N and ID1, denoting the total number of passes and the ID of Ronaldo. Respectively. Each of the next N lines, contains the information of a Pass which can be either of the above two types, i.e:
1) P ID
2) B
Constraints
1 <= T <= 100
1 <= N <= 10^5
1 <= IDs <= 10^6
Sum of N for every test case is less than 10^6For each testcase you need to print the ID of the Player having the ball after N passes.Input :
2
3 1
P 13
P 14
B
5 1
P 12
P 13
P 14
B
B
Output :
13
14
Explanation :
Testcase 1: Initially, the ball is with Ronaldo, having ID 1.
In the first pass, he passes the ball to Player with ID 13.
In the Second Pass, the player currently having the ball, ie Player with ID 13, passes it to player with ID 14.
In the last pass the player currently having the ball, ie Player with ID 14 passed the ball back to the player from whom he got the Pass, i.e the ball is passed back to Player with Player ID 13, as Player ID 13 had passed the ball to player ID 14 in the previous pass.
Testcase 2: Initially, the ball is with Ronaldo, having ID 1.
In the first pass he passes the ball to player with ID 12.
In the second pass, the second player passes the ball to palyer with ID 13, again the player with ID 13 passes the ball to player with ID 14.
Now, player with ID 14 passed back the ball to 13, and again 13 passes back the ball to 14., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
String[] line = br.readLine().trim().split(" ");
int n = Integer.parseInt(line[0]);
int curr = Integer.parseInt(line[1]);
int prev = curr;
while (n-- > 0) {
String[] currLine = br.readLine().trim().split(" ");
if (currLine[0].equals("P")) {
prev = curr;
curr = Integer.parseInt(currLine[1]);
}
if (currLine[0].equals("B")) {
int temp = curr;
curr = prev;
prev = temp;
}
}
System.out.println(curr);
System.gc();
}
br.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ronaldo has challenged Messi to beat his team in the upcoming “Friendly” fixture. But both these legends are tired of the original football rules, so they decided to play Football with a twist. Each player is assigned an ID, from 1 to 10^6. Initially Ronaldo has the ball in his posession. Given a sequence of N passes, help Messi Know which ID player has the ball, after N passes.
Note: Passes are of two types :
i) P ID, which means the player currently having the ball passes it to Player with identity ID.
ii) B, which means the player currently having the ball passes it back to the Player he got the Pass From.
Also, It is guaranteed that the given order of passes will be a valid order.The first line of input contains number of testcases T. The 1st line of each testcase, contains two integers, N and ID1, denoting the total number of passes and the ID of Ronaldo. Respectively. Each of the next N lines, contains the information of a Pass which can be either of the above two types, i.e:
1) P ID
2) B
Constraints
1 <= T <= 100
1 <= N <= 10^5
1 <= IDs <= 10^6
Sum of N for every test case is less than 10^6For each testcase you need to print the ID of the Player having the ball after N passes.Input :
2
3 1
P 13
P 14
B
5 1
P 12
P 13
P 14
B
B
Output :
13
14
Explanation :
Testcase 1: Initially, the ball is with Ronaldo, having ID 1.
In the first pass, he passes the ball to Player with ID 13.
In the Second Pass, the player currently having the ball, ie Player with ID 13, passes it to player with ID 14.
In the last pass the player currently having the ball, ie Player with ID 14 passed the ball back to the player from whom he got the Pass, i.e the ball is passed back to Player with Player ID 13, as Player ID 13 had passed the ball to player ID 14 in the previous pass.
Testcase 2: Initially, the ball is with Ronaldo, having ID 1.
In the first pass he passes the ball to player with ID 12.
In the second pass, the second player passes the ball to palyer with ID 13, again the player with ID 13 passes the ball to player with ID 14.
Now, player with ID 14 passed back the ball to 13, and again 13 passes back the ball to 14., I have written this Solution Code: for i in range(int(input())):
N, ID = map(int,input().split())
pre = 0
for i in range(N):
arr = input().split()
if len(arr)==2:
pre,ID = ID,arr[1]
else:
ID,pre = pre,ID
print(ID) if pre else print(0), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Ronaldo has challenged Messi to beat his team in the upcoming “Friendly” fixture. But both these legends are tired of the original football rules, so they decided to play Football with a twist. Each player is assigned an ID, from 1 to 10^6. Initially Ronaldo has the ball in his posession. Given a sequence of N passes, help Messi Know which ID player has the ball, after N passes.
Note: Passes are of two types :
i) P ID, which means the player currently having the ball passes it to Player with identity ID.
ii) B, which means the player currently having the ball passes it back to the Player he got the Pass From.
Also, It is guaranteed that the given order of passes will be a valid order.The first line of input contains number of testcases T. The 1st line of each testcase, contains two integers, N and ID1, denoting the total number of passes and the ID of Ronaldo. Respectively. Each of the next N lines, contains the information of a Pass which can be either of the above two types, i.e:
1) P ID
2) B
Constraints
1 <= T <= 100
1 <= N <= 10^5
1 <= IDs <= 10^6
Sum of N for every test case is less than 10^6For each testcase you need to print the ID of the Player having the ball after N passes.Input :
2
3 1
P 13
P 14
B
5 1
P 12
P 13
P 14
B
B
Output :
13
14
Explanation :
Testcase 1: Initially, the ball is with Ronaldo, having ID 1.
In the first pass, he passes the ball to Player with ID 13.
In the Second Pass, the player currently having the ball, ie Player with ID 13, passes it to player with ID 14.
In the last pass the player currently having the ball, ie Player with ID 14 passed the ball back to the player from whom he got the Pass, i.e the ball is passed back to Player with Player ID 13, as Player ID 13 had passed the ball to player ID 14 in the previous pass.
Testcase 2: Initially, the ball is with Ronaldo, having ID 1.
In the first pass he passes the ball to player with ID 12.
In the second pass, the second player passes the ball to palyer with ID 13, again the player with ID 13 passes the ball to player with ID 14.
Now, player with ID 14 passed back the ball to 13, and again 13 passes back the ball to 14., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n, id; cin >> n >> id;
int pre = 0, cur = id;
for(int i = 1; i <= n; i++){
char c; cin >> c;
if(c == 'P'){
int x; cin >> x;
pre = cur;
cur = x;
}
else{
swap(pre, cur);
}
}
cout << cur << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a pair product array pairProd[] containing positive integers of size K. Your task is to find the original array from given product array. A pair-product array for an array is the array that contains product of all pairs in ordered form.
For example: The product array for {a,b,c} will be {a*b, a*c, b*c}The input line contains T, denoting the number of testcases. Each testcase contains two lines. The first line contains K size of pair product array and second line contains elements of pair product array i.e. pairProd[i] separated by space.
Constraints:
1 <= T <= 100
3 <= K <= 10000
1 <= pairProd[i] <= 10000For each testcase print the original array separated by space in new line.Sample Input:
2
6
6 8 16 12 24 32
3
3 11 33
Sample Output:
2 3 4 8
1 3 11
Explanation:
Testcase 2: Elements of product array from the original array will be: 1*3, 1*11, 3*11., I have written this Solution Code: import math
def constructArr(pair, n) :
size = int((1 + math.sqrt(1 + 8 * n)) // 2);
arr = [0] * (size);
arr[0] = int(math.sqrt((pair[0] * pair[1]) /pair[size - 1]));
for i in range(1, size) :
arr[i] = pair[i - 1] // arr[0];
# printArr(arr, size);
for i in range(size) :
print(arr[i], end = " ");
print()
test=int(input())
for i in range(test):
n=int(input())
pair=list(map(int,input().split()))
constructArr(pair,n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a pair product array pairProd[] containing positive integers of size K. Your task is to find the original array from given product array. A pair-product array for an array is the array that contains product of all pairs in ordered form.
For example: The product array for {a,b,c} will be {a*b, a*c, b*c}The input line contains T, denoting the number of testcases. Each testcase contains two lines. The first line contains K size of pair product array and second line contains elements of pair product array i.e. pairProd[i] separated by space.
Constraints:
1 <= T <= 100
3 <= K <= 10000
1 <= pairProd[i] <= 10000For each testcase print the original array separated by space in new line.Sample Input:
2
6
6 8 16 12 24 32
3
3 11 33
Sample Output:
2 3 4 8
1 3 11
Explanation:
Testcase 2: Elements of product array from the original array will be: 1*3, 1*11, 3*11., I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static void extractOriginal(int a[],int b[],int n2)
{
b[0] =(int)Math.sqrt((a[0]*a[1])/a[n2-1]);
for(int i=1;i<n2;i++)
{
b[i]=a[i-1]/b[0];
}
}
public static void main (String[] args)
{
//code
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
for(int j=0;j<t;j++)
{
int n1=scan.nextInt();
int a[]=new int[n1];
for(int i=0;i<n1;i++)
{
a[i]=scan.nextInt();
}
int n2=(int)(1+Math.sqrt(1-4*1*(-2*n1)))/2;
int b[]=new int[n2];
extractOriginal(a,b,n2);
for(int k=0;k<n2;k++)
{
System.out.print(b[k]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a pair product array pairProd[] containing positive integers of size K. Your task is to find the original array from given product array. A pair-product array for an array is the array that contains product of all pairs in ordered form.
For example: The product array for {a,b,c} will be {a*b, a*c, b*c}The input line contains T, denoting the number of testcases. Each testcase contains two lines. The first line contains K size of pair product array and second line contains elements of pair product array i.e. pairProd[i] separated by space.
Constraints:
1 <= T <= 100
3 <= K <= 10000
1 <= pairProd[i] <= 10000For each testcase print the original array separated by space in new line.Sample Input:
2
6
6 8 16 12 24 32
3
3 11 33
Sample Output:
2 3 4 8
1 3 11
Explanation:
Testcase 2: Elements of product array from the original array will be: 1*3, 1*11, 3*11., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int n2=(1+sqrt(1+8*(n)))/2;
int x=sqrt((a[0]*a[1])/a[n2-1]);
cout<<x<<" ";
for(int i=1;i<n2;i++){
cout<<a[i-1]/x<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two integer N and M. Find the number of non- negative integers X such that N - M >= X.The first line of the input contains two integers N and M.
Constraints:
1 <= N, M <= 10<sup>9</sup>Print the number of non- negative integer values X can take.Sample Input:
6 1
Sample Output:
6, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n, m;
cin >> n >> m;
if(n < m){
cout << 0;
}
else cout << n - m + 1;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p > 0)
sum += p;
}
cout << sum;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: n=int(input())
li = list(map(int,input().strip().split()))
sum=0
for i in li:
if i>0:
sum+=i
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long arr[] = new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
if(arr[i]>0){
sum+=arr[i];
}
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array of size N. Count number of 1's and 0's in the array.First line contains n.
Next line contains n space separated integers.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
0 <= arr[i] <= 1Print two integers, count of 1s and count of 0s.Input:
5
0 1 1 0 1
Output:
3 2, I have written this Solution Code: n=int(input())
arr= input().split()
one=zero=0
for i in range(0, n):
if arr[i]=='1':
one+=1
else:
zero+=1
print(one, zero), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary array of size N. Count number of 1's and 0's in the array.First line contains n.
Next line contains n space separated integers.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
0 <= arr[i] <= 1Print two integers, count of 1s and count of 0s.Input:
5
0 1 1 0 1
Output:
3 2, I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
int n=Integer.parseInt(in.next());
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = Integer.parseInt(in.next());
}
int ones=0;
for(int i=0;i<n;i++){
ones += a[i];
}
int zeroes=n-ones;
out.print(ones + " " + zeroes);
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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 |