Found 33676 Articles for Programming

Java program to cube sum of first n natural numbers

Alshifa Hasnain
Updated on 19-Feb-2025 17:50:35

557 Views

In this article, we will learn to write a Java program to calculate the sum of cubes of the first n natural numbers. Understanding the Cube Sum Formula The sum of cubes of the first n natural numbers follows a mathematical formula − S = 13 + 23 + 33 + ... + n3 = { ( n ( n + 1 ) ) / 2 }2 Different Approaches Following are the two different approaches to printing the cube sum of first n natural numbers − Using a Loop Using a ... Read More

Longest Word in Dictionary in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:55:08

543 Views

Suppose we have a list of words representing an English Dictionary, we have to find the longest word in the given word list that can be built one character at a time by other words in words. If there is more than one possible answer, then return the longest word with the smallest lexicographical order. If there is no answer, then return the empty string.So, if the input is like ["h", "he", "hel", "hell", "hello"], then the output will be "hello"To solve this, we will follow these steps −trie := a new mapDefine a function insert(). This will take wordnow ... Read More

1-bit and 2-bit Characters in Python

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

632 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

717 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

277 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

578 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

236 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

Advertisements