Skip to content

Commit 907e481

Browse files
authoredJul 7, 2019
Add files via upload
1 parent 3263ecc commit 907e481

15 files changed

+921
-0
lines changed
 

‎NUMBER PROBLEMS/BaseAddition.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*BASE ADDITION
2+
*
3+
*
4+
*
5+
* BASE 10 ADDTION :
6+
*
7+
* 1
8+
* -------
9+
* 1234
10+
* 1239
11+
* ------
12+
* 2473
13+
*
14+
* BASE ADDITION 2
15+
*
16+
* 1111
17+
* -------
18+
* 1111
19+
* 1111
20+
* -------
21+
* 11110
22+
*
23+
*
24+
* BASE ADDITION 5
25+
*
26+
* ORIGINAL LOGIC IMPLEMENTATION OF ANY ADDITION
27+
*
28+
*
29+
* CARRY = QUOTIENT
30+
*
31+
* RESULT = REMAINDER
32+
*
33+
* CARRY 1 1 1 1
34+
* -------------------------------------------------------
35+
* 1 4 2 3
36+
* 4 3 3 3
37+
* -------------------------------------------------------
38+
* 6>5 8>5 5>5 6>5
39+
* 6/5=1 8/5=1 5/5=1 6/5=1
40+
* 6%5=1 8%5=3 5%5=0 6%5=1
41+
* -------------------------------------------------------
42+
* RESULT 1 1 3 0 1
43+
*
44+
*
45+
* AFTER ADDITION FINAL RESULT = 11301(BASE 5 )
46+
*/
47+
48+
import java.util.*;
49+
public class BaseAddition {
50+
51+
/**
52+
* @param args
53+
*/
54+
public static void main(String[] args) {
55+
// TODO Auto-generated method stub
56+
int num1,num2,base;
57+
Scanner s = new Scanner(System.in);
58+
num1=s.nextInt();
59+
num2=s.nextInt();
60+
base = s.nextInt();
61+
int carry=0,sum=0,res=0,pv=1;
62+
while(num1/pv!=0 || num2/pv!=0){
63+
int digit1 = (num1/pv)%10;
64+
int digit2 = (num2/pv)%10;
65+
sum=carry + digit1+digit2;
66+
carry = sum/base;
67+
sum = sum%base;
68+
System.out.println(sum +" "+carry);
69+
res=sum*pv+res;
70+
pv=pv*10;
71+
72+
}
73+
res = carry*pv+res;
74+
System.out.println("RESULT = "+res);
75+
}
76+
77+
}

‎NUMBER PROBLEMS/BaseSubtraction.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* BASE SUBTRACTION 5
3+
*
4+
*BORROW 3 3+5
5+
* --------------------------------
6+
* 4 3 3 3
7+
* 1 4 2 3
8+
* --------------------------------
9+
* 3-1 8-4 3-2 3-3
10+
* --------------------------------
11+
* RESULT 2 4 1 0
12+
*/
13+
14+
import java.util.*;
15+
public class BaseSubtraction {
16+
17+
/**
18+
* @param args
19+
*/
20+
public static void main(String[] args) {
21+
// TODO Auto-generated method stub
22+
Scanner s = new Scanner(System.in);
23+
int n1,n2,base;
24+
n1=s.nextInt();
25+
n2=s.nextInt();
26+
base = s.nextInt();
27+
int diff=0,borrow=0,pv=1,res=0;
28+
while(n1/pv !=0)
29+
{
30+
int d1 = (n1/pv)%10;
31+
int d2 =(n2/pv)%10;
32+
if(d1<d2)
33+
{
34+
int pv2 =pv*10;
35+
while((n1/pv2)==0)
36+
{
37+
n1= n1+base-1 *pv2;
38+
pv2=pv2*10;
39+
}
40+
n1=n1-pv2;
41+
d1=d1+base;
42+
}
43+
diff = d1-d2;
44+
45+
res = diff*pv+res;
46+
pv=pv*10;
47+
48+
}
49+
System.out.println(res);
50+
}
51+
52+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.*;
2+
public class CorrectParanthesis {
3+
public static void main(String args[])
4+
{
5+
Scanner input = new Scanner(System.in);
6+
7+
StringBuffer str = new StringBuffer();
8+
str.append(input.next()) ;
9+
Stack s = new Stack();
10+
s.push("T");
11+
12+
}
13+
}

‎NUMBER PROBLEMS/DiamondPattern.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.Scanner;
2+
3+
4+
public class DiamondPattern {
5+
6+
/**
7+
* @param args
8+
*/
9+
10+
static void print(int n)
11+
{
12+
for(int i=1;i<=n;i++)
13+
{
14+
System.out.print("* ");
15+
}
16+
}
17+
18+
static void space(int n)
19+
{
20+
for(int i=1;i<=n;i++)
21+
{
22+
System.out.print(" ");
23+
}
24+
}
25+
static void firstHalf(int rows,int space)
26+
{
27+
28+
29+
for(int i=1;i<=rows;i++)
30+
{
31+
space(space);
32+
print(i);
33+
space--;
34+
System.out.println();
35+
}
36+
}
37+
38+
static void secondHalf(int rows,int space)
39+
{
40+
for(int i=rows;i>=1;i--)
41+
{
42+
space(space);
43+
print(i);
44+
space++;
45+
System.out.println();
46+
}
47+
}
48+
public static void main(String[] args) {
49+
// TODO Auto-generated method stub
50+
Scanner input = new Scanner(System.in);
51+
System.out.println("ENTER NO OF ROWS ");
52+
int rows = input.nextInt();
53+
int space = rows-1;
54+
firstHalf(rows,space);
55+
space =1;
56+
rows=rows-1;
57+
secondHalf(rows,space);
58+
}
59+
60+
}

‎NUMBER PROBLEMS/EvenOddDigits.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.*;
2+
public class EvenOddDigits {
3+
4+
/**
5+
* @param args
6+
*/
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
Scanner s = new Scanner(System.in);
10+
11+
}
12+
13+
}

