C program to find missing integer in a sequence of numbers

Sri Harsha

You are given a sequence of n-1 distinct positive integers, all of which are less than or equal to a integer ‘n’. You have to find the integer that is missing from the range [1,2,...,n]. Solve the question without using arrays.

Input Format: One line containing the integer ‘n’ where 2<=n<=10,000 First line is followed by a sequence of ‘n-1’ distinct positive integers. Note that the sequence may not be in any particular order.

I got code by using arrays

#include<stdio.h>
int main()
{
 int i,j,n[9999],m,t;
 scanf("%d",&m);
 for(i=1;i<m;i++)
  {
   scanf("%d",&n[i]);
  }
 for(i=1;i<m;i++)
  {
   for(j=1;j<i;j++)
    {
      if(n[j]>n[j+1])
       {
         t=n[j];
         n[j]=n[j+1];
         n[j+1]=t;
        }
    }
   }
   for(i=2;i<m;i++)
    {
     if(n[i-1]!=n[i]-1)
       {
          printf("%d",n[i]-1);
          break;
       }
  }
 return(0);
 }

How can I do the same without using arrays?

Am_I_Helpful

The logic is simple. You just find the sum of the continuous numbers in series for a given n.

And, now add all those numbers provided in the question to find the sum exactly of the given numbers.

The difference is what you can say that difference between those 2 sums is the missing number.

Ex :- Let's say, n = 6.

So, you just find the sum of n consecutive integers starting from 1,...,6 is :- 6 * (6+1) / 2 = 21. Formula of sum of n consecutive integers starting from 1 is {n * (n+1)} / 2.

And, now find the sum of given n-1 numbers.

Say, numbers given are 1,2,4,5,6. Then their sum = 1 + 2 + 4 + 5 + 6 = 18.

Therefore, the missing number = sum of continuous n numbers - sum of given (n-1) numbers = 3.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C program to find missing integer in a sequence of numbers

From Dev

Find the largest sequence of numbers in an integer arraylist

From Dev

Find first missing number in a sequence of numbers

From Dev

Find Missing multiple numbers sequence in SQL server

From Dev

SQL to find missing numbers in sequence starting from min?

From Dev

C program - find largest sequence in array

From Dev

Write a program to find the minimum of a sequence of nonnegative numbers entered

From Dev

Write a program to find the minimum of a sequence of nonnegative numbers entered

From Dev

Create a program with php that will find the missing number in the series of numbers

From Dev

Python: find a sequence of numbers

From Dev

SQL summary of missing numbers in sequence

From Dev

How do I multiply a long integer with different numbers in C program?

From Dev

Given a sequence of numbers how to identify the missing numbers

From Dev

Find missing numbers in array

From Dev

Find missing numbers with SQL

From Dev

C Program to find Windowed Average of a sequence of values, given their timestamps

From Dev

C Program to find Windowed Average of a sequence of values, given their timestamps

From Dev

Find 4 sequence numbers in String Using C#

From Dev

How to find the missing number in a sequence

From Dev

shifting a sequence of numbers in C?

From Dev

shifting a sequence of numbers in C?

From Dev

How to find sequence of numbers in list

From Dev

Find repeated numbers sequence with regex

From Dev

this is a program to find prime numbers from 2 to 100 in C

From Dev

Fibonacci Sequence listing numbers upto integer

From Dev

Count the sequence of numbers while skipping missing values

From Dev

difference between numbers in sequence when ids missing

From Dev

Program that reads a sequence of integer and outputs true if sequence is ordered in ascending or descending

From Dev

Java program to sort a sequence of numbers in a text file

Related Related

  1. 1

    C program to find missing integer in a sequence of numbers

  2. 2

    Find the largest sequence of numbers in an integer arraylist

  3. 3

    Find first missing number in a sequence of numbers

  4. 4

    Find Missing multiple numbers sequence in SQL server

  5. 5

    SQL to find missing numbers in sequence starting from min?

  6. 6

    C program - find largest sequence in array

  7. 7

    Write a program to find the minimum of a sequence of nonnegative numbers entered

  8. 8

    Write a program to find the minimum of a sequence of nonnegative numbers entered

  9. 9

    Create a program with php that will find the missing number in the series of numbers

  10. 10

    Python: find a sequence of numbers

  11. 11

    SQL summary of missing numbers in sequence

  12. 12

    How do I multiply a long integer with different numbers in C program?

  13. 13

    Given a sequence of numbers how to identify the missing numbers

  14. 14

    Find missing numbers in array

  15. 15

    Find missing numbers with SQL

  16. 16

    C Program to find Windowed Average of a sequence of values, given their timestamps

  17. 17

    C Program to find Windowed Average of a sequence of values, given their timestamps

  18. 18

    Find 4 sequence numbers in String Using C#

  19. 19

    How to find the missing number in a sequence

  20. 20

    shifting a sequence of numbers in C?

  21. 21

    shifting a sequence of numbers in C?

  22. 22

    How to find sequence of numbers in list

  23. 23

    Find repeated numbers sequence with regex

  24. 24

    this is a program to find prime numbers from 2 to 100 in C

  25. 25

    Fibonacci Sequence listing numbers upto integer

  26. 26

    Count the sequence of numbers while skipping missing values

  27. 27

    difference between numbers in sequence when ids missing

  28. 28

    Program that reads a sequence of integer and outputs true if sequence is ordered in ascending or descending

  29. 29

    Java program to sort a sequence of numbers in a text file

HotTag

Archive