Found 7347 Articles for C++

Find the index of the first unique character in a given string using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:07:52

2K+ Views

Given a string ‘s’, the task is to find the first unique character which is not repeating in the given string of characters and return its index as output. If there are no such characters present in the given string, we will return ‘-1’ as output. For example, Input-1 −s = “tutorialspoint”Output −1Explanation − In the given string “tutorialspoint”, the first unique character which is not repeating is ‘u’ which is having the index ‘1’. Thus we will return ‘1’ as output.Input-2 −s = “aaasttarrs”Output −-1Explanation − In the given string “aaasttarrs’, there are no unique characters. So, we will ... Read More

Detect Capital in a given string using C++

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

454 Views

Let's suppose we have a string ‘str’ which consists of some character in it. The task is to check whether the given string has all its characters capitalized or not and return True or False respectively. For example, Input-1 −str = “INDIA”Output −TrueExplanation − Since all the character of the input string is capital, we will return true in this case.Input-2 −str = “Programmer”Output −FalseExplanation − Since all the characters of the input string are not in the capital except the first letter, we will return false in this case.The approach used to solve this problemIn the given string, we ... Read More

Delete a tail node from the given singly Linked List using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:06:46

2K+ Views

A Linked List is a linear Data Structure that contains nodes and each node has two fields; one is the value or data to be inserted and the other field stores the address of the next node.Our task here is to delete a node from the end of a Linked List. The last node is known as the tail node. If there is no node in the Linked List, then return NULL.For example −Input 1 − 1 → 2 → 3 → 4 → 5Output − 1 → 2 → 3 → 4 →Explanation − In the given singly linked ... Read More

Counting elements in two arrays using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 07:16:38

510 Views

Let us assume that we have given two unsorted arrays arr1[] and arr2[]. The task is to count the total number of elements in arr2[] for which each element of arr1[] are less than or equal to the elements present in arr2[]. However, the element in both the array may contain duplicates too.For example, Input-1 −N = 6 M = 7 arr1[N] = {1, 2, 5, 0, 6, 3} arr2[M] = {0, 0, 1, 2, 1, 3, 4, 6, 8}Output −4 5 7 2 8 6The approach used to solve this problemTo count every element of arr1[] and check if ... Read More

Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’

Dev Prakash Sharma
Updated on 05-Feb-2021 11:59:57

357 Views

Let’s suppose we have given a length of string ‘str’ and a string. The task is to count the number of substrings that starts with ‘1’ and ends with ‘1’ in a given Binary String. A Binary String contains ‘1’ and ‘0’ only. For example, Input-1 −N = 5 str = ‘11101’Output −6Explanation − In the given Binary String, we have 6 substrings that starts with ‘1’ and ends with ‘1’. The set of these substrings are {‘11’, ‘111’, ‘1110’, ‘11101’, ‘1101’, ‘101’}.Input-1 −N = 4 str = ‘0011’Output −1Explanation −In the given Binary String we have 1 substring that ... Read More

Write a program in C++ to check if a string can be obtained by rotating another string by two places

Dev Prakash Sharma
Updated on 05-Feb-2021 11:55:51

405 Views

Suppose we’ve given two strings ‘a’ and ‘b’, the task is to find whether we can obtain the string ‘b’ by rotating string ‘a’ exactly by two places in an anticlockwise or clockwise direction. For example, Input-1 −a = google b = legoogOutput −TrueExplanation − String ‘google’ can be rotated in an anticlockwise direction by two places, which results in the string ‘legoog’. Thus, we return True.Input-2 −a = tuorialst b = tutorialsOutput −FalseExplanation- String ‘tuorialst’ cannot be rotated by two places in any direction to get another string ‘tutorials’. Thus, we return False.The approach used to solve this problemFor ... Read More

C++ Program to Delete the First Node in a given Singly Linked List

Dev Prakash Sharma
Updated on 05-Feb-2021 11:54:11

8K+ 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 the address of the next node.Let us assume we have a singly linked list and we need to delete the first node from this linked list. For example, Input 1 − 4 → 3 → 2 → 1Output − 3 → 2 → 1 →Explanation − ‘4’ is the first node in the given singly linked list. After deleting the first node, the linked list will be 3→2→1.Input 2 − 1 → ... Read More

Birthday Paradox in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 11:47:09

555 Views

The birthday paradox is a very famous problem in the section of probability. The problem statement of this problem is stated as, There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate no. of people at a birthday party on the basis of having the same birthday.In the probability we know that the chance of getting ahead is 1/2 same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept, The chance of two people having the different birthday ... Read More

Find any one of the multiple repeating elements in read only array in C++

Hafeezul Kareem
Updated on 01-Feb-2021 12:22:06

73 Views

In this tutorial, we are going to write a program that finds the repeating element in the given array.Let's see the steps to solve the problem.Initialize the array.Initialize a counter map to store the frequency of each element in the array.Iterate over the array.Count each element.Print the element whose frequency is greater than 1.ExampleLet's see the code. Live Demo#include using namespace std; int findRepeatingElement(int arr[], int n) {    map frequencies;    for (int i = 0; i < n; i++) {       map::iterator itr = frequencies.find(arr[i]);       if (itr != frequencies.end()) {       ... Read More

Find amount of water wasted after filling the tank in C++

Hafeezul Kareem
Updated on 01-Feb-2021 12:18:23

233 Views

In this tutorial, we are going to solve the following problem.Given a tank with a capacity of N liters and a pump that fill the tank with S speed per minute. Unfortunately, there is a hole in the tank. And water is wasting at a speed of WS per minute while filling it.We need to calculate the amount of water wasted for a full tank.The amount of water filled per minute is equal to the difference between the water filling water and wasting water speed.Hence we can get the total time to fill the water tank by dividing the capacity ... Read More

Advertisements