Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 β€ N β€ 10^5
1 β€ Ai β€ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
double s = 0;
for(int i = 1; i <= n; i++){
double p; cin >> p;
s = s + (1/p);
}
s = 1/s;
cout << floor(s);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N elements, your task is to find the sum of the difference of maximum and minimum element of all subarrays.The first line of input contains a single integer N, and the next line of input contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 5*10<sup>4</sup>
0 ≤ Arr[i] ≤ 10<sup>5</sup>Print the sum of the difference of all the possible subarrays.Sample Input:-
4
3 1 4 2
Sample Output:-
16
Explanation:-
Subarrays of size 1:- [3], [1], [4], [2], sum = 0 + 0 + 0 + 0 = 0
Subarrays of size 2:- [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7
Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6
Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16
Sample Input:-
4
5 2 0 6
Sample Output:-
28, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a [] = new int[n];
for(int i = 0;i<n;i++){
a[i] = sc.nextInt();
}
System.out.print(subArrayRanges(a));
}
public static long subArrayRanges(int[] nums) {
int n = nums.length;
long sum = 0;
Deque<Integer> q = new ArrayDeque<>();
q.add(-1);
for (int i = 0; i <= n; i++) {
while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] <= nums[i])) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum += 1L * (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
q.clear();
q.add(-1);
for (int i = 0; i <= n; i++) {
while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] >= nums[i])) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum -= 1L * (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N elements, your task is to find the sum of the difference of maximum and minimum element of all subarrays.The first line of input contains a single integer N, and the next line of input contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 5*10<sup>4</sup>
0 ≤ Arr[i] ≤ 10<sup>5</sup>Print the sum of the difference of all the possible subarrays.Sample Input:-
4
3 1 4 2
Sample Output:-
16
Explanation:-
Subarrays of size 1:- [3], [1], [4], [2], sum = 0 + 0 + 0 + 0 = 0
Subarrays of size 2:- [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7
Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6
Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16
Sample Input:-
4
5 2 0 6
Sample Output:-
28, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 100005
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int arr[max1],left1[max1],right1[max1];
int maxsum(int n)
{
FOR(i,n){
left1[i]=right1[i]=0;
}
stack<int> s1,s2;
FOR(i,n){
while(s1.size() != 0 && arr[s1.top()]<=arr[i]) {
left1[i] += left1[s1.top()] + 1;
s1.pop();
}
s1.push(i);
}
for (int i = n - 1; i >= 0; i--) {
while (s2.size() != 0 && arr[s2.top()] < arr[i]) {
right1[i] += right1[s2.top()] + 1;
s2.pop();
}
s2.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += (left1[i] + 1) * (right1[i] + 1) * arr[i];
return ans;
}
int minsum(int n)
{ FOR(i,n){
left1[i]=right1[i]=0;
}
stack<int> s1,s2;
FOR(i,n){
while(s1.size() != 0 && arr[s1.top()]>arr[i]) {
left1[i] += left1[s1.top()] + 1;
s1.pop();
}
s1.push(i);
}
for (int i = n - 1; i >= 0; i--) {
while (s2.size() != 0 && arr[s2.top()] >= arr[i]) {
right1[i] += right1[s2.top()] + 1;
s2.pop();
}
s2.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += (left1[i] + 1) * (right1[i] + 1) * arr[i];
return ans;
}
void solve(){
int n;
cin>>n;
FOR(i,n){
cin>>arr[i];}
int maxs=maxsum(n);
int mins=minsum(n);
out1(maxs-mins);
}
signed main(){
fast();
solve();
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <strong>arr[]</strong> of size <strong>N</strong> and an element <strong>X</strong>. The task is to find and print the indices of the given element if it is present in array if not then print β<strong>Not found</strong>β without quotes.
<strong>Note</strong>: The elements may be present more than once.The first line of input contains T, denoting the number of test cases.
The first line of each test case contains N and X, N is the size of array and X is an element. Second line contains elements of array space separated. If not present then print "Not found" without quotes
<strong>Constraints</strong>:
1 <= T <= 100
1 <= N, X <= 10000
1 <= arr[i] <= 100000For each test case in new line you need to print all the positions where you find the X separated by space.
Assume 0-indexingInput:
2
5 6
2 3 6 5 6
4 3
2 4 6 5
Output:
2 4
Not found, 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--){
vector<int> v;
int n, x; cin >> n >> x;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p == x)
v.push_back(i-1);
}
if(v.size() == 0)
cout << "Not found\n";
else{
for(auto i: v)
cout << 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 integer array <strong>arr[]</strong> of size <strong>N</strong> and an element <strong>X</strong>. The task is to find and print the indices of the given element if it is present in array if not then print β<strong>Not found</strong>β without quotes.
<strong>Note</strong>: The elements may be present more than once.The first line of input contains T, denoting the number of test cases.
The first line of each test case contains N and X, N is the size of array and X is an element. Second line contains elements of array space separated. If not present then print "Not found" without quotes
<strong>Constraints</strong>:
1 <= T <= 100
1 <= N, X <= 10000
1 <= arr[i] <= 100000For each test case in new line you need to print all the positions where you find the X separated by space.
Assume 0-indexingInput:
2
5 6
2 3 6 5 6
4 3
2 4 6 5
Output:
2 4
Not found, I have written this Solution Code: def position(n,arr,x):
res = []
cnt = 0
for i in arr:
if(i == x):
res.append(cnt)
cnt += 1
return res
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <strong>arr[]</strong> of size <strong>N</strong> and an element <strong>X</strong>. The task is to find and print the indices of the given element if it is present in array if not then print β<strong>Not found</strong>β without quotes.
<strong>Note</strong>: The elements may be present more than once.The first line of input contains T, denoting the number of test cases.
The first line of each test case contains N and X, N is the size of array and X is an element. Second line contains elements of array space separated. If not present then print "Not found" without quotes
<strong>Constraints</strong>:
1 <= T <= 100
1 <= N, X <= 10000
1 <= arr[i] <= 100000For each test case in new line you need to print all the positions where you find the X separated by space.
Assume 0-indexingInput:
2
5 6
2 3 6 5 6
4 3
2 4 6 5
Output:
2 4
Not found, I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t =Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
int arr[] = new int[n];
str = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
findPositions(arr, n, x);
}
}
static void findPositions(int arr[], int n, int x)
{
boolean flag = false;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < n; i++)
{
if(arr[i] == x)
{
sb.append(i + " ");
flag = true;
}
}
if(flag ==true)
System.out.println(sb.toString());
else System.out.println("Not found");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: static void Icecreams (int N, int D){
int x=N;
while(D-->0){
x-=x/2;
x*=3;
}
System.out.println(x);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
cout << x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
printf("%d", x);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: def Icecreams(N,D):
ans = N
while D > 0:
ans = ans - ans//2
ans = ans*3
D = D-1
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton is given a sequence of N integers A<sub>1</sub>, A<sub>2</sub>, ... , A<sub>N</sub>
He wants to calculate the inverse of the sum of the inverses of the numbers i.e. <sup>1</sup>⁄(<sub><sup>1</sup>⁄<sub>A<sub>1</sub></sub> + <sup>1</sup>⁄<sub>A<sub>2</sub></sub> +. . + <sup>1</sup>⁄<sub>A<sub>N</sub></sub>)</sub>
Help Newton with his unwanted curiosity and print the answer to the above equation.The first line of the input contains a single integer N
The next line contains N integers, A<sub>1</sub>, A<sub>2</sub>, ... , A<sub>N</sub>.
<b>Constraints:</b>
1) 1 ≤ N ≤ 100
2) 1 ≤ A<sub>i</sub> ≤ 100Output the answer, up to 8 decimal places.<b>Sample Input 1:</b>
2
10 30
<b>Sample Output 1:</b>
7.50000000
<b>Sample Input 2:</b>
3
200 200 200
<b>Sample Output 2:</b>
66.66666666666667
<b>Sample Explanation 1:</b>
1/(1/10 + 1/30) = 1/(4/30) = 30/4 = 7.5
, I have written this Solution Code: // LUOGU_RID: 97628102
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
double ans = 0;
cin >> n;
for(int i = 1; i <= n; i++){
int a;
cin >> a;
ans += 1.0 / a;
}
cout << fixed << setprecision(8) << 1 / ans << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted doubly linked list containing n nodes. Your task is to remove duplicate nodes from the given list.
Example 1:
Input
1<->2<->2-<->3<->3<->4
Output:
1<->2<->3<->4
Example 2:
Input
1<->1<->1<->1
Output
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>deleteDuplicates()</b> that takes head node as parameter.
Constraints:
1 <=N <= 10000
1 <= Node. data<= 2*10000Return the head of the modified list.Sample Input:-
6
1 2 2 3 3 4
Sample Output:-
1 2 3 4
Sample Input:-
4
1 1 1 1
Sample Output:-
1, I have written this Solution Code: public static Node deleteDuplicates(Node head)
{
/* if list is empty */
if (head== null)
return head;
Node current = head;
while (current.next != null)
{
/* Compare current node with next node */
if (current.val == current.next.val)
/* delete the node pointed to by
' current->next' */
deleteNode(head, current.next);
/* else simply move to the next node */
else
current = current.next;
}
return head;
}
/* Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
public static void deleteNode(Node head, Node del)
{
/* base case */
if(head==null || del==null)
{
return ;
}
/* If node to be deleted is head node */
if(head==del)
{
head=del.next;
}
/* Change next only if node to be deleted
is NOT the last node */
if(del.next!=null)
{
del.next.prev=del.prev;
}
/* Change prev only if node to be deleted
is NOT the first node */
if (del.prev != null)
del.prev.next = del.next;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String st = br.readLine();
int len = 0;
int c=0;
for(int i=0;i<st.length();i++){
if(st.charAt(i)=='A'){
c++;
len = Math.max(len,c);
}else{
c=0;
}
}
System.out.println(len);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: S=input()
max=0
flag=0
for i in range(0,len(S)):
if(S[i]=='A' or S[i]=='B'):
if(S[i]=='A'):
flag+=1
if(flag>max):
max=flag
else:
flag=0
print(max), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, 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;
int ct = 0;
int ans = 0;
for(char c: s){
if(c == 'A')
ct++;
else
ct=0;
ans = max(ans, ct);
}
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 binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int count=search(a,0,n-1);
System.out.println(count);
}
}
public static int search(int[] a,int l,int h){
while(l<=h){
int mid=l+(h-l)/2;
if ((mid==h||a[mid+1]==0)&&(a[mid]==1))
return mid+1;
if (a[mid]==1)
l=mid+1;
else h=mid-1;
}
return 0;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = n+1;
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] == 1)
l = m;
else
h = m;
}
cout << l << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: c=int(input())
for x in range(c):
size=int(input())
s=input()
print(s.count('1')), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1000001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n;
cin>>n;
int a[n][n];
FOR(i,n){
FOR(j,n){
cin>>a[i][j];}}
int sum=0,sum1=0;;
FOR(i,n){
sum+=a[i][i];
sum1+=a[n-i-1][i];
}
out1(sum);out(sum1);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String args[])throws Exception {
InputStreamReader inr= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(inr);
String str=br.readLine();
int row = Integer.parseInt(str);
int col=row;
int [][] arr=new int [row][col];
for(int i=0;i<row;i++){
String line =br.readLine();
String[] elements = line.split(" ");
for(int j=0;j<col;j++){
arr[i][j]= Integer.parseInt(elements[j]);
}
}
int sumPrimary=0;
int sumSecondary=0;
for(int i=0;i<row;i++){
sumPrimary=sumPrimary + arr[i][i];
sumSecondary= sumSecondary + arr[i][row-1-i];
}
System.out.println(sumPrimary+ " " +sumSecondary);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code: // mat is the matrix/ 2d array
// the dimensions of array are n * n
function diagonalSum(mat, n) {
// write code here
// console.log the answer as in example
let principal = 0, secondary = 0;
for (let i = 0; i < n; i++) {
principal += mat[i][i];
secondary += mat[i][n - i - 1];
}
console.log(`${principal} ${secondary}`);
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size N*N, your task is to find the sum of the primary and secondary diagonal of the matrix.
For Matrix:-
M<sub>00</sub> M<sub>01</sub> M<sub>02</sub>
M<sub>10</sub> M<sub>11</sub> M<sub>12</sub>
M<sub>20</sub> M<sub>21</sub> M<sub>22</sub>
Primary diagonal:- M<sub>00</sub> M<sub>11</sub> M<sub>22</sub>
Secondary diagonal:- M<sub>02</sub> M<sub>11</sub> M<sub>20</sub>The first line of input contains a single integer N, The next N lines of input contains N space-separated integers depicting the values of the matrix.
Constraints:-
1 <= N <= 500
1 <= Matrix[][] <= 100000Print the sum of primary and secondary diagonal separated by a space.Sample Input:-
2
1 4
2 6
Sample Output:-
7 6
Sample Input:-
3
1 4 2
1 5 7
3 8 1
Sample Output:-
7 10, I have written this Solution Code: n = int(input())
sum1 = 0
sum2 = 0
for i in range(n):
a = [int(j) for j in input().split()]
sum1 = sum1+a[i]
sum2 = sum2+a[n-1-i]
print(sum1,sum2), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N find number of positive integers with N digits with all digits in non-decreasing order. As this can be large find ans modulo 1000000007.
For example 111227 is valid whereas 1112231 is not.Input contains one line of input containing a single integer N.
1 <= N <= 1000000000000Print a single integer containing the number of positive integers with N digits with all digits in non-decreasing order modulo 1000000007.Sample Input
1
Sample output
9
Sample Input
2
Sample Input
45, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long modulo = 1000000007;
public static long power(long a, long b, long m)
{
long ans=1;
while(b>0){
if(b%2!=0){
ans = ((ans%m)*(a%m))%m;
}
b=b/2;
a = ((a%m)*(a%m))%m;
}
return ans;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
long N = sc.nextLong();
long result = 1;
if(N == (long)Math.pow(10,12)){
System.out.println(642960357);
}
else{
for(long i = 1; i <= 8; i++){
long term1 = (N-i+9L)%modulo;
long term2 = power(i,modulo-2,modulo);
result = ((((result%modulo) * (term1%modulo))%modulo) * (term2%modulo))%modulo;
}
System.out.println(result);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N find number of positive integers with N digits with all digits in non-decreasing order. As this can be large find ans modulo 1000000007.
For example 111227 is valid whereas 1112231 is not.Input contains one line of input containing a single integer N.
1 <= N <= 1000000000000Print a single integer containing the number of positive integers with N digits with all digits in non-decreasing order modulo 1000000007.Sample Input
1
Sample output
9
Sample Input
2
Sample Input
45, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
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 = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int sz;
const int NN = 9;
class matrix{
public:
ll mat[NN][NN];
matrix(){
for(int i = 0; i < NN; i++)
for(int j = 0; j < NN; j++)
mat[i][j] = 0;
sz = NN;
}
inline matrix operator * (const matrix &a){
matrix temp;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++){
for(int k = 0; k < sz; k++){
temp.mat[i][j] += (mat[i][k] * a.mat[k][j]) % mod;
if(temp.mat[i][j] >= mod)
temp.mat[i][j] -= mod;
}
}
return temp;
}
inline matrix operator + (const matrix &a){
matrix temp;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++){
temp.mat[i][j] = mat[i][j] + a.mat[i][j] ;
if(temp.mat[i][j] >= mod)
temp.mat[i][j] -= mod;
}
return temp;
}
inline matrix operator - (const matrix &a){
matrix temp;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++){
temp.mat[i][j] = mat[i][j] - a.mat[i][j] ;
if(temp.mat[i][j] < mod)
temp.mat[i][j] += mod;
}
return temp;
}
inline void operator = (const matrix &b){
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++)
mat[i][j] = b.mat[i][j];
}
inline void print(){
for(int i = 0; i < sz; i++){
for(int j = 0; j < sz; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
}
};
matrix pow(matrix a, ll k){
matrix ans;
for(int i = 0; i < sz; i++)
ans.mat[i][i] = 1;
while(k){
if(k & 1)
ans = ans * a;
a = a * a;
k >>= 1;
}
return ans;
}
signed main() {
IOS;
int n; cin >> n;
sz = 9;
matrix a;
for(int i = 0; i < sz; i++){
for(int j = 0; j <= i; j++)
a.mat[i][j] = 1;
}
a = pow(a, n);
int ans = 0;
for(int i = 0; i < sz; i++){
ans += a.mat[i][0];
ans %= mod;
}
cout << ans;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<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 kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: void kCircleSum(int arr[],int n,int k){
long long ans=0;
for(int i=0;i<k;i++){
ans+=arr[i];
}
for(int i=0;i<n;i++){
printf("%lli ",ans);
ans+=arr[(i+k)%n]-arr[i];
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<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 kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: function kCircleSum(arr, arrSize, k)
{
var list = new Array(2*arrSize + 5)
for(var i = 0; i < arrSize; i++)
{
list[i+1] = arr[i]
list[i+arrSize+1] = list[i+1]
}
for(var i = 0; i < 2*arrSize; i++)
dp[i] = 0
for(var i=1;i<=2*arrSize;i++)
{
dp[i] = dp[i-1]+list[i]
}
var ans = ""
for(var i = 1; i <= arrSize; i++)
{
ans += (dp[i+k-1]-dp[i-1]) + " "
}
console.log(ans)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<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 kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: static void kCircleSum(int arr[],int n,int k){
long ans=0;
for(int i=0;i<k;i++){
ans+=arr[i];
}
for(int i=0;i<n;i++){
System.out.print(ans+" ");
ans+=arr[(i+k)%n]-arr[i];
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<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 kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: def kCircleSum(arr,n,k):
ans=0
for i in range (0,k):
ans=ans+arr[i]
for i in range (0,n):
print(ans,end=" ")
ans=ans+arr[int((i+k)%n)]-arr[i]
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<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 kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
void kCircleSum(int arr[],int n,int k){
long long ans=0;
for(int i=0;i<k;i++){
ans+=arr[i];
}
for(int i=0;i<n;i++){
printf("%lli ",ans);
ans+=arr[(i+k)%n]-arr[i];
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array "a" of non-Integers, check if any integer in the array is less than k.The first line contains two Integers n and k, which is the size of the array and k.
The second line contains n space- separated Integers, that is array elements.
<b>Constraints</b>
1 ≤ n ≤ 100
1 ≤ k ≤ 100
1 ≤ a[i] ≤ 100Print "true" if any element of array is less than k, otherwise "false".Sample 1:
Input:
4 3
1 1 4 8
Output:
true
Explanation:
Both Integers at Indices 0 and 1 are less than k=3., I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
public static boolean checkIfAnyLessThanK(int[] a, int k) {
for (int i = 0; i < a.length; i++) {
if (a[i] < k) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// String s=sc.nextLine();
int n=sc.nextInt();
int k=sc.nextInt();
sc.nextLine();
assert n>=1&&n<=100 : "Invalid Input";
assert k>=1&&k<=100 : "Invalid Input";
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
assert a[i]>=1&&a[i]<=100 : "Invalid Input";
}
System.out.print(checkIfAnyLessThanK(a,k));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a square matrix of size N*N. Initially all elements of this matrix are equal to 0. You are given Q queries. Each query consists of two integers, i and j (1 <= i, j <= N) wherein you increase the value of all elements in the i<sup>th</sup> row and j<sup>th</sup> column by 1. After doing this, for each query print the number of zeroes left in the matrix.The first line of the input consists of two integers N and Q.
The next Q lines each contains two integers i and j.
Constraints:
1 <= N, Q <= 10<sup>5</sup>
1 <= i, j <= NFor each query print the number of zeroes left in the matrix.Sample Input:
3 3
1 1
1 2
3 2
Sample Output:
4 2 1
Explaination:
Initially, the matrix will look like:
0 0 0
0 0 0
0 0 0
After the first query, the matrix will look something like this:
1 1 1
1 0 0
1 0 0
<b>Number of zeroes = 4</b>
After the second query, the matrix will look something like this:
1 1 1
1 1 0
1 1 0
<b>Number of zeroes = 2</b>
After the third query, the matrix will look something like this:
1 1 1
1 1 0
1 1 1
<b>Number of zeroes = 1</b>
, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n, r = 0, c = 0;
cin >> n;
int k;
cin >> k;
int ans = n*n;
vector<int> row(n + 1), col(n + 1);
while(k--){
int i, j;
cin >> i >> j;
if(row[i] == 0) ans -= n - c, row[i] = 1, r++;
if(col[j] == 0) ans -= n - r, col[j] = 1, c++;
cout << ans << ' ';
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, I have written this Solution Code: n=1
index=0
li=[]
while n!=0:
n=int(input())
li.append(n)
index=index+1
#li = list(map(int,input().strip().split()))
for i in range(0,len(li)-1):
print(li[i],end=" ")
print(li[len(li)-1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, 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=100001;
int a;
for(int i=0;i<n;i++){
a=sc.nextInt();
System.out.print(a+" ");
if(a==0){break;}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, 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;
while(cin >> n){
cout << n << " ";
if(n == 0) break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program that creates an N*N matrix. Fill each cell with the sum of row number and column number (based on 0 indexes, ie indices begin from base 0), take its transpose and print it.
Where the transpose of a matrix is a new matrix whose rows and the columns are interchanged to that of original matrix.Input contains a single integer N.
Constraints:-
1<=N<=500Print the NxN matrix.Sample input
2
Sample output
0 1
1 2
Explanation:-
0+0 0+1
1+0 1+1
Sample Input:-
3
Sample Output:-
0 1 2
1 2 3
2 3 4
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[][] a=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(i+j + " ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program that creates an N*N matrix. Fill each cell with the sum of row number and column number (based on 0 indexes, ie indices begin from base 0), take its transpose and print it.
Where the transpose of a matrix is a new matrix whose rows and the columns are interchanged to that of original matrix.Input contains a single integer N.
Constraints:-
1<=N<=500Print the NxN matrix.Sample input
2
Sample output
0 1
1 2
Explanation:-
0+0 0+1
1+0 1+1
Sample Input:-
3
Sample Output:-
0 1 2
1 2 3
2 3 4
, I have written this Solution Code: n=int(input())
for i in range(n):
for j in range(n):
print(i+ j,end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program that creates an N*N matrix. Fill each cell with the sum of row number and column number (based on 0 indexes, ie indices begin from base 0), take its transpose and print it.
Where the transpose of a matrix is a new matrix whose rows and the columns are interchanged to that of original matrix.Input contains a single integer N.
Constraints:-
1<=N<=500Print the NxN matrix.Sample input
2
Sample output
0 1
1 2
Explanation:-
0+0 0+1
1+0 1+1
Sample Input:-
3
Sample Output:-
0 1 2
1 2 3
2 3 4
, 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++){
arr[i][j]=i+j;
}}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n holding some random value, your task is to assign value 10 to the given integer.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>Assignment_Operator()</b>, which takes no parameter.
<b>Constraints:</b>
1 <= n <= 100You don't need to print anything you just need to assign value 10 to the integer n.Sample Input:-
48
Sample output:-
10
Sample Input:-
24
Sample Output:-
10, I have written this Solution Code: static void Assignment_Operator(){
n=10;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. Find the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.The first line of input contains N, the size of the array.
The second line of input contains N space seperated integers A<sub>i</sub>.
Constraints:
1 <= N <= 10<sup>5</sup>
-10<sup>9</sup> <= A<sub>i</sub> <= 10<sup>9</sup>Print the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.Sample Input:
5
3 -2 4 -1 4
Sample Output:
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
long arr[] = new long[n];
long pre[] =new long[n];
long sum=0;
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(st.nextToken());
sum+=arr[i];
pre[i]=sum;
}
int cnt=0;
for(int i=0;i<pre.length;i++){
if(i==0){
if(0>=(pre[pre.length-1]-pre[i]))cnt++;
continue;
}else if(i==pre.length-1){
if(pre[pre.length-2]>=0)cnt++;
continue;
}
if(pre[i-1]>=(pre[pre.length-1]-pre[i])){
cnt++;
}
}
System.out.print(cnt);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. Find the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.The first line of input contains N, the size of the array.
The second line of input contains N space seperated integers A<sub>i</sub>.
Constraints:
1 <= N <= 10<sup>5</sup>
-10<sup>9</sup> <= A<sub>i</sub> <= 10<sup>9</sup>Print the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.Sample Input:
5
3 -2 4 -1 4
Sample Output:
2, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long
using T = pair<int, int>;
signed main(){
int n;
cin >> n;
vector<int> a(n + 1), pre(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
int sum = pre.back();
int ans = 0;
for(int i = 1; i <= n; i++){
if(pre[i - 1] >= sum - pre[i]) ans++;
}
cout << ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted doubly linked list containing n nodes. Your task is to remove duplicate nodes from the given list.
Example 1:
Input
1<->2<->2-<->3<->3<->4
Output:
1<->2<->3<->4
Example 2:
Input
1<->1<->1<->1
Output
1<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>deleteDuplicates()</b> that takes head node as parameter.
Constraints:
1 <=N <= 10000
1 <= Node. data<= 2*10000Return the head of the modified list.Sample Input:-
6
1 2 2 3 3 4
Sample Output:-
1 2 3 4
Sample Input:-
4
1 1 1 1
Sample Output:-
1, I have written this Solution Code: public static Node deleteDuplicates(Node head)
{
/* if list is empty */
if (head== null)
return head;
Node current = head;
while (current.next != null)
{
/* Compare current node with next node */
if (current.val == current.next.val)
/* delete the node pointed to by
' current->next' */
deleteNode(head, current.next);
/* else simply move to the next node */
else
current = current.next;
}
return head;
}
/* Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
public static void deleteNode(Node head, Node del)
{
/* base case */
if(head==null || del==null)
{
return ;
}
/* If node to be deleted is head node */
if(head==del)
{
head=del.next;
}
/* Change next only if node to be deleted
is NOT the last node */
if(del.next!=null)
{
del.next.prev=del.prev;
}
/* Change prev only if node to be deleted
is NOT the first node */
if (del.prev != null)
del.prev.next = del.next;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: // arr is the array of numbers, n is the number fo elements
function replaceArray(arr, n) {
// write code here
// do not console.log
// return the new array
const newArr = []
newArr[0] = arr[0] * arr[1]
newArr[n-1] = arr[n-1] * arr[n-2]
for(let i= 1;i<n-1;i++){
newArr[i] = arr[i-1] * arr[i+1]
}
return newArr
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: n = int(input())
X = [int(x) for x in input().split()]
lst = []
for i in range(len(X)):
if i == 0:
lst.append(X[i]*X[i+1])
elif i == (len(X) - 1):
lst.append(X[i-1]*X[i])
else:
lst.append(X[i-1]*X[i+1])
for i in lst:
print(i,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long long b[n],a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=1;i<n-1;i++){
b[i]=a[i-1]*a[i+1];
}
b[0]=a[0]*a[1];
b[n-1]=a[n-1]*a[n-2];
for(int i=0;i<n;i++){
cout<<b[i]<<" ";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print(a[0]*a[1]+" ");
for(int i=1;i<n-1;i++){
System.out.print(a[i-1]*a[i+1]+" ");
}
System.out.print(a[n-1]*a[n-2]);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader(){
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1){
if (c == '\n')break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)c = read();
do{
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)return -ret;return ret;
}
public long nextLong() throws IOException{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.'){
while ((c = read()) >= '0' && c <= '9'){
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)buffer[0] = -1;
}
private byte read() throws IOException{
if (bufferPointer == bytesRead)fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException{
if (din == null)return;
din.close();
}
}
public static void main (String[] args) throws IOException{
Reader sc = new Reader();
int m = sc.nextInt();
int n = sc.nextInt();
int[][] arr = new int[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
arr[i][j] = sc.nextInt();
}
}
int max_row_index = 0;
int j = n - 1;
for (int i = 0; i < m; i++) {
while (j >= 0 && arr[i][j] == 1) {
j = j - 1;
max_row_index = i;
}
}
System.out.println(max_row_index);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: r, c = list(map(int, input().split()))
max_count = 0
max_r = 0
for i in range(r):
count = input().count("1")
if count > max_count:
max_count = count
max_r = i
print(max_r), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int a[max1][max1];
signed main()
{
int n,m;
cin>>n>>m;
FOR(i,n){
FOR(j,m){cin>>a[i][j];}}
int cnt=0;
int ans=0;
int res=0;
FOR(i,n){
cnt=0;
FOR(j,m){
if(a[i][j]==1){
cnt++;
}}
if(cnt>res){
res=cnt;
ans=i;
}
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: // mat is the matrix/ 2d array
// n,m are dimensions
function max1Row(mat, n, m) {
// write code here
// do not console.log
// return the answer as a number
let j, max_row_index = 0;
j = m - 1;
for (let i = 0; i < n; i++)
{
// Move left until a 0 is found
let flag = false;
// to check whether a row has more 1's than previous
while (j >= 0 && mat[i][j] == 1)
{
j = j - 1; // Update the index of leftmost 1
// seen so far
flag = true;//present row has more 1's than previous
}
// if the present row has more 1's than previous
if (flag)
{
max_row_index = i; // Update max_row_index
}
}
if (max_row_index == 0 && mat[0][m - 1] == 0)
return -1;
return max_row_index;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Print a right angled triangle of stars of given height N as shown in example.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 10Print the Right angled triangle of height N.Sample Input:-
3
Sample Output:-
*
**
***
Sample Input:-
4
Sample Output:-
*
**
***
****, 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());
for(int i =1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*") ;
}
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Print a right angled triangle of stars of given height N as shown in example.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 10Print the Right angled triangle of height N.Sample Input:-
3
Sample Output:-
*
**
***
Sample Input:-
4
Sample Output:-
*
**
***
****, I have written this Solution Code: x=int(input(""))
for i in range(x):
for j in range(i+1):
print("*",end='')
print(""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Print a right angled triangle of stars of given height N as shown in example.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 10Print the Right angled triangle of height N.Sample Input:-
3
Sample Output:-
*
**
***
Sample Input:-
4
Sample Output:-
*
**
***
****, I have written this Solution Code: #include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<i+1;j++){
cout<<'*';}
cout<<endl;
}}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Anya owns N triplets of integers (A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub>), for each i from 1 to N. She asks you to find a sequence of integers which satisfies the following conditions:
1. The sequence contains at least C<sub>i</sub> distinct integers from the closed interval [A<sub>i</sub>, B<sub>i</sub>], for each i from 1 to N.
2. Out of all sequences satisfying the first condition, choose a sequence with the minimum possible number of elements.
For simplicity, she asks you to just print the length of such a sequence.The first line of the input contains a single integer N denoting the number of triplets.
Then N lines follow, where the i<sup>th</sup> line contains three integers A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub> for each i from 1 to N.
<b> Constraints: </b>
1 β€ N β€ 2000
1 β€ A<sub>i</sub> β€ B<sub>i</sub> β€ 2000
0 β€ C<sub>i</sub> β€ B<sub>i</sub> - A<sub>i</sub> + 1Print a single integer β the minimum possible sequence length.Sample Input 1:
1
1 3 3
Sample Output 1:
3
Sample Explanation 1:
Since there are only 3 elements in the closed interval [1,3], and we need to take 3 of them, clearly the smallest possible length is 3.
Sample Input 2:
2
1 3 1
3 5 1
Sample Output 2:
1
Sample Explanation 2:
We can take the sequence consisting of a single element {3}., I have written this Solution Code: l = [0]*2001
k = []
n = int(input())
for i in range(n):
a,b,c = map(int,input().split())
k.append([b,(a,c)])
k.sort()
for b,aa in k:
a = aa[0]
c = aa[1]
cnt = 0
for i in range(b,a-1,-1):
if l[i]:
cnt+=1
if cnt>=c:
continue
else:
for i in range(b,a-1,-1):
if not l[i]:
l[i]=1
cnt+=1
if cnt==c:
break
print(sum(l)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Anya owns N triplets of integers (A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub>), for each i from 1 to N. She asks you to find a sequence of integers which satisfies the following conditions:
1. The sequence contains at least C<sub>i</sub> distinct integers from the closed interval [A<sub>i</sub>, B<sub>i</sub>], for each i from 1 to N.
2. Out of all sequences satisfying the first condition, choose a sequence with the minimum possible number of elements.
For simplicity, she asks you to just print the length of such a sequence.The first line of the input contains a single integer N denoting the number of triplets.
Then N lines follow, where the i<sup>th</sup> line contains three integers A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub> for each i from 1 to N.
<b> Constraints: </b>
1 β€ N β€ 2000
1 β€ A<sub>i</sub> β€ B<sub>i</sub> β€ 2000
0 β€ C<sub>i</sub> β€ B<sub>i</sub> - A<sub>i</sub> + 1Print a single integer β the minimum possible sequence length.Sample Input 1:
1
1 3 3
Sample Output 1:
3
Sample Explanation 1:
Since there are only 3 elements in the closed interval [1,3], and we need to take 3 of them, clearly the smallest possible length is 3.
Sample Input 2:
2
1 3 1
3 5 1
Sample Output 2:
1
Sample Explanation 2:
We can take the sequence consisting of a single element {3}., I have written this Solution Code: //Author: Xzirium
//Time and Date: 00:28:35 28 December 2021
//Optional FAST
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
//Required Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//Required namespaces
using namespace std;
using namespace __gnu_pbds;
//Required defines
#define endl '\n'
#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(long long a=b;a<c;a++)
#define FORD(a,b,c) for(long long a=b;a>c;a--)
//Required typedefs
template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <typename T> using ordered_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
//Required Constants
const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);
const long double PI=3.14159265358979;
// Required random number generators
// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
//Required Functions
ll power(ll b, ll e)
{
ll r = 1ll;
for(; e > 0; e /= 2, (b *= b) %= MOD)
if(e % 2) (r *= b) %= MOD;
return r;
}
ll modInverse(ll a)
{
return power(a,MOD-2);
}
//Work
int main()
{
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen ("INPUT.txt" , "r" , stdin);
//freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t clk;
clk = clock();
//-----------------------------------------------------------------------------------------------------------//
READV(N);
vector<pair<pll,ll>> Z;
FORI(i,0,N)
{
READV(a);
READV(b);
READV(c);
Z.pb({{b,a},c});
}
sort(Z.begin(),Z.end());
ordered_set1<ll> unused;
FORI(i,1,2001)
{
unused.insert(i);
}
ll ans=0;
FORI(i,0,N)
{
ll a=Z[i].fi.se;
ll b=Z[i].fi.fi;
ll c=Z[i].se;
ll curr=b-a+1-(unused.order_of_key(a-1)-unused.order_of_key(b));
while(curr<c)
{
ans++;
curr++;
auto it=unused.lower_bound(b);
unused.erase(it);
}
}
cout<<ans<<endl;
//-----------------------------------------------------------------------------------------------------------//
clk = clock() - clk;
cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We are given a string. Your task is to compress the consecutive letters of the string
For example, given string is "AAACCCBBD", thus here
A's occurrence 3 times
C's occurrence 3 times
B's occurrence 2 times
D's occurrence 1 time
So after compressing string becomes "A3C3B2D1".The first line of input contains an integer T denoting the number of test cases. Each test case will have a string provided in the new line.
Constraints:
1 <= T <= 10
1 <= sizeof(String) <= 10^6
All characters of String are upper case letters. (A-Z)
Sum of size of Strings over all testcases is <= 10^6For each testcase, in a new line, print the compressed string for each test case in a new line. Input:
2
AAACCCBBD
ABCD
Output:
A3C3B2D1
A1B1C1D1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void compress(String str, int l){
for (int i = 0; i < l; i++) {
int count = 1;
while (i < l - 1 && str.charAt(i) == str.charAt(i + 1)) {
count++;
i++;
}
System.out.print(str.charAt(i));
System.out.print(count);
}
System.out.println();
}
public static void main (String[] args) throws IOException{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(rd.readLine());
while(test-->0){
String s = rd.readLine();
int len = s.length();
compress(s,len);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We are given a string. Your task is to compress the consecutive letters of the string
For example, given string is "AAACCCBBD", thus here
A's occurrence 3 times
C's occurrence 3 times
B's occurrence 2 times
D's occurrence 1 time
So after compressing string becomes "A3C3B2D1".The first line of input contains an integer T denoting the number of test cases. Each test case will have a string provided in the new line.
Constraints:
1 <= T <= 10
1 <= sizeof(String) <= 10^6
All characters of String are upper case letters. (A-Z)
Sum of size of Strings over all testcases is <= 10^6For each testcase, in a new line, print the compressed string for each test case in a new line. Input:
2
AAACCCBBD
ABCD
Output:
A3C3B2D1
A1B1C1D1, I have written this Solution Code:
def compress(st):
n = len(st)
i = 0
while i < n:
count = 1
while (i < n-1 and st[i] == st[i + 1]):
count += 1
i += 1
i += 1
print(st[i-1] +str(count),end="")
t=int(input())
for i in range(t):
s=input()
compress(s)
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We are given a string. Your task is to compress the consecutive letters of the string
For example, given string is "AAACCCBBD", thus here
A's occurrence 3 times
C's occurrence 3 times
B's occurrence 2 times
D's occurrence 1 time
So after compressing string becomes "A3C3B2D1".The first line of input contains an integer T denoting the number of test cases. Each test case will have a string provided in the new line.
Constraints:
1 <= T <= 10
1 <= sizeof(String) <= 10^6
All characters of String are upper case letters. (A-Z)
Sum of size of Strings over all testcases is <= 10^6For each testcase, in a new line, print the compressed string for each test case in a new line. Input:
2
AAACCCBBD
ABCD
Output:
A3C3B2D1
A1B1C1D1, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
string s; cin >> s;
int c = 1;
char p = 0;
int n = s.length();
for(int i = 1; i < n; i++){
if(s[i] != s[i-1]){
cout << s[i-1] << c;
c = 1;
}
else
c++;
}
cout << s[n-1] << c << endl;
}
void testcases(){
int tt = 1;
cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of n elements, where each element is at most k away from its position in the sorted array, you need to sort the array optimally.
Note: DO NOT use STL sort() function for this question.The first line contains two elements n and k separated by a space. The second line contains n elements of array.
Constraints:
1 <= n <= 100000
1 <= k <= n
1 <= arr(i) <= 1000000Print the sorted array.Sample Input:
7 3
6 5 2 3 8 10 9
Sample Output:
2 3 5 6 8 9 10, I have written this Solution Code: try:
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
print(*l)
except:
pass, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of n elements, where each element is at most k away from its position in the sorted array, you need to sort the array optimally.
Note: DO NOT use STL sort() function for this question.The first line contains two elements n and k separated by a space. The second line contains n elements of array.
Constraints:
1 <= n <= 100000
1 <= k <= n
1 <= arr(i) <= 1000000Print the sorted array.Sample Input:
7 3
6 5 2 3 8 10 9
Sample Output:
2 3 5 6 8 9 10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
PriorityQueue<Integer> pq = new PriorityQueue<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int k = Integer.parseInt(line[1]);
line = br.readLine().split(" ");
int i=0;
for(;i<k;i++){
pq.add(Integer.parseInt(line[i]));
}
for(i=k;i<n;i++){
int x = pq.remove();
System.out.print(x+" ");
pq.add(Integer.parseInt(line[i]));
}
while(pq.size()>0)
System.out.print(pq.remove()+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of n elements, where each element is at most k away from its position in the sorted array, you need to sort the array optimally.
Note: DO NOT use STL sort() function for this question.The first line contains two elements n and k separated by a space. The second line contains n elements of array.
Constraints:
1 <= n <= 100000
1 <= k <= n
1 <= arr(i) <= 1000000Print the sorted array.Sample Input:
7 3
6 5 2 3 8 10 9
Sample Output:
2 3 5 6 8 9 10, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
priority_queue<int> p;
void solve(){
int n, k;
cin >> n >> k;
for(int i = 1; i <= k-1; i++){
int x; cin >> x;
p.push(-x);
}
for(int i = k; i <= n; i++){
int x; cin >> x;
p.push(-x);
cout << -p.top() << " ";
p.pop();
}
while(!p.empty()){
cout << -p.top() << " ";
p.pop();
}
}
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: You are given a grid having N*M elements. Find the index of the row (1-base indexing) which has the maximum sum of elements.
Note: if more than two rows have the same sum of elements, then return the smallest index.The first line of the input contains two integers N and M.
The next N lines each contain M space separated integers.
<b>Constraints:</b>
1 <= N, M <= 10<sup>3</sup>
1 <= A<sub>i, j</sub> <= 10<sup>9Print the index of the row (1-base indexing) which has the maximum sum of elements.Sample Input:
4 3
3 4 2
5 1 7
2 8 1
2 3 3
Sample Output:
2
<b>Explaination:</b>
Row number 2 has sum = 5 + 1 + 7 = 13 which is the maximum among all rows., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) {
FastReader sc=new FastReader();
int n=sc.nextInt();
int m=sc.nextInt();
int arr[][]=new int[n][m];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
arr[i][j]= sc.nextInt();
}
}
long maxsum=0;
int index=0;
for(int i=0; i<n; i++)
{
long sum=0;
for(int j=0; j<m; j++)
{
sum += arr[i][j];
}
if(sum > maxsum)
{
maxsum=sum;
index=i+1;
}
}
System.out.print(index);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a grid having N*M elements. Find the index of the row (1-base indexing) which has the maximum sum of elements.
Note: if more than two rows have the same sum of elements, then return the smallest index.The first line of the input contains two integers N and M.
The next N lines each contain M space separated integers.
<b>Constraints:</b>
1 <= N, M <= 10<sup>3</sup>
1 <= A<sub>i, j</sub> <= 10<sup>9Print the index of the row (1-base indexing) which has the maximum sum of elements.Sample Input:
4 3
3 4 2
5 1 7
2 8 1
2 3 3
Sample Output:
2
<b>Explaination:</b>
Row number 2 has sum = 5 + 1 + 7 = 13 which is the maximum among all rows., 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;
vector<vector<int>> a(n, vector<int> (m));
vector<int> sum(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
sum[i] += a[i][j];
}
}
int mx = 0, ans = 0;
for(int i = 0; i < n; i++){
if(mx < sum[i]){
mx = sum[i];
ans = i;
}
}
cout << ans + 1;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, 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(" ");
long l=Long.parseLong(str[0]);
long r=Long.parseLong(str[1]);
long k=Long.parseLong(str[2]);
long count=(r/k)-((l-1)/k);
System.out.print(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: l,r,k=[int(x)for x in input().split()]
print(r//k - (l-1)//k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long x,l,r,k;
cin>>l>>r>>k;
x=l/k;
if(l%k==0){x--;}
cout<<r/k-x;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Jerry has recently learned about BODMAS rule, so he is highly curious about brackets. He is currently aware of 3 kinds of brackets, "()", "{}", and "[]".
A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "({})" are correct (the resulting expressions are: "(1)+[1]", "({1+1}+1)" ). while the bracket sequences ")(" and "[" are not correct.
Given a bracket sequence please tell Jerry whether the bracket sequence is correct or not.The first line of the input contains an integer N, the length of the bracket sequence. The next line of the input contains a string denoting the bracket sequence.
<b>Constraints</b>
4 β€ N β€ 10^5Print "YES" without quotes if the bracket sequence is correct, else print "NO".Sample Input 1
8
({}[])[]
Sample Output 1
YES
Sample Input 2
6
([][)]
Sample Output 2
NO, I have written this Solution Code:
import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
long N = Long.parseLong(reader.readLine());
String str = reader.readLine();
if(areBracketsBalanced(str,N))
System.out.print("YES");
else
System.out.print("NO");
}
static boolean areBracketsBalanced(String expr,long N)
{
int i=0;
Stack<Character> st = new Stack<>();
while(i<expr.length())
{
char ch = expr.charAt(i);
if(ch=='('||ch=='{'||ch=='['){
st.push(ch);
}
if(ch==')'||ch=='}'||ch==']'){
if(st.isEmpty())
{
return false;
}
char c = st.pop();
if((ch==')'&& c!='(') || (ch=='}'&&c!='{') || (ch==']'&&c!='['))
return false;
}
i++;
}
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Jerry has recently learned about BODMAS rule, so he is highly curious about brackets. He is currently aware of 3 kinds of brackets, "()", "{}", and "[]".
A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "({})" are correct (the resulting expressions are: "(1)+[1]", "({1+1}+1)" ). while the bracket sequences ")(" and "[" are not correct.
Given a bracket sequence please tell Jerry whether the bracket sequence is correct or not.The first line of the input contains an integer N, the length of the bracket sequence. The next line of the input contains a string denoting the bracket sequence.
<b>Constraints</b>
4 β€ N β€ 10^5Print "YES" without quotes if the bracket sequence is correct, else print "NO".Sample Input 1
8
({}[])[]
Sample Output 1
YES
Sample Input 2
6
([][)]
Sample Output 2
NO, I have written this Solution Code: def check(ch, ch1):
if((ch == '}' and ch1 != '{') or (ch == ']' and ch1 != '[') or (ch == ')' and ch1 != '(')):
return False
return True
n = int(input())
s = input()
li = []
flag = True
for ch in s:
if(ch == '(' or ch == '[' or ch == '{'):
li.append(ch)
else:
if((len(li) > 0 and not check(ch, li[-1])) or len(li) == 0):
flag = False
break
if(len(li) > 0):
li.pop()
if(flag and len(li) == 0):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Jerry has recently learned about BODMAS rule, so he is highly curious about brackets. He is currently aware of 3 kinds of brackets, "()", "{}", and "[]".
A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "({})" are correct (the resulting expressions are: "(1)+[1]", "({1+1}+1)" ). while the bracket sequences ")(" and "[" are not correct.
Given a bracket sequence please tell Jerry whether the bracket sequence is correct or not.The first line of the input contains an integer N, the length of the bracket sequence. The next line of the input contains a string denoting the bracket sequence.
<b>Constraints</b>
4 β€ N β€ 10^5Print "YES" without quotes if the bracket sequence is correct, else print "NO".Sample Input 1
8
({}[])[]
Sample Output 1
YES
Sample Input 2
6
([][)]
Sample Output 2
NO, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
bool isBalanced(string expr) {
stack<char> s;
char ch;
for (int i=0; i<expr.length(); i++) {
if (expr[i]=='('||expr[i]=='['||expr[i]=='{') {
s.push(expr[i]);
continue;
}
if (s.empty())
return false;
switch (expr[i]) {
case ')':
ch = s.top();
s.pop();
if (ch=='{' || ch=='[')
return false;
break;
case '}':
ch = s.top();
s.pop();
if (ch=='(' || ch=='[')
return false;
break;
case ']':
ch = s.top();
s.pop();
if (ch =='(' || ch == '{')
return false;
break;
}
}
return (s.empty());
}
int main(){
int n;
cin>>n;
string str;
cin>>str;
if(isBalanced(str)){cout<<"YES";}
else{cout<<"NO";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N find number of positive integers with N digits with all digits in non-decreasing order. As this can be large find ans modulo 1000000007.
For example 111227 is valid whereas 1112231 is not.Input contains one line of input containing a single integer N.
1 <= N <= 1000000000000Print a single integer containing the number of positive integers with N digits with all digits in non-decreasing order modulo 1000000007.Sample Input
1
Sample output
9
Sample Input
2
Sample Input
45, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long modulo = 1000000007;
public static long power(long a, long b, long m)
{
long ans=1;
while(b>0){
if(b%2!=0){
ans = ((ans%m)*(a%m))%m;
}
b=b/2;
a = ((a%m)*(a%m))%m;
}
return ans;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
long N = sc.nextLong();
long result = 1;
if(N == (long)Math.pow(10,12)){
System.out.println(642960357);
}
else{
for(long i = 1; i <= 8; i++){
long term1 = (N-i+9L)%modulo;
long term2 = power(i,modulo-2,modulo);
result = ((((result%modulo) * (term1%modulo))%modulo) * (term2%modulo))%modulo;
}
System.out.println(result);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N find number of positive integers with N digits with all digits in non-decreasing order. As this can be large find ans modulo 1000000007.
For example 111227 is valid whereas 1112231 is not.Input contains one line of input containing a single integer N.
1 <= N <= 1000000000000Print a single integer containing the number of positive integers with N digits with all digits in non-decreasing order modulo 1000000007.Sample Input
1
Sample output
9
Sample Input
2
Sample Input
45, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
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 = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int sz;
const int NN = 9;
class matrix{
public:
ll mat[NN][NN];
matrix(){
for(int i = 0; i < NN; i++)
for(int j = 0; j < NN; j++)
mat[i][j] = 0;
sz = NN;
}
inline matrix operator * (const matrix &a){
matrix temp;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++){
for(int k = 0; k < sz; k++){
temp.mat[i][j] += (mat[i][k] * a.mat[k][j]) % mod;
if(temp.mat[i][j] >= mod)
temp.mat[i][j] -= mod;
}
}
return temp;
}
inline matrix operator + (const matrix &a){
matrix temp;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++){
temp.mat[i][j] = mat[i][j] + a.mat[i][j] ;
if(temp.mat[i][j] >= mod)
temp.mat[i][j] -= mod;
}
return temp;
}
inline matrix operator - (const matrix &a){
matrix temp;
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++){
temp.mat[i][j] = mat[i][j] - a.mat[i][j] ;
if(temp.mat[i][j] < mod)
temp.mat[i][j] += mod;
}
return temp;
}
inline void operator = (const matrix &b){
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++)
mat[i][j] = b.mat[i][j];
}
inline void print(){
for(int i = 0; i < sz; i++){
for(int j = 0; j < sz; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
}
};
matrix pow(matrix a, ll k){
matrix ans;
for(int i = 0; i < sz; i++)
ans.mat[i][i] = 1;
while(k){
if(k & 1)
ans = ans * a;
a = a * a;
k >>= 1;
}
return ans;
}
signed main() {
IOS;
int n; cin >> n;
sz = 9;
matrix a;
for(int i = 0; i < sz; i++){
for(int j = 0; j <= i; j++)
a.mat[i][j] = 1;
}
a = pow(a, n);
int ans = 0;
for(int i = 0; i < sz; i++){
ans += a.mat[i][0];
ans %= mod;
}
cout << ans;
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 number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException{
BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
String y[]=rd.readLine().split(" ");
long n=Long.parseLong(y[0]);
long p=Long.parseLong(y[1]);
long v=1;
while(p>0){
if((p&1L)==1L)
v=(v*n)%1000000007;
p/=2;
n=(n*n)%1000000007;
}
System.out.print(v);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code: n, p =input().split()
n, p =int(n), int(p)
def FastModularExponentiation(b, k, m):
res = 1
b = b % m
while (k > 0):
if ((k & 1) == 1):
res = (res * b) % m
k = k >> 1
b = (b * b) % m
return res
m=pow(10,9)+7
print(FastModularExponentiation(n, p, m)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 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);
}
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0) return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Driver code
signed main()
{
int x ;
int y;
cin>>x>>y;
int p = 1e9+7;
cout<< power(x, y, p);
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function isArray which takes an input which can be any data type and returns true
if it's an array else false.Could be any datatype string number object or an arraytrue or falseSample Input:-
1
[2, 3]
Sample Output
false
true, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
String str= s.next();
if(str.charAt(0)== '['){
System.out.println("true");
}
else{
System.out.println("false");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function isArray which takes an input which can be any data type and returns true
if it's an array else false.Could be any datatype string number object or an arraytrue or falseSample Input:-
1
[2, 3]
Sample Output
false
true, I have written this Solution Code: function isArray(input){
if(Array.isArray(input)) {
console.log(true)
}else{
console.log(false)
}
}, In this Programming Language: JavaScript, 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: Count the number of substrings in the string s that are equivalent to "ab". The string s contains only the characters "a" and "b".The first line contains string s.
<b>Constraints</b>
n==s. length
1 ≤ n ≤ 10<sup>5</sup>Print the count of substrings in the string s that are equivalent to "ab".Sample 1:
Input:
abababaa
Output:
3, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
public static int fun(String s) {
int n=s.length();
int ans=0;
for(int i=1;i<n;i++){
if(s.charAt(i-1)=='a'&&s.charAt(i)=='b'){
ans++;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int n=s.length();
assert n>=1&&n<=100000 : "Input not valid";
System.out.print(fun(s));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: static int isPresent(long arr[], int n, long k)
{
int left = 0;
int right = n-1;
int res = -1;
while(left<=right){
int mid = (left+right)/2;
if(arr[mid] == k){
res = 1;
break;
}else if(arr[mid] < k){
left = mid + 1;
}else{
right = mid - 1;
}
}
return res;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
unordered_map<long long,int> m;
long k;
cin>>k;
long long a;
for(int i=0;i<n;i++){
cin>>a;
m[a]++;
}
if(m.find(k)!=m.end()){
cout<<1<<endl;
}
else{
cout<<-1<<endl;
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return 1
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
def position(n,arr,x):
return binary_search(arr,0,n-1,x)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: // arr is they array to search from
// x is target
function binSearch(arr, x) {
// write code here
// do not console.log
// return the 1 or -1
let l = 0;
let r = arr.length - 1;
let mid;
while (r >= l) {
mid = l + Math.floor((r - l) / 2);
// If the element is present at the middle
// itself
if (arr[mid] == x)
return 1;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
r = mid - 1;
// Else the element can only be present
// in right subarray
else
l = mid + 1;
}
// We reach here when element is not
// present in array
return -1;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair of element from the array.
Note: AND is bitwise '&' operator.The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each test- case has two lines of the input, the first line contains an integer denoting the size of an array N and the second line of input contains N positive integers
Constraints:
1 <= T <= 50
1 <= N <= 10000
1 <= arr[i] <= 10000For each testcase, print Maximum AND Value of a pair in a separate line.Input:
2
4
4 8 12 16
4
4 8 16 2
Output:
8
0
Explanation:
Testcase 1: Pair (8, 12) has the Maximum AND Value i. e. 8.
Testcase 2: Maximum AND Value is 0., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
// Utility function to check number of elements
// having set msb as of pattern
int checkBit(int pattern, int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum and value pair
int maxAND (int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element having set msb
count = checkBit(res | (1 << bit),arr,n);
// if count >= 2 set particular bit in result
if ( count >= 2 )
res |= (1 << bit);
}
return res;
}
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
// Driver function
int main()
{
#ifndef ONLINE_JUDGE
#endif
int t;
cin>>t;
while(t>0)
{
t--;
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<maxAND(arr,n)<<endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Walter white is considered very intelligent person. He has a problem to solve. As he is suffering from cancer, can you help him solve it?
Given two integer arrays C and S of length c and s respectively. Index i of array S can be considered good if a subarray of length c can be formed starting from index i which is complimentary to array C.
Two arrays A, B of same length are considered complimentary if any cyclic permutation of A satisfies the property (A[i]- A[i-1]=B[i]-B[i-1]) for all i from 2 to length of A (1 indexing).
Calculate number of good positions in S .
<a href="https://mathworld.wolfram.com/CyclicPermutation.html">Cyclic Permutation</a>
1 2 3 4 has 4 cyclic permutations 2 3 4 1, 3 4 1 2, 4 1 2 3,1 2 3 4First line contains integer s (length of array S).
Second line contains s space separated integers of array S.
Third line contain integer c (length of array C).
Forth line contains c space separated integers of array C.
Constraints:
1 <= s <=1000000
1 <= c <=1000000
1 <= S[i], C[i] <= 10^9
Print the answer.
Input :
9
1 2 3 1 2 4 1 2 3
3
1 2 3
Output :
4
Explanation :
index 1- 1 2 3 matches with 1 2 3
index 2- 2 3 1 matches with 2 3 1(2 3 1 is cyclic permutation of 1 2 3)
index 3- 3 1 2 matches with 3 1 2(3 1 2 is cyclic permutation of 1 2 3)
index 7- 1 2 3 matches with 1 2 3
Input :
4
3 4 3 4
2
1 2
Output :
3
, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
// #define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 2000015
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
int A[sz],B[sz],C[sz],D[sz],E[sz],F[sz],G[sz];
int n,m;
signed main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>A[i];
F[i]=A[i];
}
cin>>m;
for(int i=0;i<m;i++)
{
cin>>B[i];
G[i]=B[i];
C[m-i-1]=B[i];
}
C[m]=-500000000;
for(int i=0;i<n;i++)
{
C[i+m+1]=A[n-i-1];
}
int l=0,r=0;
for(int i=1;i<=n+m;i++)
{
if(i<=r)
{
E[i]=min(r-i+1,E[i-l]);
}
while(i+E[i]<=n+m && C[E[i]]-C[0]==C[i+E[i]]-C[i])
E[i]++;
if(i+E[i]-1>r) {
l=i;r=i+E[i]-1;
}
}
for(int i=0;i<m;i++)
{
C[i]=B[i];
}
for(int i=0;i<n;i++)
{
C[i+m+1]=A[i];
}
for(int i=0;i<n;i++)
{
A[i]=E[n+m-i];
}
l=0;
r=0;
for(int i=1;i<=n+m;i++)
{
if(i<=r)
{
D[i]=min(r-i+1,D[i-l]);
}
while(i+D[i]<=n+m && C[D[i]]-C[0]==C[i+D[i]]-C[i])
D[i]++;
if(i+D[i]-1>r) {
l=i;r=i+D[i]-1;
}
}
// cout<<0<<" ";
for(int i=0;i<n;i++)
{
B[i]=D[i+m+1];
// cout<<A[i]<<" ";
}
// cout<<endl;
// for(int i=0;i<n;i++)
// {
// cout<<B[i]<<" ";
// }cout<<endl;
// for(int i=0;i<=n;i++)
// {
// cout<<i<<" ";
// }
// cout<<endl;
int cnt=0;
vector<pii> xx,yy;
for(int i=0;i<=n;i++){
int a=0;
int b=0;
if(i>0) a=A[i-1];
if(i<n) b=B[i];
// cout<<i<<" "<<a<<" "<<b<<endl;
if(a+b>=m && (a==0 || b==0 ||(F[i]-F[i-1]==G[0]-G[m-1])))
{xx.pu(mp(i-a,i+b-m)); }
if(a==m) xx.pu(mp(i-a,i-a));
if(b==m ) xx.pu(mp(i,i));
}
sort(xx.begin(),xx.end());
for(int i=0;i<xx.size();i++)
{ // cout<<xx[i].fi<<" "<<xx[i].se<<endl;
if(yy.size()==0) yy.pu(mp(xx[i].fi,xx[i].se));
else{
int p=yy.size()-1;
// cout<<i<<" "<<xx[i].fi<<" "<<xx[i].se<<" " <<yy[p].se<<endl;
if(yy[p].se>=xx[i].se) continue;
if(yy[p].se>=xx[i].fi) yy[p].se=xx[i].se;
else yy.pu(mp(xx[i].fi,xx[i].se));
}
}
for(int i=0;i<yy.size();i++)
{ // cout<<yy[i].fi<<" "<<yy[i].se<<endl;
cnt+=yy[i].se-yy[i].fi+1;
}
cout<<cnt<<endl;
}, 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: N people are waiting in line to get food. The ith person takes Arr[i] amount of time to get food. A person is happy if the amount of time he has to wait is less than or equal to the amount of time he takes to get food. The time a person waits is the sum of time people in front of him take to get food. Now you have to rearrange the people in the line such that the maximum number of people in the line are happy and report the maximum number of people that can be happy.The first line of input contains a single integer N.
The second line of input contains N integers denoting Arr[i].
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the maximum number of people that can be happy after the rearrangement.Sample Input
4
3 1 3 10
Sample Output
3
Explanation:
The optimal arrangement is 1 3 10 3
This way person 1 waits 0 units so he is happy.
Person 2 waits for 1 unit so he is happy.
Person 3 waits for 4 units so is happy.
Person 4 waits 14 units so he is unhappy., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(in.readLine());
String s[]=in.readLine().split(" ");
long[] a=new long[n];
for(int i=0;i<n;i++){
a[i]=Long.parseLong(s[i]);
}
long sum=0;
int count=0;
Arrays.sort(a);
for(int i=0;i<n;i++){
if(sum<=a[i]){
sum+=a[i];
count++;
}
}
System.out.print(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are waiting in line to get food. The ith person takes Arr[i] amount of time to get food. A person is happy if the amount of time he has to wait is less than or equal to the amount of time he takes to get food. The time a person waits is the sum of time people in front of him take to get food. Now you have to rearrange the people in the line such that the maximum number of people in the line are happy and report the maximum number of people that can be happy.The first line of input contains a single integer N.
The second line of input contains N integers denoting Arr[i].
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the maximum number of people that can be happy after the rearrangement.Sample Input
4
3 1 3 10
Sample Output
3
Explanation:
The optimal arrangement is 1 3 10 3
This way person 1 waits 0 units so he is happy.
Person 2 waits for 1 unit so he is happy.
Person 3 waits for 4 units so is happy.
Person 4 waits 14 units so he is unhappy., I have written this Solution Code: n = int(input())
li = input().split()
li = list(map(int, li))
curr = 0
ans = 0
li.sort()
for i in li:
if i >= curr:
curr += i
ans += 1
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are waiting in line to get food. The ith person takes Arr[i] amount of time to get food. A person is happy if the amount of time he has to wait is less than or equal to the amount of time he takes to get food. The time a person waits is the sum of time people in front of him take to get food. Now you have to rearrange the people in the line such that the maximum number of people in the line are happy and report the maximum number of people that can be happy.The first line of input contains a single integer N.
The second line of input contains N integers denoting Arr[i].
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the maximum number of people that can be happy after the rearrangement.Sample Input
4
3 1 3 10
Sample Output
3
Explanation:
The optimal arrangement is 1 3 10 3
This way person 1 waits 0 units so he is happy.
Person 2 waits for 1 unit so he is happy.
Person 3 waits for 4 units so is happy.
Person 4 waits 14 units so he is unhappy., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n;i++) cin >> arr[i];
sort(arr,arr+n);
int cur=0;
int ans = 0;
for(int i = 0; i < n;i++){
if(cur <= arr[i]) cur += arr[i],ans++;
}
cout << ans << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 β€ A, B, C β€ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
String a = in.readLine();
String[] nums = a.split(" ");
long[] l = new long[3];
for(int i=0; i<3; i++){
l[i] = Long.parseLong(nums[i]);
}
Arrays.sort(l);
System.out.print(l[1]);
}
catch(Exception e){
System.out.println(e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 β€ A, B, C β€ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code:
//#define ASC
//#define DBG_LOCAL
#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 <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#define int long long
// #define int __int128
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define endl '\n'
#define fi first
#define se second
// const int mod = 1e9 + 7;
const int mod=998'244'353;
const long long INF = 2e18 + 10;
// const int INF=1e9+10;
#define readv(x, n) \
vector<int> x(n); \
for (auto &i : x) \
cin >> i;
template <typename T>
using v = vector<T>;
template <typename T>
using vv = vector<vector<T>>;
template <typename T>
using vvv = vector<vector<vector<T>>>;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<vector<double>> vvd;
typedef pair<int, int> pii;
int multiply(int a, int b, int in_mod) { return (int)(1LL * a * b % in_mod); }
int mult_identity(int a) { return 1; }
const double PI = acosl(-1);
auto power(auto a, auto b, const int in_mod)
{
auto prod = mult_identity(a);
auto mult = a % 2;
while (b != 0)
{
if (b % 2)
{
prod = multiply(prod, mult, in_mod);
}
if(b/2)
mult = multiply(mult, mult, in_mod);
b /= 2;
}
return prod;
}
auto mod_inv(auto q, const int in_mod)
{
return power(q, in_mod - 2, in_mod);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define stp cout << fixed << setprecision(20);
void solv()
{
int A ,B, C;
cin>>A>>B>>C;
vector<int> values;
values.push_back(A);
values.push_back(B);
values.push_back(C);
sort(all(values));
cout<<values[1]<<endl;
}
void solve()
{
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++)
{
// cout<<"Case #"<<i<<": ";
solv();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#else
#ifdef ASC
namespace fs = std::filesystem;
std::string path = "./";
string filename;
for (const auto & entry : fs::directory_iterator(path)){
if( entry.path().extension().string() == ".in"){
filename = entry.path().filename().stem().string();
}
}
if(filename != ""){
string input_file = filename +".in";
string output_file = filename +".out";
if (fopen(input_file.c_str(), "r"))
{
freopen(input_file.c_str(), "r", stdin);
freopen(output_file.c_str(), "w", stdout);
}
}
#endif
#endif
// auto clk = clock();
// -------------------------------------Code starts here---------------------------------------------------------------------
signed t = 1;
// cin >> t;
for (signed test = 1; test <= t; test++)
{
// cout<<"Case #"<<test<<": ";
// cout<<endl;
solve();
}
// -------------------------------------Code ends here------------------------------------------------------------------
// clk = clock() - clk;
#ifndef ONLINE_JUDGE
// cerr << fixed << setprecision(6) << "\nTime: " << ((float)clk) / CLOCKS_PER_SEC << "\n";
#endif
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 β€ A, B, C β€ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code: lst = list(map(int, input().split()))
lst.sort()
print(lst[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Print a right angled triangle of stars of given height N as shown in example.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 10Print the Right angled triangle of height N.Sample Input:-
3
Sample Output:-
*
**
***
Sample Input:-
4
Sample Output:-
*
**
***
****, 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());
for(int i =1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*") ;
}
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Print a right angled triangle of stars of given height N as shown in example.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 10Print the Right angled triangle of height N.Sample Input:-
3
Sample Output:-
*
**
***
Sample Input:-
4
Sample Output:-
*
**
***
****, I have written this Solution Code: x=int(input(""))
for i in range(x):
for j in range(i+1):
print("*",end='')
print(""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Print a right angled triangle of stars of given height N as shown in example.The input contains a single integer N.
<b>Constraints:-</b>
1 ≤ N ≤ 10Print the Right angled triangle of height N.Sample Input:-
3
Sample Output:-
*
**
***
Sample Input:-
4
Sample Output:-
*
**
***
****, I have written this Solution Code: #include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<i+1;j++){
cout<<'*';}
cout<<endl;
}}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The coolness of the subarray of an array is the sum of elements in the subarray. The coolest subarray is the subarray having the maximum coolness, while the hottest subarray is the one having minimum coolness (obviously it can be negative). The coolest and the hottest subarrays are always <b>non-empty</b>.
Tono believes that cool and hot are happy together. The happiness of the array is the absolute difference between the coolness of its coolest and its hottest subarray.
Given an array A consisting of N elements, find its happiness.The first line of the input contains an integer N.
The next line contains N singly spaced integers A[1], A[2],...A[N]
Constraints
1 <= N <= 200000
-1000000000 <= A[i] <= 1000000000Output a single integer, the happiness of the array.
(The output may not fit into 32 bit integer datatype, use long long integer datatype instead).Sample Input
5
-1 2 -3 1 -5
Sample Output
9
Explanation: Coolest subarray of the array is [2], while the hottest subarray of the array is [-3, 1, -5]. The happiness of the array is 2-(-7)=9., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long KadanesAlgoMax(int[] a,int n)
{
long maxSum=Integer.MIN_VALUE;
long currSum=0;
for(int i=0;i<n;i++)
{
currSum+=a[i];
if(currSum>maxSum)maxSum=currSum;
if(currSum<0)currSum=0;
}
return maxSum;
}
public static long KadanesAlgoMin(int[] a,int n)
{
long minSum=Integer.MAX_VALUE;
long currSum=0;
for(int i=0;i<n;i++)
{
currSum+=a[i];
if(currSum<minSum)minSum=currSum;
if(currSum>0)currSum=0;
}
return minSum;
}
public static void main (String[] args)throws IOException {
BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(rd.readLine());
String[] s=rd.readLine().split(" ");
int[] a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s[i]);
}
System.out.print((long)Math.abs(KadanesAlgoMax(a,n)-KadanesAlgoMin(a,n)));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The coolness of the subarray of an array is the sum of elements in the subarray. The coolest subarray is the subarray having the maximum coolness, while the hottest subarray is the one having minimum coolness (obviously it can be negative). The coolest and the hottest subarrays are always <b>non-empty</b>.
Tono believes that cool and hot are happy together. The happiness of the array is the absolute difference between the coolness of its coolest and its hottest subarray.
Given an array A consisting of N elements, find its happiness.The first line of the input contains an integer N.
The next line contains N singly spaced integers A[1], A[2],...A[N]
Constraints
1 <= N <= 200000
-1000000000 <= A[i] <= 1000000000Output a single integer, the happiness of the array.
(The output may not fit into 32 bit integer datatype, use long long integer datatype instead).Sample Input
5
-1 2 -3 1 -5
Sample Output
9
Explanation: Coolest subarray of the array is [2], while the hottest subarray of the array is [-3, 1, -5]. The happiness of the array is 2-(-7)=9., I have written this Solution Code: l = int(input())
arr = list(map(int,input().split()))
maxSum = arr[0]
currSum =0
maxSumB = arr[0]
currSumB = 0
for j in range(0,l):
currSum = currSum + arr[j]
if(maxSum>currSum):
maxSum = currSum
if(currSum>0):
currSum = 0
currSumB = currSumB + arr[j]
if(maxSumB<currSumB):
maxSumB = currSumB
if(currSumB<0):
currSumB = 0
print(abs(maxSumB-maxSum)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The coolness of the subarray of an array is the sum of elements in the subarray. The coolest subarray is the subarray having the maximum coolness, while the hottest subarray is the one having minimum coolness (obviously it can be negative). The coolest and the hottest subarrays are always <b>non-empty</b>.
Tono believes that cool and hot are happy together. The happiness of the array is the absolute difference between the coolness of its coolest and its hottest subarray.
Given an array A consisting of N elements, find its happiness.The first line of the input contains an integer N.
The next line contains N singly spaced integers A[1], A[2],...A[N]
Constraints
1 <= N <= 200000
-1000000000 <= A[i] <= 1000000000Output a single integer, the happiness of the array.
(The output may not fit into 32 bit integer datatype, use long long integer datatype instead).Sample Input
5
-1 2 -3 1 -5
Sample Output
9
Explanation: Coolest subarray of the array is [2], while the hottest subarray of the array is [-3, 1, -5]. The happiness of the array is 2-(-7)=9., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 200005;
// 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 sum = 0;
int mx = 0;
For(i, 0, n){
sum += arr[i];
if(sum < 0){
sum = 0;
}
mx = max(sum, mx);
}
if(mx > 0)
return mx;
// all elements negative
mx = -10000000000LL;
For(i, 0, n){
mx = max(mx, arr[i]);
}
return mx;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
cin>>n;
For(i, 0, n){
cin>>arr[i];
}
int v1 = kadane();
For(i, 0, n){
arr[i]=-1*arr[i];
}
int v2 = kadane();
cout<<v1+v2;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |