Programming Articles - Page 2013 of 3363

Robot Bounded In Circle C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:54:21

337 Views

Suppose we have an infinite plane, a robot initially stands at position (0, 0) and faces north. The robot can receive one of three instructions −G − go straight 1 unit;L − turn 90 degrees to the left direction;R − turn 90 degrees to the right direction.The robot performs the instructions given in order, Instructions are repeated forever. We have to check whether there exists a circle in the plane such that the robot never leaves the circle. So if the input is like [GGLLGG], then the answer will be true. from (0, 0) to (0, 2), it will loop ... Read More

Minimum Score Triangulation of Polygon in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:49:19

379 Views

Suppose we have a value N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] are in clockwise order. Now suppose we want to triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation will be the sum of these values over all N-2 triangles in the triangulation. We have to find the smallest possible total score that we can achieve with some triangulation of the polygon. So if the input is [1, 2, 3], then the ... Read More

Uncrossed Lines in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:45:20

258 Views

Suppose we have written the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that −A[i] == B[j];The line we draw that does not intersect any other connecting (non-horizontal) line.We have to keep in mind that connecting lines cannot intersect even at the endpoints − each number can only belong to one connecting line. Find the maximum number of connecting lines. So if the input is like [1, 4, 2] and [1, 2, 4], then the output ... Read More

Longest Arithmetic Sequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:39:57

387 Views

Suppose we have an array A of integers, we have to return the length of the longest arithmetic subsequence in A. As you know that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0

Video Stitching in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:30:27

293 Views

Suppose we have a series of video clips from a sporting event that lasted T seconds. Now these video clips can be overlapping with each other and have varied lengths. Here each video clip clips[i] is an interval − it starts at clips[i][0] time and ends at clips[i][1] time. We can cut these clips into segments freely − We have to find the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event ([0, T]). If the task is impossible, return -1. So if the input is like [[0, 2], ... Read More

Number of Enclaves in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:24:45

249 Views

Suppose we have given a 2D array A, now each cell is 0 (representing sea) or 1 (representing land) Here a move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid. We have to find the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves. So if the grid is like −0000101001100000The answer will be 3, as there are three ones enclosed by 0s, and one 1 is not enclosed.To solve this, we will follow these ... Read More

Convert to Base -2 in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:19:24

1K+ Views

Suppose we have a number N, we have to find a string consisting of "0"s and "1"s that represents its value in base -2 (negative two). The returned string should have no leading zeroes, unless the string is exactly "0". So if the input is like 2, then the output will be “110”, as (-2)^2 + (-2)^1 + (-2)^0 = 2.To solve this, we will follow these steps −ret := an empty stringif N = 0, then return “0”while N is non 0rem := N mod (– 2)N := N / (-2)if rem < 0 and rem := rem + ... Read More

Binary String With Substrings Representing 1 To N in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:16:22

296 Views

Suppose we have a binary string S and a positive integer N, we have to say true if and only if for every integer X from 1 to N, the binary representation of X is a substring of the given S. So if S = “0110” and N = 3, then the result will be true, as 1, 10 and 11 all are present in 0110.To solve this, we will follow these steps −Define a method to convert(), that will take n as inputret := an empty stringwhile n is not 0ret := ret concatenate n mod 2n := n ... Read More

How can we modify an existing module in Java 9?

raja
Updated on 10-Apr-2020 17:24:53

589 Views

The module is a named, self-describing collection of code and data. The code has been organized as a set of packages containing types like Java classes and interfaces. The data includes resources and other kinds of static information. We need to declare a module then add module-info.java at the root of the source code.Below is the template of the "module-info.java" file.module {    requires ;    requires ;    exports ;    exports ;    exports to }We can use certain command-line options that help us to modify existing modules and add dependencies to them, export ... Read More

What are the different "/vars" commands in JShell in Java 9?

raja
Updated on 10-Apr-2020 13:48:38

508 Views

JShell is an interactive command-line tool introduced in Java 9. It is also called a REPL tool that takes input, evaluates it, and prints output to the user.In the JShell tool, it's possible to list all variables created by using the internal command "/vars". We have different "/vars" commands available in the JShell tool as listed below./vars /vars [ID] /vars [Variable_Name] /vars -start /vars -all/vars: This command allows to us display the list of all active variables of the current session./vars [ID]: This command displays the variable and its value, corresponding to the entered ID. This ID corresponds to the name of ... Read More

Advertisements