Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., I have written this Solution Code: def result(arr,n,k):
minnumber = n + 1
start = 0
end = 0
curr_sum = 0
while(end < n):
while(curr_sum < k and end < n):
curr_sum += arr[end]
end += 1
while( curr_sum >= k and start < n):
if (end - start < minnumber):
minnumber = end - start
curr_sum -= arr[start]
start += 1
return minnumber
n = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(result(arr, n[0], n[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Piyush has N chocolates in his extremely huge bag. He wants to buy some chocolates (maybe 0) so that the total number of chocolates he has in his bag can never be fairly divided into piles.
A division is considered fair if there are at least 2 piles and each pile has more than 1 chocolate. Moreover, each pile should contain an equal number of chocolates.
You need to help Piyush find the minimum number of chocolates he needs to buy.The first and the only line of input contains N, the total number of chocolates Piyush has in his bag currently.
Constraints
2 ≤ N ≤ 1000000000The output should contain only one integer, the minimum number of chocolates Piyush needs to buy so that the total number of chocolates can never be fairly divided.Sample Input 1
8
Sample Output 1
3
Sample Input 2
17
Sample Output 2
0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static boolean isPrime(long n)
{
boolean ans=true;
if(n<=3)
{
ans=true;
}
else if(n%2==0 || n%3==0)
{
ans=false;
}
else
{
for(int i=5;i*i<=n;i+=6)
{
if(n%i==0 || n%(i+2)==0)
{
ans=false;
break;
}
}
}
return ans;
}
public static void main (String[] args) throws IOException{
BufferedReader scan=new BufferedReader(new InputStreamReader(System.in));
long n=Long.parseLong(scan.readLine());
long ans=0;
while(true)
{
if(isPrime(n+ans))
{
break;
}
ans++;
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Piyush has N chocolates in his extremely huge bag. He wants to buy some chocolates (maybe 0) so that the total number of chocolates he has in his bag can never be fairly divided into piles.
A division is considered fair if there are at least 2 piles and each pile has more than 1 chocolate. Moreover, each pile should contain an equal number of chocolates.
You need to help Piyush find the minimum number of chocolates he needs to buy.The first and the only line of input contains N, the total number of chocolates Piyush has in his bag currently.
Constraints
2 ≤ N ≤ 1000000000The output should contain only one integer, the minimum number of chocolates Piyush needs to buy so that the total number of chocolates can never be fairly divided.Sample Input 1
8
Sample Output 1
3
Sample Input 2
17
Sample Output 2
0, I have written this Solution Code: def prime(n):
for i in range(2,int(n**.5)+1):
if(n%i==0):
return False
return True
def check(n):
if(n%2==0):
n +=1
while(True):
if(prime(n)==True):
return n
n +=2;
return n
n=int(input())
x=check(n)
print(x-n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Piyush has N chocolates in his extremely huge bag. He wants to buy some chocolates (maybe 0) so that the total number of chocolates he has in his bag can never be fairly divided into piles.
A division is considered fair if there are at least 2 piles and each pile has more than 1 chocolate. Moreover, each pile should contain an equal number of chocolates.
You need to help Piyush find the minimum number of chocolates he needs to buy.The first and the only line of input contains N, the total number of chocolates Piyush has in his bag currently.
Constraints
2 ≤ N ≤ 1000000000The output should contain only one integer, the minimum number of chocolates Piyush needs to buy so that the total number of chocolates can never be fairly divided.Sample Input 1
8
Sample Output 1
3
Sample Input 2
17
Sample Output 2
0, I have written this Solution Code: #include <bits/stdc++.h>
// #define ll long long
using namespace std;
int main(){
long long x,n;
cin>>n;
for(int i=n;i<n+500;i++){
x=i;
long long p=sqrt(x);
for(int j=2;j<=p;j++){
if(x%j==0){goto f;}
}
cout<<i-n;
return 0;
f:;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are playing a number game where you will receive a number every day for the next N days. Game goes as follows : Every day you get a number, you include it into your list. You take the maximum number in the list, half it and put it back into the list. Initially the list is empty.
You play the game for N days, what is the maximum number in your list after N days?First line contains a single integer N.
Next line contains N space separated integers.
<b>Constraints</b>
1 <= N <= 10<sup>5</sup>
1 <= numbers <= 10<sup>9</sup>A single integer denoting the answer.Input:
4
12 10 4 9
Output:
5
Explanation:
first day => 12 => list = {12} => maximum is 12 so list = {6}
second day => 10 => list = {6, 10} => maximum is 10 so list = {6, 5}
third day => 4 => list = {6, 5, 4} => maximum is 6 so list = {3, 5, 4}
fourth day => 9 => list = { 3, 5, 4, 9} => maximum is 9 so list = {3, 5, 4, 4}, I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
int n=Integer.parseInt(in.next());
PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder());
for(int i=0;i<n;i++){
int x=Integer.parseInt(in.next());
pq.add(x);
x=pq.poll();
pq.add(x/2);
}
out.print(pq.peek());
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given an array of several arrays that each contain integers and your goal is to write a function that
will sum up all the numbers in all the arrays. For example, if the input is [[3, 2], [1], [4, 12]] then your
program should output 22 because 3 + 2 + 1 + 4 + 12 = 22An array containing arrays which can contain any number of elements.Sum of all the elements in all of the arrays.Sample input:-
[[3, 2], [1], [4, 12]]
Sample output:-
22
Explanation:-
3 + 2 + 1 + 4 + 12 = 22, I have written this Solution Code: function sum_array(arr) {
// store our final answer
var sum = 0;
// loop through entire array
for (var i = 0; i < arr.length; i++) {
// loop through each inner array
for (var j = 0; j < arr[i].length; j++) {
// add this number to the current final sum
sum += arr[i][j];
}
}
console.log(sum);
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list consisting of N nodes and an integer K, your task is to reverse every alternate K nodes.
In other words , you have to reverse first k nodes , then skip the next k nodes , then reverse next k nodes and so on .
NOTE: if there are not K nodes to reverse then reverse all the nodes left (See example for better understanding)<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>ReverseAlternateK()</b> that takes head node and K as parameter.
Constraints:
1 <=k<=N<=10000Return the head of the modified linked list.Sample Input:-
8 3
1 2 3 4 5 6 7 8
Sample Output:-
3 2 1 4 5 6 8 7
Explanation:
[{1 , 2 ,3 } , {4, 5 , 6} , {7 , 8}]
Reverse 1st segment.
Skip the 2nd segment.
Reverse the 3rd segment. , I have written this Solution Code: public static Node ReverseAlternateK(Node head,int k){
Node current = head;
Node next = null, prev = null;
int count = 0;
while (current != null && count < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
if (head != null) {
head.next = current;
}
count = 0;
while (count < k - 1 && current != null) {
current = current.next;
count++;
}
if (current != null) {
current.next = ReverseAlternateK(current.next, k);
}
return prev;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: IPL is around the corner. Nitin Menon loves to represent all the numbers in the form of 1 or 0 or some combination of it. Given a number n, you are asked to help him represent n as a sum of some (not necessarily distinct) binary decimals (a number is called a binary decimal if it's a positive integer and all digits in its decimal notation are either 0 or 1). Compute the smallest number of binary decimals required for that.The input consists of an integer n denoting the number to be represented.
<b>Constraints</b>
1 ≤ n ≤ 10<sup>9</sup>Print the smallest number of binary decimals required to represent n as a sum.<b>Sample Input 1</b>
121
<b>Sample Output 1</b>
2
<b>Sample Input 2</b>
5
<b>Sample Output 2</b>
5, I have written this Solution Code: #include <bits/stdc++.h>
using i64 = long long;
int main() {
int t=1;
// std::cin >> t;
while (t--) {
int n;
std::cin >> n;
auto s = std::to_string(n);
std::cout << (*std::max_element(s.begin(), s.end()) - '0') << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <em>Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970</em>
Implement the function <code>msSinceEpoch</code>, which returns milliseconds since the Unix epoch. (Use JS built-in functions)The function takes no argumentThe function returns a numberconsole. log(msSinceEpoch()) // prints 1642595040109, I have written this Solution Code:
function msSinceEpoch() {
// write code here
// return the output , do not use console.log here
return Date.now()
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an sorted array <b>Arr[]</b> of size <b>N</b>, containing both <b>negative</b> and <b>positive</b> integers, you need to print the squared sorted output.
<b>Note</b> Try using two pointer approachThe first line of input contains T, denoting the number of test cases. Each testcase contains 2 lines. The first line contains the N size of the array. The second line contains elements of an array separated by space.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10000
-10000 ≤ A[i] ≤ 10000
The Sum of N over all test cases does not exceed 10^6For each test case you need to print the sorted squared output in new lineInput:
1
5
-7 -2 3 4 6
Output:
4 9 16 36 49, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main
{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int n = Integer.parseInt(read.readLine());
int[] arr = new int[n];
String str[] = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
arr = sortedSquares(arr);
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}
public static int[] sortedSquares(int[] A) {
int[] nums = new int[A.length];
int k=A.length-1;
int i=0, j=A.length-1;
while(i<=j){
if(Math.abs(A[i]) <= Math.abs(A[j])){
nums[k--] = A[j]*A[j];
j--;
}
else{
nums[k--] = A[i]*A[i];
i++;
}
}
return nums;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an sorted array <b>Arr[]</b> of size <b>N</b>, containing both <b>negative</b> and <b>positive</b> integers, you need to print the squared sorted output.
<b>Note</b> Try using two pointer approachThe first line of input contains T, denoting the number of test cases. Each testcase contains 2 lines. The first line contains the N size of the array. The second line contains elements of an array separated by space.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10000
-10000 ≤ A[i] ≤ 10000
The Sum of N over all test cases does not exceed 10^6For each test case you need to print the sorted squared output in new lineInput:
1
5
-7 -2 3 4 6
Output:
4 9 16 36 49, I have written this Solution Code: t = int(input())
for i in range(t):
n = int(input())
for i in sorted(map(lambda j:int(j)**2,input().split())):
print(i,end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two strings S and T. Determine whether it is possible to make S and T equal by doing the following operation at most once:
Choose two adjacent characters in S and swap them.
Note that it is allowed to choose not to do the operation.The input line contains two strings in separate lines.
<b>Constraints</b>
Each of S and T is a string of length between 2 and 100 (inclusive) consisting of lowercase English letters.
S and T have the same length.If it is possible to make S and T equal by doing the operation in Problem Statement at most once, print Yes; otherwise, print No.<b>Sample Input 1</b>
abc
acb
<b>Sample Output 1 </b>
Yes
<b>Sample Input 2</b>
aabb
bbaa
<b>Sample Output 2 </b>
No
<b>Sample Input 3</b>
abcde
abcde
<b>Sample Output 3 </b>
Yes, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
string S,T; cin >> S >> T;
string ans = "No";
if(S == T) ans = "Yes";
for(int i=0; i<S.size(); i++){
if(S[i] != T[i]){
if(0 < i){
swap(S[i-1],S[i]);
if(S == T) ans = "Yes";
swap(S[i-1],S[i]);
}
if(i+1 < S.size()){
swap(S[i],S[i+1]);
if(S == T) ans = "Yes";
swap(S[i],S[i+1]);
}
break;
}
}
cout << ans << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S of length N. Find a string R of length N such that hamming distance between S and R is 1 and R is lexicographically smallest possible.
Note: The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.Input contains a single string S containing english lowercase alphabets.
Constraints:
1 <= |S| <= 100000Print a single string R, such that R contains english lowercase letters only.Sample Input
aba
Sample Output
aaa
Explanation: hamming distance between aba and aaa = 1 and aaa is lexicographically smallest such string.
Sample Input
a
Sample Output
b, 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 s1 = br.readLine();
int count=0;
StringBuilder sb = new StringBuilder();
boolean entered = false;
boolean zEntered = false;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != 'a' && s1.charAt(i) != 'z' && !entered &&!zEntered) {
sb.append('a');
entered = true;
}
else if(s1.charAt(i) == 'z' && !zEntered &&!entered)
{
sb.append('a');
zEntered = true;
}
else
{
if((count+1)==s1.length())
{
int c = s1.charAt(i);
c++;
sb.append((char)c);
}
else
{
sb.append(s1.charAt(i));
}
count++;
}
}
System.out.print(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S of length N. Find a string R of length N such that hamming distance between S and R is 1 and R is lexicographically smallest possible.
Note: The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.Input contains a single string S containing english lowercase alphabets.
Constraints:
1 <= |S| <= 100000Print a single string R, such that R contains english lowercase letters only.Sample Input
aba
Sample Output
aaa
Explanation: hamming distance between aba and aaa = 1 and aaa is lexicographically smallest such string.
Sample Input
a
Sample Output
b, I have written this Solution Code: s = input()
ans = ""
f = 0
for i in range(len(s)):
if(s[i] != "a" and f == 0):
ans += "a"
f = 1
continue
ans += s[i]
if(f == 0):
ans = ans[:len(ans)-1]+"b"
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S of length N. Find a string R of length N such that hamming distance between S and R is 1 and R is lexicographically smallest possible.
Note: The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.Input contains a single string S containing english lowercase alphabets.
Constraints:
1 <= |S| <= 100000Print a single string R, such that R contains english lowercase letters only.Sample Input
aba
Sample Output
aaa
Explanation: hamming distance between aba and aaa = 1 and aaa is lexicographically smallest such string.
Sample Input
a
Sample Output
b, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
string s;
cin>>s;
int n=s.length();
for(int i=0;i<n;++i){
if(s[i]!='a'){
s[i]='a';
break;
}
if(i==n-1){
s[i]='b';
}
}
cout<<s;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers containing only 0 or 1. You can do the following operations on the array:
<ul><li>swap elements at two indices</li><li>choose one index and change its value from 0 to 1 or vice-versa.</li></ul>
You have to do the minimum number of the above operations such that the final array is non-decreasing.
<b>Note</b> Consider 1 based Array-indexingThe first line of input contains a single integer N.
The second line of input contains N space-separated integers denoting the array.
Constraints:
1 ≤ N ≤ 100000
elements of the array are 0 or 1.Minimum number of moves required such that the final array is non- decreasing.Sample Input 1
5
1 1 0 0 1
Sample Output 1
2
Explanation:
Swap indices (1, 3)
Swap indices (2, 4)
Sample Input 2
5
0 0 1 1 1
Sample Output 2
0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String array[] = br.readLine().trim().split(" ");
boolean decreasingOrder = false;
int[] arr = new int[n];
int totalZeroCount = 0,
totalOneCount = 0;
for(int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(array[i]);
if(i != 0 && arr[i] < arr[i - 1])
decreasingOrder = true;
if(arr[i] % 2 == 0)
++totalZeroCount;
else
++totalOneCount;
}
if(!decreasingOrder) {
System.out.println("0");
} else {
int oneCount = 0;
for(int i = 0; i < totalZeroCount; i++) {
if(arr[i] == 1)
++oneCount;
}
System.out.println(oneCount);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers containing only 0 or 1. You can do the following operations on the array:
<ul><li>swap elements at two indices</li><li>choose one index and change its value from 0 to 1 or vice-versa.</li></ul>
You have to do the minimum number of the above operations such that the final array is non-decreasing.
<b>Note</b> Consider 1 based Array-indexingThe first line of input contains a single integer N.
The second line of input contains N space-separated integers denoting the array.
Constraints:
1 ≤ N ≤ 100000
elements of the array are 0 or 1.Minimum number of moves required such that the final array is non- decreasing.Sample Input 1
5
1 1 0 0 1
Sample Output 1
2
Explanation:
Swap indices (1, 3)
Swap indices (2, 4)
Sample Input 2
5
0 0 1 1 1
Sample Output 2
0, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[i];
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i]==0) cnt++;
}
int ans = 0;
for (int i = 0; i < cnt; i++) if (a[i] == 1) ans++;
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers containing only 0 or 1. You can do the following operations on the array:
<ul><li>swap elements at two indices</li><li>choose one index and change its value from 0 to 1 or vice-versa.</li></ul>
You have to do the minimum number of the above operations such that the final array is non-decreasing.
<b>Note</b> Consider 1 based Array-indexingThe first line of input contains a single integer N.
The second line of input contains N space-separated integers denoting the array.
Constraints:
1 ≤ N ≤ 100000
elements of the array are 0 or 1.Minimum number of moves required such that the final array is non- decreasing.Sample Input 1
5
1 1 0 0 1
Sample Output 1
2
Explanation:
Swap indices (1, 3)
Swap indices (2, 4)
Sample Input 2
5
0 0 1 1 1
Sample Output 2
0, I have written this Solution Code: n=int(input())
l=list(map(int,input().split()))
x=l.count(0)
c=0
for i in range(0,x):
if(l[i]==1):
c+=1
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, 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());
int[] val = new int[n];
for(int i=0; i<n; i++){
val[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int[] freq = new int[n];
for(int i=0; i<n; i++){
freq[i] = Integer.parseInt(st.nextToken());
}
int k = Integer.parseInt(br.readLine());
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (val[j] < val[i]) {
int temp = val[i];
val[i] = val[j];
val[j] = temp;
int temp1 = freq[i];
freq[i] = freq[j];
freq[j] = temp1;
}
}
}
int element=0;
for(int i=0; i<n; i++){
for(int j=0; j<freq[i]; j++){
element++;
int value = val[i];
if(element==k){
System.out.print(value);
break;
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: def myFun():
n = int(input())
arr1 = list(map(int,input().strip().split()))
arr2 = list(map(int,input().strip().split()))
k = int(input())
arr = []
for i in range(n):
arr.append((arr1[i], arr2[i]))
arr.sort()
c = 0
for i in arr:
k -= i[1]
if k <= 0:
print(i[0])
return
myFun()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define inf 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int N;ll K;
cin>>N;
int c=0;
pair<int, ll> A[N];
for(int i=0;i<N;++i){
cin >> A[i].first ;
}
for(int i=0;i<N;++i){
cin >> A[i].second ;
}
cin>>K;
sort(A, A+N);
for(int i=0;i<N;++i){
K -= A[i].second;
if(K <= 0){
cout << A[i].first << endl;;
break;
}
}
#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: How would you add your own method to the Array object so
the following code would work?
const arr = [1, 2, 3]
console. log(arr.average())
// 2input will be an array, run like this
const anyArray = [5,6...]
anyArray.average should return average of the arraysAverage of the given arrayconst myArray = [1,2,3,4,5]
console.log(myArray.average())
// 3, I have written this Solution Code: Array.prototype.average = function() {
// calculate sum
var sum = this.reduce(function(prev, cur) { return prev + cur; });
// return sum divided by number of elements
return sum / this.length;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code:
char Race(int A, int B, int C){
if(abs(C-A)==abs(C-B)){return 'D';}
if(abs(C-A)>abs(C-B)){return 'S';}
else{
return 'N';}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code: def Race(A,B,C):
if abs(C-A) ==abs(C-B):
return 'D'
if abs(C-A)>abs(C-B):
return 'S'
return 'N'
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code:
char Race(int A, int B, int C){
if(abs(C-A)==abs(C-B)){return 'D';}
if(abs(C-A)>abs(C-B)){return 'S';}
else{
return 'N';}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code: static char Race(int A,int B,int C){
if(Math.abs(C-A)==Math.abs(C-B)){return 'D';}
if(Math.abs(C-A)>Math.abs(C-B)){return 'S';}
else{
return 'N';}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the given JS Class and also its static methods. Static member can be accessed before any objects of its class are created, and without reference to any object
The static methods works as follows
<code>returnTotalAccounts</code> : return the total number of accounts created using this class
<code> returnTotalBalance </code>: returns the total balance of all accounts created using this class
Note: Input and Output are handled by the judgeThe class Account constructor takes 2 arguments
<ul><li>accountName(string), balance(number) </li></ul>
Both the static methods <code> returnTotalAccounts</code> and <code>returnTotalBalance</code> doesn't take any argumentsThe method <code> returnTotalAccounts</code> returns totalAccounts(Number)
The method <code>returnTotalBalance</code> returns totalBalance(Number)let user1 = new Account("newton1102", 1000)
let user2 = new Account("newton2231", 2000)
let user3 = new Account("newton1212", 500)
let totalAccounts = account.returnTotalAccounts()
let totalBal = account.returnTotalBalance()
console.log(totalAccounts)
cosole.log(totalBal)
//3
//3500
</code>
</pre>, I have written this Solution Code: class Account {
static accounts = 0
static totalBalance = 0
constructor(accountNum, balance) {
this.accountNum = accountNum;
this.balance = balance;
Account.accounts += 1
Account.totalBalance += balance
}
deposit = function (amount) {
this.balance += amount;
Account.totalBalance += amount
};
static returnTotalAccounts(){
return Account.accounts
}
static returnTotalBalance(){
return Account.totalBalance
}
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two strings A and B. You need to check if string A can be constructed from string B. Print Yes if it can be constructed else print No.The input consists of two space- separated integers A and B.
<b>Constraints</b>
The length of A is lesser than or equal to the length of B.
The strings consist of lower case English alphabets.Print Yes if A can be constructed from B else print No.<b>Sample Input 1</b>
a b
<b>Sample Output 1</b>
No
<b>Sample Input 2</b>
aa aab
<b>Sample Output 2</b>
Yes, I have written this Solution Code: #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define int long long int
#define endl '\n'
void fastio()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
using namespace __gnu_pbds;
const int M = 1e9 + 7;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// Author:Abhas
void solution()
{
string t;
string s;
cin >> t >> s;
int counter[26] = {0};
// Traverse a loop through the entire String of magazine where char ch stores the char at the index of magazine...
for (char ch : s)
counter[ch - 'a']++;
// Run another for loop for ransomNote...
for (char ch : t)
// If the charachter doesn't exists in magazine for ransomNote, we return false...
if (counter[ch - 'a']-- <= 0)
{
cout << "No" << endl;
return;
}
cout << "Yes" << endl;
}
signed main()
{
fastio();
int t = 1;
// cin >> t;
while (t--)
{
solution();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have a grid of n rows and m columns (n x m grid). Ketani wants to reach cell (n, m) from cell (1, 1). There are some blocked cells in the path. Ketani cannot move over a blocked cell in her path between (1, 1) to (n, m). Moreover, she can move only right and down.
Tono knows that there is danger in the cell (n, m). So, she wants to find the minimum number of cells she needs to block so that Ketani can never reach cell (n, m). She cannot block cells (1, 1) and (n, m).
Since Tono is small, can you help her with this problem?The first line of the input contains 2 integers n and m.
Next n lines contain m characters. Character '.' represents that the cell is not blocked while character '#' represents that the cell is blocked.
It's guaranteed, that cells (1,1) and (n,m) are empty.
Constraints
3 <= n*m <= 1000000Output a single integer, the minimum number of cells Tono needs to block.Sample Input 1
4 4
....
#.#.
....
.#..
Sample Output 1
1
Explanation: Tono can just block the cell (1, 2).
Sample Input 2
3 4
....
.##.
....
Sample Output 2
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int endr, endc;
static char myArr[][];
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
endr = n-1;
endc = m-1;
char arr[][] = new char[n][m];
for(int i=0; i<n; i++) {
String s = sc.next();
for(int j=0; j<m; j++)
arr[i][j] = s.charAt(j);
}
myArr = arr;
solve();
int st = 0, en = 0;
st += check(myArr, 0, 1);
st += check(myArr, 1, 0);
en += check(myArr, endr, endc-1);
en += check(myArr, endr-1, endc);
System.out.println(Math.min(st, en));
}
public static void solve() {
for (int i=1; i<=endr; i++) {
for(int j=1; j<=endc; j++) {
if (myArr[i-1][j] == '#' && myArr[i][j-1] == '#')
myArr[i][j] = '#';
}
}
}
public static int check (char arr[][], int r, int c) {
if (r >= 0 && r <= endr && c >= 0 && c <= endc && arr[r][c] == '.')
return 1;
return 0;
}
public static void print(char arr[][], int n, int m) {
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have a grid of n rows and m columns (n x m grid). Ketani wants to reach cell (n, m) from cell (1, 1). There are some blocked cells in the path. Ketani cannot move over a blocked cell in her path between (1, 1) to (n, m). Moreover, she can move only right and down.
Tono knows that there is danger in the cell (n, m). So, she wants to find the minimum number of cells she needs to block so that Ketani can never reach cell (n, m). She cannot block cells (1, 1) and (n, m).
Since Tono is small, can you help her with this problem?The first line of the input contains 2 integers n and m.
Next n lines contain m characters. Character '.' represents that the cell is not blocked while character '#' represents that the cell is blocked.
It's guaranteed, that cells (1,1) and (n,m) are empty.
Constraints
3 <= n*m <= 1000000Output a single integer, the minimum number of cells Tono needs to block.Sample Input 1
4 4
....
#.#.
....
.#..
Sample Output 1
1
Explanation: Tono can just block the cell (1, 2).
Sample Input 2
3 4
....
.##.
....
Sample Output 2
2, I have written this Solution Code: #pragma GCC optimize "03"
#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 mod1 = 1e9 + 7;
const int mod2 = 1e9 + 11;
int n, m;
vector<vector<char> > s;
vector<vector<int> > v[2], w[2];
signed main() {
IOS;
#ifdef LOCAL
freopen("input.txt","r", stdin);
freopen("output.txt","w",stdout);
#endif
cin >> n >> m;
s.resize(n, vector<char> (m));
v[0].resize(n, vector<int> (m, 0));
w[0].resize(n, vector<int> (m, 0));
v[1].resize(n, vector<int> (m, 0));
w[1].resize(n, vector<int> (m, 0));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)
cin >> s[i][j];
}
v[0][0][0] = v[1][0][0] = 1;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i == 0 && j == 0) continue;
if(s[i][j] == '#') continue;
if(i > 0){
v[0][i][j] = (v[0][i-1][j] + v[0][i][j]) % mod1;
v[1][i][j] = (v[1][i-1][j] + v[1][i][j]) % mod2;
}
if(j > 0){
v[0][i][j] = (v[0][i][j-1] + v[0][i][j]) % mod1;
v[1][i][j] = (v[1][i][j-1] + v[1][i][j]) % mod2;
}
}
}
if(v[0][n-1][m-1] == 0 && v[1][n-1][m-1] == 0)
return cout << 0, 0;
w[0][n-1][m-1] = w[1][n-1][m-1] = 1;
for(int i = n-1; i >= 0; i--){
for(int j = m-1; j >= 0; j--){
if(i == n-1 && j == m-1) continue;
if(s[i][j] == '#') continue;
if(i < n-1){
w[0][i][j] = (w[0][i+1][j] + w[0][i][j]) % mod1;
w[1][i][j] = (w[1][i+1][j] + w[1][i][j]) % mod2;
}
if(j < m-1){
w[0][i][j] = (w[0][i][j+1] + w[0][i][j]) % mod1;
w[1][i][j] = (w[1][i][j+1] + w[1][i][j]) % mod2;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if((i == 0 && j == 0) || (i == n-1 && j == m-1)) continue;
if(s[i][j] == '#') continue;
int x = (v[0][i][j] * w[0][i][j]) % mod1;
int y = (v[1][i][j] * w[1][i][j]) % mod2;
if(x == v[0][n-1][m-1] && y == v[1][n-1][m-1])
return cout << 1, 0;
}
}
cout << 2;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of numbers is given. It has all unique elements except one. Find the only duplicate element in that array having all other unique elements.The first line contains an integer N, the number of elements in the array.
The second line contains N integers.
Constraints:
1 <= N <= 10^5
1 <= Elements of Array <= 10^5Print the single duplicate number in the arrayInput
6
1 2 3 4 4 5
Output
4
Explanation: 4 is repeated in this array
Input:
3
2 1 2
Output:
2, I have written this Solution Code: n = int(input())
arr = list(map(int, input().split()))
l = [0] * (max(arr) + 1)
for i in arr:
l[i] += 1
for i in range(len(l)):
if l[i] > 1:
print(i), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of numbers is given. It has all unique elements except one. Find the only duplicate element in that array having all other unique elements.The first line contains an integer N, the number of elements in the array.
The second line contains N integers.
Constraints:
1 <= N <= 10^5
1 <= Elements of Array <= 10^5Print the single duplicate number in the arrayInput
6
1 2 3 4 4 5
Output
4
Explanation: 4 is repeated in this array
Input:
3
2 1 2
Output:
2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int m = 100001;
int main(){
int n,k;
cin>>n;
long long a[n],sum=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<0){
a[i]=-a[i];
}
}
sort(a,a+n);
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){cout<<a[i];return 0;}
}
cout<<sum;
}
, In this Programming Language: C++, 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: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: void magicTrick(int a, int b){
cout<<a+b/2;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: static void magicTrick(int a, int b){
System.out.println(a + b/2);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: A,B = map(int,input().split(' '))
C = A+B//2
print(C)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Siddharth, a great entrepreneur, chooses a person as an intern if he/she good at maths. Thus Sid gives a simple maths problem to the candidate based on which he decides to who is gonna get the offer. Below is the description of the problem asked by Sid
Given arr <b>A[]</b> containing <b>N</b> positive integers. The task is to check whether it is possible to divide the given array into an <b>odd number of non-empty subarrays each of odd length such that each sub-array starts and ends with an odd number</b>. If it is possible to divide the array then print "<b>YES</b>" otherwise "<b>NO</b>".
<b>Note:</b> Don't forget that the complete array is also considered as a subarrayThe first line of each test case contains N size of array. Second-line contains N space-separated elements.
<b>Constraints:</b>
1 <= N <= 999999
0 <= A[i] <= 999999999Print "YES", if it is possible to divide the array. Otherwise, print "NO". Without quotes.Sample Input 1:
5
9 0 3 1 5
Sample Output 1:
YES
Sample Input 2:
3
1 2 0
Sample Output 2:
NO
Explanation:
Testcase 1: Array {9, 0, 3, 1, 5} can be divided as {9, 0, 3}, {1}, {5}., 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 n=sc.nextInt();
int[] a=new int[n];
int counter=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]%2!=0) counter++;
}
String ans="NO";
if((a[0]%2!=0)&&(a[n-1]%2!=0)&&(n%2!=0))
ans="YES";
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Siddharth, a great entrepreneur, chooses a person as an intern if he/she good at maths. Thus Sid gives a simple maths problem to the candidate based on which he decides to who is gonna get the offer. Below is the description of the problem asked by Sid
Given arr <b>A[]</b> containing <b>N</b> positive integers. The task is to check whether it is possible to divide the given array into an <b>odd number of non-empty subarrays each of odd length such that each sub-array starts and ends with an odd number</b>. If it is possible to divide the array then print "<b>YES</b>" otherwise "<b>NO</b>".
<b>Note:</b> Don't forget that the complete array is also considered as a subarrayThe first line of each test case contains N size of array. Second-line contains N space-separated elements.
<b>Constraints:</b>
1 <= N <= 999999
0 <= A[i] <= 999999999Print "YES", if it is possible to divide the array. Otherwise, print "NO". Without quotes.Sample Input 1:
5
9 0 3 1 5
Sample Output 1:
YES
Sample Input 2:
3
1 2 0
Sample Output 2:
NO
Explanation:
Testcase 1: Array {9, 0, 3, 1, 5} can be divided as {9, 0, 3}, {1}, {5}., I have written this Solution Code:
def fun(n,arr):
if n%2==0:
if arr[0]%2==1 and arr[n-1]%2==1:
for i in range(1,n):
if arr[i]%2==1 and arr[i-1]%2==1:
return "YES"
else:
return "NO"
else:
if arr[0]%2==1 and arr[n-1]%2==1:
return "YES"
else:
return "NO"
n=int(input())
arr=[int(i) for i in input().split()]
print(fun(n,arr)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Siddharth, a great entrepreneur, chooses a person as an intern if he/she good at maths. Thus Sid gives a simple maths problem to the candidate based on which he decides to who is gonna get the offer. Below is the description of the problem asked by Sid
Given arr <b>A[]</b> containing <b>N</b> positive integers. The task is to check whether it is possible to divide the given array into an <b>odd number of non-empty subarrays each of odd length such that each sub-array starts and ends with an odd number</b>. If it is possible to divide the array then print "<b>YES</b>" otherwise "<b>NO</b>".
<b>Note:</b> Don't forget that the complete array is also considered as a subarrayThe first line of each test case contains N size of array. Second-line contains N space-separated elements.
<b>Constraints:</b>
1 <= N <= 999999
0 <= A[i] <= 999999999Print "YES", if it is possible to divide the array. Otherwise, print "NO". Without quotes.Sample Input 1:
5
9 0 3 1 5
Sample Output 1:
YES
Sample Input 2:
3
1 2 0
Sample Output 2:
NO
Explanation:
Testcase 1: Array {9, 0, 3, 1, 5} can be divided as {9, 0, 3}, {1}, {5}., I have written this Solution Code: 'use strict'
function main(input)
{
const inputList = input.split('\n');
const arrSize = Number(inputList[0].split(' ')[0])
const arr = inputList[1].split(' ').
map(x => Number(x))
if(arrSize %2 == 0 || Number(arr[0]) %2 == 0 ||
Number(arr[arrSize-1])%2 == 0)
console.log("NO")
else console.log("YES")
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N. You need to print the sum of the first N natural numbers.The first line of input contains a single integer T, the next T lines contains a single integer N.
Constraints:
1 < = T < = 100
1 < = N < = 100Print the sum of first N natural numbers for each test case in a new line.Sample Input:
2
5
10
Sample Output:
15
55, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
System.out.println(n*(n+1)/2);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N. You need to print the sum of the first N natural numbers.The first line of input contains a single integer T, the next T lines contains a single integer N.
Constraints:
1 < = T < = 100
1 < = N < = 100Print the sum of first N natural numbers for each test case in a new line.Sample Input:
2
5
10
Sample Output:
15
55, I have written this Solution Code: for t in range(int(input())):
n = int(input())
print(n*(n+1)//2), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N. You need to print the sum of the first N natural numbers.The first line of input contains a single integer T, the next T lines contains a single integer N.
Constraints:
1 < = T < = 100
1 < = N < = 100Print the sum of first N natural numbers for each test case in a new line.Sample Input:
2
5
10
Sample Output:
15
55, 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;
cout<<(n*(n+1))/2<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N intervals, the i<sup>th</sup> of them being [A<sub>i</sub>, B<sub>i</sub>], where A<sub>i</sub> and B<sub>i</sub> are positive integers. Let the union of all these intervals be S. It is easy to see that S can be uniquely represented as an union of disjoint closed intervals. Your task is to find the sum of the lengths of the disjoint closed intervals that comprises S.
For example, if you are given the intervals: [1, 3], [2, 4], [5, 7] and [7, 8], then S can be uniquely represented as the union of disjoint intervals [1, 4] and [5, 8]. In this case, the answer will be 6, as (4 - 1) + (8 - 5) = 6.The first line of the input consists of a single integer N – the number of intervals.
Then N lines follow, the i<sup>th</sup> line containing two space-separated integers A<sub>i</sub> and B<sub>i</sub>.
<b> Constraints: </b>
1 ≤ N ≤ 10<sup>4</sup>
1 ≤ A<sub>i</sub> < B<sub>i</sub> ≤ 2×10<sup>5</sup>Print a single integer, the sum of the lengths of the disjoint intervals of S.Sample Input 1:
3
1 3
3 4
6 7
Sample Output 1:
4
Sample Explanation 1:
Here, S can be uniquely written as union of disjoint intervals [1, 4] and [6, 7]. Thus, answer is (4 - 1) + (7 - 6) = 4.
Sample Input 2:
4
9 12
8 10
5 6
13 15
Sample Output 2:
7
Sample Explanation 2:
Here, S can be uniquely written as union of disjoint intervals [5, 6], [8, 12] and [13, 15]. Thus, answer is (6 - 5) + (12 - 8) + (15 - 13) = 7.
, I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << (a) << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (auto i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353; // 1e9 + 7;
const ll N = 4e5 + 100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int arr[N];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
read(n);
FOR (i, 1, n)
{
readb(a, b);
a *= 2, b *= 2;
arr[a]++;
arr[b + 1]--;
}
int ans = 0, sum = 0, cur = 0;
FOR (i, 0, N - 2)
{
sum += arr[i];
if (sum) cur++;
else ans += cur/2, cur = 0;
}
print(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: void magicTrick(int a, int b){
cout<<a+b/2;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: static void magicTrick(int a, int b){
System.out.println(a + b/2);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: A,B = map(int,input().split(' '))
C = A+B//2
print(C)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's team participated in an eating competition at FoodFest in which they are asked to eat at least P dishes.
Sara's team consists of N members where a member <b>i</b> (1 <= i <= N ) takes <b>Arr[i]</b> minutes to finish a single dish. Your task is to find the minimum time to finish at least P dishes.
Note: A member can eat a dish any number of times.First line of input contains two integers N and P. The next line of input contains N space separated integers depicting the values of Array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 10
1 <= P <= 1000000000000Print the minimum time taken to eat at least P dishes.Sample Input:-
4 10
1 2 3 4
Sample Output:-
6
Explanation:-
1st member will eat 6 dishes
2nd member will eat 3 dishes
3rd member will eat 2 dishes
4th member will eat 1 dishes
total = 12
Sample Input:-
8 8
1 1 1 1 1 1 1 1
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
long p = Long.parseLong(str[1]);
long[] arr = new long[n];
long l = 0;
long mid = 0;
long cnt = 0;
long max = 1000000000000L;
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(input[i]);
}
while (l < max) {
mid = (l + max)/2;
cnt = 0;
for (int i = 0; i < n; i++) {
cnt += mid / arr[i];
}
if (cnt < p) {
l = mid + 1;
} else {
max = mid;
}
}
System.out.println(l);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's team participated in an eating competition at FoodFest in which they are asked to eat at least P dishes.
Sara's team consists of N members where a member <b>i</b> (1 <= i <= N ) takes <b>Arr[i]</b> minutes to finish a single dish. Your task is to find the minimum time to finish at least P dishes.
Note: A member can eat a dish any number of times.First line of input contains two integers N and P. The next line of input contains N space separated integers depicting the values of Array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 10
1 <= P <= 1000000000000Print the minimum time taken to eat at least P dishes.Sample Input:-
4 10
1 2 3 4
Sample Output:-
6
Explanation:-
1st member will eat 6 dishes
2nd member will eat 3 dishes
3rd member will eat 2 dishes
4th member will eat 1 dishes
total = 12
Sample Input:-
8 8
1 1 1 1 1 1 1 1
Sample Output:-
1, I have written this Solution Code: def noofdish(B,t):
a=0
for x in B:
a += t//x
return a
def findans(A,l,r,ans,p):
if(l>r):
print(ans)
else:
m=(l+r)//2
tdish=noofdish(A,m)
if tdish >=p:
findans(A,l,m-1,m,p)
else:
findans(A,m+1,r,ans,p)
n,p=[int(x) for x in input().split()]
A=[int(x) for x in input().split()]
findans(A,1,(p//n +1)*10,(p//n +1)*10,p), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's team participated in an eating competition at FoodFest in which they are asked to eat at least P dishes.
Sara's team consists of N members where a member <b>i</b> (1 <= i <= N ) takes <b>Arr[i]</b> minutes to finish a single dish. Your task is to find the minimum time to finish at least P dishes.
Note: A member can eat a dish any number of times.First line of input contains two integers N and P. The next line of input contains N space separated integers depicting the values of Array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 10
1 <= P <= 1000000000000Print the minimum time taken to eat at least P dishes.Sample Input:-
4 10
1 2 3 4
Sample Output:-
6
Explanation:-
1st member will eat 6 dishes
2nd member will eat 3 dishes
3rd member will eat 2 dishes
4th member will eat 1 dishes
total = 12
Sample Input:-
8 8
1 1 1 1 1 1 1 1
Sample Output:-
1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int p;
int n;
cin>>n>>p;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int l=0,h=1e10;
int mid=0;
int cnt=0;
int s,y;
while(l<h){
mid=(l+h);
mid/=2;
cnt=0;
for(int i=0;i<n;i++){
s=a[i];
y=2;
cnt+=mid/a[i];
}
// cout<<l<<" "<<h<<" "<<cnt<<endl;
if(cnt<p){l=mid+1;}
else{
h=mid;
}
}
cout<<l;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: static void verticalFive(){
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
}
static void horizontalFive(){
System.out.print("* * * * *");
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: def vertical5():
for i in range(0,5):
print("*",end="\n")
#print()
def horizontal5():
for i in range(0,5):
print("*",end=" ")
vertical5()
print(end="\n")
horizontal5(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code: public static long SumOfDivisors(long N){
long sum=0;
long c=(long)Math.sqrt(N);
for(long i=1;i<=c;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){sum+=N/i;}
}
}
return sum;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
def SumOfDivisors(num) :
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
i = 1
while i<= (math.sqrt(num)) :
# if 'i' is divisor of 'num'
if (num % i == 0) :
# if both divisors are same then
# add it only once else add both
if (i == (num / i)) :
result = result + i;
else :
result = result + (i + num/i);
i = i + 1
# Add 1 to the result as 1 is also
# a divisor
return (result); , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two strings S and T. Determine whether it is possible to make S and T equal by doing the following operation at most once:
Choose two adjacent characters in S and swap them.
Note that it is allowed to choose not to do the operation.The input line contains two strings in separate lines.
<b>Constraints</b>
Each of S and T is a string of length between 2 and 100 (inclusive) consisting of lowercase English letters.
S and T have the same length.If it is possible to make S and T equal by doing the operation in Problem Statement at most once, print Yes; otherwise, print No.<b>Sample Input 1</b>
abc
acb
<b>Sample Output 1 </b>
Yes
<b>Sample Input 2</b>
aabb
bbaa
<b>Sample Output 2 </b>
No
<b>Sample Input 3</b>
abcde
abcde
<b>Sample Output 3 </b>
Yes, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
string S,T; cin >> S >> T;
string ans = "No";
if(S == T) ans = "Yes";
for(int i=0; i<S.size(); i++){
if(S[i] != T[i]){
if(0 < i){
swap(S[i-1],S[i]);
if(S == T) ans = "Yes";
swap(S[i-1],S[i]);
}
if(i+1 < S.size()){
swap(S[i],S[i+1]);
if(S == T) ans = "Yes";
swap(S[i],S[i+1]);
}
break;
}
}
cout << ans << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara does not like strings which have both the uppercase and the lowercase characters. She thinks only those strings are good which have only lowercase characters(a-z) or only uppercase characters(A-Z).
Given a string s consisting of english alphabets. Find the minimum number of operations required to make it good.
In one operation you can choose any character of the string and change it into uppercase or lowercase.Input contains a single line containing the string s.
Constraint:-
1<=|s|<=100Print the minimum number of operation required to make the string good.Sample Input:
Sara
Sample Output:
1
Explanation:-
'S' should be converted to 's' respectively
Sample Input:
HELLO
Sample Output:-
0
Sample Input:-
pEOpLE
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));
String name=br.readLine();
int lower=0;
int uper=0;
for(int i=0;i<name.length();i++)
{
Character ch=name.charAt(i);
if (ch >= 'a' && ch <= 'z')
{
lower++;
}
else if(ch>='A' && ch <='Z')
{
uper++;
}
}
if(lower<uper)
{
System.out.print(lower);
}
else
{
System.out.print(uper);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara does not like strings which have both the uppercase and the lowercase characters. She thinks only those strings are good which have only lowercase characters(a-z) or only uppercase characters(A-Z).
Given a string s consisting of english alphabets. Find the minimum number of operations required to make it good.
In one operation you can choose any character of the string and change it into uppercase or lowercase.Input contains a single line containing the string s.
Constraint:-
1<=|s|<=100Print the minimum number of operation required to make the string good.Sample Input:
Sara
Sample Output:
1
Explanation:-
'S' should be converted to 's' respectively
Sample Input:
HELLO
Sample Output:-
0
Sample Input:-
pEOpLE
Sample Output:-
2
, I have written this Solution Code: n = input()
smallAlphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
bigAlphabets = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
count = 0
count1 = 0
for i in n:
if i in smallAlphabets:
count += 1
if i in bigAlphabets:
count1 += 1
if count < count1:
print(count)
else:
print(count1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara does not like strings which have both the uppercase and the lowercase characters. She thinks only those strings are good which have only lowercase characters(a-z) or only uppercase characters(A-Z).
Given a string s consisting of english alphabets. Find the minimum number of operations required to make it good.
In one operation you can choose any character of the string and change it into uppercase or lowercase.Input contains a single line containing the string s.
Constraint:-
1<=|s|<=100Print the minimum number of operation required to make the string good.Sample Input:
Sara
Sample Output:
1
Explanation:-
'S' should be converted to 's' respectively
Sample Input:
HELLO
Sample Output:-
0
Sample Input:-
pEOpLE
Sample Output:-
2
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int x=0,y=0;
string s;
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]<'a'){x++;}
}
cout<<min(x,(int)s.length()-x);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, 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));
BufferedWriter bo=new BufferedWriter(new OutputStreamWriter(System.out));
int t;
try{
t=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
return;
}
while(t-->0)
{
String[] g=br.readLine().split(" ");
int n=Integer.parseInt(g[0]);
int k=Integer.parseInt(g[1]);
if(k>n || (k==1) || (k>26))
{
if(n==1 && k==1)
bo.write("a\n");
else
bo.write(-1+"\n");
}
else
{
int extra=k-2;
boolean check=true;
while(n>extra)
{
if(check==true)
bo.write("a");
else
bo.write("b");
if(check==true)
check=false;
else
check=true;
n--;
}
for(int i=0;i<extra;i++)
bo.write((char)(i+99));
bo.write("\n");
}
}
bo.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, I have written this Solution Code: t=int(input())
for tt in range(t):
n,k=map(int,input().split())
if (k==1 and n>1) or (k>n):
print(-1)
continue
s="abcdefghijklmnopqrstuvwxyz"
ss="ab"
if (n-k)%2==0:
a=ss*((n-k)//2)+s[:k]
else:
a=ss*((n-k)//2)+s[:2]+"a"+s[2:k]
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef unsigned long long int ull;
const long double PI = acos(-1);
const ll mod=1e9+7;
const ll mod1=998244353;
const int inf = 1e9;
const ll INF=1e18;
void precompute(){
}
void TEST_CASE(){
int n,k;
cin >> n >> k;
if(k==1){
if(n>1){
cout << -1 << endl;
}else{
cout << 'a' << endl;
}
}else if(n<k){
cout << -1 << endl;
}else if(n==k){
string s="";
for(int i=0 ; i<k ; i++){
s+=('a'+i);
}
cout << s << endl;
}else{
string s="";
for(int i=0 ; i<(n-k+2) ; i++){
if(i%2){
s+="b";
}else{
s+="a";
}
}
for(int i=2 ; i<k ; i++){
s+=('a'+i);
}
cout << s << endl;
}
}
signed main(){
fast;
//freopen ("INPUT.txt","r",stdin);
//freopen ("OUTPUT.txt","w",stdout);
int test=1,TEST=1;
precompute();
cin >> test;
while(test--){
TEST_CASE();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int k;
cin>>k;
int a[n+1];
int to=0;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}
int j=1;
int s=0;
int ans=n;
for(int i=1;i<=n;++i)
{
while(s<k&&j<=n)
{
s+=a[j];
++j;
}
if(s>=k)
{
ans=min(ans,j-i);
}
s-=a[i];
}
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., 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));
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
long k=Long.parseLong(s[1]);
int a[]=new int[n];
s=br.readLine().split(" ");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s[i]);
}
int length=Integer.MAX_VALUE,i=0,j=0;
long currSum=0;
for(j=0;j<n;j++)
{
currSum+=a[j];
while(currSum>=k)
{
length=Math.min(length,j-i+1);
currSum-=a[i];
i++;
}
}
System.out.println(length);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., I have written this Solution Code: def result(arr,n,k):
minnumber = n + 1
start = 0
end = 0
curr_sum = 0
while(end < n):
while(curr_sum < k and end < n):
curr_sum += arr[end]
end += 1
while( curr_sum >= k and start < n):
if (end - start < minnumber):
minnumber = end - start
curr_sum -= arr[start]
start += 1
return minnumber
n = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(result(arr, n[0], n[1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a stack of integers and N queries. Your task is to perform these operations:-
<b>push:-</b>this operation will add an element to your current stack.
<b>pop:-</b>remove the element that is on top
<b>top:-</b>print the element which is currently on top of stack
<b>Note:-</b>if the stack is already empty then the pop operation will do nothing and 0 will be printed as a top element of the stack if it is empty.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push()</b>:- that takes the stack and the integer to be added as a parameter.
<b>pop()</b>:- that takes the stack as parameter.
<b>top()</b> :- that takes the stack as parameter.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>3</sup>You don't need to print anything else other than in <b>top</b> function in which you require to print the topmost element of your stack in a new line, if the stack is empty you just need to print 0.Input:
7
push 1
push 2
top
pop
top
pop
top
Output:
2
1
0
, I have written this Solution Code:
public static void push (Stack < Integer > st, int x)
{
st.push (x);
}
// Function to pop element from stack
public static void pop (Stack < Integer > st)
{
if (st.isEmpty () == false)
{
int x = st.pop ();
}
}
// Function to return top of stack
public static void top(Stack < Integer > st)
{
int x = 0;
if (st.isEmpty () == false)
{
x = st.peek ();
}
System.out.println (x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String StrInput[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(StrInput[0]);
int s = Integer.parseInt(StrInput[1]);
int arr[] = new int[n];
String StrInput2[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(StrInput2[i]);
}
int sum = arr[0];
int startingindex = 0;
int endingindex = 1;
int j = 0;
int i;
for(i=1;i<=n;i++)
{
if(sum < s && arr[i] != 0)
{
sum += arr[i];
}
while(sum > s && startingindex < i-1)
{
sum -= arr[startingindex];
startingindex++;
}
if(sum == s)
{
endingindex = i+1;
if(arr[0] == 0)
{
System.out.print(startingindex+2 + " " + endingindex);
}
else
{
System.out.print(startingindex+1 + " "+ endingindex);
}
break;
}
if(i == n && sum < s)
{
System.out.print(-1);
break;
}
}
}
catch(Exception e)
{
System.out.print(-1);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
unordered_map<int,int> m;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==k){cout<<1<<" "<<i+1;return 0;}
if(m.find(sum-k)!=m.end()){
cout<<m[sum-k]+2<<" "<<i+1;
return 0;
}
m[sum]=i;
}
cout<<-1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: def sumFinder(N,S,a):
currentSum = a[0]
start = 0
i = 1
while i <= N:
while currentSum > S and start < i-1:
currentSum = currentSum - a[start]
start += 1
if currentSum == S:
return (start+1,i)
if i < N:
currentSum = currentSum + a[i]
i += 1
return(-1)
N, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = sumFinder(N,S,a)
if(ans==-1):
print(ans)
else:
print(ans[0],ans[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an 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: 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: Given a BST containing N nodes. The task is to find the <b>minimum absolute difference</b> possible between any <b>two different nodes</b>.<b>User Task:</b>
Since this will be a functional problem you don't have to take input. You just have to complete the function <b>absMinDist()</b> that takes "root" node as parameter
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= node values <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to return the minimum absolute difference between any two different nodes.The driver code will take care of printing new line.Input:
2
5 3 6
5 N 9 8 N
Output:
1
1
Explanation:
Testcase 1:
5
/ \
3 6
the minimum abs difference between two nodes i.e 5 & 6 is 1.
Testcase 2:
5
/ \
N 9
/ \
8 N
the minimum absolute difference between two nodes i.e. 9 & 8 is 1., I have written this Solution Code: t=int(input())
for k in range(0,t):
arr=input().split()
new_arr=[]
for v in arr:
if(v!='N'):
new_arr.append(int(v))
new_arr=sorted(new_arr)
m=1000000000000
for i in range(0,len(new_arr)-1):
m=min(m,abs(new_arr[i+1]-new_arr[i]))
print (m), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a BST containing N nodes. The task is to find the <b>minimum absolute difference</b> possible between any <b>two different nodes</b>.<b>User Task:</b>
Since this will be a functional problem you don't have to take input. You just have to complete the function <b>absMinDist()</b> that takes "root" node as parameter
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= node values <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to return the minimum absolute difference between any two different nodes.The driver code will take care of printing new line.Input:
2
5 3 6
5 N 9 8 N
Output:
1
1
Explanation:
Testcase 1:
5
/ \
3 6
the minimum abs difference between two nodes i.e 5 & 6 is 1.
Testcase 2:
5
/ \
N 9
/ \
8 N
the minimum absolute difference between two nodes i.e. 9 & 8 is 1., I have written this Solution Code: static Integer prev = null, minDiff = Integer.MAX_VALUE;
public static int absMinDist(Node root) {
prev = null;
minDiff = Integer.MAX_VALUE;
bst(root);
return minDiff;
}
public static void bst(Node root) {
if(root==null) return;
bst(root.left);
if(prev!=null) {
minDiff = Math.min(root.data-prev, minDiff);
}
prev= root.data;
bst(root.right);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N flags, initially set to 0. Now you have to perform two operations on them:
1. Increase(F) by 1: flag F is increased by 1.
2. max_flag: all flags are set to a maximum value of any flag.
A non-empty array arr[] will be given of size M. This array represents consecutive operations:
a) If arr[K] = F, such that 1 <= F <= N then operation K is Increase(F).
b) If arr[K] = N+1 then operation K is max_flag.
The goal is to calculate the value of every flag after all operations.The input line contains T, denoting the number of test cases.
Each test case contains two lines.
The first line contains an integer N, the number of flags, and an integer M, the size of the array 'arr'.The second line contains elements of the array 'arr' separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N, M <= 10^5
1 <= arr[i] <= N+1
Sum of N and M for each test case is less than or equal to 10^5For each testcase you need to print the updated array after all operations in new line.Sample Input:
1
5 7
3 4 4 6 1 4 4
Sample Output:
3 2 2 4 2
<b>Explanation:</b>
Testcase 1:
the values of the flags after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2), I have written this Solution Code: t=int(input())
while t>0:
t-=1
n,m=map(int,input().split())
a=map(int,input().split())
b=[0]*(n+1)
for i in a:
if i==n+1:
v=max(b)
for i in range(1,n+1):
b[i]=v
else:b[i]+=1
for i in range(1,n+1):
print(b[i],end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N flags, initially set to 0. Now you have to perform two operations on them:
1. Increase(F) by 1: flag F is increased by 1.
2. max_flag: all flags are set to a maximum value of any flag.
A non-empty array arr[] will be given of size M. This array represents consecutive operations:
a) If arr[K] = F, such that 1 <= F <= N then operation K is Increase(F).
b) If arr[K] = N+1 then operation K is max_flag.
The goal is to calculate the value of every flag after all operations.The input line contains T, denoting the number of test cases.
Each test case contains two lines.
The first line contains an integer N, the number of flags, and an integer M, the size of the array 'arr'.The second line contains elements of the array 'arr' separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N, M <= 10^5
1 <= arr[i] <= N+1
Sum of N and M for each test case is less than or equal to 10^5For each testcase you need to print the updated array after all operations in new line.Sample Input:
1
5 7
3 4 4 6 1 4 4
Sample Output:
3 2 2 4 2
<b>Explanation:</b>
Testcase 1:
the values of the flags after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 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 = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
memset(a, 0, sizeof a);
int n, m;
cin >> n >> m;
int mx = 0, flag = 0;
for(int i = 1; i <= m; i++){
int p; cin >> p;
if(p == n+1){
flag = mx;
}
else{
a[p] = max(a[p], flag) + 1;
mx = max(mx, a[p]);
}
}
for(int i = 1; i <= n; i++){
a[i] = max(a[i], flag);
cout << a[i] << " ";
}
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a number N we have N! unique permutations. <b>A permutation is a sequence of integers from 1 to N of length N containing each number exactly once.</b>
For a positive integer X, <b>X! = 1 * 2 * 3 *...* X-1 * X</b>
Your task is to find the Kth smallest permutation when all possible permutations of size N are arranged in sorted order.Input contains only two integers, the value of N and K.
Constraints:-
1 <= N <= 10000
1 <= K <= min(N!,100000000)Print the Kth permutation in form of a string. i. e don't print spaces between two numbers.Sample Input:-
3 5
Sample Output:-
312
Explanation:-
All permutations of length 3 are:-
123
132
213
231
312
321
Sample Input:-
11 2
Sample Output:-
1234567891110, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int k=Integer.parseInt(s[1]);
Main m=new Main();
System.out.print(m.getPermutation(n,k));
}
public String getPermutation(int n, int k) {
int idx = 1;
for ( idx = 1; idx <= n;idx++) {
if (fact(idx) >= k) break;
}
StringBuilder ans = new StringBuilder();
for( int i = 1; i <=n-idx;i++) {
ans.append(i);
}
ArrayList<Integer> dat = new ArrayList<>(n);
for( int i = 1; i <= idx;i++) {
dat.add(i);
}
for( int i = 1; i <= idx;i++) {
int t = (int) ((k-1)/fact(idx-i));
ans.append(dat.get(t)+(n-idx));
dat.remove(t);
k = (int)(k-t*(fact(idx-i)));
}
return ans.toString();
}
public String getPermutation0(int n, int k) {
int idx = k;
StringBuilder ans = new StringBuilder();
ArrayList<Integer> dat = new ArrayList<>(n);
for( int i = 1; i <= n;i++) {
dat.add(i);
}
for(int i = 1; i <= n;i++) {
idx = (int)((k-1)/fact(n-i));
ans.append(dat.get(idx));
dat.remove(idx);
k = (int)(k - idx*fact(n-i));
}
return ans.toString();
}
public long fact(int n) {
int f = 1;
for( int i = 1; i <= n;i++) {
f *= i;
}
return f;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a number N we have N! unique permutations. <b>A permutation is a sequence of integers from 1 to N of length N containing each number exactly once.</b>
For a positive integer X, <b>X! = 1 * 2 * 3 *...* X-1 * X</b>
Your task is to find the Kth smallest permutation when all possible permutations of size N are arranged in sorted order.Input contains only two integers, the value of N and K.
Constraints:-
1 <= N <= 10000
1 <= K <= min(N!,100000000)Print the Kth permutation in form of a string. i. e don't print spaces between two numbers.Sample Input:-
3 5
Sample Output:-
312
Explanation:-
All permutations of length 3 are:-
123
132
213
231
312
321
Sample Input:-
11 2
Sample Output:-
1234567891110, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int factorial(int n) {
if (n > 12) {
// this overflows in int. So, its definitely greater than k
// which is all we care about. So, we return INT_MAX which
// is also greater than k.
return INT_MAX;
}
// Can also store these values. But this is just < 12 iteration, so meh!
int fact = 1;
for (int i = 2; i <= n; i++) fact *= i;
return fact;
}
string getPermutationi(int k, vector<int> &candidateSet) {
int n = candidateSet.size();
if (n == 0) {
return "";
}
if (k > factorial(n)) return ""; // invalid. Should never reach here.
int f = factorial(n - 1);
int pos = k / f;
k %= f;
string ch = to_string(candidateSet[pos]);
// now remove the character ch from candidateSet.
candidateSet.erase(candidateSet.begin() + pos);
return ch + getPermutationi(k, candidateSet);
}
string solve(int n, int k) {
vector<int> candidateSet;
for (int i = 1; i <= n; i++) candidateSet.push_back(i);
return getPermutationi(k - 1, candidateSet);
}
int main(){
int n,k;
cin>>n>>k;
cout<<solve(n,k);
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Prime Minister loves to travel with their cabinet ministers.
Prime Minister has X cars that can seat 4 people and Y cars that can seat 7 people each. Determine the maximum number of people that can travel together in these cars.The first and only line of input contains a number of 4- seaters and 7- seater cars separated by space.
Constraints:
1 <= X, Y <= 100Print the maximum number of people that can travel with the Prime Minister.Sample Input:
4 8
Sample Output:
72
Sample Input:
2 3
Sample Output:
29, I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader readInput = new BufferedReader(new InputStreamReader(System.in));
String arr[] = readInput.readLine().trim().split(" ");
int X = Integer.parseInt(arr[0]);
int Y = Integer.parseInt(arr[1]);
maxPeopleTravel(X, Y);
}
static void maxPeopleTravel(int X, int Y)
{
System.out.println((X*4) + (Y*7));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given an array of several arrays that each contain integers and your goal is to write a function that
will sum up all the numbers in all the arrays. For example, if the input is [[3, 2], [1], [4, 12]] then your
program should output 22 because 3 + 2 + 1 + 4 + 12 = 22An array containing arrays which can contain any number of elements.Sum of all the elements in all of the arrays.Sample input:-
[[3, 2], [1], [4, 12]]
Sample output:-
22
Explanation:-
3 + 2 + 1 + 4 + 12 = 22, I have written this Solution Code: function sum_array(arr) {
// store our final answer
var sum = 0;
// loop through entire array
for (var i = 0; i < arr.length; i++) {
// loop through each inner array
for (var j = 0; j < arr[i].length; j++) {
// add this number to the current final sum
sum += arr[i][j];
}
}
console.log(sum);
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two sorted arrays A and B of size N and M respectively.
You have to find the value V, which is the summation of (A[j] - A[i]) for all pairs of i and j such that (j - i) is present in array B.The first line contains two space separated integers N and M – size of arrays A and B respectively.
The second line contains N integers A<sub>1</sub>, A<sub>2</sub>, ... A<sub>N</sub>.
The third line contains M integers B<sub>1</sub>, B<sub>2</sub>, ... B<sub>M</sub>.
<b> Constraints: </b>
1 ≤ N ≤ 2×10<sup>5</sup>
1 ≤ M < N
1 ≤ A<sub>1</sub> ≤ A<sub>2</sub> ≤ ... ≤ A<sub>N</sub> ≤ 10<sup>8</sup>.
1 ≤ B<sub>1</sub> < B<sub>2</sub> < ... < B<sub>M</sub> < N.Print a single integer, the value of V.Sample Input 1:
4 2
1 2 3 4
1 3
Sample Output 1:
6
Sample Explanation 1:
Valid pairs of (i, j) are (1, 2), (2, 3), (3, 4), (1, 4)., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define epsi (double)(0.00000000001)
typedef long long int ll;
typedef unsigned long long int ull;
#define vi vector<ll>
#define pii pair<ll,ll>
#define vii vector<pii>
#define vvi vector<vi>
//#define max(a,b) ((a>b)?a:b)
//#define min(a,b) ((a>b)?b:a)
#define min3(a,b,c) min(min(a,b),c)
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max3(a,b,c) max(max(a,b),c)
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define ff(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define REP(a,b,c) for(int a=b; a<c; a++)
#define pb push_back
#define mp make_pair
#define endl "\n"
#define all(v) v.begin(),v.end()
#define sz(a) (ll)a.size()
#define F first
#define S second
#define ld long double
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define setbits(x) __builtin_popcountll(x)
#define trav(a,x) for(auto &a:x)
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define rev(arr) reverse(all(arr))
#define gcd(a,b) __gcd(a,b);
#define ub upper_bound // '>'
#define lb lower_bound // '>='
#define qi queue<ll>
#define fsh cout.flush()
#define si stack<ll>
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define fill(a,b) memset(a, b, sizeof(a))
template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;}
const ll INF=1LL<<60;
void solve(){
ll n,m;
cin >> n >> m;
vi v1(n),v2(m);
for(auto &i:v1){
cin >> i;
}
for(auto &i:v2){
cin >> i;
}
ll ans=0;
for(int i=1 ; i<=n ; i++){
auto it=lower_bound(all(v2),i);
ll x=it-v2.begin();
ans+=(x*v1[i-1]);
it=lower_bound(all(v2),n-i+1);
x=it-v2.begin();
ans-=(x*v1[i-1]);
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t=1;
// cin >> t;
while(t--){
solve();
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given one element K your task is to print the maximum element from the array which is less than or equal to the given element(Floor), and the minimum element from the array which is greater than or equal to the given element(Ceil).<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>floorAndCeil()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N. The Second line of input contains N space-separated integers depicting the values of the array. The third line of input contains a single integer Q. The next Q line of input contains a single integer the value of K.
Constraints:-
1 <= N <= 100000
1 <= K, Arr[i] <= 1000000000000
1 <= Q <= 10000In a new line Print two space-separated integers depicting the values of Floor and Ceil of the given number. If the floor or ceil of the element does not exist print -1.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
2 2
2 5
6 11
-1 2
15 -1, I have written this Solution Code: n = int(input())
arr = list(map(int,input().split()))
t = int(input())
def findFloor(arr, l, h, x, res):
if(l<=h):
m = l+(h-l)//2
if(arr[m] == x):
return m
if(arr[m] > x):
return findFloor(arr, l, m-1, x, res)
if(arr[m] < x):
res = m
return findFloor(arr, m+1, h, x, res)
else:
return res
def findCeil(arr, l, h, x, res):
if(l<=h):
m = l+(h-l)//2
if(arr[m] == x):
return m
if(arr[m] < x):
return findCeil(arr, m+1, h, x, res)
res = m
return findCeil(arr, l, m-1, x, res)
else:
return res
for _ in range(t):
x = int(input())
f = findFloor(arr, 0, n-1, x, -1)
c = findCeil(arr, 0, n-1, x, -1)
floor = -1
ceil = -1
if(f!=-1):
floor = arr[f]
if(c!=-1):
ceil = arr[c]
print(floor,ceil), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given one element K your task is to print the maximum element from the array which is less than or equal to the given element(Floor), and the minimum element from the array which is greater than or equal to the given element(Ceil).<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>floorAndCeil()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N. The Second line of input contains N space-separated integers depicting the values of the array. The third line of input contains a single integer Q. The next Q line of input contains a single integer the value of K.
Constraints:-
1 <= N <= 100000
1 <= K, Arr[i] <= 1000000000000
1 <= Q <= 10000In a new line Print two space-separated integers depicting the values of Floor and Ceil of the given number. If the floor or ceil of the element does not exist print -1.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
2 2
2 5
6 11
-1 2
15 -1, I have written this Solution Code: static void floorAndCeil(int a[], int n, int x){
int it = lower(a,n,x);
if(it==0){
if(a[it]==x){
System.out.println(x+" "+x);
}
else{
System.out.println("-1 "+a[it]);
}
}
else if (it==n){
it--;
System.out.println(a[it]+" -1");
}
else{
if(a[it]==x){
System.out.println(x+" "+x);
}
else{
it--;
System.out.println(a[it]+" "+a[it+1]);
}
}
}
static int lower(int a[], int n,int k){
int l=0;
int h=n-1;
int m;
int ans=n;
while(l<=h){
m=l+h;
m/=2;
if(a[m]<k){
l=m+1;
}
else{
h=m-1;
ans=m;
}
}
return ans;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given one element K your task is to print the maximum element from the array which is less than or equal to the given element(Floor), and the minimum element from the array which is greater than or equal to the given element(Ceil).<b>In case of Java only</b>
<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>floorAndCeil()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N. The Second line of input contains N space-separated integers depicting the values of the array. The third line of input contains a single integer Q. The next Q line of input contains a single integer the value of K.
Constraints:-
1 <= N <= 100000
1 <= K, Arr[i] <= 1000000000000
1 <= Q <= 10000In a new line Print two space-separated integers depicting the values of Floor and Ceil of the given number. If the floor or ceil of the element does not exist print -1.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
2 2
2 5
6 11
-1 2
15 -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'
#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;
vector<int> v;
int x;
FOR(i,n){
cin>>x;
v.EB(x);}
int q;
cin>>q;
while(q--){
cin>>x;
auto it = lower_bound(v.begin(),v.end(),x);
if(it==v.begin()){
if(*it==x){
cout<<x<<" "<<x;
}
else{
cout<<-1<<" "<<*it;
}
}
else if (it==v.end()){
it--;
cout<<*it<<" -1";
}
else{
if(*it==x){
cout<<x<<" "<<x;
}
else{
it--;
cout<<*it<<" ";
it++;
cout<<*it;}
}
END;
}
}
, 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: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: void magicTrick(int a, int b){
cout<<a+b/2;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: static void magicTrick(int a, int b){
System.out.println(a + b/2);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: A,B = map(int,input().split(' '))
C = A+B//2
print(C)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
cout << a[i] << " ";
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: // arr is unsorted array
// n is the number of elements in the array
function bubbleSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => a - b)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int temp;
for(int i=1;i<n;i++){
if(a[i]<a[i-1]){
for(int j=i;j>0;j--){
if(a[j]<a[j-1]){
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
else{
break;
}
}
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: def bubbleSort(arr):
arr.sort()
return arr
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.