0% found this document useful (0 votes)
2 views7 pages

Dsa Problems

The document outlines algorithms for finding the first repeating element in an array, identifying common elements in three sorted arrays, and printing matrices in wave and spiral formats. It includes examples and optimal approaches using data structures like unordered maps and sets. The document serves as a guide for implementing these algorithms in C++.

Uploaded by

Rishabh Mohan
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)
2 views7 pages

Dsa Problems

The document outlines algorithms for finding the first repeating element in an array, identifying common elements in three sorted arrays, and printing matrices in wave and spiral formats. It includes examples and optimal approaches using data structures like unordered maps and sets. The document serves as a guide for implementing these algorithms in C++.

Uploaded by

Rishabh Mohan
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/ 7

20 September 2023 15:07

5. Find First Repeating Element (GFG)

EXAMPLE 01: EXAMPLE 02:


INPUT: arr{1,5,3,4,3,5,6} and N=7 INPUT: arr{1,2,3,4,5,6,7} and N=7
OUTPUT: 2 OUTPUT: -1
Explanation: Value 5 is at index 2 Explanation: No repeated value

Note: Position return should be according to 1-based indexing

Brute force approach


Optimal approach with hashing technique

Step 01: Traverse array and store it's element as hashing

Step 02: Traverse array to check each element if it has occurrence in future

Hashing: We can use unordered map to store array's element in pair of key and value

Declaration of unordered map


Read more from this resource: https://www.geeksforgeeks.org/unordered_map-in-cpp-stl

6. Common Element in 3 Sorted Array (GFG)

Example 01: Example 02:


Input: Input:
n1 = 6; A = {1, 5, 10, 20, 40, 80} n1 = 3; A = {3, 3, 3}
n2 = 5; B = {6, 7, 20, 80, 100} n2 = 3; B = {3, 3, 3}
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120} n3 = 3; C = {3, 3, 3}
Output: 20 80 Output: 3

Optimal Approach:
Step 01: remove duplicates from sorted array
Step 02: store common value of all three arrays in new array

Step 01: store unique element in new array with the help of C++ STL Data Structure is set(dataType)

Declaration of set
Read more from this resource: https://www.geeksforgeeks.org/set-in-cpp-stl/

Step 02: store common value of all three arrays in new array
7. Wave Print A Matrix (GFG)

Approach:
- When number of column is even then print row top to bottom
- When number of column is odd then print row bottom to top
8. Spiral Print A Matrix (Leetcode-54)

Output:

1 2 3 4 5 6 12 18 24 30 29 28 27 26 25

19 13 7 8 9 10 11 17 23 22 21 14 15 16

startingRow = 0 startingRow = 1 startingRow = 2


endingCol = 5 endingCol = 4 endingCol = 3
endingRow = 4 endingRow = 3 endingRow = 2
startingCol = 0 startingCol = 1 startingCol = 2

Optimal Approach:
Step 01: find total elements
Step 02: iterate matrix till end of the total element and print
- Print startingRow
- Print endingCol
- Print endingRow
- Print startingCol

You might also like