The datetime module in Python can be used to find the first day of the given year. In general, this datetime module is mostly used for manipulating dates and times. Some of the Common approaches to print the first day of the given year by using Python are as follows. Datetime Module: Widely used library for manipulating dates and times in various ways. Calendar Module: Provides functions related ... Read More
A sequence is a positionally ordered collection of items, where each item can be accessed using its index number. The first element's index starts at 0. We use square brackets [] with the desired index to access an element in a sequence. If the sequence contains n items, the last item is accessed using the index n-1. In Python, there are built-in sequence types such as lists, strings, tuples, ranges, and bytes. These sequence types are classified into mutable and immutable. The mutable sequence types are those whose data can be changed after creation such, as list and byte arrays. ... Read More
Creating symbolic links (or soft links) in Python can be done using the OS module. A symbolic link is a special type of file that points to another file or directory, allowing you to access it under a different pathname. This can be useful for various reasons, such as creating shortcuts or managing different versions of files. The following methods provide different ways to create symbolic links. Using os.symlink() Function Using pathlib.Path.symlink_to() Function Using Command Line with subprocess Module Using 'os.symlink()' Function The most straightforward way to ... Read More
In this article we will learn how to find the two elements in an array whose sum is closest to zero. The array can contain both positive and negative values as well. Let's understand with the help of an example. Example Let's take an array with the following elements − arr = {3, -56, -76, -32, 45, 77, -14, 13, 92, 37} Here, two elements whose sum is closest to zero are 77 and -76, and their sum is 1. Note: In this example we have 77 and -76, which will give the sum as 1, and also ... Read More
In Java, we can easily find the square root of a number using the inbuilt function 'Math.sqrt()’. But sometimes during an interview, the interviewer may ask to write the code from scratch without using inbuilt functions. So in this article, we will discuss the different ways to find the square root of a number without using the sqrt method. 1.Brute Force Method In this brute force method of calculating the square root of a number, first we check if the input is valid or not, and if the input is 0 or 1, we directly return the same number as ... Read More
You are given a number (N); you need to find the length of the longest consecutive zeroes in the binary representation of the number using different approaches. Example Input N = 529 The binary form of 529 is 1000010001. Output The longest chain of zeroes in the binary form is 0000 with a length of 4. Hence, the answer is 4. Approach 1: Using Division and Modulus In this approach we use division and modulus operations to convert decimal to binary and then count the maximum zeroes in the binary number. Steps for implementation The following steps explain ... Read More
A lead number is a number whose sum of even digits is equal to the sum of odd digits. In this article, we will learn about lead numbers and programs to check if a number is a lead number or not. Lead number example Let’s take the number 615341. The sum of even digits is 6 + 4 = 10. The sum of odd digits is 1 + 5 + 3 + 1 = 10. Here, the sum of even digits = the sum of odd digits, so 615341 is a lead number Arithmetic Approach This approach extracts digits ... Read More
What is an associative array? An associative array stores data in key-value pairs where we can retrieve values using unique keys. There is a significant difference between standard arrays and associative arrays. Normal arrays use numbers for indexing (like arr[0], arr[1], arr[2]). Whereas in an associative array, we use meaningful names such as "name" or "city” to identify each value. Example Let's consider a list of student grades where names act as keys − Key (Student Name): Value (Marks) Raju: 85 Krishna: 90 Yash: 78 This lets us find the grades of students by using their names instead of ... Read More
In this article, we will learn how to find and print the odd numbers that are present at odd index positions in an array. First we will understand the problem with the help of an example, and then we will learn three approaches to implement it in Java. Example Consider the array = [2, 1, 4, 4, 5, 7]. The odd numbers at odd indices are 1 and 7. Explanation The indexing of an array starts from 0. Here the element 1, which is an odd number, is present at index 1, which is an odd index. In the same ... Read More
In this article, we will learn how to generate random hexadecimal number values in Java. Random hexadecimal values are used in many applications, like unique identifiers, cryptographic keys, session tokens, or colour codes. We will discuss three different ways to generate random hexadecimal values in Java − Using Random – A simple, fast method to make random hexadecimal values. Using SecureRandom – Best for secure random hexadecimal numbers. Using BigInteger – Helps produce large random hexadecimal numbers. 1. Using Random In this approach, we use the Random class to produce random numbers from 0 to 15. After ... Read More