본문 바로가기

Interview Preparation Kit/Dictionaries and Hashmaps

Count Triplets

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.

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 .

Sample Input 2

5 5
1 5 5 25 125

Sample Output 2

4

Explanation 2

The triplets satisfying are index .




이 문제는 무슨 뜻인지는 알겠는데....당췌 구현이 되지 않는다....복잡복잡...

그냥 보기엔 단순해 보이는데....같은 값이 있는 경우에 대해 마땅히 처리할 로직이 생각나지 않는다.

그냥 저냥 어떻게든 로직을 만들어 돌려보면 타임아웃이 나오게 된다...ㅠㅠ

어떨수 없이 Discussions를 참고했는데, 세상에나....Discussions에 나온 로직을 이해를 못하겠......

뭘까........정말 이렇게 처리를 할 수 있는건가????? 이런걸 생각해내는 사람들은 어디 학원을 다닌 것일까???

대단하다....대단해 ㅠ Medium 으로 갈수록 한방에 풀어내는 문제가 없다.....큰일이다...




'Interview Preparation Kit > Dictionaries and Hashmaps' 카테고리의 다른 글

Sherlock and Anagrams  (0) 2018.07.24
Two Strings  (0) 2018.07.23
Hash Tables: Ransom Note  (0) 2018.07.23