Skip to content

Commit 6896479

Browse files
commit
1 parent 3d62494 commit 6896479

15 files changed

+530
-1
lines changed

3.Array/LeetCode/Program2.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
3+
80. Remove Duplicates from Sorted Array II
4+
Example 1:
5+
Input: nums = [1,1,1,2,2,3]
6+
Output: 5, nums = [1,1,2,2,3,_]
7+
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
8+
It does not matter what you leave beyond the returned k (hence they are underscores).
9+
10+
Example 2:
11+
Input: nums = [0,0,1,1,1,1,2,3,3]
12+
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
13+
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
14+
It does not matter what you leave beyond the returned k (hence they are underscores).
15+
16+
*/
17+
18+
package LeetCode;
19+
20+
public class Program2 {
21+
public static void main(String[] args) {
22+
23+
int nums[] = {0,0,1,1,1,1,2,3,3,3};
24+
25+
int cnt=0;
26+
int ind=1;
27+
28+
for(int i=1;i<nums.length;i++){
29+
30+
if(nums[i]==nums[i-1]){
31+
32+
cnt++;
33+
}
34+
else{
35+
cnt=0;
36+
}
37+
38+
if(cnt <=1){
39+
nums[ind] = nums[i];
40+
ind++;
41+
}
42+
}
43+
44+
45+
for(int i=0;i<nums.length;i++){
46+
47+
System.out.println(nums[i]+" ");
48+
}
49+
50+
}
51+
}

3.Array/LeetCode/Program4.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/*
3+
215. Kth Largest Element in an Array
4+
Medium
5+
6+
Given an integer array nums and an integer k, return the kth largest element in the array.
7+
8+
Note that it is the kth largest element in the sorted order, not the kth distinct element.
9+
10+
You must solve it in O(n) time complexity.
11+
12+
Example 1:
13+
Input: nums = [3,2,1,5,6,4], k = 2
14+
Output: 5
15+
16+
Example 2:
17+
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
18+
Output: 4
19+
20+
*/
21+
22+
23+
package LeetCode;
24+
25+
public class Program4 {
26+
public static void main(String[] args) {
27+
28+
int nums[] = {3,2,3,1,2,4,5,5,6};
29+
30+
for(int i=0;i<nums.length;i++){
31+
32+
for(int j=i+1;j<nums.length;j++){
33+
34+
if(nums[i] > nums[j]){
35+
36+
int temp = nums[i];
37+
nums[i] =nums[j];
38+
nums[j] = temp;
39+
}
40+
}
41+
}
42+
43+
for(int i=0;i<nums.length;i++){
44+
System.out.print(nums[i]+" ");
45+
}
46+
47+
48+
49+
}
50+
}

3.Array/LeetCode/Program5.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
2562. Find the Array Concatenation Value
3+
4+
You are given a 0-indexed integer array nums.
5+
6+
The concatenation of two numbers is the number formed by concatenating their numerals.
7+
8+
For example, the concatenation of 15, 49 is 1549.
9+
The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:
10+
11+
If there exists more than one number in nums, pick the first element and last element in nums respectively and add the value of their concatenation to the concatenation value of nums, then delete the first and last element from nums.
12+
If one element exists, add its value to the concatenation value of nums, then delete it.
13+
Return the concatenation value of the nums.
14+
15+
16+
17+
Example 1:
18+
Input: nums = [7,52,2,4]
19+
Output: 596
20+
Explanation: Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.
21+
- In the first operation:
22+
We pick the first element, 7, and the last element, 4.
23+
Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74.
24+
Then we delete them from nums, so nums becomes equal to [52,2].
25+
- In the second operation:
26+
We pick the first element, 52, and the last element, 2.
27+
Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596.
28+
Then we delete them from the nums, so nums becomes empty.
29+
Since the concatenation value is 596 so the answer is 596.
30+
31+
32+
Example 2:
33+
Input: nums = [5,14,13,8,12]
34+
Output: 673
35+
Explanation: Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.
36+
- In the first operation:
37+
We pick the first element, 5, and the last element, 12.
38+
Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512.
39+
Then we delete them from the nums, so nums becomes equal to [14,13,8].
40+
- In the second operation:
41+
We pick the first element, 14, and the last element, 8.
42+
Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660.
43+
Then we delete them from the nums, so nums becomes equal to [13].
44+
- In the third operation:
45+
nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673.
46+
Then we delete it from nums, so nums become empty.
47+
Since the concatenation value is 673 so the answer is 673.
48+
*/
49+
50+
51+
package LeetCode;
52+
53+
54+
public class Program5 {
55+
public static void main(String[] args) {
56+
57+
int nums[] = {7,52,2,4};
58+
int left = 0;
59+
int right = nums.length-1;
60+
int sum=0;
61+
while(left<=right){
62+
63+
if(left==right){
64+
65+
sum += nums[left];
66+
}
67+
68+
else{
69+
70+
int n = nums[right];
71+
int cnt=0;
72+
73+
while(n!=0){
74+
n/=10;
75+
cnt++;
76+
}
77+
78+
int x =((int) (Math.pow(10, cnt)));
79+
sum += nums[left] * x +nums[right];
80+
81+
82+
83+
}
84+
left ++;
85+
right--;
86+
87+
}
88+
89+
System.out.println(sum+" ");
90+
91+
}
92+
}

