Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 1278 of 2109
Degree of an Array in C++
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 MoreX of a Kind in a Deck of Cards in C++
Suppose we have a deck of cards, each card has an integer written on it. We have to check whether we can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where the following condition satisfies: Each group has exactly X number of cards. All of the cards in each group have the same number.So, if the input is like deck = [1, 2, 3, 4, 4, 3, 2, 1], then the output will be True, as possible partitions are [1, 1], [2, 2], [3, 3], [4, 4].To ...
Read MoreLargest Time for Given Digits in C++
Suppose we have an array of 4 digits, we have to find the largest 24-hour time that can be made. We know that the smallest 24-hour time is 00:00, and the largest time is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight. We have to return the answer as a string of length 5. If there is no valid time to be returned, then return an empty string.So, if the input is like [1, 2, 3, 4], then the output will be "23:41"To solve this, we will follow these steps −Define a function ...
Read MoreFind the uncommon values concatenated from both the strings in Java
To find the uncommon values concatenated from both the strings in Java, the code is as follows −Exampleimport java.util.*; import java.lang.*; import java.io.*; public class Demo{ public static String concat_str(String str_1, String str_2){ String result = ""; int i; HashMap my_map = new HashMap(); for (i = 0; i < str_2.length(); i++) my_map.put(str_2.charAt(i), 1); for (i = 0; i < str_1.length(); i++) if (!my_map.containsKey(str_1.charAt(i))) result += str_1.charAt(i); else my_map.put(str_1.charAt(i), ...
Read MoreFind the first repeated word in a string in Java
To find the first repeated word in a string in Java, the code is as follows −Exampleimport java.util.*; public class Demo{ static char repeat_first(char my_str[]){ HashSet my_hash = new HashSet(); for (int i=0; i
Read MoreJava program to find IP Address of the client
To find the IP Address of the client, the Java code is as follows −Exampleimport java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{ public static void main(String args[]) throws Exception{ InetAddress my_localhost = InetAddress.getLocalHost(); System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim()); String my_system_address = ""; try{ URL my_url = new URL("http://bot.whatismyipaddress.com"); BufferedReader my_br = new BufferedReader(new InputStreamReader(my_url.openStream())); my_system_address = my_br.readLine().trim(); } ...
Read MoreReplace null values with default value in Java Map
To replace null values with default value in Java Map, the code is as follows −Exampleimport java.util.*; import java.util.stream.*; public class Demo{ public static Map null_vals(Map my_map, T def_val){ my_map = my_map.entrySet().stream().map(entry -> { if (entry.getValue() == null) entry.setValue(def_val); return entry; }) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); return my_map; } public static void main(String[] args){ Map my_map = new HashMap(); my_map.put(1, null); my_map.put(2, 56); ...
Read MoreReplacing 'public' with 'private' in "main" in Java
When ‘public’ is used in ‘main’ −Examplepublic class Demo{ public static void main(String args[]){ System.out.println("This is a sample only"); } }OutputThis is a sample onlyA class named Demo contains the main function that is public. It has a print function, which successfully compiles, executes and prints the message on the console.When ‘public’ is replaced with ‘private’Examplepublic class Demo{ private static void main(String args[]){ System.out.println("This is a sample only"); } }OutputError: Main method not found in class Demo, please define the main method as: public static void main(String[] args) or a ...
Read MoreJava Program to check if count of divisors is even or odd
To check if the count of divisors is even or odd, the Java code is as follows −Exampleimport java.io.*; import java.math.*; public class Demo{ static void divisor_count(int n_val){ int root_val = (int)(Math.sqrt(n_val)); if (root_val * root_val == n_val){ System.out.println("The number of divisors is an odd number"); }else{ System.out.println("The number of divisors is an even number"); } } public static void main(String args[]) throws IOException{ divisor_count(25); } }OutputThe number of divisors is an odd ...
Read MoreJava Program to check whether it is possible to make a divisible by 3 number using all digits in an array
To check whether it is possible to make a divisible by 3 number using all digits in an array, the Java code is as follows −Exampleimport java.io.*; import java.util.*; public class Demo{ public static boolean division_possible(int my_arr[], int n_val){ int rem = 0; for (int i = 0; i < n_val; i++) rem = (rem + my_arr[i]) % 3; return (rem == 0); } public static void main(String[] args){ int my_arr[] = { 66, 90, 87, 33, 123}; ...
Read More