‎NUMBER PROBLEMS/Factors.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
public class Factors {
3+
4+
/**
5+
* @param args
6+
*/
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
Scanner input = new Scanner(System.in);
10+
int num = input.nextInt();
11+
int count=0;
12+
int flag=1;
13+
for(int i=1;;i++)
14+
{
15+
//1
16+
//System.out.print("number = "+i);
17+
count =0;
18+
for(int j=1;j<=Math.sqrt(i);j++)
19+
{
20+
if(i%j==0)
21+
{
22+
//System.out.println(" factors are "+j);
23+
count++;
24+
25+
}
26+
if(count==100)
27+
{
28+
System.out.println(i);
29+
flag=0;
30+
break;
31+
}
32+
}
33+
//System.out.println();
34+
if(flag==0)
35+
break;
36+
37+
}
38+
}
39+
40+
}

‎NUMBER PROBLEMS/FactorsOfNumbers.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
public class FactorsOfNumbers {
3+
4+
static void factors(int n1){
5+
int sum=0;
6+
for(int i=1;i<n1;i++)
7+
{
8+
if(n1%i==0)
9+
{
10+
System.out.println(i+" ");
11+
sum=sum+i;
12+
}
13+
}
14+
System.out.println(sum+" ");
15+
16+
}
17+
/**
18+
* @param args
19+
*/
20+
public static void main(String[] args) {
21+
// TODO Auto-generated method stub
22+
int n1= 48, n2=75;
23+
factors(n1);
24+
factors(n2);
25+
}
26+
27+
}

