- 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

# A number and its triple in Python

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 −

## Example

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]))

## Input

[2, 3, 10, 7, 9]

## Output

True

- Related Questions & Answers
- Python - Check if a number and its triple exists in an array
- What is a triple column cash book and explain its format?
- 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
- What is Triple DES?
- 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
- Create a list of tuples from given list having number and its cube in each tuple using Python
- Give an example of a triple column cash book
- What is the number wrapper class and its methods in Java?

Advertisements