Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the new corrected code #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 74 additions & 47 deletions Hashmaps:Longest consecutive Sequence
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,88 @@ import java.util.HashMap;
import java.util.ArrayList;
public class Solution {
public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) {
HashMap<Integer,Boolean> map = new HashMap<>();
ArrayList<Integer> output = new ArrayList<>();

for(int i=0;i<arr.length;i++){
public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) {
ArrayList<Integer> output = new ArrayList<>();
HashMap<Integer, Boolean> map = new HashMap<>();
HashMap<Integer, Integer> lenMap = new HashMap<>();
for (int i=0 ; i < arr.length ; i++)
{
map.put(arr[i],true);
}
int maxStart=-1,maxLen=0;
boolean startCheck=true;

int maxlength = 0;
int start = 0;

for(int i=0;i<arr.length;i++){
if(map.get(arr[i])){
int length = 0;
int temp = arr[i];
while(map.containsKey(temp)){
length++;
map.put(temp,false);
temp = temp+1;
}
int starttemp = arr[i];
temp = arr[i]-1;
while(map.containsKey(temp)){
length++;
map.put(temp,false);
starttemp = temp;
temp = temp-1;
}
for (int i: arr)
{
if (map.get(i))
{
int currStart=i,currLen=1;

boolean flag=true;
map.put(i,false);

if(length > maxlength){
maxlength = length;
start = starttemp;
}else if(length == maxlength){
maxlength = length;
//start = 10 starttemp = 4
for(int j=0;j<arr.length;j++){
if(arr[j] == start){
start = start;
break;
}else if(arr[j] == starttemp){
start = starttemp;
break;
}
int ahead=i+1;

while(flag)
{
if(map.containsKey(ahead))
{

currLen=currLen+1;
map.put(ahead,false);
ahead=ahead+1;

}
else
{
flag=false;
}
}
flag=true;
int before=i-1;

while(flag)
{
if(map.containsKey(before))
{

currLen=currLen+1;
currStart=before;
map.put(before,false);
before=before-1;

}
else
{
flag=false;
}
}

System.out.println();
if (currLen>=maxLen)
{
maxLen=currLen;
maxStart=currStart;
lenMap.put(maxStart,maxLen);
}
}
// else{
// continue;
// }

}

for(int i = start;i<start+maxlength;i++){
output.add(i);
}

return output;

}


for (int i=0;i<arr.length;i++)
{
if (lenMap.containsKey(arr[i]) && lenMap.get(arr[i])>=maxLen)
{
maxStart=arr[i];
maxLen=lenMap.get(arr[i]);
break;
}
}
output.add(maxStart);
output.add(maxStart+maxLen-1);
return output;
}
}