SlideShare a Scribd company logo
1 of 2
Download to read offline
If Alice comes, can Ben be far behind? Now, our beloved Bob Bit fiddler has an times n 2-D
array on his hands which gives the altitudes of all points in Durham county (let's assume
Durham is square). But, Durham is not quite a volcano! So, the altitudes form an arbitrary matrix
of numbers. Bob would have ideally liked to find the highest point in Durham, but since he does
not have the nice property that Alice had, he settles for an easier target: find a locally highest
point, i.e., an entry in the n times n 2-D array that is as large as both its horizontal neighbors and
both its vertical neighbors. (Bob hopes that this will at least be a hillock if not a mountain!)
Formally, M is an n times n array, then M[i, j] is a locally highest point if M[i, j]
greaterthanorequalto max(M[i - 1, j], M[i + 1, j], M[i, j - 1], M[i, j + 1]). Note that an entry on
the boarder of the grid (which will only have 2 or 3 valid vertical/horizontal neighbors) just
needs to be as large as its valid neighbors in order to be considered a locally highest point. Can
you help Bob find such an entry in O(n) time? (An O(n log n) solution will get partial credit.)
Solution
include
#include
enum { Cm = 2 };
void findHighest(int A[][Cm], int n, int m) {
if (m <= 0) return;
for (int i = 0; i < n; i++) {
int max = *std::max_element(A[i], A[i] + m);
std::cout << max << " ";
}
}
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
findHighest(A, 2, 2);
}
prints 2 4.
void findMax(double x[][COLS])
{
int max=x[0][0];
int i,j;
for (i=0; i max)
max= x[i][j];
}
}
The time complexity will be O (n*m) where n the number of arrays which is the 1st dimension
and m the max size of each internal array ie, the 2nd dimension.
Best case - finding the max element as the first (O(1)), worst case - it is the last element checked
(O(n)).
The tricky part is the average case.
To find the average case - we need the expected number of iterations!
Since you can stop after you find the maximum, we can split the problem into two parts:
This totals inO(n)average time solution.

More Related Content

More from PRATIKSINHA7304

Wiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdfWiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdf
PRATIKSINHA7304
 
Which of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdfWhich of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdf
PRATIKSINHA7304
 
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdfWhat are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
PRATIKSINHA7304
 
What is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdfWhat is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdf
PRATIKSINHA7304
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
PRATIKSINHA7304
 
Recall the steps whereby recombinant DNA technology was used to make .pdf
Recall the steps whereby recombinant DNA technology was used to make .pdfRecall the steps whereby recombinant DNA technology was used to make .pdf
Recall the steps whereby recombinant DNA technology was used to make .pdf
PRATIKSINHA7304
 
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPlease read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
PRATIKSINHA7304
 

More from PRATIKSINHA7304 (20)

write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdfwrite a prgoram that displays four images or objects in a 2 x 2 grid.pdf
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
 
Wiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdfWiater Company operates a small manufacturing facility. On January 1,.pdf
Wiater Company operates a small manufacturing facility. On January 1,.pdf
 
Which of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdfWhich of the three types of activities reported on the statement of c.pdf
Which of the three types of activities reported on the statement of c.pdf
 
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
111. Definition Explanation (15) 1. Job speciation SolutionIII.pdf
 
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdfWhat are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
What are the pros and cons of using exFAT and ReFS on a Windows 2012.pdf
 
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdfWhat led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
What led to the demise of ‘conventional Keynesian wisdom’ in the mid.pdf
 
What is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdfWhat is the connection between relative frequency and probability.pdf
What is the connection between relative frequency and probability.pdf
 
What factors led President Truman to support the creation of Israel .pdf
What factors led President Truman to support the creation of Israel .pdfWhat factors led President Truman to support the creation of Israel .pdf
What factors led President Truman to support the creation of Israel .pdf
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
 
this is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdfthis is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdf
 
This fall Millie finally repaid her student loan. She originally bor.pdf
This fall Millie finally repaid her student loan. She originally bor.pdfThis fall Millie finally repaid her student loan. She originally bor.pdf
This fall Millie finally repaid her student loan. She originally bor.pdf
 
