Monday, 15 June 2015

You Are Given An Array Of Positive Ints Where Every Element Appears Three Times, Except One That Appears Only Once (Let's Call It x) And One That Appears Only Twice (Let's Call It y). Find x ^ 2 * y In C++ Programming Language Solution.

/* You are given an array of positive ints where every element appears three times, except one that appears only once (let's call it x) and one that appears only twice (let's call it y).

Find x ^ 2 * y
Example: [1, 1, 1, 2, 2, 3] -> 18
Solution in c++ programming language.
*/

#include <iostream>
#include<algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int ar[1001],n,setX,setY;
 int cnt[1001];
 cin >> n;
 for(int i = 0; i < n; i++){
  cin >> ar[i];
 }
 sort(ar,ar+n);
 for(int i = 0; i < 1000; i++)cnt[i] = 0;
 int singleVal,doubleVal;
 for(int i = 0; i < 1000 ; i++){
  cnt[ar[i]]++; 
 }
 for(int i = 0; i < 1000; i++){
  if(cnt[i] == 1){
   singleVal = i;
  }
  else if(cnt[i] == 2){
   doubleVal = i;
  }
 }
 int eq = (singleVal*singleVal)*doubleVal;
 cout << eq << endl;
 return 0;
}

No comments:

Post a Comment