Flipped Matrix Prequel in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:40:41

270 Views

Suppose we have one binary matrix. We have to find the maximum number of 1s we can get if we flip a row and then flip a column.So, if the input is like101010100then the output will be 8To solve this, we will follow these steps −n := size of rows in matrixm := size of columns in matrixret := 0Define an array row of size nDefine an array col of size ntotal := 0for initialize i := 0, when i < n, update (increase i by 1), do −for initialize j := 0, when j < m, update (increase j ... Read More

Furthest From Origin in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:38:02

680 Views

Suppose we have a string s where each character is either "L", "R" or "?". "L" means moved one unit left, "R" means moved one unit right, and "?" means either "L" or "R". If we are at position 0, we have to find the maximum possible distance we could be from 0 by replacing "?" with "L" or "R".So, if the input is like "LLRRL??", then the output will be 3, replace ? using L to move 5 units left and 2 units right, so maximum displacement is 3.To solve this, we will follow these steps −op := 0, ... Read More

Flip to Zeros in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:36:25

404 Views

Suppose we have one integer array called nums and this contains 0s and 1s. Suppose we have an operation where we pick an index i in nums and flip element at index i as well as all numbers to the right of i. We have to find the minimum number of operations required to make nums contain all 0s.So, if the input is like [1, 0, 1], then the output will be 3, operation on index 0, it will convert [0, 1, 0], then on index 1 [0, 0, 1], then index 2, [0, 0, 0].To solve this, we will ... Read More

Split List in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:33:00

1K+ Views

Suppose we have a list of integers called nums, we have to find whether we can partition the list into two sublists (non-empty) such that every number in the left part is strictly less than every number in the right part.So, if the input is like [6, 4, 3, 8, 10], then the output will be true, as left = [6, 4, 3] and right = [8, 10]To solve this, we will follow these steps −n := size of numsDefine an array right of size nDefine an array left of size nleft[0] := nums[0]last element of right := last element ... Read More

Contained Interval in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:31:05

381 Views

Suppose we have a two-dimensional list of intervals where each interval has two values [start, end]. We have to find whether there's an interval which contains another interval.So, if the input is like [[2, 4], [5, 11], [5, 9], [10, 10]], then the output will be true as [5, 11] is containing [5, 9].To solve this, we will follow these steps −sort the array vDefine one 2D array retfor each interval it in v −if ret is empty, then −insert it at the end of retotherwise when last element of ret >= it[0], then −return trueOtherwiseinsert it at the end ... Read More

Make List Values Equal in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:28:51

167 Views

Suppose we have a list of integers called nums. Now suppose an operation where we select some subset of integers in the list and increment all of them by one. We have to find the minimum number of required operations to make all values in the list equal to each other.So, if the input is like [1, 3, 5], then the output will be 4.To solve this, we will follow these steps −if size of nums is same as 1, then −return 0ret := 0maxVal := -infminVal := inffor initialize i := 0, when i < size of nums, update ... Read More

Longest Interval Containing One Number in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:27:25

168 Views

Suppose we have a list of distinct integers called nums. We have to find the size of the largest interval (inclusive) [start, end] such that it contains at most one number in nums.So, if the input is like nums = [10, 6, 20], then the output will be 99990, as the largest interval is [11, 100000], this contains 20 only.To solve this, we will follow these steps −ret := -infend := 100000prev := 1sort the array numsn := size of numsfor initialize i := 0, when i < size of nums, update (increase i by 1), do −if i + ... Read More

Is an Empty iframe src Valid in JavaScript

AmitDiwan
Updated on 02-Sep-2020 06:37:59

1K+ Views

For empty iframe src, use the element and set it like the following for an empty src −Example Live Demo Document To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Transform Object of Objects to Object of Array of Objects in JavaScript

AmitDiwan
Updated on 02-Sep-2020 06:19:44

765 Views

To transform object of objects to object of array of objects, use the concept of Object.fromEntries() along with map().Exampleconst studentDetails = {    'details1': {Name: "John", CountryName: "US"},    'details2': {Name: "David", CountryName: "AUS"},    'details3': {Name: "Bob", CountryName: "UK"}, }; console.log(    Object.fromEntries(Object.entries(studentDetails).map(([key,    value]) => [key, [value]])) );To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo45.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo45.js {    details1: [ { Name: 'John', CountryName: 'US' } ],    details2: [ { Name: 'David', CountryName: 'AUS' } ],    details3: ... Read More

Why HTML File Can't Find JavaScript Function from Sourced Module

AmitDiwan
Updated on 02-Sep-2020 06:14:29

2K+ Views

This may happen if you haven’t used “export” statement. Use “export” before the function which will be imported into the script file. The JavaScript file is as follows which has the file name demo.js.demo.jsconsole.log("function will import"); export function test(){    console.log("Imported!!!"); }Here is the “index.html” file that imports the above function −index.htmlExample Live Demo Document    import { test } from "./demo.js"    test(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS ... Read More

Advertisements