3.Array/LeetCode/program3.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
3+
Write the program to print the distict element from the array.
4+
5+
6+
*/
7+
8+
9+
package LeetCode;
10+
11+
import java.io.BufferedReader;
12+
import java.io.InputStreamReader;
13+
14+
public class program3 {
15+
public static void main(String[] args)throws Exception {
16+
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
19+
int size= Integer.parseInt(br.readLine());
20+
21+
System.out.println("Enter the size of the array: ");
22+
int a[] = new int[size];
23+
24+
System.out.println("Enter the element of the array: ");
25+
for(int i=0;i<a.length;i++){
26+
27+
a[i] = Integer.parseInt(br.readLine());
28+
}
29+
30+
for(int i=0;i<a.length;i++){
31+
32+
for(int j=i+1;j<a.length;j++){
33+
34+
if(a[i] > a[j]){
35+
36+
int temp = a[i];
37+
a[i] = a[j];
38+
a[j] = temp;
39+
}
40+
}
41+
}
42+
43+
44+
int i=0;
45+
for( i=1;i<=a.length-1;i++){
46+
47+
if(a[i] !=a[i-1]){
48+
49+
System.out.print(a[i-1]+" ");
50+
}
51+
}
52+
53+
54+
if(a[a.length-1]!=a[a.length-2]){
55+
56+
System.out.print(a[a.length-1]+" end");
57+
}
58+
59+
}
60+
61+
}
62+

3.Array/PracticeCodes/Program18.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void main(String[] args)throws IOException{
7676

7777
int[] a = new int[size];
7878

79-
System.out.println("Enter the aay Elements : ");
79+
System.out.println("Enter the array Elements : ");
8080
for(int i = 0 ; i < size; i++){
8181

8282
a[i] = Integer.parseInt(br.readLine());

3.Array/PracticeCodes/Program19.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Program : Given an array of size N-1 such that it only contains distinct integers
3+
in the range of 1 to N. Find the missing element.
4+
5+
Input:
6+
N = 5
7+
A[] = {1,2,3,5}
8+
Output: 4
9+
10+
Input:
11+
N = 10
12+
A[] = {6,1,2,8,3,4,7,10,5}
13+
Output: 9
14+
*/
15+
16+
17+
package PracticeCodes;
18+
19+
public class Program19 {
20+
21+
public static void main(String[] args) {
22+
23+
int a[] = {6,1,2,8,3,4,7,10,5};
24+
25+
for(int i=0;i<a.length;i++){
26+
27+
for(int j=i+1;j<a.length;j++){
28+
29+
if(a[i] > a[j]){
30+
31+
int temp = a[i];
32+
a[i] =a[j];
33+
a[j] = temp;
34+
}
35+
}
36+
}
37+
38+
for(int x= 0;x<a.length;x++){
39+
40+
System.out.print(a[x]+" ");
41+
}
42+
43+
int i=0;
44+
45+
int diff = a[i+1] - a[i];
46+
47+
boolean flag = false;
48+
49+
for(int j=0;j<a.length-1;j++){
50+
51+
if(a[j+1]-a[j]!=diff){
52+
53+
flag = true;
54+
55+
System.out.println("The missing element is: "+(a[j]+diff));
56+
}
57+
58+
}
59+
60+
if(!flag){
61+
System.out.println("There is no missing element in array...!");
62+
}
63+
64+
65+
}
66+
67+
}

0 commit comments

Comments
 (0)