problems related to arrays
class Main {
public static void main(String[] args) {
int [] arr={1,2,-1,-2};
int [] temp_neg=new int [arr.length];
int [] temp_pos=new int [arr.length];
int posindex=0;
int negindex=0;
//storing positive and negative array values
for(int i=0;i<arr.length;i++){
if(arr[i]<0){
temp_neg[negindex++]=arr[i];
}
else{
temp_pos[posindex++]=arr[i];
}
}
int [] combine=new int[temp_neg.length+temp_pos.length];
int i=0,j=0,k=0;
//iterating alternatively
while(i<negindex && j<posindex){
combine[k++]=temp_neg[i++];
combine[k++]=temp_pos[j++];
}
//if any reaminig elements int the array by using this we can be able to print
while(i<negindex){
combine[k++]=temp_neg[i++];
}
while(j<posindex){
combine[k++]=temp_pos[j++];
}
for(int ele:combine){
System.out.println(ele);
}
}
}
//subarrays
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={10,20,30,40,50};
int n=arr.length;
int ln=n*(n-1)/2;
int k1=30;
int [] temp=new int [arr.length];
int index=0,sum=0;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
for(int k=i;k<=j;k++){
System.out.print(arr[k]+" ");
}
System.out.println();
}
}
System.out.println(ln);
}
}
//kandae's algorithm maximum subarrays sum:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={4,-1,2,1};
int cur_sum=arr[0];
// int max_sum=arr[0];
for(int i=1;i<arr.length;i++){
if(cur_sum+arr[i]>arr[i]){
cur_sum=cur_sum+arr[i];
}
else{
cur_sum=arr[i];
}
}
System.out.println(cur_sum);
}
}
//for finding minimum sum of sub array:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={10,20,30};
int cur_sum=arr[0];
int min_sum=arr[0];
for(int i=1;i<arr.length;i++){
if(cur_sum+arr[i]<arr[i]){
cur_sum=cur_sum+arr[i];
}
else{
cur_sum=arr[i];
}
if(cur_sum<min_sum){
min_sum=cur_sum;
}
}
System.out.println(min_sum);
}
}
// finding the sub array which has binary digits max length
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={0,1,0,1};
int max=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]==0){
arr[i]=-1;
}
}
for(int i=0;i<arr.length;i++){
int sum=0;
for(int j=i;j<arr.length;j++){
sum+=arr[j];
if(sum==0){
int length=j-i+1;
if(length>max){
max=length;
}
}
}
}
System.out.println(max);
}
}
//finding incresing contigous subarray:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={-1,1,-1,1};
int cur_len=1;
int max_len=1;
for(int i=1;i<arr.length;i++){
if(arr[i]>arr[i-1]){
cur_len++;
}
else{
cur_len=1;
}
if(cur_len>max_len){
max_len=cur_len;
}
}
System.out.println(max_len);
}
}
//sliding window first neagative:
class Main {
public static void main(String[] args) {
int[] arr = {12, -1, -7, 8, -15, 30, 16, 28};
int N = arr.length;
int K = 3;
for (int i = 0; i <= N - K; i++) {
boolean found = false;
// Check each element in this window
for (int j = i; j < i + K; j++) {
if (arr[j] < 0) {
System.out.print(arr[j] + " ");
found = true;
break; // stop after first negative
}
}
if (!found) {
System.out.print("0 ");
}
}
}
}
Comments
Post a Comment