‎NUMBER PROBLEMS/GenerateKaprekar.java

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* REFER KAPREKAR NUMBER FOR ALGORITHM AND LOGIC IMPLEMENTATION
3+
*
4+
*LIST OF KAPREKAR NUMBERS ARE
5+
*
6+
*1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950,
7+
*5050, 5292, 7272, 7777, 9999, 17344, 22222, 38962, 77778,
8+
*82656, 95121, 99999, 142857, 148149, 181819, 187110, 208495,
9+
*318682, 329967,351352, 356643, 390313, 461539, 466830, 499500
10+
*
11+
*/
12+
public class GenerateKaprekar {
13+
14+
/**
15+
* @param args
16+
*/
17+
18+
static boolean checkKaprekar(long n)
19+
{
20+
long sq = n*n;
21+
long digit=0;
22+
long pv=1;
23+
//calculate no. of digits
24+
while(sq/pv!=0)
25+
{
26+
digit++;
27+
pv=pv*10;
28+
}
29+
long pv1=1;
30+
//System.out.println("NUMBER = "+n+" SQUARE = "+sq+" DIGIT COUNT = "+digit);
31+
32+
//assign maximum digit to right
33+
long right = (digit+1)/2;
34+
//find the place value for right
35+
while(right-- != 0)
36+
{
37+
pv1=pv1*10;
38+
}
39+
40+
long right_digit = sq%pv1;
41+
long left_digit = sq/pv1;
42+
//System.out.println("Right digit = "+right_digit+" Left digit = "+left_digit);
43+
44+
//check whether the right is zero
45+
while(right_digit==0)
46+
{
47+
pv1=pv1*10;
48+
right_digit=sq%pv1;
49+
left_digit = sq/pv1;
50+
}
51+
//System.out.println("Right digit = "+right_digit+" Left digit = "+left_digit);
52+
if(left_digit+right_digit == n)
53+
{
54+
return true;
55+
}
56+
else
57+
{
58+
try
59+
{
60+
while((left_digit%10==0) && (left_digit!=0))
61+
{
62+
left_digit=left_digit/10;
63+
}
64+
}
65+
catch(Exception e)
66+
{
67+
System.out.println("error");
68+
}
69+
if(left_digit + right_digit == n)
70+
return true;
71+
else
72+
return false;
73+
}
74+
75+
}
76+
public static void main(String[] args) {
77+
// TODO Auto-generated method stub
78+
for(int i=1;i<=80000;i++)
79+
{
80+
if(checkKaprekar(i))
81+
{
82+
System.out.print(i+" ");
83+
}
84+
}
85+
}
86+
87+
}
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* DISPLAY THE NUMBER WHICH HAS THE FIRST 'N' FACTORS
3+
*
4+
* (i.e) To find the number which the first 'n' factors
5+
*
6+
*
7+
*
8+
*
9+
* LOGIC : (OPTIMISED SOLUTION)
10+
*
11+
* REVERSE ENGINEERING
12+
*
13+
* 1) GET THE 'n' VALUE
14+
*
15+
* 2) FIND THE PRIME FACTOR OF 'n'
16+
*
17+
* 3) STORE THE POWER TO AN ARRAY (i.e) (FACTOR-1)
18+
*
19+
* 4) NOW GENERATE THE PRIME NUMBERS (i.e)2,3,5,7,9...
20+
*
21+
* 5) CALCULATE RESULT, ASSIGN THE HIGHEST POWER VALUE TO
22+
* THE LOWEST PRIMENUMBER
23+
*
24+
* WORKING :
25+
*
26+
* NUMBER =100
27+
* PRIME FACTOR OF 100 ARE 2,2,5,5
28+
* POWER ARE (2-1)(2-1)(5-1)(5-1)
29+
* GENERATE PRIME NUMBERS AND ASSIGN THE POWER VALUES
30+
* (i.e) 2^4 * 3^4 * 5^1 * 7^1 = 45360
31+
*
32+
*
33+
* HENE 45360 IS THE FIRST NUMBER WHICH HAS 100 FACTORS
34+
*
35+
*
36+
*
37+
* INPUT :- 6
38+
*
39+
*
40+
* PRIME FACTORS OF 6 ARE 2 3
41+
*
42+
*
43+
*/
44+
45+
import java.util.*;
46+
public class HundredFactorNumber {
47+
48+
/**
49+
* @param args
50+
*/
51+
static int isPrime(long n)
52+
{
53+
if(n==2)
54+
return 1;
55+
else {
56+
for(int i=2;i<=n/2;i++)
57+
{
58+
if(n%i==0)
59+
return 0;
60+
}
61+
}
62+
return 1;
63+
}
64+
public static void main(String[] args) {
65+
// TODO Auto-generated method stub
66+
Scanner s = new Scanner(System.in);
67+
System.out.println("ENTER THE NUMBER OF FACTORS : ");
68+
int n = s.nextInt();
69+
int i=2,index=0;
70+
int a[] = new int[100];
71+
while(n!=1)
72+
{
73+
while(n%i==0)
74+
{
75+
n=n/i;
76+
a[index]=i-1;
77+
index++;
78+
}
79+
i++;
80+
}
81+
long val=2,res=1;
82+
for( i=index-1;i>=0;i--)
83+
{
84+
while(isPrime(val)==0)
85+
{
86+
val++;
87+
}
88+
while(a[i]--!=0)
89+
res=res*val;
90+
val++;
91+
}
92+
System.out.println("RESULT = "+res);
93+
94+
95+
}
96+
97+
}

