Showing posts with label jumping solution. Show all posts
Showing posts with label jumping solution. Show all posts

Friday, 8 April 2022

Array Rotate Problem !!!

 PROBLEM 48: Array Rotate 


A program to rotate 'N' number of elements in an array depending on the size of the rotate. Rotation to be considered from both the ends of an array. (Without using Built-in functions) (INTERVIEW QUESTION)

(For best view experience, view in windows version)


Author - Ajay Zad

Date     - 08/04/2022


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

m = int(input("Enter the size of rotation :"))

l = []

for i in range(0,n):

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

        l.append(a)

x = 1

while x == 1:

        d = input("Enter the direction for rotation Right(r) or Left(l):")

        if d == 'l':

            l2 = list(l)        

            j = 1

            for i in range(m-1,-1,-1):

                l2[n-j] = l[i]   

                j = j + 1

            j = 0

            for i in range(m,n):

                 l2[j] = l[m+j]

                 j = j + 1

            print(l2)

            l = l2[:]        

        elif d == 'r':

            l2 = list(l)

            j = 0

            for i in range(m-1,-1,-1):

                l2[i] = l[n-(j+1)]

                j = j + 1

            j = 0

            for i in range(m,n):

                l2[m+j] = l[j]

                j = j + 1

            print(l2)

            l = l2[:]

        else:

            print("Enter the correct option")

        x = int(input("Enter 1 to continue or press any other number to exit: "))

   

The above code is in Python language.


Input:

Enter the size of the array : 8
Enter the size of rotation   : 4

Enter the elements in the array : 1
Enter the elements in the array : 2
Enter the elements in the array : 3
Enter the elements in the array : 4
Enter the elements in the array : 5
Enter the elements in the array : 6
Enter the elements in the array : 7
Enter the elements in the array : 8

Enter the direction for rotation Right(r) or Left(l): l
[5, 6, 7, 8, 1, 2, 3, 4]

Enter 1 to continue or press any other number to exit: 1

Enter the direction for rotation Right(r) or Left(l): r
[1, 2, 3, 4, 5, 6, 7, 8]

Enter 1 to continue or press any other number to exit: 2












  



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


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