|
| 1 | +package easyq; |
| 2 | +/* BY-M.NAVEEN |
| 3 | + * engineerscodes |
| 4 | + * code-sum of each rotation |
| 5 | + * sr.code-Ar7 |
| 6 | + * |
| 7 | + * */ |
| 8 | +import java.util.*; |
| 9 | +public class sumofallrotation // class name |
| 10 | +{ |
| 11 | + static Scanner nav=new Scanner(System.in); // calling Scanner and creating object nav |
| 12 | + static int array[],size,sum,highest=0,count=0,pos; // class instances variables |
| 13 | + public sumofallrotation (int c) // constructor |
| 14 | + { |
| 15 | + size=c; |
| 16 | + array=new int[size]; //intialization of variables |
| 17 | + } |
| 18 | + |
| 19 | + public static void rotation() //method to do right_rotation |
| 20 | + { |
| 21 | + int temp=array[size-1]; |
| 22 | + for(int i=size-2;i>=0;i--) |
| 23 | + { |
| 24 | + array[i+1]=array[i]; |
| 25 | + } |
| 26 | + array[0]=temp; |
| 27 | + System.out.println("after rotation element in array are::"+Arrays.toString(array)); |
| 28 | + sum(); |
| 29 | + } |
| 30 | + public static void sum() //Find value of each rotation Sum( i*arr[i]) with only rotations on given |
| 31 | + { count++; |
| 32 | + sum=0; //sum must be initizated to zero |
| 33 | + for(int i=0;i<size;i++) |
| 34 | + { sum=sum+(i*array[i]); |
| 35 | + |
| 36 | + } |
| 37 | + System.out.println(" sum of i*a[t] is "+sum+" when rotated by "+count); |
| 38 | + |
| 39 | + if(highest<=sum) |
| 40 | + {highest=sum; |
| 41 | + pos=count; |
| 42 | + } |
| 43 | + if(count==size) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + else |
| 48 | + rotation(); //recursive call |
| 49 | + } |
| 50 | + |
| 51 | + public static void input() //array input method |
| 52 | + { |
| 53 | + for(int i=0;i<array.length;i++) |
| 54 | + array[i]=nav.nextInt(); |
| 55 | + System.out.println("element in array are::"+Arrays.toString(array)); |
| 56 | + rotation(); |
| 57 | + } |
| 58 | + public static void main(String arg[]) //main method |
| 59 | + { |
| 60 | + System.out.println("enter the size of the array"); |
| 61 | + sumofallrotation n=new sumofallrotation (nav.nextInt()); |
| 62 | + System.out.println("Enter the element of array"); |
| 63 | + input(); |
| 64 | + System.out.println("the highest sum of i*a[t] is "+highest+" when rotated by "+count); |
| 65 | + |
| 66 | +} |
| 67 | +} |
0 commit comments