Skip to content

Commit 2ad535d

Browse files
committed
Problem solving in Java
1 parent 2f1066b commit 2ad535d

23 files changed

+978
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*Arrange Numbers In Array
2+
You have been given an empty array(ARR) and its size N. The only input taken from the user will be N and you need not worry about the array.
3+
Your task is to populate the array using the integer values in the range 1 to N(both inclusive) in the order - 1,3,5,.......,6,4,2.*/
4+
5+
package Array_1D;
6+
7+
import java.util.Scanner;
8+
9+
public class ArrangeNumbersInArray
10+
{
11+
public static void numberinArray(int size)
12+
{
13+
int[] arr = new int[size];
14+
15+
if(size%2==0)
16+
{
17+
for(int i=0;i<size/2;i++)
18+
{
19+
arr[i] = 2*i + 1;
20+
}
21+
22+
int p = size;
23+
for(int i=size/2;i<size;i++)
24+
{
25+
arr[i] = p;
26+
p -= 2;
27+
}
28+
printArray(arr);
29+
}
30+
31+
else
32+
{
33+
for(int i=0;i<=size/2;i++)
34+
{
35+
arr[i] = 2*i + 1;
36+
}
37+
38+
int p = size-1;
39+
for(int i=(size+2)/2;i<size;i++)
40+
{
41+
arr[i] = p;
42+
p -= 2;
43+
}
44+
printArray(arr);
45+
}
46+
}
47+
48+
public static void printArray(int arr[])
49+
{
50+
for(int i= 0; i<arr.length; i++)
51+
{
52+
System.out.print(arr[i]+ " ");
53+
}
54+
System.out.println();
55+
}
56+
57+
public static void main(String[] args)
58+
{
59+
System.out.print("Enter the number : ");
60+
Scanner sc = new Scanner(System.in);
61+
int size = sc.nextInt();
62+
numberinArray(size);
63+
}
64+
}

