Triangular Number | Practice | GeeksforGeeks

Given a number N.Check whether it is a triangular number or not.
Note: A number is termed as a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, the second row has two points, the third row has three points and so on.
The starting triangular numbers are 1, 3 (1+2), 6 (1+2+3), 10 (1+2+3+4).
Example 1:
Input:
N=55
Output:
1
Explanation:
55 is a triangular number.
It can be represented in 10 rows.
Example 2:
Input:
N=56
Output:
0
Explanation:
56 is not a triangular number.

Your Task:
You don't need to read input or print anything. Your task is to complete the function isTriangular() that takes a number N as input parameter and returns 1 if it is a triangular number. Otherwise, it returns 0.

Expected Time complexity:O(LogN)
Expected Auxillary Space:O(1)
 
Constraints:
1<=N<=106
+2
hritikpatani2 months ago
int isTriangular(int N){       int temp = N*2;       int jabe = sqrt(temp);       if(((jabe+1)*jabe == temp) || ((jabe-1)*jabe == temp))           return 1;       return 0;   }
 
 
Fucking 27 lakhs TCs in 1.6 fking seconds.
0
naveenguttedar8443 months ago
int isTriangular(int N){
       //code here
   
       N=N*8+1;
       double squr=Math.sqrt(N);
       if ((int)squr==squr) return 1;
       return 0;
   }
 
0
psanjeev0443 months ago
class Solution {   int isTriangular(int N){      int m = 2 * N;      for(int i = 1; i * i < m; i++){          if((i*i) == (m - i)) return 1;      }      return 0;   }}
+1
debujjal47 months ago
     if(N < 0){return 0;}
int sum = 0;
for(int i = 1; sum <= N; i++){
sum = sum + i;
if(sum == N){return 1;}
}
return 0;
 
0
Din Djarin9 months ago
Din Djarin
int isTriangular(int N){        N = 2*N ;
        for( int i = 1 ; i*( i + 1 ) <= N ; i++ )            if( i*( i + 1 ) == N )                return 1;
        return 0 ;    }
0
ANKIT SINHA9 months ago
ANKIT SINHA
https://uploads.disquscdn.c...
0
Rohit Saka10 months ago
Rohit Saka
class Solution {    int isTriangular(int N){         if(N < 0){            return 0;        }         int sum = 0;         for(int i = 1; sum <= N; i++){             sum = sum + i;             if(sum == N){                return 1;            }         }         return 0;    }}
0
BHOOPEN SAWANT10 months ago
BHOOPEN SAWANT
CPP code  -int isTriangular(int N){        return (int(sqrt(2*N))*(int(sqrt(2*N)+1))==2*N);    }
0
Mufeed Amir1 year ago
Mufeed Amir
Python Solution:
class Solution:    def isTriangular(self, n):        import math as mt        i=mt.floor(mt.sqrt(2*n))
        if i*(i+1)//2==n:            return 1
          return 0
0
Gazal1 year ago
Gazal
JAVA SOL. 0.2SECclass GFG {public static void main (String[] args) {//codeScanner sc=new Scanner(System.in);int t=sc.nextInt();int x,i=1;while(t-->0){    int n=sc.nextInt();     i=1;    while(n>0)    {        n-=i;       //ques is about sum of natural numbers        i++;    }  if(n==0)     System.out.println(1);    else    System.out.println(0);}}}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.