- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums, we have to check whether there are two numbers such that one is a triple of another or not.

So, if the input is like nums = [2, 3, 10, 7, 9], then the output will be True, as 9 is the triple of 3

To solve this, we will follow these steps −

i := 0

sort the list n

j := 1

while j < size of n, do

if 3*n[i] is same as n[j], then

return True

if 3*n[i] > n[j], then

j := j + 1

otherwise,

i := i + 1

return False

Let us see the following implementation to get better understanding −

class Solution: def solve(self, n): i = 0 n.sort() j = 1 while (j < len(n)): if (3*n[i] == n[j]): return True if (3*n[i] > n[j]): j += 1 else: i += 1 return False ob = Solution() print(ob.solve([2, 3, 10, 7, 9]))

[2, 3, 10, 7, 9]

True

- Related Questions & Answers
- Python - Check if a number and its triple exists in an array
- Triple Quotes in Python
- Difference between a number and its reversed number JavaScript
- Python - Check if a number and its double exists in an array
- divmod() in Python and its application
- isprintable() in Python and its application
- Program to find maximum difference of any number and its next smaller number in Python
- Finding a number and its nth multiple in an array in JavaScript
- Find sum of a number and its maximum prime factor in C++
- Calculating a number from its factorial in JavaScript
- Give an example of a triple column cash book
- Create a list of tuples from given list having number and its cube in each tuple using Python
- Finding the Largest Triple Product Array in JavaScript
- What is the number wrapper class and its methods in Java?
- Find number from its divisors in C++

Advertisements