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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:14:23+00:00 2026-06-16T06:14:23+00:00

Here’s the problem I’ve been trying to tackle for a few days. I need

  • 0

Here’s the problem I’ve been trying to tackle for a few days.

I need to write a program that gets a sorted array. The program will put 999 where two adjacent blocks have the same value, and then put all the 999 in the end of the array. I need to do this without using another array and the program must be O(n).

example input:

50,60,60,72,81,81,81,81,93,93

desired output:

50,60,72,81,93,999,999,999,999,999

another example:

1,1,2,3,4,4,5,6,6

desired output:

1,2,3,4,5,6,999,999,999

my code. It’s not working. For the first example the output is alright. for the second example i get 1,2,3,4,5,4,5,6,-14568127 (out of array bounds)

my algorithm is i walk through the array with two indexes, i and j, if a[i]!=a[i+1] then i advance i. if they are equal, j looks for the next unique value, and puts it in a[i+1].

I’d love to hear better ideas or a code to do this. in C.

while((j!=size-1)&&(a[size-1]!=a[i]))
{
     if(a[i]!=a[i+1])
     {
         i++;
         j=i;
     }
     if(a[i]==a[i+1])
     {
         j=i;
         while(a[i]==a[j])
               j++;
         a[i+1]=a[j];
         i++;
         if(j!=size-1)
              j=i;
     }
}
i++
for(;i<size;i++)
      a[i]=999;

I’ve edited the code, now I do it as chen suggested. First i iterate through the array putting 999 where the doubles are, problem arises when I want to switch though. here’s the code I wrote for re-sorting the array:
each time i put a 999 somewhere, count++.

It’s working for the two examples I gave perfectly. Thanks everyone.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
    int *a;
    int i=0,j=0,size,count=0;
    printf("Enter the size of the array\n");
    scanf("%d", &size);
    a=(int *)calloc(size,sizeof(int));
    printf("Enter %d numbers\n",size);
    for(i=0;i<size;i++)
        scanf("%d",&a[i]);
    printf("The array recieved is :\n");
    for(i=0;i<size;i++)
        printf(" %d ", a[i]);
    i=0;
    printf("\n");
    for(i=0;i<size;i++)
    {
        if(a[i]==a[i+1])
        {
            j=i+1;
            while(a[i]==a[j])
            {
                a[j]=999;
                j++;
            }
            count++;
        }
    }
    while(count!=0)
    {
        for(i=0;i<size-1;i++)
        {
            j=i;
            if(a[j]==999)
            {
                a[j]=a[j+1];
                a[j+1]=999;
            }
        }
        count--;
    }
    printf("The new array is: \n");
    for(i=0;i<size;i++)
        printf(" %d ",a[i]);
    free(a);
    getch();
}
  • 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-16T06:14:24+00:00Added an answer on June 16, 2026 at 6:14 am

    Here’s a general approach that runs in O(n):

    Assumption: The input array does not contain 999 (It’s fine if it does, you just have to use a different sentinel value):

    • Iterate through the array once:
      • For each element, look ahead to the next one
      • If it’s a duplicate to the current element, change it to 999
      • Keep looking ahead and marking duplicates as 999 until you hit a different element.
    • You want all the 999‘s at the end, so now, keep 2 separate pointers:
      • An index X to keep track of where the first 999 is in the array
      • Another index num to keep track of where we are in the swapping process
    • Iterate through the array again, swap all the valid numbers with 999 using these two pointers.

    This might sound confusing, so here’s a pseudo animation using one of your examples:
    (Using W as the sentinel in place of ‘999` for readability)

    1,1,2,3,4,4,5,6,6,
    1,W,2,3,4,4,5,6,6,
    1,W,2,3,4,W,5,6,6,
    1,W,2,3,4,W,5,6,W,
    1,2,W,3,4,W,5,6,W,
    1,2,3,W,4,W,5,6,W,
    1,2,3,4,W,W,5,6,W,
    1,2,3,4,5,W,W,6,W,
    1,2,3,4,5,6,W,W,W,
    

    And again, but labeling the W with numerical suffix this time so you can see which swaps are happening:

    1,1,2,3,4,4,5,6,6,
    1,W1,2,3,4,4,5,6,6,
    1,W1,2,3,4,W2,5,6,6,
    1,W1,2,3,4,W2,5,6,W3,
    1,2,W1,3,4,W2,5,6,W3,
    1,2,3,W1,4,W2,5,6,W3,
    1,2,3,4,W1,W2,5,6,W3,
    1,2,3,4,5,W2,W1,6,W3,
    1,2,3,4,5,6,W1,W2,W3,
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code I have. It works. The only problem is that the first
Here is another spoj problem that asks how to find the number of distinct
Here is the code in a function I'm trying to revise. This example works
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here's my search array: $aExt = array('png','jpg','gif'); I want to search through the variable:
Here's the setup - I have a view that lists products. On that same
Here is a scenario: User installs .NET application that you have made. After some
Here's a sample of a SpinBox that writes its changes to underlying variables. The
Here is my initial code: $camp_price=array( 'option 1' => array( 'id' => 'June 30
Here is my problem: I have a chatapplication and the messages are displayed in

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.