‎NUMBER PROBLEMS/KaprekarNumber.java

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* KAPREKAR NUMBER
3+
*
4+
* LOGIC :
5+
*
6+
* 1) SCAN A NUMBER 'N'
7+
*
8+
* 2) SQUARE THE NUMBER AND STORE IT IN 'SQ'
9+
*
10+
* 3) DIVIDE THE SQUARE TO EQUAL HALF
11+
* IF NO_OF_DIGITS(SQUARE) IS ODD
12+
* THEN
13+
* ASSIGN MAXMUM DIGITS TO RIGHT SIDE
14+
*
15+
* 4) RIGHT DIGIT SHOUD NOT BE EQUAL TO ZERO
16+
* IF RIGHT DIGIT ==0
17+
* THEN
18+
* TAKE THE NUMBER FROM SQUARE UNTIL
19+
* RIGHT >0
20+
*
21+
* 5) ELIMINATE IF THERE ARE ZEROS AT THE RIGHT OF THE LEFT_DIGIT
22+
* (i.e) IF LEFT = 2800
23+
* THEN MAKE LEFT = 28
24+
*
25+
* 6) ADD LEFT DIGIT + RIGHT DIGIT
26+
* IF LEFT+RIGHT == N
27+
* THEN
28+
* PRINT YES
29+
* ELSE
30+
* PRINT NO
31+
*
32+
* EXAMPLE :-
33+
*
34+
* INPUT :45
35+
*
36+
* SQ = 2045
37+
* LEFT = 20
38+
* RIGHT = 25
39+
* LEFT + RIGHT = 45 WHICH IS EQUAL TO 'N'
40+
*
41+
* THEREFORE 45 IS A KAPREKAR NUMBER
42+
*
43+
*
44+
*
45+
* EXAMPLE 2:-
46+
*
47+
* INPUT : 100
48+
*
49+
* SQUARE = 10000
50+
* LEFT = 000
51+
* RIGHT =10
52+
* SINCE LEFT SHOULD NOT BE ZERO
53+
* ITERATE THE LOOP UNTIL LEFT > ZERO
54+
* AFTER ITERATION
55+
* LEFT =10000
56+
* RIGHT =0
57+
*
58+
* LEFT + RIGHT = 10000 WHICH IS NOT EQUAL TO 'N'
59+
* THEREFOR 100 IS NOT A KAPREKAR NUMBER
60+
*
61+
*
62+
*/
63+
import java.util.*;
64+
public class KaprekarNumber {
65+
66+
/**
67+
* @param args
68+
*/
69+
70+
public static void main(String[] args) {
71+
// TODO Auto-generated method stub
72+
Scanner s = new Scanner(System.in);
73+
int n = s.nextInt();
74+
int sq = n*n;
75+
int digit=0;
76+
int pv=1;
77+
//calculate no. of digits
78+
while(sq/pv!=0)
79+
{
80+
digit++;
81+
pv=pv*10;
82+
}
83+
int pv1=1;
84+
System.out.println("NUMBER = "+n+" SQUARE = "+sq+" DIGIT COUNT = "+digit);
85+
86+
//assign maximum digit to right
87+
int right = (digit+1)/2;
88+
//find the place value for right
89+
while(right-- != 0)
90+
{
91+
pv1=pv1*10;
92+
}
93+
94+
int right_digit = sq%pv1;
95+
int left_digit = sq/pv1;
96+
System.out.println("Right digit = "+right_digit+" Left digit = "+left_digit);
97+
98+
//check whether the right is zero
99+
while(right_digit==0)
100+
{
101+
pv1=pv1*10;
102+
right_digit=sq%pv1;
103+
left_digit = sq/pv1;
104+
}
105+
System.out.println("Right digit = "+right_digit+" Left digit = "+left_digit);
106+
if(left_digit+right_digit == n)
107+
{
108+
System.out.println("Yes");
109+
}
110+
else
111+
System.out.println("No");
112+
}
113+
114+
}

