Skip to content

Commit d8d0665

Browse files
committed
added
1 parent d3cc036 commit d8d0665

File tree

13 files changed

+276
-0
lines changed

13 files changed

+276
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
public class Solution {
3+
4+
public static void arrange(int[] arr, int n) {
5+
//Your code goes here
6+
int j = 0;
7+
for(int i = 0; i<arr.length; i++){
8+
if(i%2==0){
9+
arr[j] = i+1;
10+
j++;
11+
}
12+
}
13+
for (int i = arr.length; i>0; i--) {
14+
if (i % 2 == 0) {
15+
arr[j] = i ;
16+
j++;
17+
}
18+
}
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
public class Solution{
3+
4+
public static int duplicateNumber(int arr[]) {
5+
//Your code goes here
6+
HashMap<Integer, Integer> mp = new HashMap<>();
7+
8+
for(int i = 0;i<arr.length; i++){
9+
10+
if(mp.containsKey(arr[i])){
11+
mp.put(arr[i], mp.get(arr[i])+1);
12+
}
13+
else{
14+
mp.put(arr[i], 1);
15+
}
16+
}
17+
int ans = 0;
18+
for(int i = 1;i<arr.length; i++){
19+
20+
if(mp.get(arr[i])==2){
21+
ans = arr[i];
22+
}
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution{
2+
3+
public static int findUnique(int[] arr){
4+
//Your code goes here
5+
int ans = arr[0];
6+
for(int i = 1; i <arr.length; i++){
7+
ans = ans^arr[i];
8+
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
public class Solution{
3+
4+
public static void intersections(int arr1[], int arr2[]) {
5+
//Your code goes here
6+
int s = -1;
7+
for (int i = 0; i < arr1.length; i++) {
8+
for (int j = 0; j < arr2.length; j++) {
9+
if (arr1[i] == arr2[j]){
10+
System.out.print(arr1[i]+ " ");
11+
arr1[i] = s;
12+
--s;
13+
arr2[j] = s;
14+
--s;
15+
}
16+
17+
}
18+
}
19+
// System.out.println();
20+
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
public class Solution {
3+
4+
public static int linearSearch(int arr[], int x) {
5+
//Your code goes here
6+
for(int i = 0 ; i <arr.length; i++){
7+
if(arr[i] == x){
8+
return i;
9+
}
10+
}
11+
return -1;
12+
}
13+
}

coding Ninjas/Java/Array/PairSum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.*;
2+
public class Solution {
3+
4+
public static int pairSum(int arr[], int x) {
5+
int s = 0;
6+
for(int i = 0; i<arr.length; i++){
7+
for(int j = i+1; j < arr.length; j++){
8+
if (arr[i]+arr[j] == x){
9+
s+=1;
10+
}
11+
}
12+
}
13+
return s;
14+
15+
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
public class Solution {
3+
4+
public static int sum(int[] arr) {
5+
//Your code goes here
6+
int s = 0;
7+
for( int i = 0; i<arr.length; i++){
8+
s+=arr[i];
9+
}
10+
return s;
11+
}
12+
}

coding Ninjas/Java/Array/Sort01

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.*;
2+
public class Solution {
3+
4+
public static void sortZeroesAndOne(int[] arr) {
5+
//Your code goes here
6+
int i = 0;
7+
int l = arr.length-1;
8+
while(i<l){
9+
if(arr[i] == 0){
10+
i++;
11+
}
12+
else{
13+
int temp = arr[i];
14+
arr[i] = arr[l];
15+
arr[l] = temp;
16+
l--;
17+
}
18+
}
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
public class Solution {
3+
static void swaps(int arr[], int s , int t){
4+
int temp = arr[s];
5+
arr[s] = arr[t];
6+
arr[t] = temp;
7+
}
8+
9+
public static void swapAlternate(int arr[]) {
10+
//Your code goes here
11+
for(int i = 1 ; i<arr.length; i+=2){
12+
swaps(arr,i-1, i);
13+
}
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class Solution {
3+
4+
public static int findTriplet(int[] arr, int x) {
5+
//Your code goes here
6+
int s = 0;
7+
for (int i = 0; i < arr.length; i++) {
8+
for (int j = i+1; j < arr.length; j++) {
9+
for (int j2 = j+1; j2 < arr.length; j2++) {
10+
if(arr[i]+arr[j]+arr[j2]==x){
11+
s++;
12+
}
13+
14+
}
15+
}
16+
}
17+
18+
return s;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)