Find the Longest Substring Containing Vowels in Even Counts Problem

Problem Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, ‘a’, ’e’, ‘i’, ‘o’, and ‘u’ must appear an even number of times. Examples Example 1: Input: s = "eleetminicoworoep" Output: 13 Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u. ...

Longest Subarray With Maximum Bitwise AND Problem

Problem You are given an integer array nums of size n. Consider a non-empty subarray from nums that has the maximum possible bitwise AND. In other words, let k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered. Return the length of the longest such subarray. The bitwise AND of an array is the bitwise AND of all the numbers in it. ...

XOR Queries of a Subarray Problem

Problem You are given an array arr of positive integers. You are also given the array queries where queries[i] = [lefti, righti]. For each query i compute the XOR of elements from lefti to righti (that is, arr[lefti] XOR arr[lefti + 1] XOR ... XOR arr[righti] ). Return an array answer where answer[i] is the answer to the ith query. Examples Example 1: Input: arr = [1,3,4,8], queries = [ [0,1],[1,2],[0,3],[3,3] ] Output: [2,7,14,8] Explanation: The binary representation of the elements in the array are: 1 = 0001 3 = 0011 4 = 0100 8 = 1000 The XOR values for queries are: [0,1] = 1 xor 3 = 2 [1,2] = 3 xor 4 = 7 [0,3] = 1 xor 3 xor 4 xor 8 = 14 [3,3] = 8 ...

Insert Greatest Common Divisors in Linked List Problem

Problem Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them. Return the linked list after insertion. The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers. Examples Example 1: ...

Conditional selection between x and y with bitwise operations problem

Problem Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0. Solution Method 1 - Using bitwise masks The core idea is to leverage bitwise operations to create masks based on the value of b, which will then be used to select either x or y. ...

Alternating Two-Instance Singleton Pattern Problem

Problem Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances. And in every even call of getInstance(), return the first instance and in every odd call of getInstance(), return the second instance. Solution Method 1 - Using Singleton Pattern Here is the explanation: instances: An array holding the two singleton instances. callCount: A counter to keep track of how many times getInstance is called. Private Constructor: Ensures that the class cannot be instantiated directly. synchronized getInstance: Synchronisation is used to make the method thread-safe. It ensures that only one thread can execute this method at a time, avoiding the creation of multiple instances in a multi-threaded environment. Modulo Operator (%): Used to alternate between the two singleton instances based on the call count. Lazy Initialization: The instances are created only when they are first needed. Code Java public class TwoInstanceSingleton { private static final TwoInstanceSingleton[] instances = new TwoInstanceSingleton[2]; private static int callCount = 0; // Private constructor to prevent instantiation private TwoInstanceSingleton() {} public static synchronized TwoInstanceSingleton getInstance() { int index = callCount % 2; // Lazily initialize the instance if (instances[index] == null) { instances[index] = new TwoInstanceSingleton(); } callCount++; return instances[index]; } // Example usage public static void main(String[] args) { TwoInstanceSingleton obj1 = TwoInstanceSingleton.getInstance(); TwoInstanceSingleton obj2 = TwoInstanceSingleton.getInstance(); TwoInstanceSingleton obj3 = TwoInstanceSingleton.getInstance(); TwoInstanceSingleton obj4 = TwoInstanceSingleton.getInstance(); System.out.println(obj1 == obj2); // false System.out.println(obj1 == obj3); // true System.out.println(obj2 == obj4); // true System.out.println(obj1 == obj4); // false } } ...

Delete Nodes From Linked List Present in Array Problem

Problem You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums. Examples Example 1: graph LR A[1]:::toBeDeleted --> B[2]:::toBeDeleted --> C[3]:::toBeDeleted --> D[4] --> E[5] classDef toBeDeleted fill:#ffcccc,stroke:#ff0000; ...

Find Missing Observations Problem

Problem You have observations of n + m 6-sided dice rolls with each face numbered from 1 to 6. n of the observations went missing, and you only have the observations of m rolls. Fortunately, you have also calculated the average value of the n + m rolls. You are given an integer array rolls of length m where rolls[i] is the value of the ith observation. You are also given the two integers mean and n. ...

Walking Robot Simulation Problem

Problem A robot on an infinite XY-plane starts at point (0, 0) facing north. The robot can receive a sequence of these three possible types of commands: -2: Turn left 90 degrees. -1: Turn right 90 degrees. 1 <= k <= 9: Move forward k units, one unit at a time. Some of the grid squares are obstacles. The ith obstacle is at grid point obstacles[i] = (xi, yi). If the robot runs into an obstacle, then it will instead stay in its current location and move on to the next command. ...

Find the Student that Will Replace the Chalk Problem

Problem There are n students in a class numbered from 0 to n - 1. The teacher will give each student a problem starting with the student number 0, then the student number 1, and so on until the teacher reaches the student number n - 1. After that, the teacher will restart the process, starting with the student number 0 again. You are given a 0-indexed integer array chalk and an integer k. There are initially k pieces of chalk. When the student number i is given a problem to solve, they will use chalk[i] pieces of chalk to solve that problem. However, if the current number of chalk pieces is strictly less than chalk[i], then the student number i will be asked to replace the chalk. ...

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