Programming Articles - Page 2144 of 3363

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

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

522 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

251 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

374 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

299 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

616 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

520 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

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

How to implement LongFunction using lambda and method reference in Java?

raja
Updated on 14-Jul-2020 08:10:54

333 Views

LongFunction is an in-built functional interface defined in java.util.function package. This functional interface expects a long-valued parameter as input and produces a result. LongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: apply().Syntax@FunctionalInterface public interface LongFunction {  R apply(long value) }Exampleimport java.util.function.LongFunction; public class LongFunctionTest {    public static void main(String[] args) {       LongFunction function1 = (long i) -> { // lambda expression          return i + i;       };       System.out.println("Using Lambda Expression: " + function1.apply(10));       LongFunction ... Read More

XDM – The Download Manager for Linux that ramps up Your Speed to 500%

karthikeya Boyini
Updated on 17-Jan-2020 13:01:06

357 Views

Xtreme download supervisor (xdman) is an effective download supervisor for Linux, which is developed in Java programing language. It can increase download speeds of upto 500% and is an alternative for the windows IDM (Internet Download Manager). It is compatible with many popular internet browsers such as Firefox, Chrome, Opera.Before installing Xtreme Download supervisor, Check if Java is installed or not available by typing java -version in command line.$ java -versionThe sample output should be like this –openjdk version "1.8.0_91" OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14) OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)Installing Xtreme Download Manager in LinuxTo put the ... Read More

What are the rules for a functional interface in Java?

raja
Updated on 14-Jul-2020 08:12:26

2K+ Views

A functional interface is a special kind of interface with exactly one abstract method in which lambda expression parameters and return types are matched. It provides target types for lambda expressions and method references.Rules for a functional interfaceA functional interface must have exactly one abstract method.A functional interface has any number of default methods because they are not abstract and implementation already provided by the same.A functional interface declares an abstract method overriding one of the public methods from java.lang.Object still considered as functional interface. The reason is any implementation class to this interface can have implementation for this abstract method either from ... Read More

Advertisements