Array_1D/FindUnique.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*Find Unique
2+
You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1].
3+
Now, in the given array/list, 'M' numbers are present twice and one number is present only once.*/
4+
5+
package Array_1D;
6+
7+
import java.util.Scanner;
8+
9+
public class FindUnique
10+
{
11+
public static int uniqueNumber(int[] arr)
12+
{
13+
for(int i=0;i<arr.length;i++)
14+
{
15+
int count = 0;
16+
for(int j=0;j<arr.length;j++)
17+
{
18+
if(arr[i]==arr[j])
19+
{
20+
count++;
21+
}
22+
}
23+
if(count==1)
24+
{
25+
System.out.print("Unique Value : ");
26+
return arr[i];
27+
}
28+
}
29+
return -1;
30+
}
31+
32+
public static void printArray(int arr[])
33+
{
34+
for(int i= 0; i<arr.length; i++)
35+
{
36+
System.out.print(arr[i]+ ", ");
37+
}
38+
System.out.println();
39+
}
40+
41+
public static void main(String[] args)
42+
{
43+
Scanner sc = new Scanner(System.in);
44+
System.out.print("Enter the size of array : ");
45+
int size = sc.nextInt();
46+
int[] input_arr = new int[size];
47+
for(int i= 0; i<size; i++)
48+
{
49+
System.out.print("Enter the element at "+ i + "th term : ");
50+
input_arr[i] = sc.nextInt();
51+
}
52+
printArray(input_arr);
53+
int unique_value = uniqueNumber(input_arr);
54+
System.out.println(unique_value);
55+
}
56+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*Intersection of Two Arrays
2+
You have been given two integer arrays/list(ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.*/
3+
4+
package Array_1D;
5+
6+
import java.util.Scanner;
7+
8+
public class IntersectionofTwoArrays
9+
{
10+
public static void Intersection(int arr1[], int[] arr2)
11+
{
12+
for(int i=0;i<arr1.length;i++)
13+
{
14+
for(int j=0;j<arr2.length;j++)
15+
{
16+
if(arr1[i]==arr2[j])
17+
{
18+
System.out.print(arr1[i]+" ");
19+
arr2[j] = Integer.MIN_VALUE;
20+
break;
21+
}
22+
}
23+
}
24+
}
25+
26+
public static void printArray(int arr[])
27+
{
28+
for(int i= 0; i<arr.length; i++)
29+
{
30+
System.out.print(arr[i]+ ", ");
31+
}
32+
System.out.println();
33+
}
34+
35+
public static void main(String[] args)
36+
{
37+
Scanner sc = new Scanner(System.in);
38+
System.out.print("Enter the size of array 1 : ");
39+
int size1 = sc.nextInt();
40+
41+
int[] input_arr1 = new int[size1];
42+
for(int i= 0; i<size1; i++)
43+
{
44+
System.out.print("Enter the element of array 1 "+ i + "th term : ");
45+
input_arr1[i] = sc.nextInt();
46+
}
47+
48+
System.out.println();
49+
50+
System.out.print("Enter the size of array 2 : ");
51+
int size2 = sc.nextInt();
52+
int[] input_arr2 = new int[size2];
53+
54+
for(int i= 0; i<size2; i++)
55+
{
56+
System.out.print("Enter the element of array 2 "+ i + "th term : ");
57+
input_arr2[i] = sc.nextInt();
58+
}
59+
60+
System.out.print("Array 1 : ");
61+
printArray(input_arr1);
62+
System.out.print("Array 2 : ");
63+
printArray(input_arr2);
64+
Intersection(input_arr1, input_arr2);
65+
}
66+
}

Array_1D/Sort01.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*Sort 0 1
2+
You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list.*/
3+
4+
package Array_1D;
5+
6+
import java.util.Scanner;
7+
8+
public class Sort01
9+
{
10+
public static void sort(int arr[])
11+
{
12+
int zero = 0;
13+
for(int i =0;i<arr.length;i++)
14+
{
15+
if(arr[i]==0)
16+
{
17+
zero++;
18+
}
19+
}
20+
for(int i =0;i<arr.length;i++)
21+
{
22+
if(i<zero)
23+
{
24+
arr[i] = 0;
25+
}
26+
else
27+
{
28+
arr[i] = 1;
29+
}
30+
}
31+
System.out.print("Sorted array ");
32+
printArray(arr);
33+
}
34+
35+
public static void printArray(int arr[])
36+
{
37+
for(int i= 0; i<arr.length; i++)
38+
{
39+
System.out.print(arr[i]+ ", ");
40+
}
41+
System.out.println();
42+
}
43+
44+
public static void main(String[] args)
45+
{
46+
Scanner sc = new Scanner(System.in);
47+
System.out.print("Enter the size of array : ");
48+
int size = sc.nextInt();
49+
50+
int[] input = new int[size];
51+
for(int i= 0; i<size; i++)
52+
{
53+
System.out.print("Enter the element at "+ i + "th term : ");
54+
input[i] = sc.nextInt();
55+
}
56+
System.out.print("Original array ");
57+
printArray(input);
58+
sort(input);
59+
}
60+
}

Array_1D/arraySum.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*Return Array Sum
2+
Given an array/list(ARR) of length N, you need to find and return the sum of all the elements in the array/list.*/
3+
4+
package Array_1D;
5+
6+
import java.util.Scanner;
7+
8+
public class arraySum
9+
{
10+
public static int sumofArray(int arr[])
11+
{
12+
int sum = 0;
13+
for(int i=0; i<arr.length; i++)
14+
{
15+
sum+=arr[i];
16+
}
17+
System.out.print("Sum of array is ");
18+
return sum;
19+
}
20+
21+
public static void printArray(int arr[])
22+
{
23+
for(int i= 0; i<arr.length; i++)
24+
{
25+
System.out.print(arr[i]+ ", ");
26+
}
27+
System.out.println();
28+
}
29+
30+
public static void main(String[] args)
31+
{
32+
Scanner sc = new Scanner(System.in);
33+
System.out.print("Enter the size of array : ");
34+
int size = sc.nextInt();
35+
int[] input_arr = new int[size];
36+
37+
for(int i=0; i<size; i++)
38+
{
39+
System.out.print("Enter the element at "+ i + "th term : ");
40+
input_arr[i] = sc.nextInt();
41+
}
42+
printArray(input_arr);
43+
int answer = sumofArray(input_arr);
44+
System.out.println(answer);
45+
}
46+
}

Array_1D/findDuplicate.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*Find Duplicate
2+
You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3 and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array.*/
3+
4+
5+
package Array_1D;
6+
7+
import java.util.Scanner;
8+
9+
public class findDuplicate
10+
{
11+
public static int duplicateNumber(int[] arr)
12+
{
13+
for(int i=0;i<arr.length;i++)
14+
{
15+
for(int j=i+1;j<arr.length;j++)
16+
{
17+
if(arr[i]==arr[j])
18+
{
19+
System.out.print("Duplicate Value : ");
20+
return arr[i];
21+
}
22+
}
23+
}
24+
return -1;
25+
}
26+
27+
public static void printArray(int arr[])
28+
{
29+
for(int i= 0; i<arr.length; i++)
30+
{
31+
System.out.print(arr[i]+ ", ");
32+
}
33+
System.out.println();
34+
}
35+
36+
public static void main(String[] args)
37+
{
38+
Scanner sc = new Scanner(System.in);
39+
System.out.print("Enter the size of array : ");
40+
int size = sc.nextInt();
41+
int[] input_arr = new int[size];
42+
for(int i= 0; i<size; i++)
43+
{
44+
System.out.print("Enter the element at "+ i + "th term : ");
45+
input_arr[i] = sc.nextInt();
46+
}
47+
printArray(input_arr);
48+
int duplicate_value = duplicateNumber(input_arr);
49+
System.out.println(duplicate_value);
50+
}
51+
}

0 commit comments

Comments
 (0)