This channel is an effort to help students, beginners, coders in their tech or engineering journey which is highly competitive now a days. It would bring easy-to-understand content on coding, exams like GATE, and more to help you stay competitive and grow your skills. Join me to learn, improve, and reach your goals!
AimGreat
Travelling today can"t record video on todays potd.Hope you can crack it , its simple one on binary tree.
1 year ago | [YT] | 2
View 0 replies
AimGreat
Hey Guyzz, hope you are doing well !
Having cold so not able to record videos, will come back soon ! Untill then keep coding and keep practicing ! And todays question is kinda like *Majority element* , a standard interview question. Attaching the java code here:
public List<Integer> findMajority(List<Integer> nums) {
// Your code goes here.
int n = nums.size();
if (n == 0) return new ArrayList<>();
// Step 1: Find potential candidates
int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0;
for (int num : nums) {
if (num == candidate1) {
count1++;
} else if (num == candidate2) {
count2++;
} else if (count1 == 0) {
candidate1 = num;
count1 = 1;
} else if (count2 == 0) {
candidate2 = num;
count2 = 1;
} else {
count1--;
count2--;
}
}
// Step 2: Validate the candidates
count1 = 0;
count2 = 0;
for (int num : nums) {
if (num == candidate1) {
count1++;
} else if (num == candidate2) {
count2++;
}
}
List<Integer> result = new ArrayList<>();
if (count1 > n / 3) result.add(candidate1);
if (count2 > n / 3) result.add(candidate2);
// Sort result to maintain the increasing order format
Collections.sort(result);
return result.size() == 0 ? Arrays.asList(-1) : result;
}
1 year ago | [YT] | 0
View 0 replies