"I'd translated this decimal number into binary form and if it looks like 1000 or 10 or 1 - one leading 1 and others are zeros - than return 1 else return 0
"
Анвар А. - "I'd translated this decimal number into binary form and if it looks like 1000 or 10 or 1 - one leading 1 and others are zeros - than return 1 else return 0
"See full answer
"Basic Approach
As BST inorder traversal will result in a sequence of increasing order. Store that order in a vector and get the k-1 index to get the Kth smallest element, similarly access the N-K+1 th element will be the Kth largest element
Time Complexity: O(n)
Space Complexity O(n)
Space Optimized Approach
For Kth smallest , start inorder traversal, and keep a counter, decrement the counter when you access the node element. When the counter turns 0 that elementwill be the Kth smal"
Saurabh S. - "Basic Approach
As BST inorder traversal will result in a sequence of increasing order. Store that order in a vector and get the k-1 index to get the Kth smallest element, similarly access the N-K+1 th element will be the Kth largest element
Time Complexity: O(n)
Space Complexity O(n)
Space Optimized Approach
For Kth smallest , start inorder traversal, and keep a counter, decrement the counter when you access the node element. When the counter turns 0 that elementwill be the Kth smal"See full answer
Data Structures & Algorithms
Coding
+1 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"int a_array[10] = {3,6,4,7,2,1,9};
int index = 0;
int index2 = 0;
for ( index = 0; index < sizeof(a_array); index++ )
{
int tmpindex = index + 1;
if ( tmpindex <= sizeof(a_array) )
{
for ( index2 = tmpindex; index2 < sizeof(a_array); index2++ )
{
if ( aarray[index] <= aarray[index2] )
{
print( "%d is the NGE of %d" array[index2], array[index]);
break;
"
Mark S. - "int a_array[10] = {3,6,4,7,2,1,9};
int index = 0;
int index2 = 0;
for ( index = 0; index < sizeof(a_array); index++ )
{
int tmpindex = index + 1;
if ( tmpindex <= sizeof(a_array) )
{
for ( index2 = tmpindex; index2 < sizeof(a_array); index2++ )
{
if ( aarray[index] <= aarray[index2] )
{
print( "%d is the NGE of %d" array[index2], array[index]);
break;
"See full answer
"function isPalindrome(s, start, end) {
while (s[start] === s[end] && end >= start) {
start++;
end--;
}
return end <= start;
}
function longestPalindromicSubstring(s) {
let longestPalindrome = '';
for (let i=0; i < s.length; i++) {
let j = s.length-1;
while (s[i] !== s[j] && i <= j) {
j--;
}
if (s[i] === s[j]) {
if (isPalindrome(s, i, j)) {
const validPalindrome = s.substring(i, j+1"
Tiago R. - "function isPalindrome(s, start, end) {
while (s[start] === s[end] && end >= start) {
start++;
end--;
}
return end <= start;
}
function longestPalindromicSubstring(s) {
let longestPalindrome = '';
for (let i=0; i < s.length; i++) {
let j = s.length-1;
while (s[i] !== s[j] && i <= j) {
j--;
}
if (s[i] === s[j]) {
if (isPalindrome(s, i, j)) {
const validPalindrome = s.substring(i, j+1"See full answer
" First, sort the array in ascending order. This ensures that we can easily check the triangle inequality condition. Use a loop to iterate through the array. For each triplet of consecutive elements, check if they satisfy the triangle inequality condition a+b>ca+b>c. As soon as you find a valid tuple, return it. If no valid tuple is found, return null. This approach is efficient with a time complexity of O(nlogn)O(nlogn) due to the sorting step, followed by a linear scan of the array"
Shivam P. - " First, sort the array in ascending order. This ensures that we can easily check the triangle inequality condition. Use a loop to iterate through the array. For each triplet of consecutive elements, check if they satisfy the triangle inequality condition a+b>ca+b>c. As soon as you find a valid tuple, return it. If no valid tuple is found, return null. This approach is efficient with a time complexity of O(nlogn)O(nlogn) due to the sorting step, followed by a linear scan of the array"See full answer
"Make current as root.
2 while current is not null,
if p and q are less than current,
go left.
If p and q are greater than current,
go right.
else return current.
return null"
Vaibhav D. - "Make current as root.
2 while current is not null,
if p and q are less than current,
go left.
If p and q are greater than current,
go right.
else return current.
return null"See full answer
"I first asked few clarifying questions like the return array may need not contain the list of building in the same order, to which the interviewer agreed.
Then I came up with an approach where we iterate the array from right to left and keep a max variable which will keep the value of the current max. When we find an item which is greater than max we update the max and add this element into our solution. The interviewer agreed for the approach.
I discussed few corner scenarios with the interview"
Rishabh N. - "I first asked few clarifying questions like the return array may need not contain the list of building in the same order, to which the interviewer agreed.
Then I came up with an approach where we iterate the array from right to left and keep a max variable which will keep the value of the current max. When we find an item which is greater than max we update the max and add this element into our solution. The interviewer agreed for the approach.
I discussed few corner scenarios with the interview"See full answer
"Leetcode 347: Heap + Hashtable
Follow up question: create heap with the length of K instead of N (more time complexity but less space )"
Chen J. - "Leetcode 347: Heap + Hashtable
Follow up question: create heap with the length of K instead of N (more time complexity but less space )"See full answer
"I try to solve this initially using quick select where will take a pivot element and position the remaining elements and check if the current index is answer or not and continue the same but it requires o(n*n), but interviewee is expecting the best from me, so at the end i tried solving using heaps where will check the difference between k and n-k to use min or max heap after that we will heap the array, and will keep popping the element k-1 and return the peek one which leads to answer."
Mourya C. - "I try to solve this initially using quick select where will take a pivot element and position the remaining elements and check if the current index is answer or not and continue the same but it requires o(n*n), but interviewee is expecting the best from me, so at the end i tried solving using heaps where will check the difference between k and n-k to use min or max heap after that we will heap the array, and will keep popping the element k-1 and return the peek one which leads to answer."See full answer