Thursday, 17 March 2022

Minimum Number of Jumps Problem !!!

 PROBLEM 47:  Minimum Number of Jumps


A Program to find the minimum number of jumps to reach the end of the array (starting from the first element). (Interview Question)


Author - Ajay Zad
Date    - 17/03/2022


import sys
n = int(input("Enter the value for n :"))
l = []
for i in range(0,n):
    m = int(input("Enter the elements in the array :"))
    l.append(m)

i = 0
j = l[0]
cnt = 0
cnt1 = 0
while i < n :
    while j > 0:
        j = j - 1
        cnt = cnt + 1
    else:
        try:
            j = l[cnt]
            #cnt1 will keep track of no. of jumps
            cnt1 = cnt1 + 1
        except:
            print("Number of jumps required = ",cnt1)
            sys.exit()
    i = cnt
print("Number of jumps required = ",cnt1)

The above code is in Python Language.


Input :
Enter the value for n : 5
Enter the elements in the array : 1
Enter the elements in the array : 2
Enter the elements in the array : 2
Enter the elements in the array : 3
Enter the elements in the array : 4


Output :
Number of jumps required = 3


Tuesday, 15 March 2022

Repetition Of All Numbers Problem !!!

 PROBLEM 46: Repetition Of All Numbers


A Program to find out number of times the elements are repeated in the array.

(Interview Question)


Author - Ajay Zad

Date   - 15-03-2022


n = int(input("Enter the size of the array :"))

l = []

for i in range(0,n):

    m = int(input("Enter the elements in the array (repeat the values) :"))

    l.append(m)

    

s = set(l)

l1 = list(s)

l1.sort()


for i in range(0,len(l1)):

    cnt = 0

    for j in range(0,n):

        if l1[i] == l[j]:

            cnt = cnt + 1

    print("Number ",l1[i]," is repeated for = ",cnt," times")

       

The above code is in python language.

Input : 

Enter the size of the array : 5 

Enter the elements in the array (repeat the values) :1

Enter the elements in the array (repeat the values) :2

Enter the elements in the array (repeat the values) :1

Enter the elements in the array (repeat the values) :3

Enter the elements in the array (repeat the values) :2


Output: 

Number  1  is repeated for =  2  times

Number  2  is repeated for =  1  times

Number  3  is repeated for =  2  times

Monday, 14 March 2022

Prime Problem !!!

 PROBLEM 45: Prime 


Program to find out all the prime numbers from the range of 0 to n. 


 Name - Ajay Zad

 Date - 14/03/2022 


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

cnt = 0 

print("The Prime numbers are :")

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

    for j in range(2,i):

        if i % j == 0:   

            cnt = cnt + 1

        

    if cnt == 0:

        print(i)

    cnt = 0




Input:

Enter the value for n : 

10


Output :

The Prime numbers are :

2

3

5

7

Friday, 11 March 2022

Repetition Problem !!!

PROBLEM 44:  Repetition


A Program to find out the most repeated number in the array. (Without using built-in functions)

(Interview Question)


 Author - Ajay Zad 

 Date     - 11/03/2022 


n = int(input("Input the size of the array :"))

l = []

s = set()

for i in range(0,n):

    ele = int(input("Enter the elements in the array :"))

    l.append(ele)

    #Adding no. in set as it doesn't store duplicates

    s.add(ele)

    

cnt = 0

cnt1 = 0

l2 = list(s)

for i in range(0,len(l2)):

    cnt = 0

    for j in range(0,n):

        #Comparing for equality 

        if l2[i] == l[j]:

            cnt = cnt + 1

            #Condition for counter 

            if cnt > cnt1:

                cnt1 = cnt

                m_no = l2[i]

print("The number with highest repetition : ",m_no)


Input :

Input the size of the array : 5

Enter the elements in the array : 1

Enter the elements in the array : 2

Enter the elements in the array : 2

Enter the elements in the array : 3

Enter the elements in the array : 2


Output :

The number with highest repetition : 2


                


                

    

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 



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...