This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] plusOne(int[] A) { | |
int carry = 0; | |
int val = A[A.length-1]+1; | |
A[A.length-1] = val%10; | |
carry = val/10; | |
ArrayList<Integer> al = new ArrayList<Integer>(); | |
boolean flag = true; | |
for(int i=A.length-2;i>=0;i--){ | |
val = A[i]+carry; | |
A[i] = val%10; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int maxArr(int[] arr) { | |
int max1 = Integer.MIN_VALUE; | |
int min1 = Integer.MAX_VALUE; | |
int max2 = Integer.MIN_VALUE; | |
int min2 = Integer.MAX_VALUE; | |
for(int i=0;i<arr.length;i++){ | |
max1 = Math.max(max1,arr[i]+i); | |
min1 = Math.min(min1,arr[i]+i); | |
max2 = Math.max(max2,arr[i]-i); | |
min2 = Math.min(min2,arr[i]-i); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int solve(int length, int[] arr) { | |
int sum = 0; | |
for(int i=0;i<length;i++){ | |
sum+=arr[i]; | |
} | |
int avg = sum/3; | |
if(sum%3 != 0){ | |
return 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String largestNumber(final int[] arr) { | |
int n = arr.length; | |
String sa[] = new String[n]; | |
for(int i=0;i<n;i++){ | |
sa[i] = Integer.toString(arr[i]); | |
} | |
Arrays.sort(sa, new Comparator<String>(){ | |
public int compare(String a, String b){ | |
String s1 = a+b; | |
String s2 = b+a; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void rotate(ArrayList<ArrayList<Integer>> arr) { | |
int n = arr.size(); | |
int m = arr.get(0).size(); | |
for(int i=0;i<n;i++){ | |
for(int j=i;j<m;j++){ | |
int temp = arr.get(i).get(j); | |
arr.get(i).set(j,arr.get(j).get(i)); | |
arr.get(j).set(i,temp); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int[] nextPermutation(int[] arr) { | |
if(arr.length <= 1){ | |
return arr; | |
} | |
int i = arr.length-1; | |
while(i>0 && arr[i-1] >= arr[i]){ | |
i--; | |
} | |
if(i == 0){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ArrayList<Integer> findPerm(final String str, int num) { | |
Stack<Integer> st = new Stack<>(); | |
ArrayList<Integer> al = new ArrayList<>(); | |
int val = 1; | |
for(int i=0;i<str.length();i++){ | |
char ch = str.charAt(i); | |
if(ch == 'D'){ | |
st.push(val++); | |
} | |
else{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int repeatedNumber(final int[] arr) { | |
int n = arr.length; | |
for(int i=0;i<n;i++){ | |
arr[i] = arr[i]-1; | |
} | |
for(int i=0;i<n;i++){ | |
arr[(arr[i])%n] = arr[(arr[i])%n]+n; | |
} | |
for(int i=0;i<n;i++){ | |
//System.out.println() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int solve(String[] A) { | |
double sum = 0; | |
ArrayList<Double> pq = new ArrayList<>(); | |
for(int i=0;i<A.length;i++){ | |
Double d = Double.parseDouble(A[i]); | |
if(pq.size()<3){ | |
sum+=d; | |
pq.add(d); | |
}else if(pq.size()==3 && sum>=2 && d<pq.get(2)){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int solve(ArrayList<Integer> A) { | |
Collections.sort(A); | |
int n = A.size(); | |
if(A.get(n-1) == 0){ | |
return 1; | |
} | |
for(int i=0;i<A.size()-1;i++){ | |
if(A.get(i)!=A.get(i+1)){ | |
if(A.get(i) == n-i-1){ |