Treat " \n"
as a string of length 2, then you can see that " \n"[ i == n - 1 ]
prints a space when i
is not n-1
and prints a newline when it is.
That’s a crazy way of handling the output format.
This code satisfie all conditions still it’s wrong, why? Please help me to rectify my mistake in it.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t–)
{
int n;
cin >> n;
long long int a[61];
a[0] = 1;
a[1] = 1;
int sum = 2;
for (int i = 2; i <= n - 1; i++)
{
a[i] = i;
sum = sum + a[i];
}
if (n != 1)
{
a[n] = pow(2, n) - sum;
}
for (int i = 0; i <= n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
return 0;
}
here u can check for a case n=1 print “1 1”(its not taking 0 as positive integer here)
Please help, I’m still not able to understand. Why the above solution is not getting accepted.
The solution can be find below as well as here.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
int sum = pow(2, n) - 2;
cout << "1 1";
int counter = 2;
for (int i = 1; i < n - 1; i++) {
cout << " " << counter;
sum -= counter;
counter++;
}
if (n != 1) cout << " " << sum;
if (t != 0) cout << endl;
}
return 0;
}
For input:
6
1
2
3
4
5
6
Output is correct:
1 1
1 1 2
1 1 2 4
1 1 2 3 9
1 1 2 3 4 21
1 1 2 3 4 5 48
But still shows wrong answer. @ssjgz @sharad_0_1
Always test the boundary values!
[simon@simon-laptop][09:41:57]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling heatherstanton-N1VALUES.cpp
Executing command:
g++ -std=c++17 heatherstanton-N1VALUES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
heatherstanton-N1VALUES.cpp: In function ‘int main()’:
heatherstanton-N1VALUES.cpp:8:26: warning: conversion to ‘int’ from ‘__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}’ may alter its value [-Wfloat-conversion]
int sum = pow(2, n) - 2;
~~~~~~~~~~^~~
^R
Successful
[simon@simon-laptop][09:42:03]
[~/devel/hackerrank/otherpeoples]>echo "1
60" | ./a.out
heatherstanton-N1VALUES.cpp:13:17: runtime error: signed integer overflow: -2147483648 - 2 cannot be represented in type 'int'
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 2147481879
I got my mistake!! i needed to use long long int…now it’s work
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int n;
cin>>n;
ll k=1;
vector<ll>v;
for(int i=1;i<=n+1;i++){
if(i==1||i==2)
v.push_back(1);
else
v.push_back(pow(2,k++));
}
for(int i:v)
cout<<i<<" ";
cout<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
solve();
return 0;
}
why I am getting WA? please help.
here you go
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
long long int n; cin >> n;
long long int pw=pow(2,n);
long long int sum = pw- 2;
cout << "1 1";
long long int counter = 2;
for (int i = 1; i < n - 1; i++) {
cout << " " << counter;
sum -= counter;
counter++;
}
if (n != 1) cout << " " << sum;
if (t != 0) cout << endl;
}
return 0;
}
bro can you pls tell y i am getting WA?
#include
#include<math.h>
#include
#include
using namespace std;
long long i,k;
void solve(){
long long n,power; cin>>n; vectorv;
for(i=1;i<n;i++){
if(i<=n)
v.push_back(i);
if(i==n-1){v.push_back(i);}
}
int k= accumulate(v.begin(),v.end(),0);
int po= 1ll<<n;
int m=po-k;
v.push_back(po-k);
for(int i=0;i<n+1;i++)cout<<v[i]<<" ";
cout<<"\n";
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int tc; cin>>tc;
while(tc–>0)solve();
return 0;
}
ok bro thnx
bro really thank you so much i got AC
I faced the same problem. The problem is with n=60 and occurs due to approximation by the system. You can check that pow(2,n) and pow(2,n)-2 will yield the same values. TRY PRINTING THEM OUT. It occurs maybe because pow(2,n)-2 is negligibly small from pow(2,n) and so they are virtually treated as the same.
The C++ double type can only store integers up to 2^{53} without precision loss, as explained by this stackoverflow answer.
Put n=60.value is very large more than 1e18.
Sorry for late reply.
thanks
bro can you pls say y i am getting nzec error
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s = new Scanner(System.in);
double tcash,rem,amt;
tcash=120.00;
amt=s.nextInt();
if(amt%5==0 && amt<=tcash){
rem=tcash-amt-0.50;
System.out.println(String.format(“%.2f”,rem));
}
else{
System.out.println(String.format(“%.2f”,tcash));
}
}
}
can anyone tell me what’s wrong with this approach?..
import java.util.;
import java.lang.;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
int testCases = scanner.nextInt();
while(testCases-- > 0)
{
int terms = scanner.nextInt();
if(terms+1 >= 3)
{
int i=1;
int range = terms-2;
for(;i<=range;i++)
System.out.print(i+" “);
System.out.print(i+” “+i+” “);
System.out.println((long)(Math.pow(2,terms)-((i+1)*i/2+i)));
}
else
System.out.println(1+” "+1);
}
}
}
https://www.codechef.com/viewsolution/55096247
why this is giving a WA
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
ll t;
cin>>t;
while(t--)
{
ll n;
cin >> n;
if(n==1) {
cout << "1 1\n";
continue;
}
ll x = 1 << n;
for(int i = 1 ; i<n;++i)
{
cout << i << " ";
x -= i;
}
cout << n-1 << " ";
x -= n-1;
cout << x << "\n";
}
return 0;
}