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 7418629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:55:14+00:00 2026-05-29T07:55:14+00:00

n = int(raw_input()) s = raw_input().strip().split(‘ ‘) ar = index = [0]*n; done =

  • 0
n = int(raw_input())
s = raw_input().strip().split(' ')
ar = index = [0]*n; done = [False]*n
for a in range(n): ar[a] = int(s[a])
k=0
for a in range(5):
     m = 1000000001
     for i in range(n):
         if done[i]: continue
         if ar[i] < m: m = ar[i]
     if m == 1000000001: 
         sys.stdout.write('-1\n')
         break
     #print m
     for i in range(n):
         if ar[i] == m:
             index[k] = i
             k+=1
             done[i]=True

print index

The algorithm is quite simple. ar is an array of n (>= 5) integers. I intend to store the 0 based positions of the first 5 smallest integers of the array in index.

After taking n as input, n space separated integers are inputted on the next line.

The problem is very weird – for the following input:

7
6 17 5 3 13 8 10

when I uncomment #print m in the code, it prints 3 3 0 3 0 (expected output is 3 5 6 8 10); also, an IndexError occurs (that is just a symptom of the real problem).

Variable done is working as expected (it contains a list of boolean values, where, if done[i] is True, then ar[i] should not be taken into consideration while looking for min(ar)

I did a lot of debugging by printing values of variables at different locations, but couldn’t reason out anything.

  • 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-05-29T07:55:15+00:00Added an answer on May 29, 2026 at 7:55 am

    I’m not really following your code, but this:

    ar = index = [0]*n
    

    seems quite strange to do in python, try to replace it with:

    ar = [0]*n
    index = [0]*n
    

    And see if it works.

    This example, may help to see why that seems strange:

    >>> a = b = [0] * 5
    >>> a
    [0, 0, 0, 0, 0]
    >>> b
    [0, 0, 0, 0, 0]
    >>> a[1] = 2
    >>> a
    [0, 2, 0, 0, 0]
    >>> b
    [0, 2, 0, 0, 0]
    

    You may be interested in taking a loook at Other languages have “variables”, Python has “names”.

    Side note:

    Try to make your code more readable:

    • go to a new line when required, there’s no actual need for ;
    • don’t write for-loops/if-statements in one line: there’s list comprehension for that.

    Also (if I understood what you’re trying to do), your code could be rewritten like:

    s = raw_input().strip().split(' ')
    ar = [(int(num),i) for i,num in enumerate(s)]
    ar.sort()
    print [a[1] for a in ar[:5]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: >>> num = int(raw_input('Enter the number > ')) If the user
int x = n / 3; // <-- make this faster // for instance
int x; printf(hello %n World\n, &x); printf(%d\n, x);
int[] mylist = { 2, 4, 5 }; IEnumerable<int> list1 = mylist; list1.ToList().Add(1); //
int n = 5; for(int i = 0;i!=n;i++)//condition != { //executing 5times } int
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
int main() { int var = 0;; // Typo which compiles just fine }
int main(void) { std::string foo(foo); } My understanding is that the above code uses
int bufferlength = 12488; int pointer = 1; int offset = 0; int length

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.