Found 7347 Articles for C++

Find if given number is sum of first n natural numbers in C++

sudhir sharma
Updated on 22-Jan-2021 14:02:30

224 Views

In this problem, we are given a number num. Our task is to find if the given number is the sum of first n natural numbers.  Problem Description: Here, we need to check whether the given number is the sum of first n natural numbers.Let’s take an example to understand the problem, Input: num = 55Output: yes, 10Explanation:55 is the sum of the first 10 natural numbers, 1+2+3+4+5+6+7+8+9+10.Solution Approach: A simple approach to solving the problem is finding the sum of n natural numbers until it becomes equal to or greater than num.If the sum is equal to num, return n.If at any value of ... Read More

Find if given matrix is Toeplitz or not in C++

sudhir sharma
Updated on 22-Jan-2021 14:02:51

164 Views

In this problem, we are given a 2D square matrix mat[][] of size n*n. Our task is to find if the given matrix is Toeplitz or not.Toeplitz matrix also known as diagonal matrix is a matrix in which the elements at the diagonal start from top-left corner to bottom-right corner.Let’s take an example to understand the problem, Input:          Mat[][] = {{3, 5, 1},                            {4, 3 ,2},                            {1, 2, 3}}Output: YesExplanation:  Diagonal : ... Read More

Find if array has an element whose value is half of array sum in C++

sudhir sharma
Updated on 22-Jan-2021 13:53:43

149 Views

In this problem, we are given an array arr of sorted unique values. Our task is to find if array has an element whose value is half of array sum. Problem Description: For the array arr[], we need to find element x in the array such that the sum of all elements of array is equal to 2*X.Let’s take an example to understand the problem, Input: arr[] = {2, 4, 5, 6, 7}Output: NoExplanation: Sum = 2 + 4 + 5 + 6 + 7 = 24No element found.Solution Approach: To solve the problem, we simply need to find the elements which is half of the ... Read More

Find if a string starts and ends with another given string in C++

sudhir sharma
Updated on 22-Jan-2021 13:53:31

469 Views

In this problem, we are given two strings str and corStr. Our task is to find if a string starts and ends with another given string. Let’s take an example to understand the problem, Input: str = “abcprogrammingabc” conStr = “abc”Output: TrueSolution Approach: To solve the problem, we need to check if the string starts and ends with the conStr. For this, we will find the length of string and corStr. Then we will check if len(String) > len(conStr), if not return false.Check if prefix and suffix of size corStr are equal and check they contain corStr or not.Program to illustrate the working of ... Read More

Find if a number is divisible by every number in a list in C++

sudhir sharma
Updated on 22-Jan-2021 13:53:18

203 Views

In this problem, we are given a list of n numbers and a number. Our task is to find if a number is divisible by every number in a list. We need to check if the given number divides all elements of the list or not.Let’s take an example to understand the problem, Input: list[] = [4, 10 ,6, 5, 9] num = 5Output: NoExplanation:Elements 4, 6, 9 are not divisible by 5.Solution Approach: To solve the problem, we need to check if any element of the list is divisible by num. If every number of lists is divisible by num, return true else ... Read More

Find Height of Binary Tree represented by Parent array in C++

sudhir sharma
Updated on 22-Jan-2021 13:48:56

147 Views

In this problem, we are given an array arr[] of size n that denotes a tree. Our task is to find height of Binary Tree represented by Parent array. A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −The value of the key of the left sub-tree is less than the value of its parent (root) node's key.The value of the key of the right subtree is greater than or equal to the value of its parent (root) node's key.Height of a tree is the number of nodes traversed when going from root node ... Read More

Fill array with 1's using minimum iterations of filling neighbors in C++

sudhir sharma
Updated on 22-Jan-2021 13:45:26

117 Views

 In this problem, we are given an array arr consisting of n elements that are either 0 or 1. Our task is to fill array with 1’s using minimum iterations of filling neighbors. Let’s take an example to understand the problem, Input: arr[] = {0, 1, 1, 0, 0, 1}Output: 1Solution Approach −To solve the problem, we need to know the fact that if 1 is present in a position it can convert two neighbouring 0’s to 1.If,                arr[i] is 1.Then,           arr[i-1] and arr[i+1] will be converted to 1.Using this, the ... Read More

File opening modes(r versus r+) in C++

sudhir sharma
Updated on 22-Jan-2021 13:49:44

3K+ Views

File handling in programming languages is very important for the interaction of programming with the memory for accessing files and fetching data in it.Using a program, you can read data from a file as well as write data to the file and do a lot more functions.Here, we will see reading of data from a file.In programming, before performing any operation you need to open the file. And there are multiple modes to open a file in the programming language. The access to the file is based on the mode it is opened with.Here we will learn about the difference between ... Read More

File globbing in Linux in C++

sudhir sharma
Updated on 22-Jan-2021 13:45:44

546 Views

p>File globbing also known as Path Name Expansion. It is the method of recognizing wildcard patterns in linux and  then finding the file path expansion based on these patterns.Wildcard Patterns are strings that are used for selection of multiple files based on patterns.The character patterns like “?” , “[ ]” , “*” are used for pattern matching and multiselection of the files.Example of wildcard characters using in file globbing:Asterisk (*) : the * pattern is used when we need to match 0 or more character after the string in the filename.For example: file* will match all files with name file, files, file2, or with anything ... Read More

Fifth root of a number in C++

sudhir sharma
Updated on 22-Jan-2021 13:45:29

763 Views

In this problem, we are given a number N. Our task is to find the floor value of the fifth root of a number.Fifth Root of a number is the number which when multiplied to itself 5 times returns the number.If N1/5 = a then, a*a*a*a*a = N.Let’s take an example to understand the problem, Input: N = 325Output: 3Explanation:  The fifth root of 325 is 3.179 whose floor value is 3.Solution Approach: A simple solution to the problem is traversing from 1 to n. And finding the number which when multiplied to itself five times gives the number.Here, the exact value cannot be found ... Read More

Advertisements