DSA-Asignment

Problem 1: Majority Element

Instructions: Given an array of integers, find the majority element, i.e., the element that appears more than n/2 times (where n is the size of the array). If no majority element exists, return -1.

Example Test Case:

Input: [2, 4, 5, 5, 5, 5, 5]

Output: 5

Problem 2: Find All Duplicates in an Array

Instructions: Given an array of integers, find all the elements that appear twice in the array and return them.

Example Test Case:

Input: [4, 3, 2, 7, 8, 2, 3, 1]

Output: [2, 3]

2D Array Assignment

Problem 3: print the sum of all elements in a row

Instruction: Write a function called sumOfRows that takes in three parameters: N, M, and arr. The parameters N and M represent the number of rows and columns in a 2D array respectively, while arr is the 2D array itself. The function should calculate and print the sum of each row in the 2D array, with each sum printed on a new line.

Example Test Case:

Input: let arr = [ [1, 2], [3, 4], [5, 6] ];

Output: 3 7 11

Problem 4: print the elements of the 2D array, row by row

Instruction: Given a 2D array arr with N rows and M columns, write a program to print the elements of the array row by row. The input array is represented by the variable arr, which contains N rows and M columns. Your task is to iterate through the array and print each element, row by row.

Example Test Case:

Input: let arr = [ [1, 2], [3, 4], [5, 6] ];

Output: 1 2 3 4 5 6

Problem 5: print the sum of indexes of the 2D array

Instruction: Given a 2D array arr with N rows and M columns, write a program to calculate the sum of the indexes for each position in the array.

Example Test Case:

Input: arr = [ [1, 2], [3, 4], [5, 6] ]

Output: indexSum = [ [0, 1], [1, 1], [2, 1] ]

Problem 6: print the sum of even elements present in the row

Instruction: Given a 2D array arr with N rows and M columns, write a program to calculate the sum of even elements present in each row.

Example Test Case:

Input: arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]

Output: 2 10 8

Problem 7: print the sum of odd elements present in the row

Instruction:Given a 2D array arr with N rows and M columns, write a program to calculate the sum of odd elements present in each row.

Example Test Case:

Input: arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]

Output: 4 5 16

Problem 8: print [Yes], if any particular row of a 2D array contains at least one vowel, at least print [No]

Instruction: Given 2D array is stored in a variable with the name arr, and contains lower case English characters- You have to print [Yes], if any particular row of a 2D array contains at least one vowel, at least print [No]:-

Example Test Case:

Input: arr = [ ['a', 'b'], ['c', 'd'], ['e', 'f'], ]

Output: [Yes] [No] [Yes]

Problem 9: print the sum of even elements present in the column.

Instruction: Given a 2D array arr with N rows and M columns, write a program to calculate the sum of even elements present in each column.

Example Test Case:

Input: arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]

Output: 4 10 6

Problem 10: print the sum of odd elements present in the column

Instruction: Given a 2D array arr with N rows and M columns, write a program to calculate the sum of odd elements present in each column.

Example Test Case:

Input: arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ];

Output: 8 5 12

Problem 11: print elements of array as shown in the diagram, on a single line.

Instruction: Given a 2D array matrix, write a program to print the elements of the array as shown in the diagram, on a single line.

Example Test Case:

Input: matrix = [ [1, 8, 9], [2, 7, 10], [3, 6, 11], [4, 5, 12], ]

Output: 4 3 2 1 5 6 7 12 11 10 9

Problem 12: print elements of array on a single line.

Instruction: Given a 2D array matrix, write a program to print the elements of the array in a specific pattern, on a single line.

Example Test Case:

Input: matrix = [ [1, 8, 9], [2, 7, 10], [3, 6, 11], [4, 5, 12], ]

Output: 9 10 11 12 8 7 6 5 1 2 3 4

Problem 13: Print the matrix elements starting from the top right corner and follow a zig-zag pattern.

Instruction: Given a 2D array matrix, write a program to print the elements of the array starting from the top right corner and following a zig-zag pattern.

Example Test Case:

Input: matrix = [ [4, 7, 1, 1, 7], [8, 9, 9, 6, 1], [6, 4, 9, 5, 1], [7, 7, 4, 7, 7], [8, 6, 2, 5, 5] ]

Output: 7 1 1 7 4 8 9 9 6 1 1 5 9 4 6 7 7 4 7 7 5 5 2 6 8

Longest Word in a String

Problem 14: Write a function findLongestWord that takes a string as input and returns the longest word in the string. If there are multiple longest words, return the first one encountered.

Instruction: The input string may contain alphabetic characters, digits, spaces, and punctuation. The input string is non-empty. The input string may contain multiple words separated by spaces.

Note: If the input string is empty or contains only whitespace, the function should return false. The function should ignore leading and trailing whitespace when determining the longest word.

Input: "JavaScript is the world's most popular programming language."

Output : "programming"