0% found this document useful (0 votes)
27 views9 pages

Accenture JAVA CODE ON METHODS

Questions

Uploaded by

vikramabhang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

Accenture JAVA CODE ON METHODS

Questions

Uploaded by

vikramabhang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Problem Statement:
You are given a function:
Int DesiredArray(int* Arr, int N, int K);
The function accepts an array “Arr” of size “N” and an integer
“K”.
You have to find the “K” smallest integers that are not divisible
by any of the “N” integers and return the sum of all “K”
integers.
Note: Array won’t contain 1.
Example:
Input:
K:4
N:5
Arr : [2,3,4,5,6]
Output:
32
Explanation: First K-smallest non divisible by Arri integers will
be 1,7,11,13. Hence the sum will be 32.
Example:
Input:
K:4
N:4
Arr : [3,6,9,12]
Output:
12
This function takes in three parameters:
1. Arr - an array of N integers.
2. N - the size of the array Arr.
3. K - an integer representing the number of smallest integers
to find.
Objective
Your task is to find the K smallest positive integers that are not
divisible by any of the integers present in the array Arr and
return the sum of these K integers.
Constraints
1. The array Arr will not contain the number 1.
2. The integers in the array Arr will be greater than 1.
The Least Common Multiple (LCM) of two or more integers is
the smallest positive integer that is divisible by each of the
integers. The concept of LCM is widely used in number theory
and helps in solving problems involving multiple integers, such
as finding common denominators or synchronizing cycles.
Methods to Find LCM
1. Prime Factorization:
o Find the prime factorization of each integer.

o For each prime number appearing in the


factorization, take the highest power of that prime.
o Multiply these highest powers together to get the
LCM.
2. Using GCD (Greatest Common Divisor):
o The LCM of two numbers can be found using their
GCD with the formula:
o LCM(a,b)= (a×b)/ GCD(a,b)∣
o This formula can be extended to more than two
numbers by iteratively applying it.
Applications

 Adding and Subtracting Fractions: Finding a common denominator.


 Scheduling Problems: Finding intervals at which events coincide.
 Problem Solving in Number Theory: Solving Diophantine equations and modular arithmetic
problems.

Calculation in Code

Here's an example of a Java function to calculate the LCM of two numbers using the GCD
method:

public class LCMCalculator {

public static void main(String[] args) {


int a = 8;
int b = 9;
System.out.println("LCM of " + a + " and " + b + " is: " + lcm(a, b));
}

public static int gcd(int a, int b) {


if (b == 0) {
return a;
}
return gcd(b, a % b);
}

public static int lcm(int a, int b) {


return Math.abs(a * b) / gcd(a, b);
}
}

This code finds the LCM by first calculating the GCD using a recursive function and then
applying the LCM formula.
Word Ladder
Problem:
Given two words (beginWord and endWord), and a dictionary,
find the shortest transformation sequence from beginWord to
endWord such that only one letter can be changed at a time, and
each intermediate word must exist in the dictionary.
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output:
5
Explanation:
"hit" -> "hot" -> "dot" -> "dog" -> "cog" is a sequence of 5
words.

Explanation
1. Initialization:
o Convert the wordList to a Set for faster look-up and to

avoid duplicates.
o Return 0 immediately if the endWord is not in the
wordList.
2. Breadth-First Search (BFS):
o Use a Queue to perform BFS. Start with the
beginWord.
o Initialize length to 1 to count the levels of
transformation sequences.
3. BFS Loop:
o For each word in the queue, check if it matches the
endWord. If so, return the current length.
o Transform each character of the current word to every
other possible character ('a' to 'z'), one character at a
time.
o If the new word exists in the wordSet, add it to the
queue and remove it from the wordSet to prevent re-
processing.
o Restore the original character after processing each
character position.
4. Length Increment:
o Increment the length after processing all words at the
current level of BFS.
5. Return Result:
o If the endWord is found during BFS, return the
transformation length.
o If BFS completes without finding the endWord, return
0.
Detailed BFS Execution:
1. Initialization:
o wordSet = {"hot", "dot", "dog", "lot", "log", "cog"}

o queue = [("hit", 1)]


2. Level 1:
o Dequeue "hit".
o Generate transformations: "ait", "bit", ..., "zit",
"hat", ..., "hzt", "hia", ..., "hiz".
o Valid transformation: "hot".
o Enqueue ("hot", 2), remove "hot" from wordSet.
3. Level 2:
o queue = [("hot", 2)]
o Dequeue "hot".
o Generate transformations: "aot", "bot", ..., "zot",
"hat", ..., "hzt", "hoa", ..., "hoz".
o Valid transformations: "dot", "lot".
o Enqueue ("dot", 3), ("lot", 3), remove "dot" and "lot"
from wordSet.
4. Level 3:
o queue = [("dot", 3), ("lot", 3)]
o Dequeue "dot".
o Generate transformations: "aot", "bot", ..., "zot",
"dat", ..., "dzt", "doa", ..., "doz".
o Valid transformation: "dog".
o Enqueue ("dog", 4), remove "dog" from wordSet.
o Dequeue "lot".
o Generate transformations: "aot", "bot", ..., "zot",
"lat", ..., "lzt", "loa", ..., "loz".
o Valid transformation: "log".
o Enqueue ("log", 4), remove "log" from wordSet.
5. Level 4:
o queue = [("dog", 4), ("log", 4)]
o Dequeue "dog".
o Generate transformations: "aog", "bog", ..., "zog",
"dag", ..., "dzg", "doa", ..., "doz".
o Valid transformation: "cog".
o Enqueue ("cog", 5), remove "cog" from wordSet.
o Since "cog" is the endWord, return the current level,
which is 5.

Problem: Trap Rain Water


Given an array height representing the elevation map where the width of each bar
is 1, compute how much water it can trap after raining.
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation:
 The elevation map [0,1,0,2,1,0,1,3,2,1,2,1] is represented by vertical bars.
 After the rain, the amount of water trapped between the bars is 6 units.
Optimal Approach: Two-Pointer Technique
To solve this problem optimally, you can use a two-pointer technique. The idea is
to maintain two pointers (left and right), and use two variables (leftMax and
rightMax) to keep track of the maximum heights seen from the left and right sides
respectively.

Explanation:
 Two Pointers (left and right): Start from both ends of the array.
 Track Maximum Heights (leftMax and rightMax): As you move the
pointers inward, keep track of the highest bar seen so far from the left and
right.
 Calculate Water:
o If height[left] < height[right], the trapped water at left depends on
leftMax. If leftMax is greater than height[left], water can be trapped.
o Otherwise, calculate the trapped water using rightMax when
height[right] is less than or equal to height[left].
 Move Pointers: Depending on the comparison of heights at left and right,
move the corresponding pointer inward.
Time Complexity:
 The time complexity is O(n) where n is the length of the height array. This is
because each element is processed at most once.
Space Complexity:
 The space complexity is O(1) since only a few extra variables are used.
This approach is efficient and solves the problem in linear time.

You might also like