Programming Articles

Page 8 of 2545

Bricks Falling When Hit in C++

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

Suppose we have a grid of binary values (0s and 1s) the 1s in a cell represent the bricks. A brick will not drop when that satisfies these conditions −Either brick is directly connected to the top of the gridor at least one of its adjacent (top, bottom, left, right) bricks will not drop.We will do some erasures sequentially. In each case we want to do the erasure at the location (i, j), the brick (if that is present) on that location will disappear, and then some other bricks may drop because of that erasure. We have to find the ...

Read More

How to find the age when date of birth is known? Using Java?

Narasimha Murthi
Narasimha Murthi
Updated on 11-Mar-2026 5K+ Views

Java provides a class named Period in the java.time package. This is used to calculate the time period between two given dates as days, months and, years etc.The between() method of this class accepts two LocalDate objects and finds out the period between the two given dates (number of years, months, and days) and returns as a period object.From this, you can extract the number of days, months and, years of the period in between using the getDays(), getMonths() and, getYears() respectively.Finding the ageIf you already have the date of birth of a person, to find the age −Get the ...

Read More

What is ‘this’ reference in Java?

Narasimha Murthi
Narasimha Murthi
Updated on 11-Mar-2026 3K+ Views

The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.Using “this” you can −Differentiate the instance variables from local variables if they have same names, within a constructor or a method.class Student {    int age;    Student(int age) {       this.age = age;    } }Call one type of constructor (parametrized constructor or default) from other in a class. It is known as ...

Read More

How to Get a slice of a primitive array in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 980 Views

You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Exampleimport java.util.Arrays; public class SlicingAnArray {    public static int[] sliceArray(int array[], int startIndex, int endIndex ){       int size = endIndex-startIndex;       int part[] = new int[size];       //Copying the contents of the array       for(int i=0; iarray[i]);       part = stream.toArray();       //Copying the contents of the array       for(int i=0; i

Read More

Reverse Subarray To Maximize Array Value in C++

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

Suppose we have one integer array called nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all i in range 0 to n - 1. Where n is the size of the array. We can select any subarray of the given array and reverse it. We can perform this operation only once. Then we have to find the maximum possible value of the final array.So, if the input is like [1, 5, 4, 2, 3], then the output will be 10.To solve this, we will follow these steps −ret := 0, extra := 0n := ...

Read More

Difference between sums of odd and even digits.

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 599 Views

Problem StatementWith a given long integer n, write a program to find the difference between sum of the odd digits and even digits to be 0 or not. Index starts from 0.Examplen = 1212112 Sum of odd position elements = 2 + 2 + 1 = 5 Sum of even position elements = 1 + 1 + 1 + 2 = 5 Difference = 5 - 5 = 0 Output = YesExampleclass JavaTester {    public static int difference(int n){       return (n % 11);    }    public static void main(String args[]){       int n = 1212112;       System.out.println("Number: " + n);       System.out.println(difference(n) == 0 ? "Yes" : "No");       n = 12121121;       System.out.println("Number: " + n);       System.out.println(difference(n) == 0 ? "Yes" : "No");    } }OutputNumber : 1212112 Output: Yes Number : 12121121 Output: No

Read More

How to change the Y-axis title to horizontal using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

The default direction of Y-axis title using ggplot2 in R is vertical and we can change to horizontal. For this purpose, we can use theme function of ggplot2 package. We would need to use the argument of theme function as axis.title.y=element_text(angle=0)) and this will write the Y-axis title to horizontal but the position will be changed to top.Exampleggplot(df,aes(x))+geom_histogram(bins=30)+theme(axis.title.y=element_text(angle=0))Output

Read More

Difference between sums of odd level and even level nodes of a Binary Tree in Java

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 475 Views

Problem StatementWith a given binary tree, write a program to find the difference between sum of nodes at odd level and even level. Assume root at level 1, left/right child of root at level 2 and so on.Example        5       /   \       2     6      /  \     \     1     4    8    /    /  \   3    7    9 Sum of nodes at odd level = 5 + 1 + 4 + 8 = 18 Sum of ...

Read More

Difference between the largest and the smallest primes in an array in Java

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 997 Views

Problem StatementWith a given array of integers where all elements are less than 1000000. Find the difference between the largest and the smallest primes in an array.ExampleArray: [ 1, 2, 3, 4, 5 ] Largest Prime Number = 5 Smallest Prime Number = 2 Difference = 5 - 3 = 2.SolutionUse Sieve of Eratosthenes approach, which is an efficient way to find out all prime numbers smaller than a given number. Then we will figure out the largest and smallest prime number to get the required difference.Examplepublic class JavaTester {    static int MAX = 1000000;   ...

Read More

Difference between Traits and Abstract Classes in Scala.

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 1K+ Views

TraitsTraits are similar to interfaces in Java and are created using trait keyword.Abstract ClassAbstract Class is similar to abstract classes in Java and are created using abstract keyword.Exampletrait SampleTrait {    // Abstract method    def test    // Non-Abstract method    def tutorials() {       println("Traits tutorials")    } } abstract class SampleAbstractClass {    // Abstract method    def test    // Non-abstract meythod    def tutorials() {       println("Abstract Class tutorial")    } } class Tester extends SampleAbstractClass {    def test() {       println("Welcome to Tutorialspoint") ...

Read More
Showing 71–80 of 25,445 articles
« Prev 1 6 7 8 9 10 2545 Next »
Advertisements