**Count Triplets Hacker Rank Problem Solution->**

**Problem Statement->**

You are given an array and you need to find number of tripets of indices such that the elements at those indices are in geometric progression for a given common ratio and .

For example, . If , we have and at indices and .

Function Description

Complete the countTriplets function in the editor below. It should return the number of triplets forming a geometric progression for a given as an integer.

countTriplets has the following parameter(s):

- arr: an array of integers
- r: an integer, the common ratio

Input Format

The first line contains two space-separated integers and , the size of and the common ratio.

The next line contains space-seperated integers .

Constraints

Output Format

Return the count of triplets that form a geometric progression.

///link of question->https://www.hackerrank.com/challenges/count-triplets-1/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps on HackerRank

**My Code->**

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);

string rtrim(const string &);

vector<string> split(const string &);

// Complete the countTriplets function below.

long countTriplets(vector<long> arr, long r) {

// arr.sort(arr.begin(),arr.end());

map<long long int ,long long int > lmap;

map<long long int ,long long int > rmap;

for(int i=0;i<arr.size();i++){

rmap[arr[i]]++;

}

long long int count=0;

for(int i=0;i<arr.size();i++){

long long int f=arr[i]/r;

long long int s=arr[i];

long long int t=arr[i]*r;

rmap[arr[i]]--;

if(arr[i]%r==0){

count+=lmap[f]*rmap[t];

}

lmap[arr[i]]++;

//cout<<"inside";

}

return count ;

}

int main()

{

ofstream fout(getenv("OUTPUT_PATH"));

string nr_temp;

getline(cin, nr_temp);

vector<string> nr = split(rtrim(nr_temp));

int n = stoi(nr[0]);

long r = stol(nr[1]);

string arr_temp_temp;

getline(cin, arr_temp_temp);

vector<string> arr_temp = split(rtrim(arr_temp_temp));

vector<long> arr(n);

for (int i = 0; i < n; i++) {

long arr_item = stol(arr_temp[i]);

arr[i] = arr_item;

}

long ans = countTriplets(arr, r);

fout << ans << "\n";

fout.close();

return 0;

}

string ltrim(const string &str) {

string s(str);

s.erase(

s.begin(),

find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))

);

return s;

}

string rtrim(const string &str) {

string s(str);

s.erase(

find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),

s.end()

);

return s;

}

vector<string> split(const string &str) {

vector<string> tokens;

string::size_type start = 0;

string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {

tokens.push_back(str.substr(start, end - start));

start = end + 1;

}

tokens.push_back(str.substr(start));

return tokens;

}

This was very helpful.My friend was facing this issue he did exactly the same as mentioned and it was resolved.

ReplyDelete