Find 2 missing numbers in an array of integers with two missing values

前端 未结 12 911
梦谈多话
梦谈多话 2021-01-30 09:33

How do you do this? The values are unsorted but are of [1..n] Example array [3,1,2,5,7,8]. Answer: 4, 6

I saw this solution in

12条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 10:34

    I hope this program is useful to you all, i took the limit till 10 it can be done the same way, just use n as the limit and perform the same operations.

    #include 
    #include
    
    using namespace std;
    
    int main()
    {
        int i,x[100],sum1=0,sum2=0,prod1=1,prod2=1,k,j,p=0;
        cout<<"Enter 8 elements less than 10, they should be non recurring"<>x[i];
        }
        sum1=((10)*(11))/2;
        for(i=0;i<8;i++)
        {
            sum2+=x[i];
        }
        k=sum1-sum2;
        for(i=1;i<10;i++)
        {
            prod1=prod1*i;
        }
        for(i=0;i<8;i++)
        {
            prod2=prod2*x[i];
        }
        j=prod1/prod2;
        p=sqrt((k*k)-(4*j));
        cout<<"One missing no:"<

提交回复
热议问题