Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In the following program, I have attached code and its sample output I don't know why it is printing 0 as the most significant digit.
I have tried different test cases but it just adds 0 to most significant digit.
Is it a bug or I'm missing some concept.
#include<bits/stdc++.h>
using namespace std;
int kthLargest (vector<int> arr, int n, int k){
map<int,int> a;
cout<<a.size();
vector<int>::iterator i;
for(i=arr.begin();i!=arr.end();i++)
{
a[*i]=a[*i]+1;
}
map<int,int>::iterator j;
int sum=0;
for(j=a.begin();j!=a.end();j++)
{
sum+=j->second;
if(sum>(n-k))
{
sum=j->first;
break;
}
}
return sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
int k;
cin>>k;
cout<<kthLargest(a,n,k);
return 0;
}
input
5
1 2 3 4 5
3
output
04
You are outputting 2 numbers, one immediately after the other:
cout<<a.size();
and
cout<<kthLargest(a,n,k);
The first is 0 (the map is empty), the second is the final result of your function call. The formatting is behaving as expected, and no leading 0 is being output anywhere.
You probably forgot about the cout<<a.size();, so just remove it and your code will be fine.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
HERE IS THE QUESTION I FACED ON HACKERRANK.
the hackerrank question
HERE IS THE CODE I PRINTED
#include <iostream>
using namespace std;
int main() {
int n;
int array[n];
int c,x;
cin>>n; //inputting the array size
n=n+1;
int m=n;
if(n>=1 && n<=1000 && m>=1 && m<=1000 )
{
for(c=1;c<=n;c++)//inutting the numbers
{ if(c<=10000)
{
cin>>array[c];
}
}
for(x=m-1;x>=1;x--)//outputing the numbers reversed
{
cout<<array[x]<<" ";
}
}
return 0;
}
WHEN I GIVE INPUT THE ARRAY SIZE AS 4 AND THE DATA AS 1 4 3 2,
ITS REVERSED AS 2,3,4,0
BUT IT PERFECTLY GETS REVERSED WHEN COMPILED WITH VISUAL STUDIO CODE. WHYS THAT?? AND ALSO WHEN I SUBMIT CODE SEVERAL TESTS PASS, BUT SOME GET A SEGMENTATION ERROR
As mentioned in the comments, array should be initialized with a proper capacity. You should first read the size and then create the array.
#include <iostream>
using namespace std;
int main() {
int n;
// First read the array size and then create the array.
cin>>n; //inputting the array size
int array[n];
//inputting the numbers
// Remember indices go from 0 to N-1
for(int c=0;c<n;c++)
{
cin>>array[c];
}
//outputing the numbers reversed
for(int x=n-1;x>=0;x--)
{
cout<<array[x]<<" ";
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is the problem: https://codeforces.com/contest/1256/problem/D .I'm getting runtime error("out of bound") in test case 15 here is my submission: https://codeforces.com/contest/1256/submission/84865113
Where am I doing wrong?
my approach:
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--){
int n,k;
cin>>n>>k;
string num;
cin>>num;
int start = 0;
for(int i=0;i<n;i++){
if(num[i]=='0' && k>0){
if((k-abs(i-start))>=0){
swap(num[start],num[i]);
k = k - abs(i-start);
start++;
}
else{
swap(num[i],num[i-k]);
k = 0;
}
}
}
cout<<num<<"\n";
}
return 0;
}
the range of k is out of int. You should use long long.
9428683473 is out of 2147483647(max value for type int), so you should not use int. And the var i and start should change too.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3];
int i,j,k;
for(i=0;i<3;i++)
{
ar3[i][j] = 0;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++);
printf("%d\t",ar3[i][j]);
}
getch();
return 0;
}
When I compile the code in Dev C++ it does not give any error but fails to run and the application stops working. Whats wrong with it?
On the line
ar3[i][j] = 0;
j is still garbage so you get undefined behavior, which may lead to different kinds of faults - erroneous results, segfaults, on some rare cases it may even work as expected.
Move that line inside the second loop.
you should initialize arr3[i][j] to 0 as int arr3[i][j]={0};. Also while displaying the multiplication matrix you closed the second loop with j. Don't close that otherwise you won't get desired output. I corrected it. Hope this one solves your problem.
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3] = {0}; // here goes initialization
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",ar3[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find all the divisors of a number (n) and to add to the array those divisors that are at the 1st power (that appear only once), but I get in output just zeros, what's wrong with my code?
#include<iostream>
using namespace std;
int k,A[100000],n,p,d=2,pozitia=0;
int main()
{
cin>>n;
while(n>1)
{
p=0;
while(n%d==0)
{
p=p+1;
n=n/d;
}
if (p==1) { A[pozitia]=d; pozitia++; }
d=d+1;
}
for (int i=0;i<=pozitia;i++) cout<<A[pozitia]<<" ";
return 0;
}
You print always the same value:
for (int i=0;i<=pozitia;i++)
cout<<A[pozitia]<<" ";
It should be
for (int i=0;i<pozitia;i++)
cout<<A[i]<<" ";
Also pay attention that it should be i<pozitia and not i<=pozitia because you increment pozitia each time you insert a new value so at the end pozitia will point to a not initialized value in A.
I couldn't follow your logic for computing the divisors. It seems to be much simpler than you make it out to be.
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
Here's a program that uses that logic.
#include<iostream>
using namespace std;
void printDivisors(int A[], int pozitia)
{
for (int i=0;i<pozitia;i++) cout<<A[i]<<" ";
}
void fun(int n)
{
int A[100000];
int d = 2;
int pozitia=0;
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
printDivisors(A, pozitia);
}
int main()
{
int n;
cin>>n;
fun(n);
return 0;
}
The output for input of 100:
2 4 5 10 20 25 50
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
please someone help me finding the error in this code. i've its python counterpart which is working fine, but when i code this in c++ , giving values(N) >=5 gives me an infinite loop.
[in this problem, you have to find the no. of trailing zeros in the factorial of a given number]
[here T is no. of test cases, N is the number, and z is output]
#include <iostream>
using namespace std;
int main(){
long int T,N,Z;
cin>>T;
for(int x=0;x<T;x++){
Z=0;
cin>>N;
for(int y=1;y<=N;y++){
if(y%5==0){
while(y/5!=0 && y%5==0){
Z+=1;
y/=5;
}
}
}
cout<<Z<<endl;
}
}
my python code is (which is working fine)
T=int(raw_input())
for x in range(1,T+1):
Z=0
N=int(raw_input())
for y in range(1,N+1):
if y%5==0:
while y/5!=0 and y%5==0:
Z=Z+1
y=y/5
print Z
change this:
for(int y=1;y<=N;y++){
int temp=y;
if(temp%5==0){
while(temp/5!=0 && temp%5==0){
Z+=1;
temp/=5;
}
}
}
your logic is not correct. Look at this, which i correctly submitted , hope it helps:-
#include<iostream>
using namespace std;
int tailing_zero(int n){
int ans=0;
int DIV=5;
while(n/DIV >0){
ans=ans+n/DIV;
DIV=DIV*5;
}
return ans;
}
int main(){
int run_count;
cin>>run_count;
int input[run_count];
for(int i=0;i<run_count;i++){
cin>>input[i];
}
for(int i=0;i<run_count;i++){
cout<<tailing_zero(input[i])<<endl;
}
return 0;
}