Sort Only Columns of a 2D Array in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:34:51

820 Views

We are required to write a JavaScript function that takes in a two-dimensional array of integers as the only argument.The function should sort the elements present at the column of the array in increasing or decreasing order.For example −If the input array is −const arr = [    [6, 2, 9],    [8, 1, 4],    [5, 3, 7] ];Then the array should be sorted like this −const output = [    [8, 3, 9],    [6, 2, 7],    [5, 1, 4] ];ExampleFollowing is the code −const arr = [    [6, 2, 9],    [8, 1, 4],   ... Read More

Substring in Infinitely Extended String in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:32:06

183 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument and a start index and end index as second and third argument respectively. The function should find, had that string, provided as the first argument, been extended forever by appending the same string at end each time, what would have been the substring encapsulated by the start index and the end index.For example −If the input string and the indices are −const str = 'helloo'; const start = 11; const end = 15;Then the output should be −const output = 'hel';ExampleFollowing ... Read More

Find Index of Extra Element in Sorted Array in C++

sudhir sharma
Updated on 22-Jan-2021 14:08:10

743 Views

In this problem, we are given two sorted arrays arr1 and arr2 of size n  and n+1 with all elements the same except the extra element. Our task is to find index of an extra element present in one sorted array. Problem Description: We need to find the index of an element from the n+1 size array which is not present in an array of size n.Let’s take an example to understand the problem, Input:         arr1[n]  = {3, 5, 7, 8, 9, 12}                   arr2[n+1] = {3, 4, 5, 7, 8, 9, ... Read More

Find If Two People Ever Meet After Same Number of Jumps in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:52

320 Views

In this problem, we are given four integers denoting the starting points and jumps taken by each in the race. Our task is to find if two people ever meet after same number of jumps.  Problem Description: Here, we need to check if two persons starting at points p1 and p2 taking jumps j1 and j2 will be at some point in the path or not.Let’s take an example to understand the problem, Input: p1 = 5, p2 = 9, j1 = 4, j2 = 2Output: YesExplanation:After first jump, p1 = 9, p2 = 11After second jump, p1 = 13, p2 = 13Solution Approach: For ... Read More

Find Subset of Size K with 0 Sum in Array of 1 and +1 in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:39

194 Views

In this problem, we are given an array arr[] consisting of only 1 and -1 and an integer value k. Our task is to find if there is any subset of size K with 0 sum in an array of -1 and +1. Let’s take an example to understand the problem, Input: arr[] = {-1, 1, -1, -1, 1 , 1, -1}, k = 4Output: YESExplanation:Subset of size 4, {-1, 1, -1, 1}. Sum = -1 + 1 - 1 + 1 = 0Solution Approach: We need to check if there exists any subset of size k whose sum is equal to 0. As ... Read More

Find if K Bookings are Possible with Given Arrival and Departure Times in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:24

343 Views

In this problem, we are given two arrays  consisting of N values denoting arrival and departure at hotel and an integer k. Our task is to find if k bookings are possible with given arrival and departure times. Problem Description: Here, we need to check if the hotel with k rooms is able to accommodate all arrivals and departures.Let’s take an example to understand the problem, Input:         Arrivals :     {1 4 5 7}Departures : {3 5 6 9}             K = 1Output: YesSolution approach:To solve the problem, we will store arrival and departure for ... Read More

Find Pair in Root to Leaf Path with Sum Equals to Node Data in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:11

147 Views

In this problem, we are given a Binary Tree. And we need to find if there is a pair in root to a leaf path with sum equals to root’s data. We need to check if there exists a pair of nodes that lies between root node to leaf nodes such that the sum of values of pairs is equal to the root node’s value.Let’s take an example to understand the problem,   Input:Output: YesExplanation: Root node value 7Pairs with sum value equal to root node, (2, 5), (1, 6).Solution Approach: We need to traverse the tree and find the pairs using hashing.For this we ... Read More

Find Subarray with 0 Sum in C++

sudhir sharma
Updated on 22-Jan-2021 14:05:59

413 Views

In this problem, we are given an array arr[] of size n consisting of integer values. Our task is to find if there is a subarray with 0 sum. We need to check whether the given array contains a sub-array in which the sum of all elements is equal to 0.Let’s take an example to understand the problem, Input: arr[] = {3, 1, -2, 1, 4, 5}Output: YesExplanation: Subarray {1, -2, 1} has the sum of all values equal to 0.Solution Approach: A simple solution to the problem by considering all subarrays and checking the sum of all elements is equal to 0.Another solution to ... Read More

Check if Given Matrix is Toeplitz in C++

sudhir sharma
Updated on 22-Jan-2021 14:02:51

282 Views

In this problem, we are given a 2D square matrix mat[][] of size n*n. Our task is to find if the given matrix is Toeplitz or not.Toeplitz matrix also known as diagonal matrix is a matrix in which the elements at the diagonal start from top-left corner to bottom-right corner.Let’s take an example to understand the problem, Input:          Mat[][] = {{3, 5, 1},                            {4, 3 ,2},                            {1, 2, 3}}Output: YesExplanation:  Diagonal : ... Read More

Find if Given Number is Sum of First n Natural Numbers in C++

sudhir sharma
Updated on 22-Jan-2021 14:02:30

406 Views

In this problem, we are given a number num. Our task is to find if the given number is the sum of first n natural numbers.  Problem Description: Here, we need to check whether the given number is the sum of first n natural numbers.Let’s take an example to understand the problem, Input: num = 55Output: yes, 10Explanation:55 is the sum of the first 10 natural numbers, 1+2+3+4+5+6+7+8+9+10.Solution Approach: A simple approach to solving the problem is finding the sum of n natural numbers until it becomes equal to or greater than num.If the sum is equal to num, return n.If at any value of ... Read More

Advertisements