Pseudocode Array
Pseudocode Array
Assignment : 4
Topic: Pseudocode Array
BEGIN
READ size
SET C = 0,
FOR i = 0 to size – 1
READ array [i]
ENDFOR
FOR i = 0 to size – 1
FOR j = i+1 to size – 1
IF array [i] ==array[j] THEN
PRINT array [i]
SET C = C+1
ENDIF
ENDFOR
ENDFOR
IF C==0 THEN
PRINT “No duplicates available”
ENDIF
END
2. Price of Asian paint’s shares doubles roughly every 3 years. A person
invests Rs 1 in it. Write a program to display least number of years he
should stay invested to total amount cross the given expected amount.
Also print the share values of every year.
Sample Input:
100
Sample Output:
21
BEGIN
READ amount
SET invest = 1, years = 0, sum = 0
WHILE sum < amount
PRINT years + “years :” + invest+”Amount”
Invest = invest * 2
Years = 0+3
Sum = sum + invest
ENDWHILE
END
BEGIN
READ size
SET sum = 0, k=0
FOR i=0 to size – 1
READ arr[i]
ENDFOR
FOR i=0 to size-1
FOR j=i+1 to size-1
IF arr[i] > arr[j] THEN
Temp=arr[i]
Arr[i] = arr[j]
Arr[j] = temp
ENDIF
ENDFOR
ENDFOR
BEGIN
READ N, sum.
SET count = 0, return =-1
FOR i=0 to N -1
READ arr[i]
ENDFOR
FOR i=0 to N -1
For j = i+1 to N -1
IF (arr[i] + arr[j] == sum) THEN
PRINT arr[i], arr[j]
count = count + 1
ENDIF
ENDFOR
ENDFOR
IF count ==0
PRINT return
ENDIF
END
BEGIN
READ N
FOR i=0 to N-1
READ arr[i]
ENDFOR
FOR i = 0 to N -1
FOR j = 0 to N -1
IF i ! = j THEN
Product = arr[i] * arr[j]
ENDIF
ENDFOR
PRINT product
ENDFOR
END
6. XYZ College asked their students to register for NSS and NCC if they
are willing. Some of the students registered for both. Identify them if
student ids(numeric) for each group is the input.
Sample Input:
10, 10, [2 4 7 11 16 22 29 37 46 56], [1 4 7 10 13 16 19 22 25 28]
Sample Output:
4 7 16 22
BEGIN
READ N
Initialize NSS[N], NCC[N]
FOR i = 0 to N-1
READ NSS[i]
ENDFOR
FOR i = 0 to N-1
READ NCC[i]
ENDFOR
FOR i = 0 to N-1
FOR j = 0 to N-1
IF NSS[i] == NCC[j] THEN
PRINT NSS[i]
ENDIF
ENDFOR
ENDFOR
END
BEGIN
READ N
SET j = 0 , pos[n]
FOR i = 0 to N-1
READ array[i]
ENDFOR
FOR i = 0 to N-1
IF (array[i] >0) THEN
pos [j] = array[i]
j = j+1
ELSE
Pos[N-1] = array[i]
ENDIF
ENDFOR
END
8. Write a pseudocode to accept n numbers from console. Then insert an
element at the specified position of the array.
Sample Input:
5, [1, 2, 3, 4, 5], 19, 3
Sample Output:
[1, 2, 19, 3, 4, 5]
BEGIN
READ N, pos, value
FOR i = 0 to N-1
READ array[i]
ENDFOR
FOR i=N to 0
IF i==pos-1 THEN
array[i] == value
ELSE
array[i] = array[i-1]
ENDIF
ENDFOR
FOR i = 0 to N
READ array[i]
ENDFOR
END
BEGIN
READ N, pos , value
SET j=0
FOR i = 0 to N-1
READ array[i]
ENDFOR
FOR i = 0 to N-1
IF i ==pos-1 THEN
i = i+1
pos[i] = arr[i]
ELSE
Pos[j] = arr[i]
j = j+1
ENDIF
ENDFOR
SET len=length(pos)
FOR i = 0 to len-1
READ pos[i]
ENDFOR
END
BEGIN
READ N, M
SET high=0
FOR i = 0 to N-1
FOR j= 0 to m-1
READ array[i][j]
ENDFOR
ENDFOR
FOR i = 0 to N-1
FOR j= 0 to m-1
IF array[i][j] > high THEN
High=array[i][j]
ENDIF
ENDFOR
PRINT high
SET High=0
ENDFOR
END
11.You are provided with 3 numbers: input1,input2,input3.Each of these
are four digit number within the range of >=1000 and <=999.You are
expected to find the key using the formula .Key : (Sum of Smallest
digit of all this 3 numbers) - (Sum of Largest digits of all 3 numbers)
Sample Input 1:
Input1 :3521
Input2: 2452
Input3 :1352
Sample Output 2:
-11
BEGIN
SET Sum = 0 , Sum 1 = 0
FOR i=0 to N-1
READ arr[i]
IF arr[i]>999 and arr[i]<10000 THEN
arr[i]=arr[i]
ELSEIF
SET i=i-1
ENDIF
ENDFOR
FOR i=0 to N-1
SET Small = 100, big=0
WHILE arr[i]! = 0
Rem =arr[i]%10
IF rem<small
Rem = small
IF rem > big
Rem = big
arr[i] = arr[i]/10
ENDWHILE
Sum = sum+small
Sum1 = sum+big
ENDFOR
COMPUTE key = sum-sum1
PRINT key
END
BEGIN
READ N,value
SET k=0,s=0,c=0
FOR i = 2 to N
FOR j = 1 to N
IF i%j == 0 THEN
SET c=c+1
ENDIF
ENDFOR
IF c==2 THEN
arr[k] = i
c = 0, k++
ENDIF
ENDFOR
SET len= length (arr)
FOR i=0 to len-1
FOR j=i+1 to len-1
IF i+j == value THEN
PRINT “yes”
s=s+1
ENDIF
ENDFOR
ENDFOR
IF s==0 THEN
PRINT “No”
ENDIF
END
BEGIN
READ N
SET c=1 , s=0, j=1
FOR i=0 to n-1
READ arr[i]
ENDFOR
FOR (int i to n-1)
FOR (int j=i+1 to n-1)
IF (arr[i]>arr[j])
Temp= arr[i]
Arr[i] = arr[j]
Arr[j] = temp
ENDIF
ENDFOR
ENDFOR
FOR i=0 to n-1
IF arr[i]+1 ==arr [j] THEN
c =c+1
ELSE
IF s<c THEN
s=s+c
ENDIF
c=1
ENDIF
INCREMENT j by 1
ENDFOR
PRINT s
END
14.Given an array of integers (both odd and even), the task is to arrange
them in such a way that odd and even values come in alternate fashion
in non-decreasing order(ascending) respectively.
If the smallest value is Even then we have to print Even-Odd
pattern.
If the smallest value is Odd then we have to print Odd-Even
pattern.
Note: No. of odd elements must be equal to No. of even elements in
the input array.
Input 1: arr[] = {1, 3, 2, 5, 4, 7, 10}
Output 1: 1, 2, 3, 4, 5, 10, 7
Smallest value is 1(Odd) so output will be Odd-Even pattern.
Input 2: arr[] = {9, 8, 13, 2, 19, 14}
Output 2: 2, 9, 8, 13, 14, 19
Smallest value is 2(Even) so output will be Even-Odd pattern
BEGIN
READ N
SET k=0
FOR i=0 to n-1
READ arr[i]
ENDFOR
FOR i=0 to size-1
FOR j=i+1 to size-1
IF arr[i] > arr[j] THEN
Temp=arr[i]
Arr[i] = arr[j]
Arr[j] = temp
ENDIF
ENDFOR
ENDFOR
SET k=0
IF arr[k] %2==0 THEN
FOR i=0 to n-1
IF i%2==0
IF arr[k]%2==0 THEN
PRINT arr[k]
k++
ELSEIF arr[k+1]%2==0 THEN
Temp=arr[k]
Arr[k]=arr[k+1]
Arr[k+1]=temp
PRINT arr[k]
k++
ELSE arr[k+2]%2==0 THEN
Temp=arr[k]
Arr[k]=arr[k+2]
Arr[k+2]=temp
PRINT arr[k]
k++
ENDIF
ELSE
IF arr[k]%2! = 0 THEN
PRINT arr[k]
k++
ELSEIF arr[k+1]%2!=0 THEN
Temp=arr[k]
Arr[k]=arr[k+1]
Arr[k+1]=temp
PRINT arr[k]
k++
15.Company Discount
A Company is planning a big sale at which they will have a customer
special promotional discount. Each customer when purchasing a
product from the company a Unique customer ID numbered from 0
to N-1 is assigned and, the Andy, marketing head of the company has
selected bill amount of the N customers, for the discount, to be given
to the customer whose bill amount are perfect squares and customer
may use this discount on a future purchase. Write the pseudocode to
help Andy find the number of customers who will be given discount.
Sample Input:
N:6
A[] : 25 77 54 81 45 34
SampleOutput :
2
BEGIN
READ N,size
SET c=0
FOR i = 0 to size-1
READ array[i]
ENDFOR
FOR i = 1 to 20
SET square[i]=i*i
ENDFOR
FOR i = 0 to N-1
FOR j= 0 to N-1
IF array[i]==square[i]
c=c+1
ENDIF
ENDFOR
ENDFOR
IF c==0
PRINT “no customer eligible to discount”
ELSE
PRINT c
ENDIF
END
16.Longest Decreasing Sub-sequence (LDS) Given an integer array A
(1<=length(A)<=1000), Find the length of its Longest Decreasing
Subsequence (LDS). The elements of an LDS are sorted in decreasing
order. A sequence is a particular order in which related objects follow
each other, A sub-sequence is a sequence obtained by omitting some of
the elements of a large sequence. You need to fill in a function that
takes integer n and an integer array A containing n integers and
return the length of LDS.
Sample Input 1:
N:3 A[] :{1,3,2}
Sample Output 1:
2
Explanation:
{3,2}is the longest decreasing sub-sequence of {1,3,2}. Hence the
length of the subsequence is 2.
Sample Input 2:
N:5 A[] :{41,18467,6334,26500,19169,18000}
Sample Output 2:
3
Explanation:
{26500,19169,18000} is the longest decreasing sub-sequence of
(41,18467,6334,26500,19169,18000). Hence the length of the
subsequence is 3
BEGIN
READ N,size
SET c=1,s=1
FOR i = 0 to size-1
READ array[i]
ENDFOR
FOR i = 0 to N-1
IF array[i]>array [i+1] THEN
c=c+1
ELSE
c=1
ENDIF
IF s<c THEN
s=c
ENDIF
ENDFOR
PRINT s
END