Given a sorted array of integers, search a given key in the array using binary search

Posted on | Wednesday, 17 May 2023 | No Comments

 Given a sorted array of integers, search a given key in the array using binary search.

Note: Do not use any inbuilt functions/libraries for your main logic.
(Try to practice both iterative and recursive codes for Binary Search)

Input Format

First line of input contains two integers, N - size of the array and K - search key. Second line contains the elements of the sorted array.

Constraints

1 <= N <= 102
0 <= ar[i] <= 109

Output Format

Print "true" if key is present in the array, otherwise, print false.

Sample Input 0

5 19
2 19 23 35 38

Sample Output 0

true
x,y=map(int,input().split())
p=list(map(int,input().split()))
a=0
for i in range(x):
    if p[i]==y:
        print("true")
        break
for i in range(x):
    if p[i]!=y:
        a=a+1
    
if a==x:
    print("false")

Given a string, toggle the case of each character in the given string.

No Comments

 Given a string, toggle the case of each character in the given string.

Input Format

Input contains a string S, consisting of lowercase and uppercase characters.

Constraints

1 <= len(S) <= 100

Output Format

Print the toggled string.

Sample Input 0

abdBd

Sample Output 0

ABDbD
string = input()

string1 = ''

for i in range(len(string)):
    if(string[i] >= 'a' and string[i] <= 'z'): 
        string1 = string1 + chr((ord(string[i]) - 32)) 
    elif(string[i] >= 'A' and string[i] <= 'Z'):
        string1 = string1 + chr((ord(string[i]) + 32))
    else:
        string1 = string1 + string[i]
 

print(string1)

Given a string, print count of vowels and consonants in the string.

No Comments

 Given a string, print count of vowels and consonants in the string.

Input Format

Input contains a string S, consisting of lowercase and uppercase characters.

Constraints

1 <= len(S) <= 100

Output Format

Print count of vowels and consonants in the given string, separated by space.

Sample Input 0

aBxbbiAasPw

Sample Output 0

4 7
vcount = 0;  
ccount = 0;  
str = input()
str = str.lower();  
for i in range(0,len(str)):   
    
    if str[i] in ('a',"e","i","o","u"):  
        vcount = vcount + 1;  
    elif (str[i] >= 'a' and str[i] <= 'z'):  
        ccount = ccount + 1;  
        
print(vcount,ccount);  

Given a string, check if it contains only digits. Using Python

No Comments

 Given a string, check if it contains only digits.

Input Format

Input contains a string S, consisting of ascii characters.

Constraints

1 <= len(S) <= 100

Output Format

Print "Yes" if string contains only digits, "No" otherwise.

Sample Input 0

123456786543

Sample Output 0

Yes
g=input()
if g.isnumeric():
    print("Yes")
else:
    print("No")

check if it contains only vowels. Using Python

No Comments

 Given a string, check if it contains only vowels.

Input Format

Input contains a string S, consisting of lowercase and uppercase characters.

Constraints

1 <= len(S) <= 100

Output Format

Print "Yes" if string contains only vowels, "No" Otherwise.

Sample Input 0

SmartInterviews

Sample Output 0

No
vcount = 0;  
ccount = 0;  
str = input()
   
str = str.lower();  
for i in range(0,len(str)):   
        if str[i] in ('a',"e","i","o","u"):  
        vcount = vcount + 1;  
    elif (str[i] >= 'a' and str[i] <= 'z'):  
        ccount = ccount + 1;  
if ccount==0:
    print("Yes")
else :
    print("No")

Search

Followers