Skip to content
Open
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions ujjwalmishra/ujjwalmishraCSE27/MoveZeros.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Scanner;

public class MoveZeros {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);


System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] nums = new int[n];


System.out.print("Enter the elements: ");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}


int index = 0;
for (int i = 0; i < n; i++) {
if (nums[i] != 0) {
nums[index] = nums[i];
index++;
}
}


while (index < n) {
nums[index] = 0;
index++;
}

System.out.print("Output: ");
for (int i = 0; i < n; i++) {
System.out.print(nums[i] + " ");
}
}
}
38 changes: 38 additions & 0 deletions ujjwalmishra/ujjwalmishraCSE27/PivotIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Scanner;

public class PivotIndex {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input array size
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] nums = new int[n];

// Input array elements
System.out.print("Enter the elements: ");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}

// Calculate total sum of the array
int total = 0;
for (int i = 0; i < n; i++) {
total += nums[i];
}

int leftSum = 0;
int pivotIndex = -1;

for (int i = 0; i < n; i++) {
int rightSum = total - leftSum - nums[i];
if (leftSum == rightSum) {
pivotIndex = i;
break;
}
leftSum += nums[i];
}

System.out.println("Pivot Index: " + pivotIndex);
}
}