Skip to content

Commit 773c2c1

Browse files
committed
Updated RadixSort code which will work for negative and positive numbers, also updated few files which failed during build failure
1 parent 6baadc0 commit 773c2c1

10 files changed

+146
-159
lines changed

Armstrong.java

+52-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1-
import java.util.*;
2-
class Armstrong
3-
{
4-
public static void main(String args[])
5-
{
6-
Scanner sc=new Scanner(System.in);
7-
System.out.println("Enter a no.:");
8-
int a=sc.nextInt();
9-
int r,sum=0,temp;
10-
temp=a;
11-
while(a>0)
12-
{
13-
r=a%10;
14-
sum=(r*r*r)+sum;
15-
a=a/10;
16-
}
17-
if(temp==sum)
18-
{
19-
System.out.println("Armstrong no.");
20-
}
21-
else
22-
{
23-
System.out.println("not an armstrng no.");
24-
}
25-
}
1+
// Java program to determine whether
2+
// the number is Armstrong number or not
3+
public class Armstrong {
4+
5+
// Function to calculate x raised
6+
// to the power y
7+
int power(int x, long y)
8+
{
9+
if (y == 0)
10+
return 1;
11+
if (y % 2 == 0)
12+
return power(x, y / 2) * power(x, y / 2);
13+
return x * power(x, y / 2) * power(x, y / 2);
14+
}
15+
16+
// Function to calculate order of the number
17+
int order(int x)
18+
{
19+
int n = 0;
20+
while (x != 0) {
21+
n++;
22+
x = x / 10;
23+
}
24+
return n;
25+
}
26+
27+
// Function to check whether the given
28+
// number is Armstrong number or not
29+
boolean isArmstrong(int x)
30+
{
31+
// Calling order function
32+
int n = order(x);
33+
int temp = x, sum = 0;
34+
while (temp != 0) {
35+
int r = temp % 10;
36+
sum = sum + power(r, n);
37+
temp = temp / 10;
38+
}
39+
40+
// If satisfies Armstrong condition
41+
return (sum == x);
42+
}
43+
44+
// Driver Code
45+
public static void main(String[] args)
46+
{
47+
Armstrong ob = new Armstrong();
48+
int x = 153;
49+
System.out.println(ob.isArmstrong(x));
50+
x = 1253;
51+
System.out.println(ob.isArmstrong(x));
52+
}
2653
}

Java-problem-2023.iml

+3
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
1010
</component>
11+
<component name="SonarLintModuleSettings">
12+
<option name="uniqueId" value="13b23f24-718d-44ac-bde8-fcc80fed13a1" />
13+
</component>
1114
</module>

RadixSort.java

+84-71
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,92 @@
1-
// Radix sort Java implementation
1+
// Radix Sort implementation
22

33
import java.util.Arrays;
44

5-
class RadixSort {
6-
7-
// A utility function to get maximum value in arr[]
8-
static int getMax(int arr[], int n)
9-
{
10-
int mx = arr[0];
11-
for (int i = 1; i < n; i++)
12-
if (arr[i] > mx)
13-
mx = arr[i];
14-
return mx;
5+
public class RadixSort {
6+
private static final int BASE = 10;
7+
public static void main(String[] args) {
8+
int[] arr = new int[]{1, -2, -456, 803, 1000, -2033, -9, 0, 10};
9+
long startTime = System.currentTimeMillis();
10+
System.out.println("Original Array: " + Arrays.toString(arr));
11+
radixSort(arr);
12+
long endTime = System.currentTimeMillis();
13+
System.out.println("Sorted Array: " + Arrays.toString(arr) + "\nTime taken: " +
14+
((endTime-startTime)*0.001)+" seconds");
1515
}
16-
17-
// A function to do counting sort of arr[] according to
18-
// the digit represented by exp.
19-
static void countSort(int arr[], int n, int exp)
20-
{
21-
int output[] = new int[n]; // output array
22-
int i;
23-
int count[] = new int[10];
24-
Arrays.fill(count, 0);
25-
26-
// Store count of occurrences in count[]
27-
for (i = 0; i < n; i++)
28-
count[(arr[i] / exp) % 10]++;
29-
30-
// Change count[i] so that count[i] now contains
31-
// actual position of this digit in output[]
32-
for (i = 1; i < 10; i++)
33-
count[i] += count[i - 1];
34-
35-
// Build the output array
36-
for (i = n - 1; i >= 0; i--) {
37-
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
38-
count[(arr[i] / exp) % 10]--;
16+
private static void radixSort(int[] arr){
17+
if(arr.length==0)
18+
return;
19+
// Initialising these arrays for sorting the negative and the positive elements separately.
20+
int[] positive = Arrays.stream(arr).filter(i -> i>=0).toArray();
21+
int[] negative = Arrays.stream(arr).filter(i -> i<0).toArray();
22+
if(positive.length>0){
23+
int max = positive[0];
24+
// Find the maximum value present
25+
for (int j : positive) {
26+
max = Math.max(max, j);
27+
}
28+
// Looping through the elements and applying Counting Sort based on the number of digits
29+
// present in the maximum element
30+
for(int exp=1; max/exp>0; exp*=10){
31+
countingSort(positive, positive.length, exp);
32+
}
33+
}
34+
if(negative.length>0){
35+
// Converting the negative elements in the array to positive for sorting purpose
36+
for(int i=0; i<negative.length; i++){
37+
negative[i] *= -1;
38+
}
39+
// Find the maximum value present
40+
int max = negative[0];
41+
for (int j : negative) {
42+
max = Math.max(max, j);
43+
}
44+
// Looping through the elements and applying Counting Sort based on the number of digits
45+
// present in the maximum element
46+
for(int exp=1; max/exp>0; exp*=BASE){
47+
countingSort(negative, negative.length, exp);
48+
}
49+
}
50+
int index = 0;
51+
// Including the negative elements in the reverse order since (-9 < -1) &
52+
// the negative elements were sorted based on the absolute values of the elements in the array
53+
for (int i= negative.length-1; i>=0; i--) {
54+
arr[index++] = -negative[i];
55+
}
56+
// Appending the positive elements after the negative elements in the array
57+
for (int j : positive) {
58+
arr[index++] = j;
3959
}
40-
41-
// Copy the output array to arr[], so that arr[] now
42-
// contains sorted numbers according to current
43-
// digit
44-
for (i = 0; i < n; i++)
45-
arr[i] = output[i];
46-
}
47-
48-
// The main function to that sorts arr[] of
49-
// size n using Radix Sort
50-
static void radixsort(int arr[], int n)
51-
{
52-
// Find the maximum number to know number of digits
53-
int m = getMax(arr, n);
54-
55-
// Do counting sort for every digit. Note that
56-
// instead of passing digit number, exp is passed.
57-
// exp is 10^i where i is current digit number
58-
for (int exp = 1; m / exp > 0; exp *= 10)
59-
countSort(arr, n, exp);
60-
}
61-
62-
// A utility function to print an array
63-
static void print(int arr[], int n)
64-
{
65-
for (int i = 0; i < n; i++)
66-
System.out.print(arr[i] + " ");
6760
}
68-
69-
// Main driver method
70-
public static void main(String[] args)
71-
{
72-
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
73-
int n = arr.length;
74-
75-
// Function Call
76-
radixsort(arr, n);
77-
print(arr, n);
61+
private static void countingSort(int[] arr, int n, int exp){
62+
// Initialising the count array with size = BASE
63+
int[] count = new int[BASE];
64+
// Initialising the output array with size = number of elements in positive or negative array
65+
int[] output = new int[n];
66+
// Counting the frequency of the numbers < BASE i.e. from [0..9] in this case
67+
for (int j : arr) {
68+
count[(j / exp) % BASE]++;
69+
}
70+
// Calculate cumulative count
71+
for(int i=1; i<BASE; i++){
72+
count[i] += count[i-1];
73+
}
74+
// Building the output array
75+
for(int i=n-1; i>=0; i--){
76+
output[count[(arr[i]/exp)%BASE]-1] = arr[i];
77+
count[(arr[i]/exp)%BASE]--;
78+
}
79+
// Copying the array elements present in output array to the original array
80+
System.arraycopy(output, 0, arr, 0, n);
7881
}
7982
}
83+
84+
/*
85+
Time Complexity: O(d*(n+b)), d = number of digits
86+
n = number of elements in the array
87+
b = the base of the number system being used which is 10 here,
88+
this can be taken as 20, 30 anything based on the range of elements present,
89+
but the larger the base the more space will be required.
90+
Auxiliary Space Complexity: O(n+b), n = number of elements
91+
b = base of the number system being used which is 10 here
92+
*/

armstrong.java

-53
This file was deleted.
File renamed without changes.

graph/algorithms/FloydWarshallAlgorithm.java

-7
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,3 @@ public static void main(String[] args) {
6363
Calls the floydWarshall method to compute and print the shortest distances between all pairs of nodes in the graph.
6464
The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a weighted graph, including both positive and negative edge weights. It is suitable for finding the shortest paths in dense graphs or when the graph's structure changes over time.*/
6565

66-
67-
68-
69-
70-
71-
Regenerate
72-

java/solution/ConcatinateWords.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
15
class concatinate_word_problem {
26
class Trie{
37
Trie[] children;
@@ -34,7 +38,7 @@ private boolean searchWord(String words, int count){
3438
return (count >1 && node.isWord) ? true : false;
3539
}
3640
public List<String> findAllConcatenatedWordsInADict(String[] words) {
37-
Arrays.sort(words, (a,b)-> a.length()-b.length());
41+
Arrays.sort(words, (a, b)-> a.length()-b.length());
3842
ArrayList<String> result = new ArrayList<>();
3943

4044
for(String word : words){

java/solution/FindOccouranceIndicesInArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//Problem Statement: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/?envType=daily-question&envId=2023-10-09
1+
package java.solution;//Problem Statement: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/?envType=daily-question&envId=2023-10-09
22
// Topic: Binary Search
33

44
class Solution {
5-
//flag == true ====> we are searching first occourance else otherwise
5+
//flag == true ====> we are searching first occurrence else otherwise
66
public int[] searchRange(int[] nums, int target) {
77
return new int[] {helper(nums, target, true), helper(nums, target, false)};
88
}
File renamed without changes.

Interface.java renamed to q6.java

File renamed without changes.

0 commit comments

Comments
 (0)