t@_@n
BAN USERCode in Java
Time : O(n) , n=length of string
Space : O(1)
private static void twosComplement(String input){
char[] chars=input.toCharArray();
int consecutiveOnesCount=0;
for(int i=chars.length-1;i>=0;i--){
if(chars[i]=='0' && i+consecutiveOnesCount==chars.length-1){
consecutiveOnesCount++;
}else if(chars[i]=='1' && i+consecutiveOnesCount!=chars.length-1){
chars[i]='0';
}else if(chars[i]=='0' && i+consecutiveOnesCount!=chars.length-1){
chars[i]='1';
}
}
String output=new String(chars);
if(consecutiveOnesCount==chars.length){
output="1"+output;
}
System.out.println("2's complement of input is --> "+output);
}
Code in Java
Time : O(n)
Space : O(n)
private static void removeDuplicates(String input){
char[] chars=input.toCharArray();
int[] alphabetCount=new int[26];//It will keep count of every appeared alphabet in given string
for(Character c : chars){
alphabetCount[c-97]++;
}
String output="";
for(Character c : chars){
if(alphabetCount[c-97]==1){
output+=c;
}
}
System.out.println("Output string is --> "+output);
}
Solution in Java
Time : O(n)
Space : O(n)
private static void checkBracketBalancing(String input){
char[] inputArr=input.toCharArray();
Map<Character,Character> charPairs=new HashMap<Character,Character>();
charPairs.put(')', '(');
charPairs.put('}', '{');
charPairs.put(']', '[');
Stack<Character> charst=new Stack<Character>();
for(Character c : inputArr){
if(charPairs.get(c)!=null){
if(!charst.isEmpty() && charPairs.get(c).equals(charst.peek())){
charst.pop();
}else{
System.out.println("Unbalanced");
return;
}
}else{
charst.push(c);
}
}
if(charst.isEmpty()){
System.out.println("Balanced");
}
}
private static void convertPalindrome(String[] input){
if(input==null){
return;
}
for(int i=0;i<input.length;i++){
char[] chars=input[i].toCharArray();
int[] occurances=new int[26];
int oddCount=0;
for(int j=0;j<chars.length;j++){
int ascii=chars[j];
occurances[ascii-97]++;
if(occurances[ascii-97]%2!=0){
oddCount++;
}else{
oddCount--;
}
}
if(oddCount>1){
input[i]="-1";
}else{
String palindrome="";
Character oddchar=null;
Character foundChar=null;
for(int k=0;k<occurances.length;k++){
if(occurances[k]!=0){
if(occurances[k]==1){
oddchar=(char)(k+97);
}else{
foundChar=(char)(k+97);
for(int l=1;l<=occurances[k]/2;l++){
palindrome+=foundChar;
}
}
}
}
if(null!=oddchar){
palindrome+=oddchar;
}
for(int m=occurances.length-1;m>=0;m--){
if(occurances[m]!=0){
if(occurances[m]!=1){
foundChar=(char)(m+97);
for(int l=1;l<=occurances[m]/2;l++){
palindrome+=foundChar;
}
}
}
}
input[i]=palindrome;
}
}
}
O(n) time - Code in Java
Here if same no is repeated n times then it will use number n times to find sum
private static void findSumPairsLinear(int[] a,int sum){
Map<Integer,Node> data=new HashMap<Integer,Node>();
for(int i=0;i<a.length;i++){
if(null==data.get(a[i])){
data.put(a[i], new ArrayProblems().new Node(1,i));
}else{
data.get(a[i]).setCount(data.get(a[i]).getCount()+1);
}
}
for(int i=0;i<a.length;i++){
if(data.get(sum-a[i])!=null){
Node pair1=data.get(sum-a[i]);
Node pair2=data.get(a[i]);
if(sum-a[i]==a[i] && pair1.getCount()<2){
continue;
}
System.out.println("Pair is -> "+a[i]+" "+a[pair1.getIndex()]);
if(pair1.getCount()==1){
data.remove(sum-a[i]);
}else{
pair1.setCount(pair1.getCount()-1);
}
if(pair2.getCount()==1){
data.remove(a[i]);
}else{
pair2.setCount(pair2.getCount()-1);
}
}
}
}
Repjaramsmond, Passenger rate clerk at Sportswest
I am a Passenger Rate Clerk . Charter representative Provides fare information to passengers traveling on non scheduled or chartered motor ...
RepEllaWilliams, Analyst at A9
I am a perceptive systems analyst adept at designing innovative IT solutions and enhancing existing systems with new features. I ...
RepSuzaneVenable, Applications Developer at 247quickbookshelp
I am an employment manager typically in charge of planning the hiring processes, setting guidelines and timelines, and establishing budgets ...
RepHenryBrown, Associate at A9
I am an experienced budget analyst skilled at researching and consolidating financial and budget information. I am taught record keeping ...
Repgladysloweg, Brokerage clerk at Bell Markets
I am Gladys from Defiance city . I am working as a Brokerage clerk with tasks associated with securities such as ...
RepGarzaHodge, Backend Developer at Apache Design
I am Garza, and I work in many different research, education, and health care settings with varying roles, levels of ...
RepOishiGreen, Android test engineer at ABC TECH SUPPORT
I am a results-oriented debt collector with 3 years of experience and knowledge in the leasing/finance sector. I have ...
Replloismyers, Grounds maintenance worker at Eagle Hardware & Garden
I am a ground maintenance worker who typically mows grass, edges, and fertilises lawns. I also weed and mulch landscape ...
RepNevaLucas, Analyst at Apache Design
I was the Training manager in Checker Auto Parts Center. My current research projects are about astrology, vashikaran health outcomes ...
Repmoruytmeks, Airport service agent at Stratabiz
I am an airport service agent . I work in an airport , providing information and assistance to the flying public. In ...
Kadane algorithm in Java
- t@_@n February 09, 2016