We are given an array of elements. We need to find if it is possible to change the value of any one element to make the array elements consecutive. If not possible, return -1 ; otherwise, the element needs to be changed. Let's suppose we have an array {4, 3, 9, 5, 6} and we have to sort this given array. Then start from the smallest and largest element checking the number of mismatches. If the number of mismatches is more than 1 on both sides of the array, the answer is -1. Otherwise, it is possible to get the ... Read More
Given a double circular linked list and key, we have to search the key in the linked list and give a proper message if found. Suppose we have a linked list with the specific characters, and we have to search for an element in it. So let's start with the following linked list − 5 8 9 2 4 We will use 4 as a key to finding the solution to the given problem. A double-linked list has no fixed head, so we will start from any node and then mark that node as ... Read More
An ordered collection of data elements, each with a link to its next element (and sometimes its predecessor), Suppose there is a linked list, then we need to find the second smallest element. The below are the following scenarios. Let’s assume some simple input and output scenarios Let’s assume this scenario that we are having a linked list contains elements in it are "8->4->6->2->9, ". Then after iterating entire linked list, the second smallest element will be 8. Input = 8->4->6->2->9 Output = 8 Programing way of implementing the linked list Node* head = new Node(8); head->next = new ... Read More
We are given an array of words, and we need to find the word whose frequency is the second largest in the array. Let’s assume some simple input and output scenarios Let’s assume we are having an array which consists of elements like [“point, ” “world, ” “articles, ” “articles, ” “articles, ” “world, ” “world, ” “world, ” “point”]. The frequency of words are − “point”: 2 “world”: 4 “articles”: 3 // This will be the second most repeated word in the array. So the second most repeated word is “articles, ” and our output is “articles.” Let’s ... Read More
In this article, we are given integers in an array, and we must find the smallest number which is greater than 1 that divides all the elements in the array. For example, let us consider a sample array [30, 90, 15, 45, 165]. vector arr = {30, 90, 15, 45, 165}; result = solve(arr); Now we can find the array's GCD(greatest common divisor). If it comes out to be 1 that means that only 1 can divide the whole array, and we can return -1 or "Not possible." If it's an integer, then this integer divides the whole ... Read More
In this article, we are given a linked list that contains elements from 1 to n and duplicates. Elements 1 to n will always be there with duplicates from [1..n]. We need to replace every duplicate element with n+1, n+2, and so on. Lets consider an example 1→2→2→4→5→3→6→6 Next n = 42. So every duplicate is replaced with n+1, n+2, and so on. The next 42 is replaced with 47, and the next 46 is replaced with 48, leaving the first instance as it is. First of all, we need to construct a binary tree in the main method as ... Read More
In this method, we need to replace every element in the given matrix with the maximum Greatest Common Divisor (GCD) of that row and column. Let us look at some input scenarios − Suppose we are given a 2D matrix of dimensions m*n is; Input: [[3, 2, 1, 4] [7, 6, 2, 8] [14, 20, 25, 17]]; In the above matrix, row 1 gcd(3, 2, 1, 4) = 1 and Column 2 gcd(3, 7, 14) = 1. So element 2 (1st row and 2nd column) becomes the maximum (1, 1) = 1. So on for all elements and our ... Read More
A correlation matrix is a table containing correlation coefficients for many variables. Each cell in the table represents the correlation between two variables. The value might range between -1 and 1. A correlation matrix is used for summarizing the data, diagnose the advanced analysis, and as an input for a more complicated study. Correlation matrix is used to represent the relationship between the variables in the data set. It is a type of matrix that helps programmers analyze the relationship between data components. It represents the correlation coefficient between 0 and 1. A positive value implies a good correlation, a ... Read More
To find the least greater element on the right of an array of integers, we need to consider an array of integers. For each element, we need to find an element that is greater than the current element and is the least from all the candidate elements we have. Suppose we have elements such as [5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88]. vector arr = {5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88}; solve(arr); We can list our requirements and then think over the requirements to find a solution. We need a ... Read More
We are given a linked list, and we need to find elements in the given linked list which are greater and present in the right of the current element. The count of these elements needs to be substituted into the current node's value. Let's take a linked list with the following characters and replace each node with its Surpasser count − 4 -> 6 -> 1 -> 4 -> 6 -> 8 -> 5 -> 8 -> 3 From backward, traverse the linked list (so we do not need to worry about elements to the left of the current). ... Read More