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
LINK TO THE ACTUALL PROBLEM
I am trying to solve a question on codechef but getting a runtime error. Although when i executed same code on virtual studio code, no error was observed.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
string s[n],b;
int i=0;
while(i<n)
{
i++;
cin>>s[i];
if(i==0) b=s[0];
else {
if(b.length()<=s[i].length()) b=s[i];
}
}
cout<<b;
}
return 0;
}
This is because you are incrementing i and then using it for reading the string s[i], so at one point it reads for the element n+1 which is not allocated so the error that occurs here is Segmentation Error.
What you have to do is increment the i after reading the string s[i]. You can correct this by moving the i++; statement to the end of the while block.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
string s[n],b;
int i=0;
while(i<n){
cin>>s[i];
if(i==0)
b=s[0];
else
if(b.length()<=s[i].length())
b=s[i];
i++;
}
cout<<b;
}
return 0;
}
#theWellHopeErr's answer is correct and gets accepted on geeksforgeeks when the fast i/o lines are removed. I have commented them out below.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int n;
cin>>n;
string s[n],b;
int i=0;
while(i<n){
cin>>s[i];
if(i==0)
b=s[0];
else
if(b.length()<=s[i].length())
b=s[i];
i++;
}
cout<<b<<"\n";
}
return 0;
}
Also, string s[n] is not to be done. Always better to save memory. This can be done just by storing the max length in a variable maxLen and updating your result only when you encounter a longer string
int n;
cin>>n;
string result;
int maxLen = 0;
for(int i = 0; i < n; i++)
{
string s;
cin>>s;
if(s.length() > maxLen)
{
maxLen = s.length();
result = s;
}
}
cout<<result<<"\n";
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have added bits/stdc+.h and vector both.
Still this error is coming .
Can anyone tell me why this is happening.
#include <bits/stdc++.h>
#include<vector>
void rotate(int arr[], int n);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n] , i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
rotate(a, n);
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
return 0;
}
// } Driver Code Ends
//User function Template for C++
void rotate(int arr[], int n)
{
vector<int> a;
a[0] = arr[n-1];
for(int i = 0 ; i<n-1 ;i++)
{
a.insert(a.back(), arr[i]);
}
for(int j : a)
cout<<j;
}
main.cpp:30:5: error: ‘vector’ was not declared in this scope
vector<int> a;
^~~~~~
Follow these: (EDITED)
(SOLUTION TO YOUR PROBLEM) Use, using namespace std as it means if the compiler finds something that is not declared in the current scope then it will go and check std.
Don't mix c and c++ syntax. Either use printf or cout.
Also check the 1st comment on this answer, as there is something you should know about "using namespace std" and "cout/cin".
No need to work two times, you can also declare and define your function at once.
Solution (but have an error in other parts)
#include <bits/stdc++.h>
using namespace std;
void rotate(int arr[], int n)
{
vector<int> a;
a[0] = arr[n - 1];
for (int i = 0 ; i < n - 1 ; i++)
{
a.insert(a.back(), arr[i]); // ITS YOUR SYNTAX, CONSIDER TO UPDATE IT
}
for (auto &it : a)
cout << it;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
cin>>t;
int a[n] , i;
for (i = 0; i < n; i++)
cin>>a[i];
rotate(a, n);
for (i = 0; i < n; i++)
cout<<a[i];
cout<<"\n";
}
return 0;
}
CHECK LINE NO 9 line,
and see if it's correct or not.
a.insert(a.back(), arr[i]); WRONG
You are doing something wrong there. Check this statement
error: ‘vector’ was not declared in this scope
Solved by using namespace
If you liked this answer. Please consider ticking this answer.:)
With reference to the problem named "Strange List" in codeforces following is my code
But the problem is All test case is passing but the last test case it is showing"MEMORY EXCEEDED LIMIT" and the last test case value is also huge like for n it is 100000.
Please guide me the way to tackle it
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll ele,sum=0,i,j,h,n,x;
cin>>n>>x;
vector <ll> vec;
for(i=0;i<n;i++)
{
cin>>ele;
vec.push_back(ele);
}
for(i=0;i>=0;i++)
{
if(vec.at(i)%x==1)
{
break;
}
else
{
h=vec.at(i)/x;
for(j=0;j<x;j++)
{
vec.push_back(h);
}
}
}
for(i=0;i<vec.size();i++)
{
sum+=vec.at(i);
}
cout<<sum<<"\n";
}
}
You can use Dynamic Programming to solve this problem
Implementation of storing results can be done by using Vector of pairs
Below is the AC code demonstrates how these kinds of problems can be done efficiently in terms of memory consumption......Happy Coding :)
CODE
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t;
cin>>t;
while(t--){
ll n,x;
cin>>n>>x;
vector<pair<ll,ll>> a(n);
for(int i=0;i<n;i++){
cin>>a[i].first;
a[i].second=a[i].first; // Storing initial results in second part of pair
}
ll s=0;
ll d=0;
ll r=0;
ll k=0;
while(++r){
for(int i=0;i<n;i++){
if(a[i].first%x==0){
s+=a[i].second;
a[i].first=a[i].first/x;
a[i].second=(a[i].first*pow(x,r)); // updating results
}
else{
s+=a[i].second;
if(d==0)
k=i;
d=1;
}
}
if(d)
break;
}
for(int i=0;i<k;i++)
s+=a[i].second; // getting desired sum
cout<<s<<"\n"; // printing answer
}
}
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
I'm getting WA(Wrong Answer) in "Compress the List" problem Code(CMPRSS) of CodeChef, here is the link of the problem: https://www.codechef.com/problems/CMPRSS
I've checked the sample output given in the problem and also some self made test cases and It's working properly.I'm not getting what's wrong in my code
here is my approach:
#include <bits/stdc++.h>
using namespace std;
vector<int> number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"\n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"\n";
}
number.clear();
}
return 0;
}
You forgot to check if number[temp+1] exists in the part
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
Therefore, it may read beyond the array and produce wrong output.
Try this case:
2
5
1 2 3 4 5
3
1 2 3
The part should be like this:
while (temp+1 < static_cast<int>(number.size()) && number[temp+1]-number[temp]==1){
temp++;
count++;
}
Your logic is partially correct because you forgot to check the upper limit index of vector: Please check the missing code in the bold letter or between ** code **:
#include <bits/stdc++.h>
using namespace std;
vector number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (**temp+1<n &&** number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"\n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"\n";
}
number.clear();
}
return 0;
}
Check my solution.
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,ctr=0;
cin>>n;
int a[n],ptr=750;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<ptr){
ctr++;
ptr=a[i];
}
}
cout<<ctr-(n/5)+1<<"\n";
}
return 0;
}
All other answers say I would have divided by zero or accessed out of bounds but here I do not find any such situation