The term refers to a. Global Analysis of the proteins produced in .pdf
The term  refers to  a. Global Analysis of the proteins produced in .pdfThe term  refers to  a. Global Analysis of the proteins produced in .pdf
The term refers to a. Global Analysis of the proteins produced in .pdf
 
The inflation rate of Country Alpha in 2014 was 3.5. Due to their w.pdf
The inflation rate of Country Alpha in 2014 was 3.5. Due to their w.pdfThe inflation rate of Country Alpha in 2014 was 3.5. Due to their w.pdf
The inflation rate of Country Alpha in 2014 was 3.5. Due to their w.pdf
 
The issues of inflation, unemployment, and business cycles are Multip.pdf
The issues of inflation, unemployment, and business cycles are Multip.pdfThe issues of inflation, unemployment, and business cycles are Multip.pdf
The issues of inflation, unemployment, and business cycles are Multip.pdf
 
Need to make a program using the scanner class that outputs F to C 3.pdf
Need to make a program using the scanner class that outputs F to C 3.pdfNeed to make a program using the scanner class that outputs F to C 3.pdf
Need to make a program using the scanner class that outputs F to C 3.pdf
 
Name the process by which RNA is used to make a polypeptide or prote.pdf
Name the process by which RNA is used to make a polypeptide or prote.pdfName the process by which RNA is used to make a polypeptide or prote.pdf
Name the process by which RNA is used to make a polypeptide or prote.pdf
 
Recall the steps whereby recombinant DNA technology was used to make .pdf
Recall the steps whereby recombinant DNA technology was used to make .pdfRecall the steps whereby recombinant DNA technology was used to make .pdf
Recall the steps whereby recombinant DNA technology was used to make .pdf
 
Rex and Felix are the sole shareholders of the Dogs and Cats Corpora.pdf
Rex and Felix are the sole shareholders of the Dogs and Cats Corpora.pdfRex and Felix are the sole shareholders of the Dogs and Cats Corpora.pdf
Rex and Felix are the sole shareholders of the Dogs and Cats Corpora.pdf
 
Programming in Coday we are working with singly linked lists. Las.pdf
Programming in Coday we are working with singly linked lists. Las.pdfProgramming in Coday we are working with singly linked lists. Las.pdf
Programming in Coday we are working with singly linked lists. Las.pdf
 
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPlease read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 

If Alice comes, can Ben be far behind Now, our beloved Bob Bit fiddl.pdf

  • 1. If Alice comes, can Ben be far behind? Now, our beloved Bob Bit fiddler has an times n 2-D array on his hands which gives the altitudes of all points in Durham county (let's assume Durham is square). But, Durham is not quite a volcano! So, the altitudes form an arbitrary matrix of numbers. Bob would have ideally liked to find the highest point in Durham, but since he does not have the nice property that Alice had, he settles for an easier target: find a locally highest point, i.e., an entry in the n times n 2-D array that is as large as both its horizontal neighbors and both its vertical neighbors. (Bob hopes that this will at least be a hillock if not a mountain!) Formally, M is an n times n array, then M[i, j] is a locally highest point if M[i, j] greaterthanorequalto max(M[i - 1, j], M[i + 1, j], M[i, j - 1], M[i, j + 1]). Note that an entry on the boarder of the grid (which will only have 2 or 3 valid vertical/horizontal neighbors) just needs to be as large as its valid neighbors in order to be considered a locally highest point. Can you help Bob find such an entry in O(n) time? (An O(n log n) solution will get partial credit.) Solution include #include enum { Cm = 2 }; void findHighest(int A[][Cm], int n, int m) { if (m <= 0) return; for (int i = 0; i < n; i++) { int max = *std::max_element(A[i], A[i] + m); std::cout << max << " "; } } int main() { int A[2][2] = {{1, 2}, {3, 4}}; findHighest(A, 2, 2); } prints 2 4. void findMax(double x[][COLS]) { int max=x[0][0]; int i,j; for (i=0; i max)
  • 2. max= x[i][j]; } } The time complexity will be O (n*m) where n the number of arrays which is the 1st dimension and m the max size of each internal array ie, the 2nd dimension. Best case - finding the max element as the first (O(1)), worst case - it is the last element checked (O(n)). The tricky part is the average case. To find the average case - we need the expected number of iterations! Since you can stop after you find the maximum, we can split the problem into two parts: This totals inO(n)average time solution.