Server Side Programming Articles

Page 1281 of 2109

Remove 9 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 312 Views

Suppose we have an integer n, we have to return nth integer after following this operation: Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, we will have a new integer sequence like 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... We have to keep in mind that 1 will be the first integer.So, if the input is like 9, then the output will be 10To solve this, we will follow these steps −ret := 0s := 1while n is non-zero, do −ret := ret + (n mod 9) ...

Read More

Minimize Max Distance to Gas Station in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have one horizontal number line. On that number line, we have gas stations at positions stations[0], stations[1], ..., stations[N-1], where N = size of the stations array. Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized. We have to find the smallest possible value of D.So, if the input is like stations = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 9, then the output will be 0.5To solve this, we will follow these steps −Define a function ok(), this will take x, array ...

Read More

Number of Valid Subarrays in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 533 Views

Suppose we have an array A of integers, we have to find the number of non-empty continuous subarrays that satisfy this condition: The leftmost element of the subarray is not larger than other elements in the subarray.So, if the input is like [1, 4, 2, 5, 3], then the output will be 11, as there are 11 valid subarrays, they are like [1], [4], [2], [5], [3], [1, 4], [2, 5], [1, 4, 2], [2, 5, 3], [1, 4, 2, 5], [1, 4, 2, 5, 3].To solve this, we will follow these steps −ret := 0n := size of numsDefine ...

Read More

Maximum Number of Ones in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 218 Views

Suppose we have a matrix M with dimensions w x h, such that every cell has value 0 or 1, and any square sub-matrix of M of size l x l has at most maxOnes number of ones. We have to find the maximum possible number of ones that the matrix M can have.So, if the input is like w = 3, h = 3, l = 2, maxOnes = 1, then the output will be 4 as in a 3*3 matrix, no 2*2 sub-matrix can have more than 1 one. The best solution that has 4 ones is −101000101To ...

Read More

Minimum Time to Build Blocks in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 252 Views

Suppose we have a list of blocks, if we have blocks[i] = t, this means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker. Single worker can either split into two workers or build a block then go home. These two decisions take some time. The time cost of spliting one worker into two workers is given as a number called split.So, if the input is like blocks = [1, 2], and split = 5, then the output will be 7 as we can split the worker ...

Read More

Handshakes That Don't Cross in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 380 Views

Suppose we have an even number of people n that stand around a circle and each person shakes hands with someone else, so that there will be n / 2 handshakes total. We have to find the number of ways these handshakes could occur such that none of the handshakes cross. The answers may be very large so return the answer mod 10^9 + 7.So, if the input is like n = 2, then the output will be 1To solve this, we will follow these steps −m := 10^9 + 7Define an array dp of size (n+1)dp[0] := 1for initialize i := 0, when i

Read More

Sum of list with stream filter in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

To get sum of list with stream filter in Java, the code is as follows −Exampleimport java.util.*; public class Demo {    public static void main(String[] args)    {       List my_list = new ArrayList();       my_list.add(11);       my_list.add(35);       my_list.add(56);       my_list.add(78);       my_list.add(91);       System.out.println(sum(my_list));    }    public static int sum(List my_list)    {       System.out.println("In the main function, the sum of list with filter is ");       return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum();    } }OutputIn ...

Read More

How to input multiple values from user in one line in Java?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 14K+ Views

To input multiple values from user in one line, the code is as follows −Exampleimport java.util.Scanner; public class Demo {    public static void main(String[] args) {       System.out.print("Enter two floating point values : ");       Scanner my_scan = new Scanner(System.in);       double double_val = my_scan.nextFloat();       int int_val = my_scan.nextInt();       System.out.println("The floating point value is : " + double_val + " and the integer value is : "       + int_val);    } }Input56.789 99OutputEnter two floating point values : The floating point value is : ...

Read More

Method Overloading and Ambiguity in Varargs in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

There are ambiguities while using variable arguments in Java. This happens because two methods can definitely be valid enough to be called by data values. Due to this, the compiler doesn’t have the knowledge as to which method to call.Examplepublic class Demo {    static void my_fun(double ... my_Val){       System.out.print("fun(double ...): " + "Number of args: " + my_Val.length );       for(double x : my_Val)       System.out.print(x + " ");       System.out.println();    }    static void my_fun(boolean ... my_Val){       System.out.print("fun(boolean ...) " + "The number of arguments: ...

Read More

Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 364 Views

To find the sum of such series, the Java program is as follows −Examplepublic class Demo {    static long my_val = 1000000007;    public static long compute_val(long my_int){       return ((my_int % my_val) * (my_int % my_val)) % my_val;    }    public static void main(String[] args){       long my_int = 45687234;       System.out.println("The computed value is ");       System.out.print(compute_val(my_int));    } }OutputThe computed value is 335959495A class named Demo defines a function named ‘compute_val’ that computes and returns the sum of a specific series. In the main function, the long ...

Read More
Showing 12801–12810 of 21,090 articles
Advertisements