Skip to content

Commit 280e026

Browse files
committed
Added Coding Problems Excercises Of InterviewBit
0 parents  commit 280e026

1,683 files changed

Lines changed: 158392 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.metadata/.bak_0.log

Lines changed: 11803 additions & 0 deletions
Large diffs are not rendered by default.

.metadata/.lock

Whitespace-only changes.

.metadata/.log

Lines changed: 2924 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
4+
5+
public class Solution {
6+
7+
public static long factorial(long number) {
8+
if (number <= 1)
9+
return 1;
10+
long temp=1;
11+
while(number>1){
12+
temp=temp*number;
13+
number=number-1;
14+
}
15+
return temp;
16+
}
17+
public static int findIndex(StringBuilder a, char b){
18+
for(int i=0;i< a.length();i++){
19+
if(a.charAt(i)==b){
20+
return i;
21+
}
22+
}
23+
return -1;
24+
}
25+
public int findRank(String a) {
26+
char[] bSort=new char[a.length()];
27+
for(int i=0;i<a.length();i++){
28+
bSort[i]=a.charAt(i);
29+
}
30+
Arrays.sort(bSort);
31+
StringBuilder sb=new StringBuilder();
32+
for(char c:bSort){
33+
sb.append(c);
34+
}
35+
int ans=0;
36+
int count=0;
37+
for(int i=0;i<a.length();i++){
38+
++count;
39+
int rank=findIndex(sb,a.charAt(i));
40+
sb.deleteCharAt(rank);
41+
ans=ans+( (rank)*(int)factorial(a.length()-count) );
42+
//System.out.println(ans);
43+
}
44+
return ans+1;
45+
}
46+
public static void main(String[] args){
47+
Solution ss=new Solution();
48+
System.out.println(ss.findRank("cca"));
49+
}
50+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
4+
5+
public class Solution {
6+
7+
8+
public static long factorial(long number) {
9+
if (number <= 1)
10+
return 1;
11+
long temp=1;
12+
while(number>1){
13+
temp=(temp*number);
14+
number=number-1;
15+
}
16+
17+
18+
return temp;
19+
}
20+
public static long inverseNumber(long deno) {
21+
// Inverse modulo : https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
22+
// Calculate num ^ MOD-2 % MOD
23+
long ans = 1, base = ( long) deno;
24+
long mod= 1000003;
25+
int power = (int) (mod - 2);
26+
while (power > 0) {
27+
if (power == 1) {
28+
return (ans * base) % mod;
29+
}
30+
if (power % 2 == 0) {
31+
base = (base * base) % mod;
32+
power /= 2;
33+
} else {
34+
ans = (ans * base) % mod;
35+
power--;
36+
}
37+
}
38+
return ans;
39+
}
40+
public static int findIndex(StringBuilder a, char b){
41+
int state=-1;
42+
for(int i=0;i< a.length();i++){
43+
if(a.charAt(i)==b){
44+
return i;
45+
}
46+
}
47+
return state;
48+
}
49+
public int findRank(String a) {
50+
char[] bSort=new char[a.length()];
51+
HashMap<String,Integer> hh=new HashMap<>();
52+
for(int i=0;i<a.length();i++){
53+
bSort[i]=a.charAt(i);
54+
if(hh.containsKey(a.charAt(i)+"")){
55+
Integer cc=hh.get(a.charAt(i)+"");
56+
hh.put(a.charAt(i)+"", cc+1);
57+
}else{
58+
hh.put(a.charAt(i)+"", new Integer(1));
59+
}
60+
61+
}
62+
Arrays.sort(bSort);
63+
64+
StringBuilder sb=new StringBuilder();
65+
for(char c:bSort){
66+
sb.append(c);
67+
}
68+
long ans=1;
69+
int count=0;
70+
for(int i=0;i<a.length();i++){
71+
++count;
72+
int rank=findIndex(sb,a.charAt(i));
73+
//System.out.println(deno);
74+
long ret=factorial(a.length()-count);
75+
ret=ret%1000003;
76+
long deno=1;
77+
for (String key : hh.keySet()) {
78+
deno=(deno*(factorial(hh.get(key))));
79+
}
80+
//System.out.println(deno+" , "+(((int)Math.pow(deno, 1000001))%1000003));
81+
// deno= (((int)Math.pow(deno, 1000001))%1000003);
82+
ans= ((ans+(rank*ret*inverseNumber((deno)))%1000003));
83+
sb.deleteCharAt(rank);
84+
Integer cd=hh.get(a.charAt(i)+"");
85+
cd--;
86+
if(cd==0){
87+
hh.remove(a.charAt(i)+"");
88+
}else{
89+
hh.put(a.charAt(i)+"", cd);
90+
}
91+
92+
93+
}
94+
95+
return (int)ans;
96+
}
97+
98+
public static void main(String[] args){
99+
Solution ss=new Solution();
100+
System.out.println(ss.findRank("asasdsdsadasdadsad"));//asasdsdsadasdadsadasdsa
101+
//System.out.println((720*4/24)%1000003);
102+
//System.out.println((720*4*((int)Math.pow(24, 1000001)%1000003))%1000003);
103+
//System.out.println(720*4*inverseNumber(24)%1000003);
104+
105+
}
106+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.String;
2+
3+
import java.math.BigInteger;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Scanner;
7+
8+
public class WordsLenLast {
9+
10+
public ArrayList<String> fullJustify(ArrayList<String> words, int L) {
11+
12+
ArrayList<String> res = new ArrayList<String>();
13+
14+
if(words.size() == 0 || L == 0){
15+
res.add("");
16+
return res;
17+
}
18+
19+
if(words.size() == 1 && words.get(0).length() == 0){
20+
res.add(generateSpaces(L));
21+
return res;
22+
}
23+
24+
int index = 0;
25+
while(index < words.size()){
26+
if(words.get(index).length()>L){
27+
res.clear();
28+
break;
29+
}
30+
//Need to take care of the cases when words contains any number of ""(empty string).
31+
if(words.get(index).length() == 0){
32+
index++;
33+
continue;
34+
}
35+
36+
int cur = index+1;
37+
int len = words.get(index).length();
38+
String curLine = words.get(index);
39+
40+
while(cur<words.size()){
41+
int temp = len + words.get(cur).length() + 1;
42+
if(temp<=L){
43+
curLine += " "+words.get(cur);
44+
len = temp;
45+
index = cur;
46+
cur ++;
47+
}
48+
else{
49+
index = cur-1;
50+
break;
51+
}
52+
}
53+
index++;
54+
if(index == words.size()){
55+
curLine = justifyString(curLine,L,true);
56+
}
57+
else curLine = justifyString(curLine,L,false);
58+
res.add(curLine);
59+
}
60+
return res;
61+
}
62+
63+
public String justifyString(String s,int L, boolean isLastLine){
64+
String reg = "\\s+";
65+
String[] last = s.split(reg);
66+
if(!isLastLine){
67+
int len = 0;
68+
for(String str : last)
69+
len += str.length();
70+
71+
int space = L-len;
72+
int slots = last.length-1;
73+
int remain = 0;
74+
int even = space;
75+
if(slots!=0){
76+
remain = space % slots;
77+
even = space / slots;
78+
}
79+
80+
StringBuilder refined = new StringBuilder();
81+
for(String str : last){
82+
if(space>0){
83+
str += generateSpaces(even);
84+
space -= even;
85+
if(remain>0){
86+
str += " ";
87+
remain--;
88+
space--;
89+
}
90+
}
91+
refined.append(str);
92+
}
93+
return refined.toString();
94+
}
95+
else{
96+
StringBuilder refineLast = new StringBuilder();
97+
refineLast.append(last[0]);
98+
99+
for(int i=1;i<last.length;i++){
100+
refineLast.append(" ");
101+
refineLast.append(last[i]);
102+
}
103+
104+
String res = refineLast.toString();
105+
int space = L-res.length();
106+
res += generateSpaces(space);
107+
108+
return res;
109+
}
110+
}
111+
112+
public String generateSpaces(int length){
113+
if(length<1)
114+
return "";
115+
StringBuilder res = new StringBuilder();
116+
while(length>0){
117+
res.append(" ");
118+
length--;
119+
}
120+
return res.toString();
121+
}
122+
123+
124+
public static void main(String[] args) {
125+
// TODO Auto-generated method stub
126+
Scanner sc=new Scanner(System.in);
127+
sc.close();
128+
WordsLenLast s1=new WordsLenLast();
129+
String[] aa1={"This", "is", "an", "example", "of", "text", "justification."};//new ArrayList<>();
130+
ArrayList<String> aa=new ArrayList<>(Arrays.asList(aa1));
131+
System.out.println(s1.fullJustify(aa, 10));
132+
133+
}
134+
135+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.BinarySearch.insert_if_not_found;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
import java.util.Scanner;
5+
/**
6+
* Assuming no duplicates
7+
* @author srinath
8+
*
9+
*/
10+
public class Solution1 {
11+
static int search(List<Integer>a,int b){
12+
int start=0;
13+
int end=a.size()-1;
14+
int result=-1;
15+
if(b<a.get(0)){
16+
return start;
17+
}
18+
else if(b>a.get(end)){
19+
return end;
20+
}
21+
while(start<=end){
22+
int index=start+((end-start)/2);
23+
if(a.get(index)==b){
24+
result=index;
25+
end=index-1;
26+
}
27+
else if(a.get(index)>b){
28+
end=index-1;
29+
}
30+
else{
31+
start=end+1;
32+
}
33+
}
34+
if(result==-1){
35+
return end+1;
36+
}else{
37+
return result;
38+
}
39+
}
40+
41+
public static void manin(String[] args){
42+
Scanner sc=new Scanner(System.in);
43+
List<Integer> ip = new ArrayList<>();
44+
while(sc.hasNextInt()){
45+
ip.add(sc.nextInt());
46+
}
47+
search(ip,5);
48+
sc.close();//tr
49+
}
50+
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.String;
2+
3+
import java.math.BigInteger;
4+
import java.util.Scanner;
5+
6+
public class WordsLenLast {
7+
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
Scanner sc=new Scanner(System.in);
11+
String s="444444444444444444444444" ;
12+
BigInteger ans=new BigInteger(s);
13+
System.out.println(ans);
14+
15+
System.out.println(s.trim());
16+
sc.close();
17+
18+
}
19+
20+
}

0 commit comments

Comments
 (0)