Filter Restaurants by Vegan-Friendly Price and Distance in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:15:53

153 Views

Suppose we have an array of restaurants where restaurants[i] have [idi, ratingi, vegan friendly, pricei, distancei]. We have to filter the restaurants using three filters.The vegan-friendly filter will be either true (meaning we should only include restaurants with vegan-friendly set to true) or false (meaning we can include any restaurant).The maxPrice filter and max distance filter which are the maximum value for price and distance of restaurants we should consider respectively.We have to find the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id in decreasing ... Read More

Sort the Matrix Diagonally in C++

Arnab Chakraborty
Updated on 29-Apr-2020 11:06:15

371 Views

Suppose we have N x M matrix, we have to sort this diagonally in increasing order from top-left to the bottom right. So if the matrix is like −331122121112The output matrix will be −111112221233To solve this, we will follow these steps −Define a method called solve(), this will take si, sj and matrix matn := number of rows and m := number of columnsmake an array called tempi:= si and j := sj, and index := 0while i < n and j < minsert m[i, j] into temp, then increase i and j by 1sort temp arrayset index := 0, ... Read More

Changes of Class Loaders in Java 9

raja
Updated on 29-Apr-2020 10:49:22

942 Views

All java programs run on Java Virtual Machine (JVM). After compilation, a java class gets transformed into a platform and machine-independent bytecode, and compiled classes are stored as .class files. Whenever we try to use it, ClassLoader loads that class into memory. The classes get introduced into the Java environment when they are referenced by name. The loading of classes has been done by the class loader, once the class starts running, and the main() method is a way to start that class.There are few minor changes of class loaders in Java 9:The system class loader is no more in Java 9, ... Read More

Delete Leaves with a Given Value in C++

Arnab Chakraborty
Updated on 29-Apr-2020 10:24:59

162 Views

Suppose we have a binary tree and an integer target, we have to delete all the leaf nodes with value target. We have to keep in mind that once we delete a leaf node with a value target if it's parent node becomes a leaf node and has the value target, it should also be deleted (we need to continue doing that until we can't). So if the tree is like below, and the target is 2, then the final tree will be like the last one −To solve this, we will follow these steps −Define a recursive method called ... Read More

Print Words Vertically in Python

Arnab Chakraborty
Updated on 29-Apr-2020 09:50:19

5K+ Views

Suppose we have a string s. We have to find all the words vertically in the same order in which they appear in s. Here words are returned as a list of strings, we have to complete with spaces when is necessary. (Trailing spaces are not allowed). Each word would be put on only one column and that in one column there will be only one word. So if the input string is “HOW ARE YOU”, then the output will be [“HAY”, “ORO”, “WEU”]To solve this, we will follow these steps −s := make a list of strings split by ... Read More

Display All Modules with ClassLoaders in Java 9

raja
Updated on 29-Apr-2020 09:11:04

514 Views

Before Java 9, the extension and the application class loader are an instance of the java.net.URLClassLoader class. In Java 9, the classification of class loaders has changed, instead of an external class loader, we have the Platform class loader. The purpose of using Platform class loader is that classes loaded by the bootstrap class loader have all permissions by default.In the below example, we can display all modules with classloaders.Exampleimport static java.util.Objects.isNull; public class Java9ClassLoaderTest {    public static void main(String args[]) {       ModuleLayer layer = ModuleLayer.boot();       layer.modules().forEach(module -> { ... Read More

Find N Unique Integers that Sum up to Zero in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:38:05

337 Views

Suppose we have an integer n. We have to return any array that contains n unique integers, such that they add up to 0. So if input is n = 5, then one possible output will be [-7, -1, 1, 3, 4]To solve this, we will follow these steps −take an array A as final answer, and take x := 0for i in range 0 to n – 2A[i] = (i + 1)x := x + i + 1A[n – 1] = xreturn AExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){    cout

Find K Length Substrings with No Repeated Characters in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:31:57

547 Views

Suppose we have a string S, we have to find the number of substrings of length K where no characters are repeated. So if S = “heyfriendshowareyou” and K is 5, then output will be 15, as the strings are [heyfr, eyfri, yfrie, frien, riend, iends, endsh, ndsho, dshow, showa, howar, oware, warey, areyo, reyou]To solve this, we will follow these steps −create one empty map m, and left := 0 and right := -1 and ans := 0while right < length of string – 1if right – left + 1 = k, thenincrease ans by 1decrease m[str[left]] by 1increase ... Read More

Keys and Rooms in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:29:03

540 Views

Suppose we have N rooms and we start in room 0. In each room there exists a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. So in other words, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = number of rooms. A key rooms[i][j] = v, this opens the room with number v So if the input is [[1], [2], [3], []]. then output will be true. There are few more points that ... Read More

Replace Elements with Greatest Element on Right Side in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:28:08

426 Views

Suppose we have an array A. We have to replace every element by the greatest element on the right side of this element. And replace the last one by -1. So if A = [5, 17, 40, 6, 3, 8, 2], then it will be [40,40,8,8,8,2,-1]To solve this, we will follow these steps −We will read the array element from right to left.take e := -1for i := n – 1 to 0temp := ee := max between e and array[i]array[i] := tempreturn arrayExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){    cout

Advertisements