Posts

Showing posts from October, 2020

How to check if given year is a leap year or not in Java - The Coding Shala

Home >> Java Program >> check leap year  In this post, we will learn how to check if a given year is a leap year or not. We will write a Java Program to check leap year. Java Program to check leap year Leap years are those years in which the year's number is either divisible by 4 but not divisible by 100 or divisible by 400. Example 1: Input: 2100 Output: Regular Example 2: Input: 2000 Output: Leap Java Program:   import java.util.Scanner ; class Main { public static void main ( String [] args ) { // put your code here Scanner sc = new Scanner ( System . in ); int year = sc . nextInt (); if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) { System . out . println ( "Leap" ); } else { System . out . println ( "Regular" ); } } } Other Posts You May Like Java Program to add two numbers Java Program to

LeetCode - Range Sum Query Immutable Solution - The Coding Shala

Home >> LeetCode >> Range Sum Query Immutable  In this post, we will learn how to solve LeetCode's Range Sum Query Immutable Problem and its solution in Java. Range Sum Query Immutable Problem Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Implement the NumArray class:  NumArray(int[] nums) Initializes the object with the integer array nums. int sumRange(int i, int j) Return the sum of the elements of the nums array in the range [i, j] inclusive (i.e., sum(nums[i], nums[i + 1], ... , nums[j])). Example 1: Input: ["NumArray", "sumRange", "sumRange", "sumRange"]              [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]] Output: [null, 1, -1, -3] Practice this problem on LeetCode: (Click Here) Range Sum Query - Immutable Java Solution Approach 1: First, make a sum array that contains sum till every index then return sum[j] - sum[i-1]. Java Program:  class NumArra