Found 7347 Articles for C++

Write a program in C++ to insert a Node at the beginning of the given Singly linked list

Dev Prakash Sharma
Updated on 05-Feb-2021 12:43:04

6K+ Views

A linked List is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields Data Field and address of the next node.Let us assume we have given a singly linked list the task is to insert a node at the beginning of the given linked list. For example, Input-1 − 1 → 2 → 3 → 4Insert ‘5’ at the head or beginning of the given linked list.Output − 5 → 1 → 2 → 3 → 4Explanation − After inserting the node at the beginning of the linked list ... Read More

What are pandigital numbers. Approach to find the pandigital Numbers using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:36:00

379 Views

Pandigital Number − In Mathematics, a Pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once.Pandigital numbers are the integers in which each digit is used as the base at least one time.For example, 1245678 is a pandigital number.Approach to solve this problemTake Input a number and a base.Check the base if it is less than 2 and greater than 10 then return 1 otherwise check the number if it is pandigital or not.An Integer function is_pandigital(long long n, int base) takes a number and a ... Read More

Check if two strings are anagram of each other using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:35:32

2K+ Views

Let’s Suppose we have given two strings ‘a’ and ‘b. We have to check that the given two strings are anagram of each other or not. Two Strings are said to be anagram of each other if one string contains the same character as another.For ExampleInput-1 −a= anagram b= gnaramaOutput −TrueExplanation − String ‘gnarama’ has the same character as String ‘anagram’ has. Hence we return True.Input-2 −a= programmer b= mprogretmrqpOutput −FalseExplanation − String ‘b’ has more characters than the string ‘a’ and thus we can say that the length of the string is different. Thus we return False.The approach used ... Read More

Given a sorted array of 0’s and 1’s, find the transition point of the array in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:34:33

521 Views

Given an array of sorted numbers containing only 0s and 1s, find the transition point. A transition point is the index of the first ‘1’ appearing in the array. For example, Input-1 −N = 6 arr[ ] = {0, 0, 0, 0, 1, 1}Output −4Explanation − Since in the given array containing 0’s and 1’s we can see that the element at the index ‘4’ is having the number ‘1’.Input-2 −N = 5 arr[ ] = {0, 0, 1, 1, 1}Output −2Explanation − In the given array containing 0’s and 1’s, we can see that the element at the index ... Read More

Write a program in C++ to find the top K frequent element in an array of integers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:22:16

818 Views

Suppose we have an array of integers of size N and a key K. Our task is to print the top K most frequent element of the array. For example, Input-1 −N = 6 K = 2 arr[ ] = {1 ,1, 1, 2, 2, 3}Output −1 2Explanation − In the given array of integers, the top K=2 elements whose frequency is most in the array are {1, 2}.Input-2 −N = 2 K = 1 arr[ ] = {1, 2}Output −1Explanation − In the given array of integers, the top K=1 elements whose frequency is most in the array are ... Read More

Write a program in C++ to find the most frequent element in a given array of integers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:21:32

3K+ Views

Let’s suppose we have an array of integers of size N. The task is to find the most frequent element present in the given array of integers. For example, Input-1 −N = 8 A[ ] = {1, 2, 4, 3, 3, 1, 1, 5}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus the output is ‘1’.Input-2 −N = 6 A[ ] = {1, 4, 4, 4, 1, 1}Output-a −1Output-b −4Explanation: In the given array of integers, the most appearing number is ‘1’ and ‘4’. Thus we can return the output to any one ... Read More

Write a program in C++ to find the missing positive number in a given array of unsorted integers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:15:36

357 Views

Let’s suppose we have given an array of unsorted integers. The task is to find the positive missing number which is not present in the given array in the range [0 to n]. For example, Input-1 −N = 9 arr = [0, 2, 5, 9, 1, 7, 4, 3, 6]Output −8Explanation − In the given unsorted array, ‘8’ is the only positive integer that is missing, thus the output is ‘8’.Input-2  −>N= 1 arr= [0]Output −1Explanation − In the given array, ‘1’ is the only positive integer that is missing, thus the output is ‘1’.Approach to solve this problemThere are ... Read More

Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:09:55

4K+ Views

Let’s suppose we have given an array of unsorted integers of size N. The task is to find the distinct max and second max element which are present in the array. The array may contain duplicate elements also. So we have to find only distinct elements. For example, Input-1 −N = 5 A[ ] = { 2, 2, 1, 3, 4 }Output −4 3Explanation − From the given array, we can see ‘4’ is the maximum and ‘3’ is the second maximum.Input-2 −N = 4 A[ ] = { 1, 3, 3, 2 }Output −3 2Explanation − from the given ... Read More

Write a program in C++ to find the length of the largest subarray with zero sum

Dev Prakash Sharma
Updated on 05-Feb-2021 12:09:17

223 Views

Suppose we have given an array of N integers having the task is to find the length of the subarray having a maximum length. If there is not any subarray whose length is maximum or sum is equal to 0 then return ‘0’. For example, Input-1 −N = 8 A[ ] = {15, -5, -1, 5, 1, 4 }Output −4Explanation − The largest subarray with zero-sum is { -5, -1, 5, 1} which is having a length of 4.Input-2 −N = 5 A[ ] = {3, 2 ,4, 8, -1}Output −0Explanation − Since there are no subarrays present whose sum ... Read More

Find the Kth Node from the end in a given singly Linked List using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:08:35

382 Views

A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields Data Field and address of the next node. Let us assume we have given a singly linked list the task is to find the kth node from the end in a given singly linked list. For example, Input −1→2→3→4→7→8→9 K= 4Output −Node from the 4th Position is − 4Explanation − Since in the given singly linked list ‘4th’ node from the end is ‘4’, we will return the output as ‘4’.Approach to solve this problemInitially, we ... Read More

Advertisements