0% found this document useful (0 votes)
14 views17 pages

Day3 - Array - QB

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Day3 - Array - QB

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Day 3

• Product of All Elements Except Self. Solve using single for loop.
• Input: 1 2 3 4
• Output: 24 12 8 6
Reverse Array
Input: 1 2 3 4 5
Output: 5 4 3 2 1
• Split Array and Add Second Part to First
• Input: 1 2 3 4 5 6 7 8
• Output: 5 6 7 8 1 2 3 4
• Move Zeros to End: Move all zeros in an array to the end while
maintaining the order of non-zero elements.
Input: [0, 1, 0, 13, 12]
Output: 1 13 12 0 0
• Find second largest in even and odd position.
• Note: Use single for loop
• Input: 3 6 8 7 5 10 12 9
• Output: 8 9
• Explanation:
• Even positions Elements: [3, 8, 5, 12]. The second largest element is 8.
• Odd positions Elements: [6, 7, 10, 9]. The second largest element is 9.
• Given a list of N integers, the task is to find the largest divisor
for each element in the list other than the number itself.
• The divisor should be from the list. If there is no such divisor,
print -1.
• Sample Input :
6
5, 16, 4, 8, 9, 10
• Sample Output : -1 8 -1 4 -1 5
• Find the contiguous subarray with the largest sum.
• Input:
6
4 -1 2 1 -5 4
• Output:
6
Explanation:
The subarray [4, -1, 2, 1] has the largest sum of 6.
• Find smallest element in each rows.
• Input:
3 //row and col size
351
728
496
• Output: 1 2 4
• Print matrix in snake pattern
• Input:
3 //row and col size
351
728
496
• Output: 3 5 1 8 2 7 4 9 6
• Matrix rotation 90 Degree
• hint-> transpose then reverse each row
• Input :
3 // row and col size
1 2 3
4 5 6
7 8 9
• Output:
7 4 1
8 5 2
9 6 3
Find lucky number. A lucky number is an element of the matrix such
that it is the minimum element in its row and maximum in its column.
Eg: Input:
03 07 08
09 11 13
15 16 17
Output:
15
Movie seat arrangements
• Samantha wants to book n tickets for the movie "Vettaiyan." She prefers
that the seats be consecutive. Help her determine whether there are n
contiguous seats available for booking. Note: In the seating arrangement, a
0 represents an available seat, and a 1 indicates a booked seat. Seats are
arranged in M X M matrix's.
• Input1: Input2:
5 // M 5 // M
3 // n 4 // n
10101 10101
11111 11111
10010 10010
00000 01010
Output1: Seats available Output2: Seats not available
Queen attack position
• Input:
4// board size
2 3 // 2nd row 3rd column
• Output:
(1,4) (3,1) (4,1) (1,2) (3,4) (1,3) (3,3) (2,2) (2,4)
• Explanation:
• For a given two-dimensional integer array/list of size (N x M), print the
array/list in a sine wave order, i.e, print the first column top to bottom,
next column bottom to top and so on.
• Input Format: The first line have the M value, Second line have N value,
Third-line onwards, the next 'N' lines or rows represent the ith row values,
Each of the ith rows constitutes column values separated by a single space
• Sample Input:
3
4
1234
5678
9 10 11 12
• Sample Output: 1 5 9 10 6 2 3 7 11 12 8 4
Stock buy and sell :The cost of stock on each day is given in an array A[] of size N.
Find all the days on which you buy and sell the stock so that in between those days
your profit is maximum.
Input:
N=7
A[] = {100,180,260,310,40,535,695}
Output:
(0 3) (4 6)
Explanation:
We can buy stock on day 0,and sell it on 3rd day, which will give us maximum
profit. Now, we buy stock on day 4 and sell it on day 6.
Paint in Time :Dilpreet wants to paint his dog's home that has n boards with different
lengths. The length of ith board is given by arr[i] where arr[] is an array of n integers. He
hired k painters for this work and each painter takes 1 unit time to paint 1 unit of the
board. The problem is to find the minimum time to get this job done if all painters start
together with the constraint that any painter will only paint continuous boards, say boards
numbered {2,3,4} or only board {1} or nothing but not boards {2,4,5}.

Input: n = 5 k = 3

arr[] = {5,10,30,20,15}

Output: 35

Explanation: Painter 1 allocation : {5,10}, Painter 2 allocation : {30}, Painter 3 allocation :


{20,15}. Job will be done when all painters finish i.e. at time = max(5+10, 30, 20+15) = 35

You might also like