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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:31:32+00:00 2026-05-27T22:31:32+00:00

This question is for real brainiacs, cause it should be done without an auxiliary

  • 0

This question is for real brainiacs, cause it should be done without an auxiliary array
and has to be most efficient!

C program – needs to recieve an array with X numbers
(suppose X=4 array : 5,4,3,2)
and check if the array has all the numbers from 0 to X-1
(If X is 44 it needs to check if all numbers between 0 to 43 inside the array).

It has to be super efficient – I mean, running on the array 43 times is not an option!

Do you have any idea how to do this?? I’m trying to figure this one for hours without any success!!

It has to be O(n).

  • 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-27T22:31:32+00:00Added an answer on May 27, 2026 at 10:31 pm

    I don’t understand what I’m missing in this question but AFAIK I don’t see a reason why any (reasonable) solution should be anything more-/worse- than O(n) time (and space) complexity.

    From the above comments and answers, I understand the following:

    • Negative numbers : I’m not sure whether negative numbers are allowed or not. The OP says check if all the array has all the numbers from 0 to X-1. So anything less than 0 is not expected in the array. So I assume negative numbers are not allowed
    • Duplicate numbers : referring to the same quote from the OP – check if all the array has all the numbers from 0 to X-1 I guess if X is the size of the array and all numbers from 0 to X-1 should be present, the I guess no duplicate numbers are allowed.

    So making the above assumptions, we can use one bit to check whether i (0 <= i <= X-1) is present or not. If i is duplicate then it will fail mentioning that there is a duplicate number.

    It scans the whole array one time – O(n) and just uses one bit per element, so X is 10 the X bits are needed – for example consider we need to handle 1000000 elements and sizeof(int) is 4 then we need 3.82M of memory to hold the array elements and only 0.48M is used for storing the presence of an element.

    #include <stdio.h>
    
    #define X 10
    #define MIN 0
    #define MAX X-1
    
    int main(void)
    {
        //int arr[X] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        //int arr[X] = {6, 7, 8, 9, 0, 5, 3, 2, 11, 12};
        //int arr[X] = {1, 1, 2, 4, 5, 6, 7, 8, 2, 2};
        int arr[X] = {1, 3, 2, 4, 5, 6, -7, 8, 2, 2}; 
    
        /* if X is more than sizeof(int) then change 
           the flag type to a wider one! */
        int flag = 0;
    
        int i;
    
        for(i=0 ;i<X ; i++) {
            if (arr[i] >= MIN && arr[i] <= MAX) {
                if (flag & 1<<arr[i]) {
                    printf("Duplicate found : %d \n", arr[i]);
                    return -1; 
                }   
                flag |= 1<<arr[i];
            } else {
                printf("Failure at %d : %d \n", i, arr[i]);
                return -1; 
            }   
        }   
    
        printf("Success \n");
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this should be a real easy question but I can't seem to find
This question has a relation with my previous question, What is the real memory
This question has (for me) no real implication. I just found this out by
This question has been asked before but with no real answer. I wan't to
This question relates to recursion. Consider the program shown below (not my real code,
The reason for me asking this question is:- I get a real buzz out
This is more an observation than a real question: MS-Access (and VBA in general)
If this is not a real question then feel free to close ;)
Sorry for this not being a real question, but Sometime back i remember seeing
This is a real newbie question. I have simple app that selects a picture

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.