0% found this document useful (0 votes)
73 views

Pseudocode Array

1. The pseudocode takes in an array as input and prints any duplicate elements. If there are no duplicates, it prints "No Duplicates Present". It uses two nested for loops to compare each element to every other element. 2. The pseudocode calculates the minimum number of years an investment of Rs. 1 would need to be held to exceed a given expected amount, assuming the share price doubles every 3 years. It uses a while loop to calculate the share value each year and sum until the expected amount is exceeded. 3. The pseudocode removes any duplicate elements from an input array. It uses nested for loops to compare each element to every other and increments a count if duplicates are found. It prints
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Pseudocode Array

1. The pseudocode takes in an array as input and prints any duplicate elements. If there are no duplicates, it prints "No Duplicates Present". It uses two nested for loops to compare each element to every other element. 2. The pseudocode calculates the minimum number of years an investment of Rs. 1 would need to be held to exceed a given expected amount, assuming the share price doubles every 3 years. It uses a while loop to calculate the share value each year and sum until the expected amount is exceeded. 3. The pseudocode removes any duplicate elements from an input array. It uses nested for loops to compare each element to every other and increments a count if duplicates are found. It prints
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment

Assignment : 4
Topic: Pseudocode Array

1. Write a Pseudocode to find duplicate elements in the array and print


the duplicates element(s). If not present print “No Duplicates
Present”.
SampleInput1:
10
4 5 6 6 2 56 66 56 4 5
SampleOutput1:
4 5 6 56
SampleInput2: 5 3 4 5 22 78
SampleOutput2:
No Duplicates Found

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

3. Write a pseudocode to find and remove duplicate elements in the


array and reprint. If not present print “No Duplicates Present”.
SampleInput1:
10
4 5 6 6 2 56 66 56 4 5
SampleOutput1:
4 5 6 2 56 66
SampleInput2:
5
3 4 5 22 78
SampleOutput2:
No Duplicates Found

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

FOR i=0 to size-1


SET j=1 , pos[k]=arr[i]
IF arr[i] ==arr[j] THEN
i++;
j++;
sum=sum+1
ENDIF
INCREMENT j by 1
INCERMENT k by 1
ENDFOR
SET len=lenth(pos)
IF sum==0 THEN
PRINT “No duplicates Found”
ELSE
FOR i=0 to len – 1
PRINT pos[i]
ENDFOR
ENDIF
END

4. Write a pseudocode to find pair of elements in the array having sum


of 10. If not found any, return -1.
Sample Input1:
1283
Sample Output1:
(2,8)
Sample Input2:
12345
Sample Output2:
-1

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

5. Write a pseudocode to replace each element of the array with product


of all other elements in each array of integers.
Sample Input:
4, [1 2 3 4]
Sample Output:
24 12 8 6

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

7. Write a pseudocode to arrange the elements of a given array of


integers where all positive integers appear before all the negative
integers.
Sample Input:9, [1 2 -3 4 5 6 -7 8 9]
Sample Output: 1 2 4 5 6 8 9 -3 -7

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

9. Write a pseudocode to accept n numbers from console. Then delete an


element at the specified position of the array.
Sample Input:
5, [1, 2, 3, 4, 5], 3
Sample Output:
1,2,4,5

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

10.A Company has a sales record of N products for M days.The company


wishes to know the maximum revenue received from a given product
of the N products each day. Write a pseudocode to find the highest
revenue received each day.
Sample Input:
M : 5 (days)
N : Variable Quantity (products)
100 198 333
22 232 221 111 78
223 565
200
Sample Output:
333 232 764

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

12.Write a pseudocode to check whether a number can be expressed as a


sum of two prime numbers
Sample Input 1:
13
Sample Output 1:
Yes
Sample Input 2:
11
Sample Output 2:
No

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

13.In Finland, the main occupation of its citizens is to perform tasks on


numbers. One such important task is finding interesting number
sequences. As per the Finland Research institute, an interesting
sequence is defined as a sequence of numbers that are consecutive, For
instance: {10,11,12,13} is an interesting sequence but {2,5,8} is not.
Now, citizens are given a set of distinct numbers to extract the length
of the largest, interesting sequence that can be obtained by
rearranging numbers within the set. Assume distinct numbers will be
in the range[9,1000].
Sample Input 1:
11
5 4 3 2 1 11 10 14 13 12 9
Sample Output 1:
6
Sample Input 2:
19
24 73 90 25 82 51 23 21 22
Sample Output 2:
5

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

ELSE arr[k+2]%2!=0 THEN


Temp=arr[k]
Arr[k]=arr[k+2]
Arr[k+2]=temp
PRINT arr[k]
k++
ENDIF
ENDFOR
ELSE
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++

ELSE arr[k+2]%2==0 THEN


Temp=arr[k]
Arr[k]=arr[k+2]
Arr[k+2]=temp
PRINT arr[k]
k++
ENDIF
ENDFOR

FOR k=0 to n-1


PRINT arr[k]
ENDFOR
END

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

You might also like