Program
Program
Java/Kotlin String,
and what are some different approaches that can be used to achieve this?
return result;
}
public static void main(String[] args)
{
String str = "anan";
System.out.println("Max occurring character is "
+ getMaxOccurringChar(str));
}
}
while(number > 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
System.out.println("Sum of Digits: "+sum);
}
}
class GFG {
if (max_ending_here < 0) {
max_ending_here = 0;
s = i + 1;
}
}
System.out.println("Maximum contiguous sum is "
+ max_so_far);
System.out.println("Starting index " + start);
System.out.println("Ending index " + end);
}
// Driver code
public static void main(String[] args)
{
int a[] = { 3, 4, 7, 3, 1 };
int n = a.length;
maxSubArraySum(a, n);
}
}
5) Can you provide a program in Java/Kotlin that finds all the subarrays in an
array whose sum is
zero? How does your implementation work? Can you explain the time and space
complexity of
your solution? Are there any optimizations that can be made to improve the
efficiency of your
program
6)Write a Java/Kotlin program to find the sum of all the numbers in a given string.
For example, if
the input string is "1ab2d4hj6", the program should output the sum of numbers in
the string,
which is 13.
The program should take a string as input from the user and use regular expressions
to extract
all the numbers from the string. It should then iterate over the extracted numbers
and add them
up to get the final sum. If there are no numbers in the string, the program should
output 0.
import java.io.*;
class GFG {
if (Character.isDigit(ch))
temp += ch;
else {
sum += Integer.parseInt(temp);
temp = "0";
}
}
return sum + Integer.parseInt(temp);
}
public static void main(String[] args)
{
String str = "1ab2d4hj6";
System.out.println(findSum(str));
}
}
7) Write a Java/Kotlin program that takes two strings as input and finds the number
of occurrences
of the second string in the first string.
Example:
Input:
String 1: "hello world"
String 2: "l"
Output:
Number of occurrences of "l" in "hello world" is 3
import java.io.*;
class GFG
{
static int count(String a, String b, int m, int n)
{
if ((m == 0 && n == 0) || n == 0)
return 1;
if (m == 0)
return 0;
8) Write a Java/Kotlin program to find the sum of all the numbers in a given
string. Your program
should take a string as input from the user and then find all the numbers in the
string. It should
then add up all the numbers and print the sum to the console.
Here are some requirements for your program:
The string should be read from the console using the Scanner class.
Your program should use regular expressions to find all the numbers in the string.
The sum of the numbers should be calculated using a loop.
If there are no numbers in the string, your program should print a message saying
so.
Example:
Input: "Hello 123 world 456"
Output: 579
import java.io.*;
class GFG {
int sum = 0;
if (Character.isDigit(ch))
temp += ch;
else {
sum += Integer.parseInt(temp);
temp = "0";
}
}
// Function call
System.out.println(findSum(str));
}
}
9). Write a Java/Kotlin program to find all the duplicate elements in an integer
array with less time
complexity.
Example input:
int[] arr = {4, 2, 4, 5, 2, 3, 1, 1, 6, 7, 7};
Expected output:
Duplicate elements in the given array are:
4 2 1 7
class DuplicateElement {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {4, 2, 4, 5, 2, 3, 1, 1, 6, 7, 7};
class ReverseArray {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
//Loop through the array in reverse order
for (int i = arr.length-1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
10) You have been given an integer array and a key integer. Your task is to write a
Java/Kotlin
function that finds the number in the array which has the highest frequency and
replaces it with
the given key integer
class GFG
{
return element_having_max_freq;
}
// Driver program
public static void main(String[] args)
{
int[] arr = { 40, 50, 30, 40, 50, 30, 30 };
int n = arr.length;
System.out.print(mostFrequent(arr, n));
}
}
return;
}
// like 20
if (i < 0)
{
i = 0;
decrease(s, i);
}
// like 7721
else
decrease(s, i);
return s.toString();
}
// Driver code
public static void main(String[] args)
{
StringBuilder s = new StringBuilder("45");
System.out.println(primeDigits(s));
s = new StringBuilder("1000");
System.out.println(primeDigits(s));
s = new StringBuilder("7721");
System.out.println(primeDigits(s));
s = new StringBuilder("7221");
System.out.println(primeDigits(s));
s = new StringBuilder("74545678912345689748593275897894708927680");
System.out.println(primeDigits(s));
}
}