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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:48:35+00:00 2026-06-15T10:48:35+00:00

I came by this problem where we are asked to store a string and

  • 0

I came by this problem where we are asked to store a string and a no. associated with it and we are supposed to remove the minimum no.(and its string) from the list and also remove the no.s(and strings) stored after it.The input is a stream of no.,string pair and an input of -1 means we need to remove the samllest from the list and the pairs above it.Output should be the count of items above the minimum numbered item.
e.g. 2 abcd
1 aabb
3 dbbb
-1
o/p 1 (since minimum is 1 aabb and there is just one item after it i.e. 3 dbbb;our list now contains just 2 abcd).
another -1 would produce as o/p 0.
I have tried this using linked list but it seems to take more time than expected.I need a better data structure or algorithm for the same.
Here’s my code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct data
    {
        int no,pos;
        char *st;
        struct data *next;
    }data;
void insert(data *start,int n,char *s);
void minimum(data *start);
int total=0,min=100001,posn=0;//total=total no of nodes,rem=nodes after minimum
data *temp;
int main()
    {
        int N,n;
        data *start;
        start=(data*)malloc(sizeof(data));
        start->pos=0;
        start->no=100002;
        start->next=NULL;
        char c,s[16];
        scanf("%d",&N);
        while(N)
            {
                scanf("%d",&n);
                if(n!=-1)
                    {
                        scanf("%c",&c);
                        scanf("%s",s);
                        total++;
                        posn++;
                        insert(start,n,s);
                    }
                else
                    {
                        printf("%d %s\n",total-(temp->next->pos),temp->next->st);
                        posn=temp->pos;
                        total=temp->pos;
                        temp->next=NULL;
                        minimum(start);
                    }
                N--;
            }
    }
void insert(data *start,int n,char *s)
    {
    while(start->next!=NULL)
        start=start->next;
    if(n<=min)
        {
            temp=start;
            min=n;
        }
    start->next=(data*)malloc(sizeof(data));
    start=start->next;
    start->no=n;
    start->st=(char*)malloc(sizeof(char)*(strlen(s)));
    strcpy(start->st,s);
    start->pos=posn;
    start->next=NULL;
    return;
    }
void minimum(data *start)
    {
        min=100001;
        while(start->next!=NULL)
            {
                if(start->next->no<=min)
                    {
                        min=start->next->no;
                        temp=start;
                        start=start->next;
                    }
            }
        return;
    }

Any help would be appreciated.

  • 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-15T10:48:36+00:00Added an answer on June 15, 2026 at 10:48 am

    Well here are some issues with your code:

    1. start=start->next; should be done in every iteration and not only when a new min is found.
    2. You are always missing the head of the list, iterate while(start != NULL) (and check start->no instead of the next node). (It seems start is a dummy variable, if it is indeed the case – you are only missing it – which is perfectly fine).
    3. You are missing a return type for the minimum() function, it shouldn’t be void. If your list contains ints, the return type should be int, and when you exhaust the loop you should return min;. (If you are interested in returning the min string attached, store an extra variable char* minElement, modify it when you modify min, and return it (minelement) when the loop is exhausted. (Note it is a bad practice to store the answer in a global variable, a better approach is to return it from the function)
    4. You should initialize min to INT_MAX rather an arbitrary number.

    Regarding the optimization issue:

    If you are interested only in finding the minimum element, a heap is a data structure designed exactly for it! It is simple and very efficeint Data Structure for retrieving minimum.

    Also note, if your Data structure does not support deletions, you can cache min in each insertion, something along the lines of this pseudo code:

    add to insertion function:

    if (newValue < min):
       min <- newValue
    

    and min is simple as retrieving the cached min.

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

Sidebar

Related Questions

I recently came in contact with this interesting problem. You are given a string
I came across this problem when I want to check what I input is
I came across this problem when trying to decode json into an array ,
i came across this problem, my code in the viewWillAppear method is not executed
While doing some small regex task I came upon this problem. I have a
I'm using GAE with python and I came across this problem, I'm trying to
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
I was configuring syntax highlighting for my blog when I came across this problem.
Problem This question actually came up at work today. We are planning an experiment
I am watching a video about [LINQ][1] and came across a problem. In this

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.