C++ Array manipulation - c++

Hey guys this is the question's link from hackerrank
hackerrank problem
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long int sizeArr, operation;
cin>>sizeArr>>operation;
long int array[sizeArr];
for(long int i=0;i<sizeArr;i++)
array[i]=0;
for(long int i=0;i<operation;i++)
{
long int a,b,k;
cin>>a>>b>>k;
for(long int j=a-1;j<=b-1;j++)
array[j]+=k;
}
sort(array,array+sizeArr);
cout<<array[sizeArr-1];
}
I coded it like this and the another person code it like
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int N,M,a,b,k,i,j,max=0,x=0;
cin>>N>>M;
long int *Arr = new long int[N+1]();
for(i=0;i<M;i++)
{
cin>>a>>b>>k;
Arr[a]+=k;
if((b+1)<=N) Arr[b+1]-=k;
}
for(i=1;i<=N;i++)
{
x=x+Arr[i];
if(max<x) max=x;
}
cout<<max;
return 0;
}
Mine one didn't clear all the test cases but the second one's code did. Any suggestions.

The problem is that your colleague has this if((b+1)<=N) Arr[b+1]-=k; which means he sometimes subtracts but in your code, you only add in here array[j]+=k;.
And also your while function has a bigger range than your colleague (for example if a user gives as a=1 and b=3 you go through 0,1,2 but your colleague only goes at 1 and 3. I am not sure that this is the problem but you should check out.

Related

Want to print the hexadecimal value of a pointer as 'human readable' unsigned value

This is what I coded on Mobile C
#include <iostream>
using namespace std;
int main()
{
int i{14},j{20};
int *pj{&j},*pi{&i};
cout<<endl<<(unsigned)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned)pj;
cout<<"\t"<<*pj;
--pi;
++pj;
cout<<endl<<(unsigned)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned)pj;
cout<<"\t"<<*pj;
return 0;
}
And this was the output:
2582956840 14
2582956836 20
2582956836 20
2582956840 14
But when I'm trying to do this on visual code (fedora 36) and the terminal compiler g++ is throwing errors, as mentioned above in the Imgur image.
EDIT2:
I did this:
#include <iostream>
using namespace std;
int main()
{
unsigned int i{14},j{20};
unsigned int *pj{&j},*pi{&i};
cout<<endl<<(unsigned*)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned*)pj;
cout<<"\t"<<*pj;
--pi;
++pj;
cout<<endl<<(unsigned*)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned*)pj;
cout<<"\t"<<*pj;
return 0;
}
And got this output:
0x7fff539232d0 14
0x7fff539232d4 20
0x7fff539232cc 21863
0x7fff539232d8 1402090200
Strange! It was compiled using onlinegdb C++ compiler.
You can get the pointers value by doing this:
#include <iostream>
int main() {
int a = 2;
cout < &a;
}
This is pretty much as best you can do to make it human readable.
Also you can look at Displaying the address of a string if you want to look at any other approaches.

Why is this code running showing SIGKILL error?

the code runs printing 94601019043208 in endless lines and then shows SIGKILL error.please help me out handling this error.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t>0)
{
long long int l,r,x,y;
cin>>x>>y>>l>>r;
long long int res;
if(x>y)
res=x;
else
res=y;
long long int sum,i;
for(i=res;i<=r;i++)
{
sum=((x&i)*(y&i));
if(sum==(x*y))
break;
}
cout<<i<<endl;
t=t-1;
}
return 0;
}

Why is mpz_nextprime returning an even number (54)?

I have to find out the first prime number grater then 4 using library and I get 54 (q2 variable in the code). Any idea why? Could be the malfunction related to the 32 bit processor of the computer? I would be very grateful for your help!
Hereby the code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
void generate_Keys()
{
mpz_t q,q2;
int rndBit;
gmp_randstate_t gmpRandState2;
mpz_init(q);
mpz_init(q2);
rndBit = 4;
gmp_randinit_default(gmpRandState2);
mpz_urandomb(q, gmpRandState2, rndBit);
cout<<" q="<<q;//4
mpz_nextprime(q2,q);
cout<<" nextprime="<<q2;//54
}
int main()
{
generate_Keys();
return 0;
}

Why is in the following similar codes one giving runtime error and other is not

Well this is really weird
Consider the following two codes :-
#include <iostream>
using namespace std;
int main() {
int a[]={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
AND
#include <iostream>
using namespace std;
int main() {
int a[]={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;
}
The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .
#include <iostream>
using namespace std;
int main() {
int a[]={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
}
the above code runs fine and gives output 1, 2
but
#include <iostream>
using namespace std;
int main() {
int a[]={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
}
this code gives runtime error on codeblocks(gcc) , I am just getting more confused
I have both runtime errors.
The result of recoding with the pointer variable below.
#include <iostream>
using namespace std;
int main() {
int a[]={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
It is already established that
*ptr[0] = a[0];
with unallocated pointer is undefined behavior.
The solution
ptr[0] = &a[0];
is also already given. This works, but it is limited because now, the ptr array is tied to a, which you not always want. In that case you might want
ptr[0] = new int(a[0]);
This initializes the array with values from a but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).
#include <memory>
#include <vector>
#include <iostream>
int main() {
int a[]={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;
for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}
for(auto& i: ptrs)
{
std::cout << *i;
}
}

Curious behaviour of bitwise AND

I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
1==0 takes more precedence than pos&1. Try if((pos&1)==0){