Three Consecutive Odds Problem

Problem Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false. Examples Example 1: Input: arr = [2,6,4,1] Output: false Explanation: There are no three consecutive odds. Example 2: Input: arr = [1,2,34,3,4,5,7,23,12] Output: true Explanation: [5,7,23] are three consecutive odds. ...

Find length of the linked list

Problem Given a linked list, return the length or size of the linked list. Examples Input: head=[1, 2, 8, 3, 7, 0, 4] Output : 7 Explanation: Given linkedlist has 7 nodes Solution Method 1 - Recursive Code Java class Solution { public int size(ListNode head) { // Base case: if the head is null, return 0 if (head == null) { return 0; } // Recursive case: 1 + length of the rest of the list return 1 + size(head.next); } } ...

Find Center of Star Graph Problem

Problem There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node. You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph. ...

Nth Number with sum of digits as 10

Problem A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. Examples Example 1: Input: n = 1 Output: 19 Example 2: Input: n = 2 Output: 28 Solution Method 1 - Brute Force Keep on generating the numbers with sum of digits as 10, and when count is n, return the number. ...

Minimum Number of Moves to Seat Everyone Problem

Problem There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student. You may perform the following move any number of times: Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1) Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat. ...

Relative Sort Array Problem

Problem Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order. Examples Example 1: Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] Output: [2,2,2,1,4,3,3,9,6,7,19] ...

Max Stack Problem

Problem Design a max stack that supports push, pop, top, peekMax and popMax. push(x) – Push element x onto stack. pop() – Remove the element on top of the stack and return it. top() – Get the element on the top. peekMax() – Retrieve the maximum element in the stack. popMax() – Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one. Examples Example 1: ...

Height Checker Problem

Problem A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line. You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed). ...

Find Common Characters Problem

Problem Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order. Examples Example 1: Input: words = ["bella","label","roller"] Output: ["e","l","l"] Example 2: Input: words = ["cool","lock","cook"] Output: ["c","o"] ...

Decompress Run-Length Encoded List Problem

Problem We are given a list nums of integers representing a list compressed with run-length encoding. Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list. Return the decompressed list. Examples Example 1: ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy