sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 31 of 98

Find the nth term of the given series 0, 0, 2, 1, 4, 2, 6, 3, 8, 4… in C++

sudhir sharma
sudhir sharma
Updated on 24-Jan-2022 513 Views

In this problem, we are given an integer value N. Our task is to Find the nth term of the given series −0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8, 18, 9, 20, 10… Let’s take an example to understand the problem, Input − N = 6 Output − 2Solution ApproachTo find the Nth term of the series, we need to closely observe the series. It is the mixture of two series and odd and even terms of the series. Let’s see each of them, At even positions −T(2) = 0T(4) ...

Read More

Practice Questions on Time Complexity Analysis in C++

sudhir sharma
sudhir sharma
Updated on 02-Aug-2021 10K+ Views

Time complexity of any algorithm is the time taken by the algorithm to complete. It is an important metric to show the efficiency of the algorithm and for comparative analysis. We tend to reduce the time complexity of algorithm that makes it more effective.Example 1Find the time complexity of the following code snippetsfor(i= 0 ; i < n; i++){    cout

Read More

Find pairs with given sum in doubly linked list in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 401 Views

In this problem, we are given a doubly linked list and a value sum. Our task is to find pairs with a given sum in a doubly linked list.Let’s take an example to understand the problem, Inputhead − 2 5 6 9 12 x = 11Output(2, 9), (5, 6)ExplanationFor pairs (2, 9), the sum of values is 11 For pairs (5, 6), the sum of values is 11Solution ApproachA simple solution to the problem is traversing the whole linked-list and taking elements one by one and finding the element in the remaining linked list whose sum ...

Read More

Find speed of man from speed of stream and ratio of time with up and down streams in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 187 Views

In this problem, we are given two values S and N denoting the speed of stream in Km/h and ratio of time with up and down streams. Our task is to Find speed of man from the speed of stream and ratio of time with up and down streams.Let’s take an example to understand the problem, InputS = 5, N = 2Output15Solution ApproachA simple solution to the problem is by using the mathematical formula for the rowing problems. So, let’s see how the formula will work −speed of man = x km/h speed of stream = S km/h speed of ...

Read More

Find smallest permutation of given number in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 872 Views

In this problem, we are given a large number N. Our task is to find the smallest permutation of a given number.Let’s take an example to understand the problem, InputN = 4529016Output1024569Solution ApproachA simple solution to the problem is by storing the long integer value to a string. Then we will sort the string which is our result. But if there are any leading zeros, we will shift them after the first non zero value.Program to illustrate the working of our solution, Example Live Demo#include using namespace std; string smallestNumPer(string s) {    int len = s.length();    sort(s.begin(), s.end()); ...

Read More

Find smallest number with given number of digits and sum of digits in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 1K+ Views

In this problem, we are given two values that are the sum (denoting the sum of digits) and digit (denoting the number of digits). Our task is to find the smallest number with a given number of digits and sum of digits.Let’s take an example to understand the problem, Inputsum = 15, dgiti = 2Output69ExplanationAll 2 digits numbers with sum 15 are : 69, 78, 87, 96.Solution ApproachA simple solution to the problem is by considering all the numbers with digit count as digit and find the smallest number whose sum of digit is equal to the sum.An efficient solution ...

Read More

Find smallest and largest elements in singly linked list in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 983 Views

In this problem, we are given a singly linked list. Our task is to find the smallest and largest elements in single linked list.Let’s take an example to understand the problem, Inputlinked List : 5 -> 2 -> 7 -> 3 ->9 -> 1 -> 4OutputSmallest element = 1 Largest element = 9Solution ApproachA simple solution to the problem is using by traversing the linked list node by node. Prior to this, we will initialise maxElement and minElement to the value of the first element i.e. head -> data. Then we will traverse the linked list element by element. And ...

Read More

Find smallest and largest element from square matrix diagonals in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 1K+ Views

In this problem, we are given a square matrix of size nXn. Our task is to Find smallest and largest element from square matrix diagonals. We need to find the smallest and largest elements of the primary and secondary diagonals of the matrox.Let’s take an example to understand the problem, Inputmat[][] = {    {3, 4, 7},    {5, 2, 1},    {1, 8, 6} }OutputSmallest element in Primary Diagonal = 2 Largest element in Primary Diagonal = 6 Smallest element in Secondary Diagonal = 1 Largest element in Secondary Diagonal = 7Solution ApproachA simple solution to solve the problem ...

Read More

Find single Movement in a Matrix in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 176 Views

In this problem, we are given four values x1, y1, x2, y2 denoting two points (x1, y1) and (x2, y2). Our task is to find single movement in a Matrix. We need to find the direction using which we can move from one point (x1, y1) to (x2, y2). There can be any number of moves by the direction needed to be single and we need to return the direction in the form - “left” , “right”, “up”, “down”. Otherwise return -1, denoting “not possible”.Let’s take an example to understand the problem, Inputx1 = 2, y1 = 1, x2 = ...

Read More

Find size of the largest &lsquo;+&rsquo; formed by all ones in a binary matrix in C++

sudhir sharma
sudhir sharma
Updated on 16-Mar-2021 270 Views

In this problem, we are given an NxN binary matrix bin[][]. Our task is to find the size of the largest ‘+’ formed by all ones in a binary matrix.Let’s take an example to understand the problem, Input0 1 1 1 1 1 0 1 0Output5Solution ApproachA simple solution to the problem is to find the largest ‘+’ for which we need to find the maximum number of 1’s in one direction for a point in the matrix which needs to be the same in all four directions for a given 1. For this, we will create one matrix for ...

Read More
Showing 301–310 of 975 articles
« Prev 1 29 30 31 32 33 98 Next »
Advertisements