You have 2.2 years of experience and you don’t know the time complexity of binary search and single iteration of for loop. I think you need to improve a lot. The pagination concept was also wrong and you can’t figure out what kind of indexing you should use to make search efficient. Good luck. Take it as a constructive feedback
Sum will take O(n) time. The problem can easily be solved using Binary Search(for finding the index of first occurence of 1) and then subtract the that index from length of array, this solution will only take O(log n), that's way better than O(n)
public class CountOnes { public static void main(String[] args) { String binarySequence = "000001111"; int result = countOnes(binarySequence); System.out.println("The number of 1s in the binary sequence is: " + result); } static int countOnes(String binarySequence) { int count = 0; for (char digit : binarySequence.toCharArray()) { if (digit == '1') { count++; } } return count; } }
Upper bound - lower bound can also be done in the last question
🙌
You have 2.2 years of experience and you don’t know the time complexity of binary search and single iteration of for loop. I think you need to improve a lot. The pagination concept was also wrong and you can’t figure out what kind of indexing you should use to make search efficient. Good luck. Take it as a constructive feedback
Do sum of array bro it will give count of 1's
I will try 🙌
Sum will take O(n) time.
The problem can easily be solved using Binary Search(for finding the index of first occurence of 1) and then subtract the that index from length of array, this solution will only take O(log n), that's way better than O(n)
How you go so much interview I need your help to get interviews
Instagram - aarav._sharma._
@@krishnakantsharma2201 yes, how did you get so many interviews?
public class CountOnes {
public static void main(String[] args) {
String binarySequence = "000001111";
int result = countOnes(binarySequence);
System.out.println("The number of 1s in the binary sequence is: " + result);
}
static int countOnes(String binarySequence) {
int count = 0;
for (char digit : binarySequence.toCharArray()) {
if (digit == '1') {
count++;
}
}
return count;
}
}
🙌🙌