GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Program to find number of X element in an array

in an N numbers of array a number is a X element if it is divisible by atleast one other element in the array. Program to find how many such elements exists in an given array

example 1 : in array [1,2,3] , number of X elements is 2 ( 2 and 3 as they are divisible by number 1 )

example 2 : in array [2,3,5,8] , number of X elements is 1 ( 8 is divisible by 2 )

example 3 : in array [2,3,6,12] , number of X elements is 2 ( 6 is divisible by 2 and 3 , 12 is divisible by 2 and 3 and 6 )

 

Write a memory efficient program to solve this problem.

asked Sep 10, 2016 by anonymous

1 Answer

#include <iostream>

using namespace std;

int main()
{
   int ARRAY[4] = {2,3,6,12} ;

   int i,j,Count=0;

   for(i=0;i<4;i++)
   {
       for(j=0;j<4;j++)
       {
           if(j!=i)
           {
               if(ARRAY[i]%ARRAY[j]==0)
               {
                   Count++;
                   break;
               }
           }
       }
   }
   cout<<"No. of X elements  =  "<<Count<<endl;

   return 0;
}

Hope it helped.

I apologize if it's not clear friend, but do let me know if there is any confusion.

I will try my best to make it clear.

"KEEP CODING"

answered Sep 14, 2016 by Mazhar MIK
...