Showing posts with label pattern programming. Show all posts
Showing posts with label pattern programming. Show all posts

Sunday, 14 May 2023

Print Pattern

 

PROBLEM 60: Print Pattern

(For best view experience, view in windows version)

Problem Reference : GeeksForGeeks

Author : Ajay Zad
Date    : 14/05/2023


Print a sequence of numbers starting with N where A[0] = N, in which  A[i+1] = A[i] - 5,  until A[i] > 0. After that A[i+1] = A[i] + 5  repeat it until A[i] = N.

Example 1:

Input: N = 16
Output: 16 11 6 1 -4 1 6 11 16
Explaination: The value decreases until it 
is greater than 0. After that it increases 
and stops when it becomes 16 again.

Example 2:

Input: N = 10
Output: 10 5 0 5 10
Explaination: It follows the same logic as 
per the above example.

Solution:

class Solution:
    def pattern(self, N):
        # code here
        l = []
        k = 0
        N1 = N
        while True:
            if k == 0:
                l.append(N)
                N = N - 5
                if N <= 0:
                    k = 1
            else:
                l.append(N)
                N = N + 5
                if N1 == N:
                    l.append(N1)
                    return l
if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        N = int(input())
        
        ob = Solution()
        ans = ob.pattern(N)
        for i in ans:
            print(i, end = " ")
        print()


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 104 

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












  



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 :

              *

        *    *    *

    *    *    *    *    *

 *    *    *    *    *    *

    *    *    *    *    *

        *    *    *

              *

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