Showing posts with label I f conditions. Show all posts
Showing posts with label I f conditions. Show all posts

Thursday, 10 March 2022

Missing Number Problem !!!

 PROBLEM 43 : Missing Number 

A Program to find out the missing number from a range of 0-N numbers in a series. 

(Interview Question)


Author - Ajay Zad

Date   - 10/03/2022 


import sys    

n = int(input("Enter the value for n : "))

l = []

for i in range(1,n):

    n1 = int(input("Enter the value less than or equal to 'n' without repeating the values :"))

    l.append(n1)

    

l.sort()

j = 1

for i in range(0,n-1):

    if j != l[i]:

        print("The missing number is ",j)

        sys.exit()

    j = j + 1

    

print("The missing number is ",n)


                                                                    OR


n = int(input("Enter the value for n : "))

l = []

sum = 0

for i in range(1,n):

    n1 = int(input("Enter the value less than or equal to 'n' without repeating the values :"))

    l.append(n1)

    sum = sum + n1

    

sum1 = 0    

for i in range(1,n+1):

    sum1 = sum1 + i

    

print("The missing number is ",(sum1-sum))


The above code is in Python language.


Input :

Enter the value for n : 5

Enter the value less than or equal to 'n' without repeating the values : 3

Enter the value less than or equal to 'n' without repeating the values : 1

Enter the value less than or equal to 'n' without repeating the values : 5

Enter the value less than or equal to 'n' without repeating the values : 2


Output :

The missing number is 4

Tuesday, 8 March 2022

Diamond Problem !!!

 PROBLEM 42 : Diamond 


A program to display the shape of  a diamond using asterisk (*) symbol for 'N' number of lines 


Author - Ajay Zad

date   - 08/03/2022 


# n defines number of lines     

n = int(input("Enter for n :"))

stars = 1

spaces = n     

for i in range(0,n):

    #loop for implementing spaces

    for j in range(spaces,0,-1):

        print(" ",end="")

    #loop for printing stars

    for k in range(0,stars):

        print("*",end="")

    print()

    stars = stars + 2

    spaces = spaces - 1

    

stars = (n*2) - 1

spaces = 1

for i in range(0,n):

    for j in range(0,spaces):

        print(" ",end="")

    for k in range(stars,0,-1):

        print("*",end="")

    print()

    stars = stars - 2

    spaces = spaces + 1



The above code is in Python language.


Input :

Enter for n : 4


Output :

              *

        *    *    *

    *    *    *    *    *

 *    *    *    *    *    *

    *    *    *    *    *

        *    *    *

              *

Monday, 7 March 2022

First & Last Problem !!!

PROBLEM 41 : First & Last


A program to find whether the first digit of  the input and the last digit of the input are same or not. 




 Author - Ajay Zad
 date   - 07/03/2022 

num = int(input("Enter the number :"))
#Converting from int data type to string data type
string = str(num)         
#Finding the length of the string           
length = len(string)       
#Comparing for equality          
if  string[0] == string[length-1]:     
    print("Both the digits are same")
else:
    print("Both the digits are not same")



Input :
Enter the number : 10011

Output :
Both the digits are same


Input :
Enter the number : 10022

Output:
Both the digits are not same 



Friday, 10 December 2021

Dislike of Threes Problem !!!

 PROBLEM 36: Dislike of Threes


Problem Reference: Codeforces


Polycarp doesn't like integers that are divisible by  or end with the digit  in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.

Polycarp starts to write out the positive (greater than  Output the -th element of this sequence (the elements are numbered from ).


Input:

The first line contains one integer  () — the number of test cases. Then  test cases follow.

Each test case consists of one line containing one integer  ().


Output:

For each test case, output in a separate line one integer  — the -th element of the sequence that was written out by Polycarp.


Solution:


  1. t = int(input())
  2. while 0 < t:
  3. n = int(input())
  4. i = 1
  5. j = 1
  6. while j == 1:
  7. if i % 3 == 0 or i % 10 == 3:
  8. i = i + 1
  9. else:
  10. n = n - 1
  11. if n == 0:
  12. print(i)
  13. break
  14. i = i + 1
  15. t = t - 1


The above solution is in python language.



Example
input
10
1
2
3
4
5
6
7
8
9
1000
output
1
2
4
5
7
8
10
11
14
1666

Thursday, 9 December 2021

Spy Detected Problem !!!

 PROBLEM 35: Spy Detected!


Problem Reference: Codeforces


You are given an array a consisting of n (n3) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array [4,11,4,4] all numbers except one are equal to 4).

Print the index of the element that does not equal others. The numbers in the array are numbered from one.


Input:

The first line contains a single integer t (1t100). Then t test cases follow.

The first line of each test case contains a single integer n (3n100) — the length of the array a.

The second line of each test case contains n integers a1,a2,,an (1ai100).

It is guaranteed that all the numbers except one in the a array are the same.


Output:

For each test case, output a single integer — the index of the element that is not equal to others.


Solution: 


  1. t = int(input())
  2. i = 0
  3. while i < t:
  4. n = int(input())
  5. a = [int(a) for a in input().split()]
  6. j = 1
  7. while j <= len(a):
  8. if a[j] == a[j-1]:
  9. j = j + 1
  10. else:
  11. break
  12. if j == 1 and a[j] == a[j+1]:
  13. print(j)
  14. else:
  15. print(j+1)
  16. i = i + 1


The above solution is in python langauge


Example
input
4
4
11 13 11 11
5
1 4 4 4 4
10
3 3 3 3 10 3 3 3 3 3
3
20 20 10
output
2
1
5
3

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...