‎NUMBER PROBLEMS/Maain.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*List of Prime numbers upto 'n' natural natural numbers
2+
*
3+
* OPTIMIZED SOLUTION FOR PRIME NUMBER
4+
* INPUT : 10
5+
* OUTPUT : 2 3 5 7
6+
*
7+
* INPUT : 50
8+
* OUTPUT : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
9+
*
10+
* LOGIC :
11+
*
12+
* IF N= EVEN NUMBER RETURN FALSE
13+
*
14+
* IF N = ODD NUMBER
15+
* THEN
16+
* LOOP FORM i=3 TO SQRT(N)
17+
* IF(N%i==0)
18+
* THEN RETURN FALSE
19+
*IF THE LOOP COMPLETELY RUNS THEN RETURN TRUE
20+
*
21+
*
22+
* */
23+
24+
25+
import java.util.*;
26+
public class Maain {
27+
static boolean isPrime(int n)
28+
{
29+
int flag=1;
30+
if(n==2 )
31+
return true;
32+
else if(n%2==0)
33+
return false;
34+
else
35+
{
36+
for(int i=3;i<=Math.sqrt(n);i=i+2)
37+
{
38+
if(n%i==0)
39+
{
40+
flag=0;
41+
return false;
42+
}
43+
}
44+
}
45+
return true;
46+
}
47+
/**
48+
* @param args
49+
*/
50+
public static void main(String[] args) {
51+
// TODO Auto-generated method stub
52+
int number;
53+
Scanner input = new Scanner(System.in);
54+
System.out.println("ENTER THE RANGE : ");
55+
number=input.nextInt();
56+
for(int i=2;i<=number;i++)
57+
{
58+
//System.out.print(i+" ");
59+
if(isPrime(i))
60+
{
61+
System.out.print(i+" ");
62+
}
63+
}
64+
}
65+
66+
}

‎NUMBER PROBLEMS/MainClass.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
public class MainClass {
3+
4+
/**
5+
* @param args
6+
*/
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
}

‎NUMBER PROBLEMS/MinMax.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
import java.math.*;
3+
public class MinMax {
4+
5+
/**
6+
* @param args
7+
*/
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
Scanner s = new Scanner(System.in);
11+
int m1,m2,m3;
12+
m1=s.nextInt();
13+
m2=s.nextInt();
14+
m3=s.nextInt();
15+
int min1 = Math.min(Math.min(m1, m2), m3);
16+
int min3 = Math.max(Math.max(m1, m2), m3);
17+
int min2 = Math.min(Math.max(m1,m2), Math.max(m2,m3));
18+
System.out.println(min1+" "+min2+" "+min3);
19+
}
20+
21+
}

