google面试题8

来源:互联网 发布:雷欧奥特曼mac队员 编辑:程序博客网 时间:2024/05/16 23:40

You are given an array that contains integers. The integers content is such that every integer occurs 3 times in that array leaving one integer that appears only once.
Fastest way to find that single integer
-- using memory.
-- not using any external memory.

eg: [2,1,4,5,1,4,2,2,4,1]

#include <cstdio>int FindOnce(int A[], int size){    int ones = 0;    int twos = 0;    int not_three;        for (int i = 0; i < size; i++)    {           twos |= (ones & A[i]);        ones ^= A[i];        not_three = ~(ones & twos);        ones &= not_three;        twos &= not_three;    }       return ones;}int main(){    int A[] = {4, 5, 4, 4, 1, 2, 1, 2, 2, 1};     int size = sizeof(A) / sizeof(A[0]);    printf("%d\n", FindOnce(A, size));    return 0;}

Explanation for siva.sai.2020's code.

The code works in similar line with the question of "finding the element which appears once in an array - containing other elements each appearing twice". Solution is to XOR all the elements and you get the answer.

Basically, it makes use of the fact that x^x = 0. So all paired elements get XOR'd and vanish leaving the lonely element.
Since XOR operation is associative, commutative.. it does not matter in what fashion elements appear in array, we still get the answer.

Now, in the current question - if we apply the above idea, it will not work because - we got to have every unique element appearing even number of times. So instead of getting the answer, we will end up getting XOR of all unique elements which is not what we want.

To rectify this mistake, the code makes use of 2 variables.
ones - At any point of time, this variable holds XOR of all the elements which have
appeared "only" once.
twos - At any point of time, this variable holds XOR of all the elements which have
appeared "only" twice.

So if at any point time,
1. A new number appears - It gets XOR'd to the variable "ones".
2. A number gets repeated(appears twice) - It is removed from "ones" and XOR'd to the
variable "twice".
3. A number appears for the third time - It gets removed from both "ones" and "twice".

The final answer we want is the value present in "ones" - coz, it holds the unique element.

So if we explain how steps 1 to 3 happens in the code, we are done.
Before explaining above 3 steps, lets look at last three lines of the code,

not_threes = ~(ones & twos)
ones & = not_threes
twos & = not_threes

All it does is, common 1's between "ones" and "twos" are converted to zero.

For simplicity, in all the below explanations - consider we have got only 4 elements in the array (one unique element and 3 repeated elements - in any order).

Explanation for step 1
------------------------
Lets say a new element(x) appears.
CURRENT SITUATION - Both variables - "ones" and "twos" has not recorded "x".

Observe the statement "twos| = ones & x".
Since bit representation of "x" is not present in "ones", AND condition yields nothing. So "twos" does not get bit representation of "x".
But, in next step "ones ^= x" - "ones" ends up adding bits of "x". Thus new element gets recorded in "ones" but not in "twos".

The last 3 lines of code as explained already, converts common 1's b/w "ones" and "twos" to zeros.
Since as of now, only "ones" has "x" and not "twos" - last 3 lines does nothing.

Explanation for step 2.
------------------------
Lets say an element(x) appears twice.
CURRENT SITUATION - "ones" has recorded "x" but not "twos".

Now due to the statement, "twos| = ones & x" - "twos" ends up getting bits of x.
But due to the statement, "ones ^ = x" - "ones" removes "x" from its binary representation.

Again, last 3 lines of code does nothing.
So ultimately, "twos" ends up getting bits of "x" and "ones" ends up losing bits of "x".

Explanation for step 3.
-------------------------
Lets say an element(x) appears for the third time.
CURRENT SITUATION - "ones" does not have bit representation of "x" but "twos" has.

Though "ones & x" does not yield nothing .. "twos" by itself has bit representation of "x". So after this statement, "two" has bit representation of "x".
Due to "ones^=x", after this step, "one" also ends up getting bit representation of "x".

Now last 3 lines of code removes common 1's of "ones" and "twos" - which is the bit representation of "x".
Thus both "ones" and "twos" ends up losing bit representation of "x".

1st example
------------
2, 2, 2, 4

After first iteration,
ones = 2, twos = 0
After second iteration,
ones = 0, twos = 2
After third iteration,
ones = 0, twos = 0
After fourth iteration,
ones = 4, twos = 0

2nd example
------------
4, 2, 2, 2

After first iteration,
ones = 4, twos = 0
After second iteration,
ones = 6, twos = 0
After third iteration,
ones = 4, twos = 2
After fourth iteration,
ones = 4, twos = 0

Explanation becomes much more complicated when there are more elements in the array in mixed up fashion. But again due to associativity of XOR operation - We actually end up getting answer.


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两颗牙中间黑了怎么办 牙齿酸痛怎么办才能好 牙齿被磨小了疼怎么办 牙齿有黑点蛀牙怎么办 小孩有蛀牙牙痛怎么办 牙齿修补后疼痛怎么办 腹部绞痛出冷汗怎么办 结石运动后尿血怎么办 透析病人尿血该怎么办 宝宝吃药就吐怎么办 肚子坠胀尿血怎么办 透析病人回来血尿怎么办 血尿腹痛腰疼怎么办 新诺明 吃多了 怎么办 吃下火药拉肚子怎么办 打哈欠停不下来怎么办 小孩一直咳不停怎么办 孩子咳嗽咳不停怎么办 孩子咳嗽出冷汗怎么办 宝宝一直咳不停怎么办 尿血右上腹疼怎么办 尿道感染尿出血怎么办 儿童血尿腹痛是怎么办 肚子胀痛拉二天血尿怎么办 小牛肚尿道发炎怎么办 吃奶小牛涨肚怎么办 尿路感染尿出血了怎么办 尿血还带血块怎么办 尿道感染尿血了怎么办 老人小便带血怎么办 胎儿双肾盂扩张怎么办 宝宝发烧后血尿怎么办 孕妇肾盂分离16怎么办 急性尿道炎尿血严重怎么办 尿急尿频尿血严重怎么办 打预防针两天后发烧怎么办 孩子发烧心跳快怎么办 拉肚子脱水人犯困怎么办 拉肚子拉脱水了怎么办 孕妇肚子疼拉水怎么办 腹泻拉脱水了怎么办