Thursday, 31 December 2020

Union of Two arrays.

Given two different sorted arrays. We need to print union of two arrays. ( Elements which are common in both the arrays)

arr1={1,3,4,5,7}

arr2={2,3,5,6}

Output: {1,2,3,4,5,6,7}

Source code:


public class Main
{
public static void main(String[] args)
{
int arr1[]={1,3,4,5,7};
int arr2[]={2,3,5,6};
union(arr1,arr2);
}
public static void union(int arr1[],int arr2[])
{
int i=0,j=0;

while(i<arr1.length&&j<arr2.length)
{
if(arr1[i]<arr2[j])
{
System.out.print(arr1[i]+" ");
i++;
}
else if(arr1[i]>arr2[j])
{
System.out.print(arr2[j]
+" ");

j++;
}
else if(arr1[i]==arr2[j])
{
System.out.print(arr1[i]
+" ");

i++;
j++;
}
}

while(i<arr1.length)
System.out.print(arr1[i++]
+" ");

while(j<arr2.length)
System.out.print(arr2[j++]
+" ");

}
}

Monday, 28 December 2020

Initializing a string with repeated character of specific length

Source code:

import java.util.*;

import java.io.*;

public class Main

{

public static void main(String[] args) 

{

   char c='0';

            int n=10;

  char ch[]=new char[n];

  Arrays.fill(ch,c);

  String s=new String(ch);

  System.out.println(s);   

}

}

output:
0000000000

Sunday, 27 December 2020

Minimum flips required to get given binary String.

Given a string S, the task is to find minimum flips required to convert an initial binary string consisting of only zeroes to S where every flip of a character flips all succeeding characters as well.

Input: S = “01011”
Output: 3
Explanation:
Initial String – “00000”
Flip the 2nd bit – “01111”
Flip the 3rd bit – “01000”
Flip the 4th bit – “01011”
Total Flips = 3
Input: S = “01001”
Output: 3
Explanation:
Initial String – “00000”
Flip the 2nd bit – “01111”
Flip the 3rd bit – “01000”
Flip the 5th bit – “01001”
Total Flips = 3

Source Code:

public class FlipDigitsOfStringToGetTarget {

public static void main(String[] args) {

String target="01011";
int n=target.length();
System.out.print("Minimum flips required: "+minFlips(target,n));

}
public static int minFlips(String target,int n)
{
int count=0;
char curr='1';

for(int i=0;i<n;i++)
{
char ch=target.charAt(i);

if(ch==curr)
{
++count;
curr=(char)(48+(curr%2));
}
}
return count;
}

}

Sunday, 20 December 2020

Anagram Check of 2 strings

Given a program to check whether two strings are anagram or not. 

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

 Example 1:

SILENT

LISTEN

Example 2:

aab

aba

In the above case both strings have same number of characters(characters used exactly once).


Solution:

Approach 1:
Time Complexity: O(n2)

Source code:

public class Main {
public static void main(String[] args)
{
String s="aaab",s2="abba";
boolean isAnagram=false,visited[]=new boolean[s2.length()];
if(s.length()==s2.length())
{
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
isAnagram=false;
for(int j=0;j<s2.length();j++)
{
if(s2.charAt(j)==ch&&visited[j]==false)
{
isAnagram=true;
visited[j]=true;
break;
}
}
if(!isAnagram)
break;
}
}
if(isAnagram)
System.out.println("Anagram");
else
System.out.println("Not an Anagram");
}
}

Approach 2:

Taking two 256 arrays and making a note of count at its respective indices.
Eg: if 'a' is encountered then index at 97 is increased.

public class Main
{
public static void main(String[] args)
{
String a="aab";
String b="aba";
boolean isAnagram=true;
int[] al=new int[256];
int[] bl=new int[256];

for(char c:a.toCharArray()){
int index=(int)c;
al[index]++;
}
for(char c:b.toCharArray()){
int index=(int) c;
bl[index]++;
}
for(int i=0;i<256;i++){
if(al[i]!=bl[i]){
isAnagram=false;
break;
}
}
if(isAnagram)
System.out.println("Anagram");
else
System.out.println("Not an anagram");
}
}



Approach 3:

Taking one integer array and when character encountered in first string the increment and after iterating second string decrement the same index. At last check whether all the elements in array are zeros.

public class Main
{
public static void main(String[] args)
{
String a="aab";
String b="aba";
boolean isAnagram=true;
int[] al=new int[256];

for(char c:a.toCharArray()){
int index=(int)c;
al[index]++;
}
for(char c:b.toCharArray()){
int index=(int) c;
al[index]--;
}
for(int i=0;i<256;i++){
if(al[i]!=0)
{
isAnagram=false;
break;
}
}
if(isAnagram)
System.out.println("Anagram");
else
System.out.println("Not an anagram");
}
}



Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...