While I was solving a question from codeforces(link to the question: https://codeforces.com/contest/1466/problem/A) I encountered weird output from the below code
#include<iostream>
#include<vector>
#include<set>
using namespace std;
void solve(){
int64_t n;
cin>>n;
if (n<2){
cout<<0<<endl;
return;
}
vector<int64_t> vec(n, 0);
for(auto &i: vec){
cin>>i;
}
set<int64_t> s;
for(int64_t i=0; i<n-1; ++i){
for(int64_t j=i+1; j<n; ++j){
s.insert(vec[j]-vec[i]);
}
}
cout<<s.size()<<endl;
}
int main(){
int64_t t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Input:
8
4
1 2 4 5
3
1 3 5
3
2 6 8
2
1 2
1
50
5
3 4 5 6 8
3
1 25 26
6
1 2 4 8 16 32
Ouptut given by my code:
4
2
3
1
0
53
1
1
When I removed if block it passed all the pretests/testcases.
if (n<2){
cout<<0<<endl;
return;
}
Expected output:
4
2
3
1
0
5
3
15
After removing the if block I get the same output as above. I'm not able to understand how if block is affecting the size of set in the next iteration.
Compiled with g++ same output with -std=c++11 -std=c++14 and -std=c++17
if (n<2){
cout<<0<<endl;
return;
}
Since you're returning here, it means that if n is 1, you never read that one value out of std::cin.
If you remove that if, then that one value will get read in here:
vector<int64_t> vec(n, 0);
for(auto &i: vec){
cin>>i;
}
So if you want the if to work as expected, it should be:
if (n<2){
cout<<0<<endl;
if(n == 1) { std::cin >> n; } // remove 1 item from stream
return;
}
Related
// printing in spiral order matrix
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int arr[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>arr[i][j];
}
}
// print
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
// spiral print
int row_start=0,row_end=n-1,col_start=0,col_end=m-1;
while(row_start<=row_end && col_start<=col_end){
for(int j=col_start; j<=col_end; j++){
cout<<arr[row_start][j]<<" ";
}
row_start++;
for(int i=row_start; i<=row_end; i++){
cout<<arr[i][col_end]<<" ";
}
col_end--;
for(int j=col_end; j>=col_start; j--){
cout<<arr[row_end][j]<<" ";
}
row_end--;
for(int i=row_end; i>=row_start; i--){
cout<<arr[i][col_start]<<" ";
}
col_start++;
}
return 0;
}
My output is:
PS C:\Users\anmol\Desktop\c++projectwork> ./a
3 4
1 2 3 4 5 6 7 8 9 0 1 2
1 2 3 4
5 6 7 8
9 0 1 2
1 2 3 4 8 2 1 0 9 5 6 7 6
I am getting an extra '6' at the end.
which is not required however this type of problem only come when matrix is rectangular.
But code works fine for square matrix.
Please tell me where i went wrong..
Suppose you have a below matrix. Let's dry run your code on below example.
1 2 3 4
5 6 7 8
9 10 11 12
Dry Run
1st for loop: prints 1 2 3 4 row_start=1,row_end=2,col_start=0,col_end=3
2nd for loop: prints 8 12 row_start=1,row_end=2,col_start=0,col_end=2
3rd for loop: prints 11 10 9 row_start=1,row_end=1,col_start=0,col_end=2
4th for loop: prints 5 row_start=1,row_end=1,col_start=1,col_end=2
All condition of while loop are true 2nd Iteration of while loop:
1st for loop: prints 6 7 row_start=2,row_end=1,col_start=1,col_end=2
2nd for loop: won't do anything row_start=2,row_end=1,col_start=1,col_end=1
3rd for loop: prints 6
DO you see the problem?
you should run 3rd for loop only if
"row_start < row_end"
similarly you should check for
"col_start<col_end"
before running the 4th loop
# At first case I put the temp variable to condition statement in while loop#
int tab[8]={0,1,7,8,7,6,5,2};
int n=8;
int j;
int temp;
for(int i=1;i<n;i++)
{
temp=tab[i];
j=i-1;
while(j>=0&&tab[j]>temp)
{
tab[j+1]=tab[j];
--j;
}
tab[j+1] = temp;
}
And the resault that I get is :
0 1 7 8 7 6 5 2
0 1 2 5 6 7 7 8
But on the other hand when instead I use tab[i] in condition statement in while loop
for(int i=1;i<n;i++)
{
temp=tab[i];
j=i-1;
while(j>=0&&tab[j]>tab[i])
{
tab[j+1]=tab[j];
--j;
}
tab[j+1] = temp;
}
I get this result:
0 1 7 8 7 6 5 2
0 1 7 7 6 5 2 8
And I can't find any difference between passing these values and why it behaves like that.
I think that I am using the same values.
It's mine first question on that page , be kind to me please, have a nice day
Because in first iteration of while loop if tab[j] > tab[i]
tab[j+1]=tab[j]; changes tab[i] to tab[i - 1],
So essentially your whole program in second case is equivalent to this
#include <iostream>
int main ()
{
int tab[8]={0,1,7,8,7,6,5,2};
int n=8;
int j;
int temp;
for(int i=1;i<n;i++)
{
temp=tab[i];
j=i-1;
if (tab[j]>tab[i])
{
tab[j+1]=tab[j];
--j;
}
tab[j+1] = temp;
}
for(auto&& a: tab) {
std::cout << a << " ";
}
}
my code is not working for Input:
10 3 *12* 4 2 9 13 0 8 11 1 7 5 6
Its Correct output is:
12 12 *13* 9 9 13 -1 8 11 -1 7 -1 6 -1
And Your Code's output is:
12 12 *-1* 9 13 13 -1 8 11 -1 7 -1 6 -1
what I can see, it is because in the while(!s.empty() && a>s.top()) part I m not storing the index values for those elements for which a<s.top(), I'm not able to think of any way to do so.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll a,i,c[n];
memset(c,-1,sizeof(c));
stack <int> s;
for(i=0;i<n;i++){
cin>>a;
if(s.empty()){
s.push(a);
}
else{
if(a>s.top()){
int k=i;
while(!s.empty() && a>s.top()){
s.pop();
c[k-1]=a;
k--;
}
s.push(a);
}
else{
s.push(a);
continue;
}
}
}
for(i=0;i<n;i++)
cout<<c[i]<<" ";
cout<<endl;
}
}
There is a little bug in your code. When you update the value of c[index]=value, the index is not correct. While processing, you pop out some of the elements from the stack and push the value at the end(example: a value at a location of 10 can be pushed at anywhere between 0 and 10), but during stepping backward you don't know what was the correct location of that value.
So, I made slide changes in your code, kept track of the location of the value along with the value itself. So, when I update c[index]=value, I know the correct index of s.top() element.
int main() {
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll a,i,c[n];
memset(c,-1,sizeof(c));
stack < pair<int,int> > s;
for(i=0;i<n;i++){
cin>>a;
if(s.empty()){
s.push({a,i});
}
else{
if(a>s.top().first){
while(!s.empty() && a>s.top().first){
c[s.top().second]=a;
s.pop();
}
s.push({a,i});
}
else{
s.push({a,i});
continue;
}
}
}
for(i=0;i<n;i++)
cout<<c[i]<<" ";
cout<<endl;
}
}
Input & Output:
1
15
14 10 3 12 4 2 9 13 0 8 11 1 7 5 6
-1 12 12 13 9 9 13 -1 8 11 -1 7 -1 6 -1
I was solving subarray with given sum,Where we have to print the starting and ending index of array if subarray with sum is found , when I tried with two test cases simultaneously i got wrong result
But when I was tried one at a time I got right answer in both.
You please also check in your IDE this is happening in every IDE.
Testcase (Simultaneously)
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output
2 4 (expected 2 4)
2 5 (But expected 1 5)
But when I tried like this for second test cases
1
10 15
1 2 3 4 5 6 7 8 9 10
Output : 1 5(As expected)
I got correct answer ,why my program this kind of weird behaviour ?
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
vector<int>a;
unordered_map<int, int>seen;
int main()
{
int t;
cin >> t;
while (t--) {
int n, s;
cin >> n >> s;
a.resize(n);
int sum = 0;
seen[0] = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (seen.find(sum - s) != seen.end()) {
int x;
x = seen[sum - s] + 2;
cout << x << " " << i + 1 << endl;
break;
}
else {
seen[sum] = i;
}
}
seen.clear();
a.clear();
//cout<<endl;
}
return 0;
}
Hi guys i've to calculate the longest sequence of numbers without any repetitions and return the size of the sub-segment.
The point is that im missing something at some point but I don't know where.
int resolverCaso() {
int num;
int cont = 0;
cin >> num;
int var;
TreeMap<int,int> a;
int aux;
int max = 0;
for (int i = 0; i < num; i++) {
cin >> var;
if (!a.contains(var)) {
a[var] = i;
aux = var;
cont++;
}
else {
if (a[aux]==i-1 && var==aux) {
cont = 1;
a = TreeMap<int, int>();
a[var] = i;
}
else {
a.erase(var);
a[var] = i;
}
}
if (cont > max) {
max = cont;
}
}
return max;
}
I've tried the following cases with this outputs and everything seems to be ok.
E:1 2 3 1 2 3 O:3
E:2 2 2 2 O:1
E:4 5 6 7 6 O:4
E:7 8 9 10 7 8 9 11 2 O:6
E:7 8 9 10 10 10 1 2 3 4 O:5
E:3 4 2 3 4 2 8 9 10 11 O:7
E:0 O:0 ( empty vector ).
E:1 O:1
So basically im looking for some sequence that doesn't work with my code.
Thanks.
The problem is with
else {
a.erase(var);
a[var] = i;
}
You need to do more here. Try the sequence 1 3 4 2 3 4 2 8 9 10 11.