46 views

Uploaded by neel_aakash

- Solution Final - Computer Programming - Spring 2014
- 1999-02 const T vs T const
- joan file
- Weeel Speed Sensor
- DVE_Error_2013.06-sp1
- t13CPointersArithmetic.pps
- Actitycompilation(Programming)
- Cpp Constants Literals
- enall
- Lampiran Program
- SynapseIndia Reviews on PHP Development Array Function- Part 2
- Arrays
- Cs4 Final Ans
- c Technical1
- AdvanceSTLC++
- RTRT Setting for Instrumentation
- c and Data Structures Q and A
- Edited by Foxit Reader Copyright(C) by Foxit
- OOPS colegge notes
- Strings in C

You are on page 1of 2

Find the missing element in a given permutation.

A zero-indexed array A consisting of N different integers is given. The array co

ntains integers in the range [1..(N + 1)], which means that exactly one element

is missing.

Your goal is to find that missing element.

Write a function:

int solution(vector<int> &A);

that, given a zero-indexed array A, returns the value of the missing element.

For example, given array A such that:

A[0] = 2

A[1] = 3

A[2] = 1

A[3] = 5

the function should return 4, as it is the missing element.

Assume that:

N is an integer within the range [0..100,000];

the elements of A are all distinct;

each element of array A is an integer within the range [1..(N + 1)].

Complexity:

expected worst-case time complexity is O(N);

expected worst-case space complexity is O(1), beyond input storage (not

counting the storage required for input arguments).

Elements of input arrays can be modified.

*/

#include <algorithm>

#include <vector>

#include <iostream>

/* Correctness 80%, Performance 60% Task score 70% */

int solutionF(std::vector<int> &A)

{

const int size = A.size();

if ((0 == size) || (size > 100000)) return -1;

const long sumOfArray = std::accumulate(A.begin(), A.end(), 0);

const long sumOfSequence = (size + 1) * (size + 2) / 2;

int missingElement = static_cast<int>(sumOfSequence - sumOfArray);

return missingElement;

}

/* Detected time complexity: O(N) or O(N * log(N)). Correctness 100%, Performanc

e 100% Task score 100% */

int solution(std::vector<int> &A)

{

const int size = A.size();

if ((size > 100000)) return -1;

const int offset = (size + 2) >> 1; // make range symmetric between [-N/2..N/

2] to avoid integer overflow in Gauss sum for large N

// "gauss" should nominally be a zero sum, but if N is odd then N/2 is truncat

ed

// and offset is off by 1/2 for each of the N elements. This formula compensat

es for that.

const int gauss = (size & 1) * offset;

long long sum = 0;

for (int i = 0; i < size; ++i) {

sum += (A[i] - offset); // this is the O(n) part of summing all elemen

ts in the array

}

const int misingNumber = gauss - sum + offset; // find the missing number

return misingNumber;

}

int _tmain()

{

std::vector<int> A;

A.push_back(1);

A.push_back(2);

A.push_back(3);

A.push_back(5);

A.push_back(4);

std::cout << "Result: " << solution(A) << std::endl;

_getch();

return 0;

}

- Solution Final - Computer Programming - Spring 2014Uploaded bynicencool78
- 1999-02 const T vs T constUploaded byladera80
- joan fileUploaded byjoan_bating
- Weeel Speed SensorUploaded byhamamine mustapha
- DVE_Error_2013.06-sp1Uploaded bySmita Gadekar-Jadhav
- t13CPointersArithmetic.ppsUploaded byAnjali Naidu
- Actitycompilation(Programming)Uploaded byjaracimanes
- Cpp Constants LiteralsUploaded byeem55374
- enallUploaded byInilah Isi Batinku
- Lampiran ProgramUploaded byVina Alfi Madjidah
- SynapseIndia Reviews on PHP Development Array Function- Part 2Uploaded bysynapseindia
- ArraysUploaded bykishorinaren
- Cs4 Final AnsUploaded byBen_Scarlato_2410
- c Technical1Uploaded byMian Babu
- AdvanceSTLC++Uploaded byrashed44
- RTRT Setting for InstrumentationUploaded byvishal kumar sinha
- c and Data Structures Q and AUploaded bysarath
- Edited by Foxit Reader Copyright(C) by FoxitUploaded bysunil kumar yadav
- OOPS colegge notesUploaded bySrinathReddy
- Strings in CUploaded bySerban Petrescu
- LECT12.pdfUploaded byAnirodh
- 4.+Arrays in cUploaded byshuriyaa
- CSE2123-Lecture-01-02 (1)Uploaded byShahid Khattak
- C++ SummaryUploaded byramakantsawant
- CheatSheetP1Uploaded by2622799308i
- Dynamic ArrayUploaded byMahar Hasnain
- PbaUploaded byFabian Baez Farias
- 01(1)Uploaded byTina
- FibonacciUploaded byAndrew Cooper
- CS F213 RL 7.1Uploaded bySAURABH MITTAL

- ikrma aaaUploaded byIkram Ali
- 1492-pp028_-en-pUploaded byAnonymous 1AAjd0
- Komptech CribusUploaded bybaleizaobz
- Predominant EngineersUploaded bybappabatliboi
- My ResumeUploaded byPykah Watif
- 5. Owb091500(Slide) Usn9810 v900r015c10(Mme) Data Configuration Issue1.01Uploaded byfourneel
- tuto 1Uploaded byZafirahAhmadFauzi
- 57989Uploaded byKang Asu
- Wps Mma PipeUploaded byDimitris Nikou
- Fibe Modem ManualUploaded bykenhug09
- Flat Plate Air CollectorUploaded byMk Frost
- honeydUploaded byAbdallah Jimmy
- A Review Study Ability Zeolite Synthetis Combination for Catalytic CrackingUploaded byBoby Royan Naira Azdlil
- Heat PacUploaded byTasha Gutierrez
- MAN-Service_Experience_Small_Bore_Four-stroke_Engines.pdfUploaded byvangeliskyriakos8998
- Dr Fixit Epoxy Floor Screed 90 1Uploaded byaamitsharma
- Security Compliance Wp 12c 1896112Uploaded byjlc1967
- Strategy From Different PerspectivesUploaded byAnand Thorat
- Javahelp 2 0 GuideUploaded byapi-3697260
- SELA - FAQ.pdfUploaded byTraining Portal
- Camel Intro TutorialUploaded byanafc1403
- Alternity ROG Player's Handbook Starships PL 8Uploaded byjack_of_fools
- AECTP-300(3)Uploaded byKeith Neuman
- ChapterUploaded byChetan Sharma
- Tender-Competition for Creation of MuralUploaded byPC Hayastan
- Module 4 Member Design - Lecture 9 Local Buckling and Section ClassificationUploaded bySebastian Anthony Toti
- MHO_Application for Drinking WaterUploaded byJaniel Lou Salinas
- In 910 DS WebServicesGuide EnUploaded byEvrim AY
- Asterix Supervisor User ManualUploaded byNonso Okeke
- 05-lienauUploaded bygunjandpatel05