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")

Min Max Sum Hackerrank

Posted on | Tuesday, 16 May 2023 | No Comments



 Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Example

The minimum sum is  and the maximum sum is . The function prints

16 24

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

Input Format

A single line of five space-separated integers.

Constraints

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

The numbers are , and . Calculate the following sums using four of the five integers:

  1. Sum everything except , the sum is .
  2. Sum everything except , the sum is .
  3. Sum everything except , the sum is .
  4. Sum everything except , the sum is .
  5. Sum everything except , the sum is .

code:-
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def miniMaxSum(arr):
    # Write your code here
    c=sum(arr)-min(arr)
    d=(sum(arr)-max(arr))
    print(d,c)
if __name__ == '__main__':

    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)


Search

Followers