We are tasked with determining whether a cycle exists in an
array arr[0..n-1] that contains both positive and negative integers, following
specific movement rules. If the value at index i is positive, the next index to
visit is calculated by moving forward arr[i]%n steps, which is expressed as (i
+ arr[i])%n. Conversely, if the value is negative, the movement is backward by
arr[i]%n steps, represented as (i - arr[i])%n. Here, n denotes the total number
of elements in the array. A value of arr[i]%n equal to zero indicates that
there is no movement from index i.
To detect circular connections in an array, identifying
repeated elements and flagging them as part of a circular loop. However, the
code has a couple of logical issues that need to be addressed.
Code
package
com.demo; import
java.util.HashMap; import
java.util.Map; public class
CheckArrayLoop { public static void main(String[] args) { int mm[] = {4, 3, 2, 1, 2}; // Printing the array System.out.print("Array:
"); for (int i = 0; i < mm.length;
i++) { System.out.print(mm[i] +
"->"); } // Calling the function to detect
cycles cycleOfArray(mm); } // Function to detect repeated elements
(potential circular connections) public static void cycleOfArray(int
mat[]) { Map<Integer, Integer> map = new
HashMap<>(); // Populate the map with the
frequency of each element for (int i = 0; i < mat.length;
i++) { map.put(mat[i],
map.getOrDefault(mat[i], 0) + 1); } boolean foundCycle = false; System.out.println("\nChecking
for circular connections:"); // Check for circular connections
(repeated elements) for (Map.Entry<Integer,
Integer> entry : map.entrySet()) { if (entry.getValue() > 1) {
System.out.println("Circular connection found with element:
" + entry.getKey()); foundCycle = true; } } // If no cycles are found, print the
corresponding message if (!foundCycle) { System.out.println("No
circular connections found."); } } } |
Explanation:
1. Array Initialization and Display:
In the main method, an array mm is initialized:
java
int mm[] =
{4, 3, 2, 1, 2}; |
This part of the code works as intended, outputting the
elements of the array.
for(int i =
0; i < mm.length; i++){ System.out.print(mm[i] +
"->"); } |
2. Detecting Cyclic Repetitions:
The function cycleOfArray attempts to identify repeated
elements using a Map. The logic populates a map with the frequency of each
element:
java
Map<String,
Integer> map = new HashMap<>(); for (int i =
0; i < mat.length; i++) { if (map.size() > 0 &&
map.containsKey(String.valueOf(mat[i]))) { map.put(String.valueOf(mat[i]),
map.get(String.valueOf(mat[i])) + 1); } else { map.put(String.valueOf(mat[i]), 1); } } |
3. Circular Detection Logic:
The following loop attempts to check if there are repeated
elements:
java
boolean flag
= false; for (int i =
0; i < mat.length; i++) { if (map.size() > 0 &&
map.containsKey(String.valueOf(mat[i])) &&
map.get(String.valueOf(mat[i])) > 1) { System.out.println("Circular
connection" + mat[i]); flag = true; } if (i == mat.length && !flag) { System.out.println("No search
circule"); } } |
Changes Made:
- Simplified
the map population: Used map.getOrDefault(mat[i], 0) + 1 to handle
inserting or updating the count of elements in the map.
- Corrected
the cycle detection: The map is now iterated after it is populated to
check for elements with a count greater than 1, indicating a circular
connection.
- Output:
Ø
The program prints all elements that have
circular connections.
Ø
If no circular connections are found, it
displays a clear message indicating that.
Example Output:
For the array {4, 3, 2, 1, 2}, the output will be:
plaintext
Array:
4->3->2->1->2-> Checking for
circular connections: Circular
connection found with element: 2 |
To detect circular connections in an array |
0 Comments