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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:00:54+00:00 2026-05-28T20:00:54+00:00

Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)

  • 0

Possible Duplicate:
Easy interview question got harder: given numbers 1..100, find the missing number(s)
Find the missing and duplicate elements in an array in linear time and constant space

I saw an interesting Question on one forum.

you have 100 elements from 1 to 100 but byy mistake one of those number overlapped another by repeating itself.
E.g. 1,99,3,…,99,100
Array is not in sorted format , how to find the repeating number ?

I know Hash can do it O(n) time and O(n) space, I need O(1) space.

  • 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-28T20:00:55+00:00Added an answer on May 28, 2026 at 8:00 pm

    We can do it in O(n) and constant space:

    1. For every element, calculate index = Math.abs(a[i]) - 1
    2. Check the value at index
      • If it is positive, multiply by -1, i.e., make it negative.
      • if it is negative, return (index+1) as answer, as it means we have seen this index before.

    “”

    static int findDup(int[] a){
        for(int i=0;i<a.length;i++){
            int index = Math.abs(a[i]) - 1;
            if(a[index] < 0)
                return index+1;
            else
                a[index] = -1 * a[index];
        }
        return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
Possible Duplicate: Is there an easy way to convert a number to a word
Possible Duplicate: Is there an easy way in .NET to get “st”, “nd”, “rd”
Possible Duplicate: Try to describe polymorphism as easy as you can What is polymorphism?
Possible Duplicate: Make error: missing separator I've made many makefiles, but today I was
Possible Duplicate: What's the best place to find Git reference content online while kernel.org
Possible Duplicate: Validate numbers in JavaScript - IsNumeric() var miscCharge = $(#miscCharge).val(); I want
Possible Duplicate: In Perl, how can I check from which module a given function
Possible Duplicate: Easy to use/learn PHP framework? Do most of professional programmers recommend using
Possible Duplicate: Easy way of populating Javabeans based on request parameters Hi, I have

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.