Found 7197 Articles for C++

Rectangle with minimum possible difference between the length and the width in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:09:52

290 Views

Given an area of rectangle as input. The goal is to find the sides of a rectangle such that the difference between the length and breadth is minimum.Area of Rectangle = length * breadth.ExamplesInput − Area = 100Output −Side of Rectangle with minimum difference:Length = 10, Breadth = 10Explanation − Sides with area = 100.2 - 50, 4 - 25, 5 - 20, 10 - 10. Sides with minimum difference are 10-10 with difference = 0. As we know tha square is a rectangle with equal length on all sides.Input − Area = 254Output − Side of Rectangle with minimum difference :Length = 127, Breadth ... Read More

Recursive program to insert a star between pair of identical characters in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:06:09

705 Views

Given a string str1 as input. The goal is to insert a ‘*’ between a pair of identical characters in the input string and return the resultant string using a recursive approach.If the input string is str1= "wellness" then output will be "wel*lnes*s"ExamplesInput − str1="happiness"Output − String after adding * : hap*pines*sExplanation − Adding * between pairs pp and ss will gives us resultant string hap*pines*sInput − str1=”swimmmmingggg pooool”Output − String after adding * : swim*m*m*ming*g*g*g po*o*o*olExplanation − Adding * between pairs mm, gg and oo will gives us resultant string swim*m*m*ming*g*g*g po*o*o*olApproach used in the below program is as followsIn this approach take string ... Read More

Recursive Approach to find nth node from the end in the linked list in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:59:53

586 Views

Given a Singly linked list and positive integer N as input. The goal is to find the Nth node from the end in the given list using recursion. If the input list has nodes a → b → c → d → e → f and N is 4 then the 4th node from the end will be c.We will first traverse till the last node in the list and while returning from recursion (backtracking) increment count. When count is equal to N then return pointer to current node as result.Let us see various input output scenarios for this −Input − ... Read More

Recursive approach for alternating split of Linked List in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:56:48

380 Views

Given a Singly linked list as input. The goal is to split the list into two singly linked lists that have alternate nodes of the original list. If the input list has nodes a → b → c → d → e → f then after the split, two sub-lists will be a → c → e and b → d → f.We will take two pointers N1 and N2 one pointing to the head of the original list and another pointing to the head → next. Now move both pointers to the next of next node and create sublists.ExamplesInput − ... Read More

Recursive insertion and traversal linked list in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:53:40

1K+ Views

We are given with integer values that will be used to form a linked list. The task is to firstly insert and then traverse a singly linked list using a recursive approach.Recursive addition of nodes at the endIf head is NULL → add node to headElse add to head( head → next )Recursive traversal of nodesIf head is NULL → exitElse print( head → next )ExamplesInput − 1 - 2 - 7 - 9 - 10Output − Linked list : 1 → 2 → 7 → 9 → 10 → NULLInput − 12 - 21 - 17 - 94 - 18Output − Linked list ... Read More

Recursive program to linearly search an element in a given array in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:18:37

2K+ Views

Given an integer array Arr[] containing integer numbers in any order. The goal is to find the input integer val present in the array using recursive search on the array.If val is not found in the input array Arr[] then return -1. Print the index of val if found in Arr[].ExamplesInput −Arr[] = {11, 43, 24, 50, 93, 26, 78} val=26Output − 26 found at index 5Explanation −Elements in the array start from index 0 to index=array length -1. First index=0 last index=6 : 11 != 26, 78 != 26 → 0+1 , 6-1 First index=1 last index=5 : 43 != 26, 26 ... Read More

Row-wise common elements in two diagonals of a square matrix in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:15:22

255 Views

Given a 2D square matrix as input. The goal is to find the elements that are common in both its primary and secondary diagonals. If the input matrix is1 2 3 2 2 4 1 4 7Then its primary diagonal is 1 2 7 and the secondary diagonal is 3 2 1. Common element is 2.There will always be at least one common element in both.ExamplesInput − Matrix[][5] = {{1, 2, 1}, {4, 1, 6}, {1, 8, 1}};Output − Row-wise common elements in diagonals:3Explanation − The matrix is:1 2 1 4 1 6 1 8 1Primary diagonal=1 1 1, Secondary diagonal= 1 1 ... Read More

Row-wise vs column-wise traversal of matrix in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:08:30

4K+ Views

A matrix can be traversed in two ways. Row-mise traversal visits each row one by one starting from first row then second and so on till the last row. Elements in the row are returned from index 0 to the last index.In Column-wise traversal, elements are traversed from the first column to the last column in order.In 2D matrix M[i][j]. Index i is used for representing rows and index j is used for representing columns. For row-wise traversal, start fromi=0th row and 0

Reduce a number to 1 by performing given operations in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:39:26

1K+ Views

Given an integer Number as input. The goal is to find the minimum number of steps or operations required to reduce the input Number to 1. Operations that can be performed will be-:If Number is even, then Divide it by 2.If Number is odd, then increment or decrement it by 1.ExamplesInput − Number=28Output − Minimum steps to reduce 28 to 1: 6Explanation−28 is even - divide by 2 = 1414 is even - divide by 2 = 77 is odd - increment by 1 = 88 is even - divide by 2 = 44 is even - divide by 2 = 22 ... Read More

Reduce the fraction to its lowest form in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:32:43

6K+ Views

Given two integers Num1 and Num2 as input. The integers can be represented as fraction Num1/Num2. The goal is to reduce this fraction to its lowest form.Using GCD to find highest denominatorWe will calculate the greatest common divisor of both numbers.Divide both numbers by that gcdSet both variables as quotient after division.Lowest fraction will be Num1/Num2.ExamplesInput − Num1=22 Num2=10Output − Num1 = 11 Num2 = 5Lowest Fraction : 11/5Explanation− GCD of 22 and 10 is 2.22/2=11 and 10/2=5Lowest fraction is 11/5Input− Num1=36 Num2=40Output− Num1 = 9 Num2 = 10Lowest Fraction : 9/10Explanation − GCD of 36 and 40 is 4.40/4=10 and 36/4=9Lowest ... Read More

Advertisements