diff --git a/ujjwalmishra/ujjwalmishraCSE27/MoveZeros.java b/ujjwalmishra/ujjwalmishraCSE27/MoveZeros.java new file mode 100644 index 0000000..4b13fc8 --- /dev/null +++ b/ujjwalmishra/ujjwalmishraCSE27/MoveZeros.java @@ -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] + " "); + } + } +} diff --git a/ujjwalmishra/ujjwalmishraCSE27/PivotIndex.java b/ujjwalmishra/ujjwalmishraCSE27/PivotIndex.java new file mode 100644 index 0000000..e418c9b --- /dev/null +++ b/ujjwalmishra/ujjwalmishraCSE27/PivotIndex.java @@ -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); + } +}