Find the missing numbers
来源:互联网 发布:网络执行官 编辑:程序博客网 时间:2024/05/08 01:38
Question:
Design and implement an algorithm (C++ function) that, given an array of integers 0 to 2n-1 with two values missing, determines (and displays) the two missing integers. For example, if n is 3, the given integers might be {0,1,3,4,5,7}, so the output would be 2 and 6.
Note that the given integers are in ordered. You may assume there are no duplicate values in the array.What is the runtime of your algorithm? What is the memory requirement of your algorithm?
test IDE: Microsoft Visual Studio 2005
test OS:Microsoft Windows 7
The code as following may solve this problem:
// MissingNumber.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <cstdio>int _tmain(int argc, _TCHAR* argv[]){int nCount;printf("Input N:");scanf("%d", &nCount);nCount *= 2;int iComp =0;int *pData = new int[nCount];printf("Input numbers:");for(int i = 0;i < nCount; ++i){scanf("%d", &pData[i]);}const int MAX_NUM = nCount + 1;printf("The missing numbers are:"); // note the loop criterion, it can help you to work till the upbound for(int i = 0;i < nCount || iComp <= MAX_NUM;) { if (pData[i] == iComp) { ++iComp; ++i; } else { printf("%d ", iComp); ++iComp; } } return 0;}
Analysis:The runtime complexity is O(n) and the memory requirement is O(1).
Thinking:
What if the given integers are in any order ?
A resolution may work out to the thinking: You can use quick sort before finding the missing integers.
- Find the missing numbers
- Find the missing letter
- *[Lintcode]Find the Missing Number
- Find the biggest k numbers
- Q5.7 Find the missing number
- 数据流小问题--find the missing elements
- C/C++ Find the missing number
- lintcode Find the Missing Number II
- lintcode-196. Find the Missing Number
- Find The Shortest Path In Triangle Numbers
- Find the disappear numbers in a array
- PIQ59: Find the missing number in the increasing sequence
- find the two same numbers in 1 million random numbers
- Find the missing integer from 0 to n
- Find out the missing indexes and their usefulness
- Find the missing integer----programming pearls(second chapter)
- 寻找数组中缺少的整数(Find the missing integer)
- LintCode --find-the-missing-number(寻找缺失的数)
- 2012 Microsoft Intern Hiring Written Test : 6
- 联想Y450 gt130m显卡驱动安装
- stock初始步骤
- openjdk和jdk的区别与联系
- 屏幕白屏解决方法
- Find the missing numbers
- SQLite语法
- Screen 1000 warning message
- 遍历文件夹获取文件名
- 面向过程,面向对象的深入理解一
- 用javaAPI取得java进程的PID
- NSData 与 NSString,Byte数组,UIImage 的相互转换
- WM_GETINFO相关
- 关于BSP,BIOS,和bootloader区别