Accenture JAVA CODE ON METHODS
Accenture JAVA CODE ON METHODS
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.
Calculation in Code
Here's an example of a Java function to calculate the LCM of two numbers using the GCD
method:
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"}
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.