Count Triplets | HackerRank

You are given an array and you need to find number of triplets 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 .
Output Format
Return the count of triplets that form a geometric progression.
Sample Input 0
4 2
1 2 2 4
Sample Output 0
2
Explanation 0
There are  triplets in satisfying our criteria, whose indices are  and 
Sample Input 1
6 3
1 3 9 9 27 81
Sample Output 1
6
Explanation 1
The triplets satisfying are index  and .

Solution (C++)

#include <bits/stdc++.h>
using namespace std;

// Complete the countTriplets function below.
long countTriplets(vector<long> arr, long r) {
    long long county=0,n=arr.size();
    map<long long,long long>mp1,mp2;
    for(int i=0; i
        if(mp2.count(arr[i])) county+=mp2[arr[i]];
        if(mp1.count(arr[i])) mp2[arr[i]*r]+=mp1[arr[i]];
        mp1[arr[i]*r]++; 
    }
    return county;
}

int main()
{
    long int num,r;
    cin>>num>>r;
    vector<long>vec(num);
    for(int i=0; i>vec[i];
    cout<
    return 0;
}

Comments

Popular posts from this blog

Boxes through a Tunnel | HackerRank

Array Manipulation | HackerRank

String Validators | HackerRank