Skip to content

Commit f5960ba

Browse files
committed
2 parents 093eee0 + 2ad76e0 commit f5960ba

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using two ArrayLists, add all even numbers to one list and all odds to another. Do another pass to insert them at the correct indices.
2+
3+
https://leetcode.com/problems/sort-array-by-parity-ii/discuss/194416/java-easy-to-understand-arraylist-solution
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.ArrayList;
2+
3+
class Solution {
4+
public int[] sortArrayByParityII(int[] A) {
5+
ArrayList<Integer> evens = new ArrayList<Integer>();
6+
ArrayList<Integer> odds = new ArrayList<Integer>();
7+
for(int i = 0; i < A.length; i++){
8+
if(A[i]%2==0)
9+
evens.add(A[i]);
10+
else
11+
odds.add(A[i]);
12+
}
13+
int index = 0;
14+
for(Integer val: evens){
15+
A[index] = val;
16+
index += 2;
17+
}
18+
index = 1;
19+
for(Integer val: odds){
20+
A[index] = val;
21+
index += 2;
22+
}
23+
return A;
24+
}
25+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.DataStructures_And_Algorithms.Graph;
2+
import java.util.ArrayList;
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
class City {
7+
int index;
8+
String name;
9+
String code;
10+
LinkedList<City> connectedCity = new LinkedList<>();
11+
12+
public City(int i, String name, String code) {
13+
index = i;
14+
this.name = name;
15+
this.code = code;
16+
}
17+
}
18+
19+
public class GraphProject {
20+
21+
int size =0;
22+
List<City> cities = new ArrayList<>();
23+
24+
void addCity(String code, String name){
25+
int i = size++;
26+
City city = new City(i,name, code);
27+
cities.add(city);
28+
}
29+
30+
City cityLookup(String code){
31+
for(City c:cities){
32+
if(c.code.equalsIgnoreCase(code)){
33+
return c;
34+
}
35+
}
36+
return null;
37+
}
38+
39+
void connectCity(String code1, String code2){
40+
City c1 = cityLookup(code1);
41+
City c2 = cityLookup(code2);
42+
if( c1 == null || c2 == null){
43+
System.out.println("Cannot connect, Invalid city code.");
44+
return;
45+
}
46+
c1.connectedCity.add(c2);
47+
}
48+
49+
private void findBFSpath(String code) {
50+
boolean[] visited = new boolean[size];
51+
City city = cityLookup(code);
52+
System.out.println("\n printing BFS path from city "+ city.name);
53+
LinkedList<City> linkedCity = new LinkedList<>();
54+
linkedCity.add(city);
55+
visited[city.index] = true;
56+
57+
while(!linkedCity.isEmpty()){
58+
City currentCity = linkedCity.poll();
59+
System.out.println(" >> "+ currentCity.name);
60+
61+
for(City cc:currentCity.connectedCity){
62+
if(!visited[cc.index]){
63+
visited[cc.index] = true;
64+
linkedCity.add(cc);
65+
}
66+
}
67+
}
68+
69+
}
70+
71+
private void findDFSpath(String code) {
72+
boolean[] visited = new boolean[size];
73+
City city = cityLookup(code);
74+
System.out.println("printing DFS path from city "+ city.name);
75+
DFS(city, visited);
76+
}
77+
78+
private void DFS(City city, boolean[] visited) {
79+
visited[city.index] = true;
80+
for(City c :city.connectedCity){
81+
if(!visited[c.index]){
82+
DFS(c,visited);
83+
}
84+
}
85+
System.out.println(" >> "+city.name);
86+
}
87+
public static void main(String[] args) {
88+
89+
GraphProject g = new GraphProject();
90+
91+
//Adding cities
92+
g.addCity("LR", "Larkana");
93+
g.addCity("SK", "Sukkur");
94+
g.addCity("NW" , "Nawabshah");
95+
g.addCity("JM", "Jamshoro");
96+
97+
//Connecting cities
98+
g.connectCity("LR", "SK");
99+
g.connectCity("SK", "NW");
100+
g.connectCity("NW", "JM");
101+
102+
g.addCity("HY", "Hyderabad");
103+
g.addCity("KR", "Karachi");
104+
g.addCity("IS","Islamabad");
105+
g.addCity("RP", "Rawalpindi");
106+
107+
g.connectCity("HY", "KR");
108+
g.connectCity("KR", "IS");
109+
g.connectCity("IS", "RP");
110+
111+
// finding DFS path from Larkana
112+
g.findDFSpath("LR");
113+
114+
// finding BFS path from Hyder
115+
g.findBFSpath("HY");
116+
117+
118+
}
119+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int arrayPairSum(vector<int>& nums) {
4+
int n = nums.size();
5+
sort(nums.begin(),nums.end());
6+
int total=0;
7+
for(int i=0;i<n;i++)
8+
{
9+
if(i%2==0)
10+
{
11+
total += min(nums[i],nums[i+1]);
12+
}
13+
}
14+
return total;
15+
}
16+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h2>561. Array Partition I</h2><h3>Easy</h3><hr><div><p>Given an integer array <code>nums</code> of <code>2n</code> integers, group these integers into <code>n</code> pairs <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> such that the sum of <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> for all <code>i</code> is <strong>maximized</strong>. Return<em> the maximized sum</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong>Example 1:</strong></p>
5+
6+
<pre><strong>Input:</strong> nums = [1,4,3,2]
7+
<strong>Output:</strong> 4
8+
<strong>Explanation:</strong> All possible pairings (ignoring the ordering of elements) are:
9+
1. (1, 4), (2, 3) -&gt; min(1, 4) + min(2, 3) = 1 + 2 = 3
10+
2. (1, 3), (2, 4) -&gt; min(1, 3) + min(2, 4) = 1 + 2 = 3
11+
3. (1, 2), (3, 4) -&gt; min(1, 2) + min(3, 4) = 1 + 3 = 4
12+
So the maximum possible sum is 4.</pre>
13+
14+
<p><strong>Example 2:</strong></p>
15+
16+
<pre><strong>Input:</strong> nums = [6,2,6,5,1,2]
17+
<strong>Output:</strong> 9
18+
<strong>Explanation:</strong> The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
<p><strong>Constraints:</strong></p>
23+
24+
<ul>
25+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
26+
<li><code>nums.length == 2 * n</code></li>
27+
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
28+
</ul>
29+
</div>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
class Solution{
6+
public:
7+
vector<int> duplicates(int arr[], int n) {
8+
9+
vector<int> v;
10+
int last;
11+
sort(arr,arr+n);
12+
for(int i=0;i<n;i++){
13+
if(arr[i]==arr[i+1] && arr[i]!=last)
14+
{v.push_back(arr[i]);
15+
last=arr[i];
16+
}
17+
}
18+
if(v.size()==0)
19+
v.push_back(-1);
20+
return v;
21+
}
22+
};
23+
24+
25+
int main() {
26+
27+
int n;
28+
cin >> n;
29+
int a[n];
30+
for (int i = 0; i < n; i++) cin >> a[i];
31+
Solution obj;
32+
vector<int> ans = obj.duplicates(a, n);
33+
for (int i : ans) cout << i << ' ';
34+
cout << endl;
35+
36+
return 0;
37+
}
38+
39+

0 commit comments

Comments
 (0)