Server Side Programming Articles - Page 1936 of 2650

Subtract the Product and Sum of Digits of an Integer in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:19:46

497 Views

Suppose we have one number. We have to find the sum of digits and product of digits. After that find the difference between sum and product. So if the number is 5362, then sum is 5 + 3 + 6 + 2 = 16, and 5 * 3 * 6 * 2 = 180. So 180 – 16 = 164To solve this take each digit and add and multiply one by one, then return the difference.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; class Solution {    public:       int subtractProductAndSum(int n) {          int prod = 1;          int sum = 0;          for(int t = n;t;t/=10){             sum += t % 10;             prod *= t % 10;          }          return prod - sum;       } }; main(){    Solution ob;    cout

Minimum Time Visiting All Points in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:17:15

239 Views

Suppose there are some points given as an array. We have to find the minimum time in seconds to visit all points. There are some conditions.In one second, it can move vertically, horizontally and diagonallyWe have to visit the points in the same order as they appear in the array.So if the points are [(1, 1), (3, 4), (-1, 0)], then output will be 7. If we check the sequence for the shortest route, the sequence will be (1, 1), (2, 2), (3, 3), (3, 4), (2, 3), (1, 2), (0, 1), (-1, 0)To solve this we will just find ... Read More

Cells with Odd Values in a Matrix in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:14:01

363 Views

Suppose there are n and m which are the dimensions of a matrix. These are initialized by zeros. And indices are given where indices[i] = [ri, ci]. For each pair of [ri, ci] we have to increment all cells in row ri and column ci by 1. The output will be the number of cells with odd values in the matrix after applying the increment to all indices.To solve this, we will follow these steps −Initialize odd := 0, and x := row count of the matrixcreate a matrix matfor i in range 0 to xr = input[i, 0], c ... Read More

Find Positive Integer Solution for a Given Equation in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:11:04

284 Views

Suppose we have a function f that takes two parameters (x, y). We have to return all pairs of x and y, for which f(x, y) = z. The z is given as input, and x, y are positive integers. The function is constantly increasing function. So f(x, y) < f(x + 1, y) and f(x, y) < f(x, y + 1).To solve this we will perform straight-forward approach. Take i in range 1 to 1000, and j in range 1 to 1000, for all combinations of i, j, if f(i, j) = 0, then return true, otherwise false.Consider the ... Read More

Check If It Is a Straight Line in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:07:21

593 Views

Suppose we have a list of data-points consisting of (x, y) coordinates, we have to check whether the data-points are forming straight line or not. So if the points are like [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)], then they are forming straight line.To solve this, we will take the differences between each consecutive datapoints, and find the slope. For the first one find the slope. For all other points check whether the slope is same or not. if they are same, then simply return true, otherwise falseExampleLet us see the following implementation to get ... Read More

Split a String in Balanced Strings in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:05:26

500 Views

As we know that the balanced strings are those which have equal quantity of left and right characters. Suppose we have a balanced string s split it in the maximum amount of balanced strings. We have to return the maximum amount of splitted balanced strings. So if the string is “RLRRLLRLRL”, then output will be 4. as there are four balanced strings. “RL”, “RRLL”, “RL” and “RL” each substring has equal amount of L and R.To solve this, we will follow these steps −initialize cnt := 0, and ans := 0for i := 0 to size of the stringcnt := ... Read More

Unique Number of Occurrences in Python

Akshitha Mote
Updated on 20-Dec-2024 17:35:36

893 Views

Suppose we have an array, and we need to check whether each element has a unique number of occurrences. If no such element exists, we return false; otherwise, we return true. For example, given the array [1, 1, 2, 2, 2, 3, 4, 4, 4, 4], the function will return true because no two elements have the same number of occurrences: 1 occurs twice, 2 occurs three times, 3 occurs once, and 4 occurs four times. Various Techniques to Find Unique Occurrences Following are the various techniques to find the unique occurrence of the elements in a list − ... Read More

Print all possible combinations of r elements in a given array of size n in C++

sudhir sharma
Updated on 17-Jan-2020 11:42:28

2K+ Views

In this problem, we are given an array of size n and a positive integer r. Our task is to print all possible combinations of the elements of the array of size r.Let’s take an example to understand the problem −Input: {5, 6, 7, 8} ; r = 3 Output : {5, 6, 7}, {5, 6, 8}, {5, 7, 8}, {6, 7, 8}To solve this problem an approach would be fixing elements and then recuring or looping over others to find all combinations. In this, we have to fix first n-r+1 elements only and loop or recur over the rest.Example#include ... Read More

Print all possible expressions that evaluate to a target in C++

sudhir sharma
Updated on 17-Jan-2020 11:38:28

323 Views

In this problem, we are given a string of integers from 0 to 9 and a target value. We have to print out ways in which we can generate expression using +, -, and * operation which is evaluated to the value equal to target.Let’s take an example to understand the topic better −Input: string = “123” , target= 6 Output: { “1+2+3”, “1*2*3” }To solve this problem, we will be creating expressions by placing all possible binary operators between digits and then checking the result of the expression with the target value.We will pass all values to a recursive ... Read More

Print all possible paths from top left to bottom right of a mXn matrix in C++

sudhir sharma
Updated on 17-Jan-2020 11:36:41

382 Views

In this problem, we are given an mXn 2D matrix and we have to print all possible paths from top left to the bottom right of the matrix. For traversal, we can move only right and down in the matrix.Let’s take an example to understand the topic better −Input: 1 3 5 2 8 9 Output: 1 -> 3 -> 5 -> 9 1 -> 3 -> 8 -> 9 1 -> 2 -> 8 -> 9To solve this problem, we will move from one cell to another and print the path on going down and right. We will do ... Read More

Advertisements