Programming Articles - Page 1884 of 3366

1-bit and 2-bit Characters in Python

Sarika Singh
Updated on 14-Jul-2025 17:10:22

654 Views

What Are 1-bit and 2-bit Characters? In computers, everything is stored in the form of bits, i.e., the smallest pieces of data that can be either 0 or 1. Now, when we talk about 1-bit or 2-bit characters, we mean how many of these bits are used to make a single character (like a letter or symbol). A 1-bit character is just a single 0. It counts as one character by itself. A 2-bit character is made of two bits and can be either 10 or 11. If we are given a list of bits (containing only 0s ... Read More

Java Program for Common Divisors of Two Numbers

AmitDiwan
Updated on 18-Nov-2024 22:33:06

735 Views

In this article, we will learn to find the common divisors of two numbers using Java. The program will use a recursive method to calculate the greatest common divisor (GCD) of the two numbers, and then determine how many divisors are shared by both numbers. The output will display the total number of common divisors. Problem Statement Write a Java program to find and count the common divisors of two given numbers. Below is a demonstration of the same − Input val_1= 68 val_2= 34 Output The common divisors between the two numbers is4 Steps to find the common divisors ... Read More

Design HashMap in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:50:24

2K+ Views

Suppose we want to design a HashMap without using any built-in hash table libraries. There will be different functions as follows −put(key, value) − This will insert a value associated with key into the HashMap. If the value already exists in the HashMap, update the value.get(key) − This will return the value to which the specified key is mapped, otherwise -1 when this map contains no mapping for the key.remove(key) − This will Remove the mapping for the value key if this map contains the mapping for the key.So, if the input is like After initialization, call the put and ... Read More

Java Program for Bitonic Sort

AmitDiwan
Updated on 04-Jul-2020 09:50:29

286 Views

In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Example Live Demopublic class Demo{    void compare_swap(int my_arr[], int i, int j, int direction){       if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){          int temp = my_arr[i];          my_arr[i] = my_arr[j];          my_arr[j] = temp;       }    }    void merge_vals(int my_arr[], int low, int cnt, int direction){ ... Read More

Design HashSet in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:48:10

4K+ Views

Suppose we want to design a HashSet data structure without using any built-in hash table libraries. There will be different functions like −add(x) − Inserts a value x into the HashSet.contains(x) − Checks whether the value x is present in the HashSet or not.remove(x) − Removes x from the HashSet. In case the value does not exist in the HashSet, do nothing.So, to test it Initialize the hash set, then call add(1), add(3), contains(1), contains(2), add(2), contains(2), remove(2), contains(2)., then the output will be true (1 is present), false (2 is not present), true (2 is present), false (2 is ... Read More

Java Program for Binary Search (Recursive)

AmitDiwan
Updated on 04-Jul-2020 09:47:14

7K+ Views

Following is the program for Recursive Binary Search in Java −Example Live Demopublic class Demo{    int rec_bin_search(int my_arr[], int left, int right, int x){       if (right >= left){          int mid = left + (right - left) / 2;          if (my_arr[mid] == x)          return mid;          if (my_arr[mid] > x)          return rec_bin_search(my_arr, left, mid - 1, x);          return rec_bin_search(my_arr, mid + 1, right, x);       }       return -1;    } ... Read More

Kth Largest Element in a Stream in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:44:48

589 Views

Suppose we want to design a class to find the kth largest element in a stream. It is the kth largest element in the sorted order, not the kth distinct element.The KthLargest class will have a constructor which accepts an integer k and an array nums, that will contain initial elements from the stream. For each call to the method KthLargest.add, will return the element representing the kth largest element in the stream.So, if the input is like k = 3, initial elements = [4, 5, 8, 2], then call add(3), add(5), add(10), add(9), add(4). , then the output will ... Read More

Java Program for Anagram Substring Search

AmitDiwan
Updated on 04-Jul-2020 09:44:12

243 Views

Following is an example for Anagram Substring Search in Java −Example Live Demopublic class Demo{    static final int max_val = 256;    static boolean compare_vals(char my_arr_1[], char my_arr_2[]){       for (int i = 0; i < max_val; i++)       if (my_arr_1[i] != my_arr_2[i])       return false;       return true;    }    static void search_subs(String my_pattern, String my_text){       int pat_len = my_pattern.length();       int txt_len = my_text.length();       char[] count_pat = new char[max_val];       char[] count_txt = new char[max_val];       for ... Read More

Degree of an Array in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:43:10

639 Views

Suppose we have an array of non-negative integers called nums, the degree of this array is actually the maximum frequency of any one of its elements. We have to find the smallest possible length of a contiguous subarray of nums, that has the same degree as nums.So, if the input is like [1, 2, 2, 3, 1], then the output will be 2, this is because the input array has a degree of 2 because both elements 1 and 2 appear twice. The subarrays that have the same degree − [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, ... Read More

Java Program for Comb Sort

AmitDiwan
Updated on 04-Jul-2020 09:41:16

279 Views

The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Example Live Demoimport java.util.Arrays; public class Demo{    void comb_sort(int nums[]){       int len_gap = nums.length;       float shrink_val = 1.3f;       boolean swap = false;       while (len_gap > 1 || swap) {          if (len_gap > 1) {             len_gap = (int)(len_gap / shrink_val);          }          swap ... Read More

Advertisements