Skip to content

Commit

Permalink
start 0 and end 1 and then get(end) and get(mid)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumant3086 committed Jan 20, 2025
1 parent 80551ee commit 5a5bc7d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Count Palindrome Words In A String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Count Palindrome Words In A String
// Easy
// 0/40
// Average time to solve is 15m
// Contributed by
// Asked in companies
// Problem statement
// You are given a string S of words. Your task is to find the number of palindrome words in the given string S. A word is called palindrome, if it reads the same backwards as forwards.

// Note:
// Words are separated by one or more whitespace characters.
// For Example:
// For the given string “Madam oyo cat”, “Madam”, and “oyo” are the palindrome words
// Detailed explanation ( Input/output format, Notes, Images )
// Constraints:
// 1 <= T <= 10
// 0 <= |S| <= 10^5

// All the characters of the string S contain whitespace, lowercase, and uppercase English letters only.

// Time limit: 1 second
// Sample Input 1:
// 1
// Nitin and I are good friends
// Sample Output 1:
// 2
// Explanation For Sample Input 1:
// For the first test case, there are 2 palindrome words only i.e “Nitin” and “I”.
// Sample Input 2:
// 2
// Madam taught us the level order traversal of a binary tree yesterday
// We love coding ninjas
// Sample output 2:
// 3
// 0

#include <bits/stdc++.h>
bool isPal(string &s,int low,int high){
if(s.size()==0){
return false;
}
if(low>high)return false;
while(low<high){
char l=s[low];
char r=s[high];
if(l>='A' && l<='Z')l+=32;
if(r>='A' && r<='Z')r+=32;
if(l!=r)return false;
low++;
high--;
}
return true;

}
int countNumberOfPalindromeWords(string s)
{
//Your code goes here
int count=0;
int idx=0;
int n=s.size();
for(int i=0;i<n;i++){
if(s[i]==' '){
if(isPal(s,idx,i-1))count++;
idx=i+1;
}
}
if(isPal(s,idx,n-1))count++;
return count;
}
71 changes: 71 additions & 0 deletions Search In Infinite Sorted 0-1 Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Search In Infinite Sorted 0-1 Array
// Easy
// 40/40
// Average time to solve is 10m
// 33 upvotes
// Asked in companies
// Problem statement
// You are given an infinite array consisting of only ones and zeroes, in sorted order. You have to find the index of the first occurrence of 1.

// Example:
// If the array is 0 0 0 0 1 1 1 1… then, the first occurrence of 1 will be at index 4 therefore the answer here is 4.
// Note:
// As the array size is infinite, the actual array won’t be given to you. Instead, you will be able to access the array elements by calling a method named ‘get’.

// get(i) : returns the value present at index I.

// Indexing is 0-based.

// Instead of representing an infinite array in the input, we give the index of the first occurrence of 1 in the input itself. However, this input will be completely hidden from the user.

// It is guaranteed that the answer will fit in a 64-bit integer.
// Detailed explanation ( Input/output format, Notes, Images )
// Constraints:
// 0 <= ARR[i] <= 1

// Time limit: 1sec
// Sample Input 1:
// 10
// Sample Output 1:
// 10
// Sample Input 2:
// 1
// Sample Output 2:
// 1


/************************************************************
Use get function that returns the value at index i
in the infinite sorted binary array.
get(i)
{
}
************************************************************/

long long firstOne()
{

// Write your code here.
long long start=0;
long long end=1;
while(get(end)==0){
start=end;
end*=2;
}
long long ans=-1;
while(start<=end){
long long mid=(start+end)/2;
if(get(mid)==1){
ans=mid;
end=mid-1;
}else{
start=mid+1;
}
}
return ans;
}

0 comments on commit 5a5bc7d

Please sign in to comment.