int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int h=1;
int sum=(n*(n-1))/2;
for(int i=1;i<sqrt(n);i++){
if(i*(i+1)/2 <= n)
h=i+1;
}
cout<<h<<endl;
}
}
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int h=1;
int sum=(n*(n-1))/2;
for(int i=1;i<sqrt(n);i++){
if(i*(i+1)/2 <= n)
h=i+1;
}
cout<<h<<endl;
}
}
int main() {
// your code goes here
int T;
cin>>T;
while(T–){
long N; // range of N is given
cin>>N;
int count=0; // intializing count out of the for loop
for(int i=1;i<=N;i++){
N = N-i; // actually by reducing the coins in the series
// like 1, 2, 3,4 ,5
count++; // using the actual count we can easily solve it
if(N==0) // breaking the loop is most important
break;
}
cout<<count++<<endl;
}
}
hope you get my solution having any doubt you can ask me!
find x such that x*(x+1) >= n.
do bs and minimize x.
<>
l = 1, r = n
while(r>=l) {
mid=(l+r)/2;
if (mid*(mid+1) <= n) {
ans = min(ans,mid);
l=mid+1;
}
else r=mid-1;
}
</>
or u can solve qe
x^2 + x - 2n = 0;
D =(1 + 8n);
x = -1 +/- sqrt(D) / 2*a;