Skip to content

Commit 80472b5

Browse files
authored
Merge branch 'main' into main
2 parents 5b0ad9f + 8b68bdf commit 80472b5

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

Programs/PrintPattern.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Program to print a pattern using nested for loops
3+
* eg:- *
4+
* * *
5+
* * * *
6+
* * * * *
7+
* * * * * *
8+
* @author arshcoder13 - https://github.com/arshcoder13/
9+
**/
10+
11+
public class PrintPattern {
12+
13+
public static void main(String args[]){
14+
15+
for(int i=1;i<=5;i++){
16+
for(int j=1;j<=i;j++){
17+
System.out.print("* ");
18+
}
19+
System.out.println();
20+
}
21+
22+
}
23+
24+
}

Programs/Two_Sum.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//In this code a array is taken along with an target element. The sum of two elements matching with the target element is displayed
2+
3+
4+
import java.util.*;
5+
public class Two_Sum {
6+
public static void main(String args[])
7+
{
8+
try (Scanner sc = new Scanner(System.in)) {
9+
int n = sc.nextInt();
10+
// length of the array is taken
11+
int a[] = new int[n];
12+
for(int i = 0; i < n; i++)
13+
{
14+
a[i] = sc.nextInt();
15+
}
16+
int target=sc.nextInt();
17+
// target value is initialzized
18+
int array[] = new int[2];
19+
a=twoSum(array,target);
20+
// method is called
21+
for(int i=0;i<2;i++)
22+
{
23+
System.out.print(a[i]+" ");
24+
}
25+
System.out.println();
26+
}
27+
}
28+
public static int[] twoSum(int[] n, int target) {
29+
Map<Integer,Integer> nm=new HashMap<>();
30+
// hashmap is created to store the array elements
31+
int array[]=new int[2];
32+
int k=n.length;
33+
for(int i=0;i<k;i++)
34+
{
35+
if(nm.containsKey(target-n[i]))
36+
{
37+
array[0]=nm.get(target-n[i]);
38+
array[1]=i;
39+
break;
40+
// The sum of the elements equals to the target is storee
41+
}
42+
nm.put(n[i],i);
43+
}
44+
return array;
45+
// the answer array is returned
46+
}
47+
}

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ It is very easy to contribute, you may follow these steps -
111111
84. [Sieve of Eratosthenes](https://github.com/siddhant2002/Java-1/blob/main/Programs/Sieve_Of_Eratosthenes.java) - Program to multiply two matrices.
112112
85. [Reverse a LinkedList](https://github.com/siddhant2002/Java-1/blob/main/Programs/ReverseList.java) - Program to reverse a linkedList.
113113
86. [Peg Solitaire](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Games/peg-solitaire/) - Peg Solitaire Game
114-
87. [ToDO List](./Programs/ToDoList.java) - Simple ToDo list program.
115-
88. [Ceaser Cipher Encryption](./Programs/CeaserCipher.java) - Encrypt Text with Ceaser Cipher Methodology.
116-
89. [Recursion](./Programs/Check_If_Sorted.java) - Check if an Array is sorted using recursion.
117-
90. [Patterns](./Programs/patterns) - Print out patterns in terminal (Steps included).
114+
87. [PrintPattern](https://github.com/arshcoder13/Java/blob/main/Programs/PrintPattern.java) - Print Pattern using Nested For Loops.
115+
88. [Two Sum](https://github.com/siddhant2002/Java-1/blob/main/Programs/Two_Sum.java) - Sum of two elements which is equal to the target element.
116+
89. [ToDO List](./Programs/ToDoList.java) - Simple ToDo list program.
117+
90. [Ceaser Cipher Encryption](./Programs/CeaserCipher.java) - Encrypt Text with Ceaser Cipher Methodology.
118+
91. [Recursion](./Programs/Check_If_Sorted.java) - Check if an Array is sorted using recursion.
119+
92. [Patterns](./Programs/patterns) - Print out patterns in terminal (Steps included).
120+
118121
# Contributors -
119122
## A big thank you to all our contributors!!!
120123

0 commit comments

Comments
 (0)