Found 7347 Articles for C++

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

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

276 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

1K+ 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

176 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

901 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

5K+ 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

Reduce the array to a single integer with the given operation in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:30:11

148 Views

Given an integer variable Number as input. Let us consider an array containing elements in the range 1 to Number in any order. If we perform an operation Number-1 times on an array such thatWe select two elements A and B from the arrayRemove A and B from arrayAdd sum of squares of A and B to the arrayWe will get a single integer value in the end; the goal is to find the maximum possible value for that element.Using Priority QueueIn order to maximize the end result, we will have to choose A and B such that they are ... Read More

Reduce the array to a single element with the given operation in C++

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

178 Views

Given an integer variable Number as input. Let us consider an array containing elements in the range 1 to Number in sorted order. If we perform an operation on an array such that at each step the elements at odd positions are removed. Then the goal is to perform this operation N number of times till only a single element is left. Print that element at the end.Note-: The positioning of elements is such that the array at index 0 is at 1st position and so on.Test Cases for Number of elements in arrayInput Number=1, output = 1Input Number=2, output ... Read More

Recursive Selection Sort in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:14:49

6K+ Views

Selection Sort is one of the sorting algorithms used to sort data by iterating an array from the beginning and replacing each element with the smallest element in the list. As we move forward the left array is sorted, and the right array is unsorted. Each move places the next smallest to the current position of the index by swapping.Selection Sort Algorithmint arr[5]= { 5,4,2,1,3 };int i, j ;Traverse from index i=0 to i

Advertisements