-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms
85 lines (79 loc) · 2.49 KB
/
Algorithms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
1.Get alternative number from a number:
Input: 1234567
Output: 246
*****
public static void main(String args[]){
int base=14235478;
int[] holder= new int[Integer.toString(base).length()];
int i=0;
//int count=1;
while(base>0){
int rem = base%10;
int newBase = base/10;
base=newBase;
holder[i++]=rem;
}
System.out.print(holder);
int count=1;
int k=0;
int[] finalArr= new int[10];
for(int j=holder.length-1;j>=0;j--){
if (count%2==0)
finalArr[k++]=holder[j];
count++;
}
System.out.print(finalArr);
}
*******
******* ******* ******* ******* ******* ******* ******* ******* *******
Get largest element from arranging with 5 in between integers
public static void main(String args[]) {
int base = 670;
//outcomes - 5268,2568,2658,2685
// 670 - 5670,6570,6750,6705
String numSt = "";
int SIZE = 0;
while (base > 0) {
numSt = numSt + base % 10;
base = base / 10;
++SIZE;
}
int MAX_VALUE = 0;
int POINT = numSt.length() - 1;
int POSSIBILITIES = SIZE + 1;
String NEW_VALUE = "";
while (POSSIBILITIES > 0) {
for (int i = numSt.length() - 1; i >= 0; i--) {
if (i == POINT)
NEW_VALUE = NEW_VALUE + "5";
NEW_VALUE = NEW_VALUE + numSt.charAt(i);
}
if (Integer.parseInt(NEW_VALUE) > MAX_VALUE)
MAX_VALUE = Integer.parseInt(NEW_VALUE);
POSSIBILITIES--;
NEW_VALUE = "";
POINT--;
}
System.out.println(MAX_VALUE);
}
******* ******* ******* ******* ******* ******* *******
https://www.geeksforgeeks.org/calculate-sum-of-all-numbers-present-in-a-string/
public static void main(String[] args)
{
String s = "45a5bb50ccc8";
StringBuffer holder= new StringBuffer();
int sum=0;
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i))){
holder.append(s.charAt(i));
if(i!=s.length()-1)
continue;
}
if(holder.length()>0)
{
sum=sum+Integer.parseInt(holder.toString());
holder.setLength(0);
}
}
System.out.print(sum);
}