
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Python - Check if a number and its triple exists in an array
- Triple Quotes in Python
- What is a triple column cash book and explain its format?
- Triple Talaq: Meaning and Nature
- Python - Check if a number and its double exists in an array
- Difference between a number and its reversed number JavaScript
- Program to find maximum difference of any number and its next smaller number in Python
- What is Triple DES?
- isprintable() in Python and its application
- divmod() in Python and its application
- Create a list of tuples from given list having number and its cube in each tuple using Python
- Finding a number and its nth multiple in an array in JavaScript
- Give an example of a triple column cash book
- Finding the Largest Triple Product Array in JavaScript
- Find sum of a number and its maximum prime factor in C++

Advertisements