Find all distinct divisible pairs in array

Here given code implementation process.

//C++ Program 
//Find all distinct divisible pairs in array
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;
//Find distinct divisible pairs in given array
void distinct_pairs(int collection[], int size)
{
  map < int, int > mp;
  //Get array elements and count its repeated occurrence 
  for (int i = 0; i < size; ++i)
  {
    mp[collection[i]]++;
  }
  map < int, int > ::iterator outer, inner;
  int auxiliary = 0;
  bool status = false;
  for (outer = mp.begin(); outer != mp.end();)
  {
    auxiliary = outer->first;
    //Check that whether elements are repeated in array or not
    //And number is not zero
    if (outer->second > 1 && auxiliary != 0)
    {
      //When have repeated elements are exist in array
      status = true;
      //Display divisible element pairs
      cout << "[ " << auxiliary << " " << auxiliary << " ]" << endl;
    }
    ++outer;
    for (inner = outer; inner != mp.end(); ++inner)
    {
      if (auxiliary != 0 && inner->first != 0 && (inner->first % auxiliary == 0) && mp.find((inner->first / auxiliary)) != mp.end())
      {
        status = true;
        //Display divisible element pairs
        cout << "[ " << inner->first  << " " << auxiliary << " ]" << endl;
      }
    }
  }
  if (status == false)
  {
    //When no get divisible pair 
    cout << "None\n";
  }
}
int main()
{
  //Define collection of array elements
  int collection[] = {
    1 , 8 , 2 , 3 , 1 , 2 , 3 , 9 , 4 , 0
  };
  //Get the size of array
  int size = sizeof(collection) / sizeof(collection[0]);
  distinct_pairs(collection, size);
  return 0;
}

Output

[ 1 1 ]
[ 2 1 ]
[ 3 1 ]
[ 4 1 ]
[ 8 1 ]
[ 9 1 ]
[ 2 2 ]
[ 4 2 ]
[ 8 2 ]
[ 3 3 ]
[ 9 3 ]
[ 8 4 ]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved