Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:31:30+00:00 2026-06-05T09:31:30+00:00

I was writing a code , in which when i declare a vector globally

  • 0

I was writing a code , in which when i declare a vector globally , it gives wrong answer but when i declare it in the main function . it becomes right . so i want to know the difference between the two declarations

this is a code which find the min of first enteries, and 2nd min of 2nd enteries and then if min >= 2nd min print no . here i declared vector in the main class but when i declare it with my INTs declared , the site on which i was submitting gives me wrong answer.

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>

using namespace std;

#define MAX 20000;
int a,b,c,d,i,z;


int main()
{
        scanf("%d %d", &a, &b);
        while( a!=0 && b!=0)
        {
            z = MAX;
            for(i=0;i<a;i++)
            {
                scanf("%d",&c);
                if(z>c)
                {
                    z=c;
                }
            }


            vector<int> v;  
            for(i=0;i<b;i++)
            {
                scanf("%d",&d);
                v.push_back(d);
            }
            sort(v.begin(),v.end());

            if (z >= v[1])
            printf("N\n");
            else
            printf("Y\n");
        scanf("%d %d", &a, &b);
        }       
    return 0;
}

Sudhanshu

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:31:32+00:00Added an answer on June 5, 2026 at 9:31 am

    If you declare the vector as a global variable, you should invoke vector::clear() in the begin of the loop to clear all of the elements stored in the last loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a code, in which one of the files uses set_intersection function,
I'm am writing code which calculates an insurance premium up to the 65th birthday.
I'm writing some code which could really do with some simple compile time metaprogramming.
I am writing a code which compares file1 (single column of entries) with file
I'm writing code in which I use SQL to test several different conditions before
I'm writing some code which involves running a few shell commands from Python and
I am writing some code in which i need to get a line from
I'm writing a compiler which uses C as an intermediate code which is (Currently)
I'm writing code that uses sched_setaffinity, which requires kernel 2.5.8 or later. I've been
I'm writing code to download email from various servers, some of which are outside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.