Skip to content

Add SpiralMatrix.java to print a matrix in spiral order #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Developer.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
43 changes: 43 additions & 0 deletions Programs/Fibo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Programs;

import java.util.*;

public class Fibo {

public static void main(String[] args) {
// Print first n Fibonacci numbers
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
sc.close();

int a = 0, b = 1;
System.out.println("Fibonacci Series:");
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int c = a + b;
a = b;
b = c;
}

// Dynamic Programming approach to find nth Fibonacci number
int term = 10; // Change this to desired term index
System.out.println("\n\n" + term + "th Fibonacci number using DP: " + fibdp(term));
}

// Bottom-up Dynamic Programming approach
public static int fibdp(int n) {
if (n == 0) return 0;
if (n == 1) return 1;

int[] storage = new int[n + 1];
storage[0] = 0;
storage[1] = 1;

for (int i = 2; i <= n; i++) {
storage[i] = storage[i - 1] + storage[i - 2];
}

return storage[n];
}
}
39 changes: 39 additions & 0 deletions Programs/MajorityElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Programs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MajorityElement {
private static List<Integer> majorityElement(int[] v){

int n = v.length;
List<Integer> ans = new ArrayList<>();
int min = n/3 +1;
HashMap<Integer,Integer> mpp = new HashMap<>();

for (int j : v) {
int val = mpp.getOrDefault(j, 0);
mpp.put(j, val + 1);

if (mpp.get(j) == min) {
ans.add(j);
}

if(ans.size() == 2)
break;
}

return ans;
}

public static void main(String[] args) {
int[] arr = {1,2,2,2,1,1};
List<Integer> ans = majorityElement(arr);
System.out.print("The majority elements are: ");
for (Integer an : ans) {
System.out.print(an + " ");
}
System.out.println();
}
}
48 changes: 48 additions & 0 deletions Programs/SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Programs;

import java.util.ArrayList;
import java.util.List;

public class SpiralMatrix {
public static void main(String[] args){
int[][] mat= {{1, 2, 3,4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};
List<Integer> ans = spiral(mat);
System.out.println(ans);
}

public static List<Integer> spiral(int[][] matrix){

List<Integer> ans = new ArrayList<>();

int n = matrix.length, m = matrix[0].length;
int left = 0, top = 0;
int right = m-1, bottom = n-1;

while(top<=bottom && left <= right){

for(int i = left; i <= right;i++){
ans.add(matrix[top][i]);
}
top++;

for(int i = top;i <= bottom; i++){
ans.add(matrix[i][right]);
}
right--;

for(int i = right; i >= left; i--){
ans.add(matrix[bottom][i]);
}

bottom --;

for(int i = bottom; i>= top; i--){
ans.add(matrix[i][left]);
}

left++;

}
return ans;
}
}
60 changes: 60 additions & 0 deletions Programs/TrafficSignal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package Programs;

class TrafficSignal
{
private String colour;

private TrafficSignal(String colour)
{
this.colour=colour;
}
private boolean isRed()
{
return colour.equals("red");
}
private boolean isYellow()
{
return colour.equals("yellow");
}
private boolean isGreen()
{
return colour.equals("green");
}

//Driver Code
public static void main(String[] args)
{
TrafficSignal light=new TrafficSignal("red");

while(true)
{
if(light.isRed())
{
System.out.println("The Signal is red.");
light.colour = "yellow";
}
else if(light.isYellow())
{
System.out.println("The Signal is yellow.");
light.colour = "green";
}
else if(light.isGreen())
{
System.out.println("The Signal is green.");
light.colour = "red";
}
for(int i = 10; i > 0; i--)
{
try{
System.out.print(" "+i);
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Delay not applied.");
}
}
System.out.println();
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
}
46 changes: 0 additions & 46 deletions Programs/fibo.java

This file was deleted.

3 changes: 3 additions & 0 deletions out/production/Developer/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/Developer/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions out/production/Developer/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/Developer/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading