Interview Questions (87): Two arrays containing questions (from Microsoft interview questions)

Source: Internet
Author: User

Question:

You have given two arrays, say

A: 4, 1, 6, 2, 8, 9, 5, 3, 2, 9, 8, 4, 6
B: 6, 1, 2, 9, 8

Where B contains elements which are in a consecutive locations but may be in any order.
Find their starting and ending indexes in A. (Be careful of duplicate numbers ).

 

Answer is (1, 5)

 

First, give the code, and then explain it with the Code as follows:

# Include <iostream> <br/> # include <map> </P> <p> using namespace STD; </P> <p> void findconsecutivesubarrlocation (int A [], int Lena, int B [], int lenb) <br/>{< br/> Map <int, int> bmap; <br/> Map <int, int> windowmap; <br/> Map <int, int> diffmap; <br/> for (INT I = 0; I <lenb; I ++) <br/>{< br/> If (bmap. count (B [I]) = 0) <br/> bmap [B [I] = 1; <br/> else <br/> ++ bmap [B [I]; <br/> If (windowmap. count (A [I]) = 0) <br/> windowmap [A [I] = 1; <br/> else <br/> ++ windowmap [A [I]; <br/>}</P> <p> Map <int, int >:: iterator it = bmap. begin (); <br/> int sameelement = 0; <br/> while (it! = Bmap. End () <br/>{< br/> If (windowmap. Count (* It). First )! = 0) <br/>{< br/> diffmap [(* it ). first] = windowmap [(* it ). first]-(* it ). second; <br/> If (diffmap [(* it ). first] = 0) <br/> sameelement ++; <br/>}< br/> else <br/> diffmap [(* it ). first] =-(* it ). second; <br/> it ++; <br/>}</P> <p> If (sameelement = lenb) <br/>{ <br/> cout <"---------- find one ---------" <Endl; <br/> cout <"Start index: "<0 <Endl; <br/> cout <" End index: "<lenB-1 <Endl; <br/>}</P> <p> For (INT I = lenb; I <Lena; I ++) <br/> {<br/> If (diffmap. Count (A [I-lenb])! = 0) <br/>{< br/> diffmap [A [I-lenb] --; <br/> If (diffmap [A [I-lenb] = 0) <br/> sameelement ++; <br/> else if (diffmap [A [I-lenb] =-1) <br/> sameelement --; <br/>}< br/> If (diffmap. count (A [I])! = 0) <br/>{< br/> diffmap [A [I] ++; <br/> If (diffmap [A [I] = 0) <br/> sameelement ++; <br/> else if (diffmap [A [I] = 1) <br/> sameelement --; <br/>}< br/> If (sameelement = diffmap. size () <br/>{< br/> cout <"---------- find one ---------" <Endl; <br/> cout <"Start index: "<I-lenb + 1 <Endl; <br/> cout <" End index: "<I + 1 <Endl; <br/>}</P> <p> int main () <br/> {<br/> int A [] = {4, 1, 2, 1, 8, 9, 2, 1, 2, 9, 8, 4, 6 }; <br/> int B [] = {1, 1, 2, 8, 9}; <br/> int Lena = sizeof () /sizeof (INT); <br/> int lenb = sizeof (B)/sizeof (INT); <br/> findconsecutivesubarrlocation (A, Lena, B, lenb ); <br/> return 0; <br/>}< br/>

 

 

Ideas:

Ex,

Int A [] = {4, 1, 2, 1, 8, 9, 5, 3, 2, 9, 8, 4, 6 };
Int B [] = {1, 1, 2, 8, 9 };
Int Lena = 13;
Int lenb = 5;
Map <int, int> bmap;
Map <int, int> windowmap;
Map <int, int> diffmap;

Initialize map first:
Bmap =}
Windowmap =}
Diffmap = {1:0, 9:-1}
"1: 0" means that the current Sliding Window of array a exactly has the same number of "1" as array B, while "9: -1 indicates that the current Sliding Window of A is missing 1 "9" compared with that of B ". In addition, we noticed that diffmap and bmap have the same key.

The variable "sameelement" in the Code indicates the number of pair pairs in diffmap matching bmap. Obviously, the value of the initialized "sameelement" variable will be 3 (, in diffmap ).

Next
In array A, move the window with a size of lenb without moving forward. You only need to check the elements El drawn on the left of the sliding window and the elements er slide on the right to update diffmap and sameelement, as shown below:

If (diffmap. Count (A [I-lenb])! = 0)
{
Diffmap [A [I-lenb] --;
If (diffmap [A [I-lenb] = 0)
Sameelement ++;
Else if (diffmap [A [I-lenb] =-1)
Sameelement --;
}
If (diffmap. Count (A [I])! = 0)
{
Diffmap [A [I] ++;
If (diffmap [A [I] = 0)
Sameelement ++;
Else if (diffmap [A [I] = 1)
Sameelement --;
}
If (sameelement = diffmap. Size ())
{
Cout <"---------- find one ---------" <Endl;
Cout <"Start index:" <I-lenb + 1 <Endl;
Cout <"End index:" <I + 1 <Endl;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.