Skip to content

Commit 695b580

Browse files
Merge pull request #148 from wakandarajyam/main
Improved Readability and Fixed errors
2 parents 8f7c601 + 843c517 commit 695b580

7 files changed

+171
-214
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
https://practice.geeksforgeeks.org/problems/find-n-th-term-of-series-1-3-6-10-15-215506/1/?category%5B%5D=Mathematical&category%5B%5D=Mathematical&difficulty%5B%5D=-2&difficulty%5B%5D=-1&difficulty%5B%5D=0&page=1&query=category%5B%5DMathematicaldifficulty%5B%5D-2difficulty%5B%5D-1difficulty%5B%5D0page1category%5B%5DMathematical#
2-
1+
//Question Link: https://practice.geeksforgeeks.org/problems/find-n-th-term-of-series-1-3-6-10-15-215506/1
32
class Solution {
3+
4+
//O(N) Solution
45
static int findNthTerm(int N) {
56
int sum = 0;
67
for(int i = 0; i<=N; i++) {
78
sum += i;
89
}
9-
return sum;
10+
return sum;
1011
}
11-
};
12-
13-
// or
14-
15-
return (N*N+N)/2;
16-
12+
13+
//O(1) -> Simple Mathematical formula to calculate sum of first N natural Numbers
14+
static int findNthTerm(int N) {
15+
return (N*(N+1))>>1;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,54 @@
1-
2-
3-
// using tree data structure.
4-
class Compute
5-
{
6-
static pair getMinMax(long a[], long n)
7-
{
8-
9-
TreeSet<Long> tree = new TreeSet<>();
10-
for(long arr : a){
11-
tree.add(arr);
12-
}
13-
14-
return new pair (tree.first(), tree.last());
15-
}
1+
import java.util.TreeSet;
2+
3+
class Compute {
4+
// using tree data structure.
5+
// O(NlogN)
6+
static pair getMinMax1(long a[], long n) {
7+
8+
TreeSet<Long> tree = new TreeSet<>();
9+
for (long arr : a) {
10+
tree.add(arr);
11+
}
12+
13+
return new pair(tree.first(), tree.last());
14+
}
15+
16+
// using one loop and min max.
17+
// O(N)
18+
static pair getMinMax(long a[], long n) {
19+
if (n == 0)
20+
throw new RuntimeException("Can't find min and max for array of length 0.");
21+
// Write your code here
22+
long max = a[0], min = a[0];
23+
24+
for (int i = 0; i < a.length; i++) {
25+
if (a[i] > max) {
26+
max = a[i];
27+
}
28+
if (a[i] < min) {
29+
min = a[i];
30+
}
31+
}
32+
return new pair(min, max);
33+
}
1634
}
1735

36+
class pair {
37+
long min, max;
1838

19-
20-
// using one loop and min max.
21-
22-
23-
class Compute
24-
{
25-
static pair getMinMax(long a[], long n)
26-
{
27-
//Write your code here
28-
long max = Integer.MIN_VALUE;
29-
long min = Integer.MAX_VALUE;
30-
31-
for(int i =0; i<a.length; i++){
32-
if(a[i]>max){
33-
max = a[i];
34-
}
35-
if(a[i]<min){
36-
min = a[i];
37-
}
38-
}
39-
return new pair(min,max);
40-
}
39+
pair(Long long1, Long long2) {
40+
min = long1;
41+
max = long2;
42+
}
4143
}
4244

45+
public class Main {
46+
public static void main(String[] args) {
4347

48+
long a[] = { 1, 7, -1, 6, 9, -100 };
4449

45-
// 3
46-
47-
48-
int n = arr.length;
49-
int j =n-1;
50-
int min = arr[0];
51-
int max = arr[0];
52-
while(j>0){
53-
if(arr[j]>max){
54-
max=arr[j];
55-
}
56-
if(arr[j]<min){
57-
min=arr[j];
58-
}
59-
j--;
60-
}
61-
System.out.print(min+" "+max);
62-
63-
64-
//4
65-
50+
pair pair = Compute.getMinMax(a, a.length);
6651

67-
public class Main {
68-
public static void main(String[] args) {
69-
70-
int n = arr.length;
71-
int ans = 0;
72-
int i = 0;
73-
int j = n-1;
74-
while(i<j){
75-
if(arr[j]>arr[i]){
76-
j--;
77-
}else{
78-
i++;
79-
}
80-
}
81-
ans = arr[i];
82-
System.out.print(ans);
83-
}
52+
System.out.printf("Min: %d and Max: %d.\n", pair.min, pair.max);
53+
}
8454
}

src/Java/GFG/KthSmallestElement.java

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
// TLE if using PriorityQueue
2-
3-
class Solution{
4-
public static int kthSmallest(int[] arr, int l, int r, int k)
5-
{
6-
7-
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
8-
9-
for(int i =0; i<arr.length; i++){
10-
pq.add(arr[i]);
11-
if(pq.size()>k){
12-
pq.poll();
13-
}
14-
}
15-
return pq.peek();
16-
}
1+
import java.util.Comparator;
2+
import java.util.PriorityQueue;
3+
4+
class Solution {
5+
6+
// O(N logK)
7+
public static int kthSmallest(int[] arr, int k) {
8+
9+
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
10+
11+
for (int i = 0; i < arr.length; i++) {
12+
pq.add(arr[i]);
13+
if (pq.size() > k) {
14+
pq.poll();
15+
}
16+
}
17+
return pq.peek();
18+
}
19+
20+
// sorting
21+
// O(N*N)
22+
public static int kthSmallest2(int[] arr, int k) {
23+
24+
Arrays.sort(arr);
25+
return arr[k];
26+
}
1727
}
1828

1929

20-
// sorting
30+
2131

src/Java/GFG/KthSmallestElementPriortyQueue.java

-18
This file was deleted.
+46-56
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,50 @@
1-
https://practice.geeksforgeeks.org/problems/missing-number-in-array1416/1#
2-
3-
class Solution {
4-
int MissingNumber(int array[], int n) {
5-
int [] arr = array;
6-
long sum =0; // sum is 10;
7-
for(int i =0; i<=n; i++){
8-
sum +=i; // in which doing sum of 1 ,3 4, 5
9-
}
10-
long sum2 = 0;
11-
for(int i=0; i<n-1; i++){
12-
sum2+=arr[i]; // in which sum n-1 like 1 , 2, 3 = 6
13-
}
14-
return (int) (sum-sum2); // 10-6 = 4
15-
}
16-
}
17-
18-
19-
20-
class Solution {
21-
int MissingNumber(int array[], int n) {
22-
Arrays.sort(array); // sorting the array starting and ending
23-
for(int i =0; i<array.length; i++){
24-
if(array[i] != i+1){ // in this. arr[i] = 3 & i=3
25-
// and +1 3!=4 then i=3 and +1 return 3+1 =4
26-
return i+1;
27-
}
28-
}
29-
return n;
30-
}
31-
}
32-
1+
import java.util.Arrays;
332

3+
//https://practice.geeksforgeeks.org/problems/missing-number-in-array1416/1#
344

355
class Solution {
36-
int MissingNumber(int array[], int n) {
37-
int sum = (n*(n+1))/2; // 10
38-
for(int i=0;i<n-1;i++)
39-
{
40-
sum -= array[i];
41-
// 10 -1 , 9-2 , 7 -3 , sum = 4 return 4
42-
}
43-
return sum;
44-
}
6+
int missingNumber(int array[], int n) {
7+
int[] arr = array;
8+
long sum = 0; // sum is 10;
9+
10+
for (int i = 0; i <= n; i++) {
11+
sum += i; // in which doing sum of 1 ,3 4, 5
12+
}
13+
14+
long sum2 = 0;
15+
for (int i = 0; i < n - 1; i++) {
16+
sum2 += arr[i]; // in which sum n-1 like 1 , 2, 3 = 6
17+
}
18+
return (int) (sum - sum2); // 10-6 = 4
19+
}
20+
21+
int missingNumber2(int array[], int n) {
22+
Arrays.sort(array);
23+
for (int i = 0; i < array.length; i++) {
24+
if (array[i] != i + 1) {
25+
// in this. arr[i] = 3 & i=3
26+
// and +1 3!=4 then i=3 and +1 return 3+1 =4
27+
return i + 1;
28+
}
29+
}
30+
return n;
31+
}
32+
33+
int missingNumber3(int array[], int n) {
34+
int sum = (n * (n + 1)) / 2; // 10
35+
for (int i = 0; i < n - 1; i++) {
36+
sum -= array[i];
37+
//10 -1 , 9-2 , 7 -3 , sum = 4 return 4
38+
}
39+
return sum;
40+
}
41+
42+
int missingNumber4(int array[], int n) {
43+
Arrays.sort(array);
44+
for (int i = 1; i < n; i++) {
45+
if (array[i - 1] != i)
46+
return i;
47+
}
48+
return n;
49+
}
4550
}
46-
47-
48-
49-
50-
class Solution {
51-
int MissingNumber(int array[], int n) {
52-
Arrays.sort(array);
53-
for(int i=1;i<n;i++){
54-
if(array[i-1]!=i) return i;
55-
}
56-
return n ;
57-
}
58-
}
59-
60-

src/Java/GFG/Prime Number.java

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
https://practice.geeksforgeeks.org/problems/prime-number2314/0/?category%5B%5D=Mathematical&category%5B%5D=Mathematical&difficulty%5B%5D=-2&difficulty%5B%5D=-1&difficulty%5B%5D=0&page=1&query=category%5B%5DMathematicaldifficulty%5B%5D-2difficulty%5B%5D-1difficulty%5B%5D0page1category%5B%5DMathematical#
1+
//Question Link: https://practice.geeksforgeeks.org/problems/prime-number2314/0/
2+
class Solution {
3+
static int isPrime(int N) {
4+
// code here
5+
if (N == 1 || N == 0) {
6+
return 0;
7+
}
8+
if (N == 2) {
9+
return 1;
10+
}
11+
for (int i = 2; i * i <= N; i++) {
12+
if (N % i == 0) {
13+
return 0;
14+
}
15+
}
216

3-
class Solution{
4-
static int isPrime(int N){
5-
// code here
6-
if(N==1 || N==0){
7-
return 0;
8-
}
9-
if(N==2){
10-
return 1;
11-
}
12-
for(int i=2; i*i<=N; i++){
13-
if(N%i==0){
14-
return 0;
15-
}
16-
}
17-
18-
return 1;
19-
}
20-
}
17+
return 1;
18+
}
19+
}

0 commit comments

Comments
 (0)