Found 7197 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

579 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

714 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’

Nishu Kumari
Updated on 08-Aug-2025 10:33:58

509 Views

We are given the length of a binary string str and the string itself. The task is to count the number of substrings that start with '1' and end with '1'. A binary string contains only '0's and '1's, and a substring is any continuous part of the given string. Let's look at a few example scenarios to understand the problem clearly: Scenario 1 Input: N = 5, str = "11101" Output: 6 Explanation: In the given binary string, there are 6 substrings that start and end with '1'. These are: ("11", "111", "1110", "11101", "1101", and "101"). Scenario ... Read More

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

Nishu Kumari
Updated on 20-Aug-2025 17:21:04

501 Views

We are given two strings, a and b and the task is to find whether we can obtain the string b by rotating string a exactly two places in either an anticlockwise (left) or clockwise (right) direction. Let's take an example scenario to understand the problem clearly — Scenario 1- Input: a = "google", b = "legoog" Output: True Explanation: Rotating "google" two places anticlockwise gives "legoog", which matches string b. Thus, we return True. Scenario 2- Input 2: a = "tuorialst", b = "tutorials" Output: False Explanation: String "tuorialst" cannot be ... 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

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

754 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

95 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

310 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