Found 26504 Articles for Server Side Programming

C++ Partition a Number into Two Divisible Parts

Prateek Jangid
Updated on 25-Nov-2021 10:09:04

263 Views

In this problem, we are given a string that can be interpreted as a number. Now we have to perform the partition that string into two parts such that the first part is divisible by A and the second part is divisible by B (two integers given to us). For example −Input : str = "123", a = 12, b = 3 Output : YES 12 3 "12" is divisible by a and "3" is divisible by b. Input : str = "1200", a = 4, b = 3 Output : YES 12 00 Input : str = ... Read More

C++ Partitioning a Linked List Around a Given Value and Keeping the Original Order

Prateek Jangid
Updated on 25-Nov-2021 10:07:04

269 Views

Given a linked list in this tutorial, and we need to keep all the numbers smaller than x at the start of the list and the others at the back. We also need to retain their order the same as previously. for exampleInput : 1->4->3->2->5->2->3, x = 3 Output: 1->2->2->3->3->4->5 Input : 1->4->2->10 x = 3 Output: 1->2->4->10 Input : 10->4->20->10->3 x = 3 Output: 3->10->4->20->10To solve this problem, we need to make three linked lists now. When we encounter a number smaller than x, then we insert it into the first list. Now for a value equal ... Read More

C++ Pandigital Number in a Given Base

Prateek Jangid
Updated on 25-Nov-2021 10:04:57

587 Views

A number that contains all the digits from 0 to base B is called the Pandigital number in that base. However, some numbers have digits from 1 to 9 and are called zeroless pandigital numbers. Some Examples of pandigital numbers are 0123456789, 0789564312, etc.In this tutorial, we will discuss a problem where we are given a number and a base, and we need to check whether the number is pandigital in the given base or not, for example −Input: num = “9651723467380AZ”, base = 10 Output: YES Explanation: num contains all the digits in the base 10 i.e from 0 ... Read More

C++ Pairwise Swap Elements of a Given Linked List

Prateek Jangid
Updated on 25-Nov-2021 09:59:09

416 Views

To solve a problem in which we are required to swap the pairwise nodes present in a linked list and then print it, for exampleInput : 1->2->3->4->5->6->NULL Output : 2->1->4->3->6->5->NULL Input : 1->2->3->4->5->NULL Output : 2->1->4->3->5->NULL Input : 1->NULL Output : 1->NULLThere are two ways to approach the solution both to have a time complexity of O(N), where N is the size of our provided linked list, so now we are going to explore both of the approachesIterative ApproachWe will iterate through the linked list elements in this approach, and pairwise swap them until they ... Read More

C++ Pairwise Swap Leaf Nodes in a Binary Tree

Prateek Jangid
Updated on 25-Nov-2021 09:55:10

326 Views

Given a binary tree. The task is to pairwise swap the leaf nodes, for example −Input −Output −We will keep track of two pointers that point to the two adjacent leaf nodes and swap their values in the given problem.Approach to Find the SolutionIn this approach, we traverse the tree, find the leaf nodes, and keep track of our counter to check the current count. The main trick is that our counter is odd, so our first pointer points to that node now. When our counter becomes even, we swap the data, and hence our leaf nodes are swapped.ExampleC++ Code ... Read More

C++ Largest Subset with Sum of Every Pair as Prime

Prateek Jangid
Updated on 25-Nov-2021 09:52:03

232 Views

To find the largest subset from the given array in which the sum of every pair is a prime number. Assuming maximum element is 100000, for example −Input: nums[ ] = { 3, 2, 1, 1 } Output: size = 3, subset = { 2, 1, 1 } Explanation: Subsets can be formed: {3, 2}, {2, 1} and { 2, 1, 1}, In {2, 1, 1} sum of pair (2, 1) is 3 which is prime, and the sum of pairs (1, 1) is 2 which is also a prime number. Input: nums[ ] = {1, 4, 3, 2} ... Read More

C++ Largest Subtree having Equal No of 1's and 0's

Prateek Jangid
Updated on 25-Nov-2021 09:49:47

185 Views

Given a binary tree. Now we are tasked to find the largest subtree having an equal number of 1's and 0's; the tree only contains 0's and 1's.Approach to Find the SolutionIn this approach, we are going to replace all the nodes with values of 0 to -1. Doing this will make our program simpler as we now need to find the largest subtree with a sum equal to 0.ExampleC++ Code for the Above Approach  #include using namespace std; int maxi = -1; struct node { // structure of our tree node     int data;     struct ... Read More

C++ Program to find the Largest Divisible Subset in Array

Prateek Jangid
Updated on 25-Nov-2021 09:48:55

293 Views

This tutorial will discuss a problem where we are given an array of distinct positive integers. We need to find the largest subset such that for every pair larger element is divided by a smaller element, for example −Input: nums[ ] = { 1, 4, 2, 6, 7} Output: 1 2 4 Explanation: All Divisible subsets are: (1, 2, 4), (1, 2, 6), (1, 7), etc We have 2 subsets of length 3 in which all the pairs satisfy the condition. Input: nums[ ] = { 1, 2, 3, 6 } Output: 6 2 1Approach to Find the SolutionThere ... Read More

C++ Program to find the Largest Divisible Pairs Subset

Prateek Jangid
Updated on 25-Nov-2021 09:44:14

216 Views

To solve a problem in which we are given an array consisting of distinct elements. Now our task is to find the subset such that every pair is evenly divisible, i.e, every large element is divisible by every smaller element, for example.Input : arr[] = {10, 5, 3, 15, 20} Output : 3 Explanation: The largest subset is 10, 5, 20. 10 is divisible by 5, and 20 is divisible by 10. Input : arr[] = {18, 1, 3, 6, 13, 17} Output : 4 Explanation: The largest subset is 18, 1, 3, 6, In the subsequence, 3 is ... Read More

Numbers that are Bitwise AND of At Least One Non-Empty Sub-Array using C++

Prateek Jangid
Updated on 25-Nov-2021 09:39:39

157 Views

To solve a problem where we are given an array, and we need to find all possible integers which are bitwise AND of at least one not empty subarray, for example −Input : nums[ ] = { 3, 5, 1, 2, 8 } Output : { 2, 5, 0, 3, 8, 1 } Explanation: 2 is the bitwise AND of subarray {2}, 5 is the bitwise AND of subarray {5}, 0 is the bitwise AND of subarray {1, 2}, {2, 8} and {1, 2, 8}, 3 is the bitwise AND of subarray {3}, 8 is the bitwise AND of subarray ... Read More

Advertisements