‎NUMBER PROBLEMS/MonthInJava.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* INPUT : 10
3+
* OUTPUT : October
4+
*
5+
* INPUT : 20
6+
* OUTPUT : Invalid
7+
*
8+
*
9+
* LOGIC :
10+
*
11+
* USE PREDEFINED FUNCTION
12+
*
13+
* Month.of(monthNumber).name() -> returns the monthname
14+
* (i.e) OCTOBER
15+
*
16+
* TO CONVERT THE OUTPUT "OCTOBER" -> October
17+
*
18+
* use substring() and toUpperCase() and toLowerCase()
19+
*/
20+
21+
22+
import java.time.*;
23+
import java.util.*;
24+
public class MonthInJava {
25+
26+
/**
27+
* @param args
28+
*/
29+
public static void main(String[] args) {
30+
// TODO Auto-generated method stub
31+
Scanner input = new Scanner(System.in);
32+
System.out.println("ENTER MONTH NUMBER : ");
33+
int monthNumber=input.nextInt();
34+
if(monthNumber >=1 && monthNumber<=12)
35+
{
36+
String str = null;
37+
str= Month.of(monthNumber).name().toLowerCase();
38+
String str1 = str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase();
39+
System.out.println(str1);
40+
}
41+
else
42+
{
43+
System.out.println("Invalid");
44+
}
45+
}
46+
47+
}
+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* NEXT LARGEST NUMBER
3+
*
4+
* INPUT : 459863
5+
* OUTPUT : 46589
6+
*
7+
* LOGIC :
8+
*
9+
* 4 5 9 8 6 3
10+
*
11+
* STEP 1 : INITIAL SETUP
12+
*
13+
* PLACEVALUE =10
14+
* LEFT = (NUM / PLACEVALUE) % 10
15+
* RIGHT = NUM % 10
16+
*
17+
*STEP 2 :
18+
*
19+
* PLACEVALUE RIGHT LEFT RIGHT<LEFT ACTIONS
20+
*
21+
* 10 3 6 CONTINUE RIGTH =LEFT, LEFT = (NUM/PLACEVALUE)%10
22+
* 100 6 8 CONTINUE "
23+
* 1000 8 9 CONTINUE "
24+
* 10000 9 5 BREAK
25+
*
26+
* THEREFORE WE GOT PLACEVALUE =10000 AND LEFT = 5 , RIGHT =9
27+
*
28+
* STEP 3 :
29+
*
30+
* FROME STEP TWO WE GOT THAT 5 HAS TO BE REPLACED WITH THE VALUES
31+
*
32+
* (I.E) WE CAN REPLACE 5 WITH EITHER
33+
*
34+
* 9
35+
* 8
36+
* 6
37+
* 3
38+
*
39+
* NOW PICK THE VALUE WHICH IS GREATER THAN 5
40+
*
41+
*PLACEVALUE CONDITION ACTION RIGHT
42+
*
43+
* 1 3>5 CONTINUE 6
44+
* 10 6>5 BREAK
45+
*
46+
*
47+
* STEP 4 : NOW SWAP THE VALUES
48+
*
49+
* FROM STEP2 AND STEP 3 WE GOT THAT WHICH VALUES HAVE TO BE REPLACED
50+
*
51+
* WE KNOW
52+
*
53+
* LEFT = 5 PLACEVALUE = 10000
54+
* RIGHT = 6 PLACEVALUE = 10
55+
*
56+
* PERFORM SWAP
57+
*
58+
* 4 5 9 8 6
59+
*
60+
* 4 6 9 8 5
61+
*
62+
*
63+
* STEP 5 : PERFORM PARTIAL REVERSING
64+
*
65+
* FINAL RESULT = 4 6 5 8 9
66+
*
67+
*
68+
* STEP 6 : PRINT ALL THE POSSIBILITIES
69+
*
70+
*
71+
*/
72+
import java.util.*;
73+
public class NextLargestNumber {
74+
75+
/**
76+
* @param args
77+
*/
78+
static int smallest(int n)
79+
{
80+
int k=0;
81+
int a[]=new int[10];
82+
while(n!=0)
83+
{
84+
a[k]=n%10;
85+
n=n/10;
86+
k++;
87+
}
88+
for(int i=0;i<k;i++)
89+
{
90+
for(int j=i+1;j<k;j++)
91+
{
92+
if(a[i]>a[j])
93+
{
94+
int temp=a[i];
95+
a[i]=a[j];
96+
a[j]=temp;
97+
}
98+
}
99+
}
100+
101+
//for(int i=0;i<k;i++)
102+
//printf("%d ",a[i]);
103+
int res=0;
104+
int i=0;
105+
while(k--!=0)
106+
{
107+
res = res*10+a[i];
108+
i++;
109+
}
110+
return res;
111+
}
112+
static int swap(int number,int pv1,int pv2)
113+
{
114+
int val1 = (number/pv1)%10;
115+
int val2 =(number/pv2)%10;
116+
number = number - val1*pv1 + val2*pv1;
117+
number = number - val2*pv2 + val1*pv2;
118+
119+
return number;
120+
}
121+
122+
static int reverse(int number,int pv)
123+
{
124+
int rev = number/pv;
125+
int num =number%pv;
126+
int power =1;
127+
while(num/power!=0)
128+
{
129+
int digit = (number/power)%10;
130+
rev =rev*10+digit;
131+
power=power*10;
132+
}
133+
return rev;
134+
}
135+
static int nextLargest(int number)
136+
{
137+
int placeValue1 =10;
138+
int placeValue2 = 1;
139+
int left = (number/placeValue1)%10;
140+
int right = (number)%10;
141+
142+
//find the position which value has to be replaced
143+
while(number/placeValue1 !=0)
144+
{
145+
146+
if(left<right)
147+
{
148+
while(true)
149+
{
150+
151+
right=(number/placeValue2)%10;
152+
153+
if(right>left)
154+
break;
155+
placeValue2=placeValue2*10;
156+
}
157+
number = swap(number,placeValue1,placeValue2);
158+
number = reverse(number,placeValue1);
159+
break;
160+
}
161+
162+
placeValue1= placeValue1*10;
163+
right = left;
164+
left =(number/placeValue1)%10;
165+
}
166+
return number;
167+
168+
}
169+
public static void main(String[] args) {
170+
// TODO Auto-generated method stub
171+
Scanner input = new Scanner(System.in);
172+
int number = input.nextInt();
173+
//int b =input.nextInt();
174+
// if(number>b)
175+
// number=smallest(number);
176+
number =nextLargest(number);
177+
System.out.println("NEXT LARGEST = "+number);
178+
int count =0;
179+
int next=0,flag=0;
180+
while(true)
181+
{
182+
183+
next = nextLargest(number);
184+
System.out.println(next);
185+
if(next==number)
186+
break;
187+
188+
count++;
189+
number=next;
190+
}
191+
System.out.println("TOTAL POSSIBILITIES "+count);
192+
193+
}
194+
195+
}

0 commit comments

Comments
 (0)
Please sign in to comment.